/Mimoza/AppsLayers/AgentLayer/PolicyManager.cs

# · C# · 250 lines · 200 code · 50 blank · 0 comment · 25 complexity · 6db8c5321e0821e923874baabb26f4ae MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using Mimoza.PluginLayer;
  7. namespace Mimoza.AgentLayer
  8. {
  9. public class PolicyManager : BaseObject
  10. {
  11. public PolicyManager(Configuration config)
  12. : base(config)
  13. {
  14. m_pluginManager = new Mimoza.PluginLayer.ExecutePluginManager();
  15. m_pluginManager.Init();
  16. LastSyncId = Configuration.AgentDatabase.GetHostEffectivePolicyId();
  17. }
  18. override public void Init()
  19. {
  20. Configuration.ObjectLayer.PolicyRepository.OnObjectChanged += OnPolicyChanged;
  21. }
  22. override public void Shutdown()
  23. {
  24. Configuration.ObjectLayer.PolicyRepository.OnObjectChanged -= OnPolicyChanged;
  25. m_pluginManager.Shutdown();
  26. }
  27. public Guid LastSyncId
  28. {
  29. get { return m_lastSyncId; }
  30. set { m_lastSyncId = value; }
  31. }
  32. public void ApplySyncItem(PolicySyncItem syncItem)
  33. {
  34. List<ObjectLayer.Policy> policies = Configuration.AgentDatabase.GetRootPolicies();
  35. LastSyncId = syncItem.SyncId;
  36. Configuration.AgentDatabase.ReplaceServerPolicies(syncItem.Policies, LastSyncId);
  37. PluginResult pluginResult = new PluginResult();
  38. ApplyEffectivePoliciesToPlugins(syncItem, pluginResult);
  39. Configuration.AgentDatabase.SaveLocalHostModificationId(syncItem.HostModificationId);
  40. ProcessPluginResult.Process(Configuration, pluginResult);
  41. }
  42. void ApplyEffectivePoliciesToPlugins(PolicySyncItem syncItem, PluginResult pluginResult)
  43. {
  44. Common.Logger.Log.Info("PolicyManager.ApplyPoliciesToPlugins...");
  45. if (!IsNeedToApplyPolicies(syncItem.SyncId, syncItem.HostModificationId))
  46. {
  47. Common.Logger.Log.Info("PolicyManager.ApplyPoliciesToPlugins. syncId is equal sync is not need.");
  48. return;
  49. }
  50. List<ObjectLayer.Policy> effectivePolicies =
  51. new List<Mimoza.ObjectLayer.Policy>(Configuration.AgentDatabase.GetLocalHostContainer().GetEffectivePolicy());
  52. List<ObjectLayer.Policy> syncedPolicies = Configuration.AgentDatabase.GetSyncPolicies();
  53. Common.Logger.Log.Info("PolicyManager.ApplyPoliciesToPlugins effectivePolicies.size - " +
  54. effectivePolicies.Count + " syncedPolicies.sizze - " + syncedPolicies.Count + ".");
  55. foreach (ObjectLayer.Policy policy in effectivePolicies)
  56. {
  57. ObjectLayer.ObjectLayerEventType operation;
  58. if (IsNeedToApplyToPlugin(policy, syncedPolicies, out operation))
  59. {
  60. ApplyToPlugin(policy, operation, pluginResult);
  61. }
  62. }
  63. foreach (ObjectLayer.Policy existPolicy in syncedPolicies)
  64. {
  65. bool bExistInNew = (effectivePolicies.FindIndex(p => p.ID == existPolicy.ID) >= 0);
  66. if (!bExistInNew)
  67. {
  68. ApplyToPlugin(existPolicy, Mimoza.ObjectLayer.ObjectLayerEventType.ObjectDeleted, pluginResult);
  69. }
  70. }
  71. Configuration.AgentDatabase.ReplaceSyncPolicies(effectivePolicies, syncItem.SyncId);
  72. Common.Logger.Log.Info("PolicyManager.ApplyPoliciesToPlugins...OK");
  73. }
  74. bool IsNeedToApplyPolicies(Guid syncId, Guid hostModificationId)
  75. {
  76. if (Configuration.AgentDatabase.GetSyncEffectivePolicyId() != syncId ||
  77. Configuration.AgentDatabase.GetLocalHostModificationId() != hostModificationId) return true;
  78. else return false;
  79. }
  80. void ApplyToPlugin(ObjectLayer.Policy policy, ObjectLayer.ObjectLayerEventType operationType,
  81. PluginResult pluginResult)
  82. {
  83. Common.Logger.Log.Info("PolicyManager.ApplyToPlugin. Id - '" +
  84. policy.PluginID + "' operationType - " + operationType.ToString() + " ...");
  85. Mimoza.PluginLayer.IExecutePlugin pPlugin = m_pluginManager.Get(policy.PluginID);
  86. if (pPlugin == null)
  87. {
  88. Common.Logger.Log.Warn("PolicyManager.ApplyToPlugin. Error plugin wasn't found. Id - '" +
  89. policy.PluginID + "'");
  90. return;
  91. }
  92. try
  93. {
  94. switch (operationType)
  95. {
  96. case Mimoza.ObjectLayer.ObjectLayerEventType.ObjectInserted:
  97. pPlugin.Add(policy);
  98. break;
  99. case Mimoza.ObjectLayer.ObjectLayerEventType.ObjectUpdated:
  100. pPlugin.Update(policy);
  101. break;
  102. case Mimoza.ObjectLayer.ObjectLayerEventType.ObjectDeleted:
  103. pPlugin.Remove(policy);
  104. break;
  105. }
  106. Mimoza.PluginLayer.PluginResult res = Mimoza.PluginLayer.PluginResultStore.GetResult();
  107. if (res != null)
  108. {
  109. if (res.NeedToReboot) pluginResult.NeedToReboot = true;
  110. PluginResultStore.ResetResult();
  111. }
  112. }
  113. catch (System.Exception e)
  114. {
  115. Common.Logger.Log.Warn("PolicyManager.ApplyToPlugin. Error was occured - '" +
  116. e.ToString() + "'.");
  117. return;
  118. }
  119. Common.Logger.Log.Info("PolicyManager.ApplyToPlugin. Id - '" +
  120. policy.PluginID + "'...OK");
  121. }
  122. void OnPolicyChanged(object sender, Mimoza.ObjectLayer.ObjectLayer_ObjectChangedEventArgs eventArg)
  123. {
  124. if (!(eventArg.Object is Mimoza.ObjectLayer.Policy))
  125. {
  126. Common.Logger.Log.Error("PolicyManager.OnPolicyChanged ERROR Object is not policy.");
  127. return;
  128. }
  129. Mimoza.ObjectLayer.Policy modifiedPolicy = eventArg.Object as Mimoza.ObjectLayer.Policy;
  130. if (modifiedPolicy.Record == null)
  131. {
  132. Common.Logger.Log.Error("PolicyManager.OnPolicyChanged ERROR Policy is empty.");
  133. return;
  134. }
  135. if (modifiedPolicy.Parent!=null && modifiedPolicy.Parent.IsSystem)
  136. {
  137. Common.Logger.Log.Debug("PolicyManager.OnPolicyChanged policy ('{0}') from system container modified.",
  138. modifiedPolicy.Name);
  139. return;
  140. }
  141. if (modifiedPolicy.Parent != Configuration.AgentDatabase.GetLocalHostContainer())
  142. {
  143. Common.Logger.Log.Debug("PolicyManager.OnPolicyChanged policy ('{0}') is not from local host container.",
  144. modifiedPolicy.ID.ToString() );
  145. return;
  146. }
  147. Common.Logger.Log.Info("PolicyManager.OnPolicyChanged opertion - '" + eventArg.EventType.ToString() +
  148. "' id - '" + modifiedPolicy.ID.ToString() + "' Name - '" + modifiedPolicy.Name + "' Operation - " +
  149. eventArg.EventType.ToString() + ".");
  150. Configuration.AgentDatabase.SaveSyncEffectivePolicyId(Guid.NewGuid());
  151. PluginLayer.PluginResult result = new Mimoza.PluginLayer.PluginResult();
  152. ApplyEffectivePoliciesToPlugins(new PolicySyncItem(LastSyncId), result);
  153. ProcessPluginResult.Process(Configuration, result);
  154. }
  155. static bool IsNeedToApplyToPlugin(ObjectLayer.Policy policy, List<ObjectLayer.Policy> syncedPolicies,
  156. out ObjectLayer.ObjectLayerEventType operation)
  157. {
  158. int index = (-1);
  159. bool bExist = ((index = syncedPolicies.FindIndex(p => p.ID == policy.ID)) >= 0);
  160. bool bNeedToApply = true;
  161. if (bExist && syncedPolicies[index].DataModificationID == policy.DataModificationID) bNeedToApply = false;
  162. operation = bExist ? Mimoza.ObjectLayer.ObjectLayerEventType.ObjectUpdated :
  163. Mimoza.ObjectLayer.ObjectLayerEventType.ObjectInserted;
  164. return bNeedToApply;
  165. }
  166. Guid m_lastSyncId = new Guid();
  167. Mimoza.PluginLayer.ExecutePluginManager m_pluginManager;
  168. }
  169. public class PolicySyncItem
  170. {
  171. public PolicySyncItem()
  172. {
  173. }
  174. public PolicySyncItem(Guid lastSyncId)
  175. {
  176. SyncId = lastSyncId;
  177. }
  178. public Guid SyncId
  179. {
  180. get { return m_syncId; }
  181. set { m_syncId = value; }
  182. }
  183. public Guid HostModificationId
  184. {
  185. get { return m_hostModificationId; }
  186. set { m_hostModificationId = value; }
  187. }
  188. public void AddPolicy(ObjectLayer.Policy policy)
  189. {
  190. m_policyList.Add(policy);
  191. }
  192. public List<ObjectLayer.Policy> Policies
  193. {
  194. get { return m_policyList; }
  195. }
  196. Guid m_syncId;
  197. Guid m_hostModificationId = Guid.Empty;
  198. List<ObjectLayer.Policy> m_policyList = new List<Mimoza.ObjectLayer.Policy>();
  199. }
  200. }