Allocate Rule
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / wrapper / RuleMgtWrapper.java
index 484fb5f..196b21a 100644 (file)
@@ -23,7 +23,6 @@ import javax.inject.Inject;
 import javax.inject.Singleton;\r
 import lombok.extern.slf4j.Slf4j;\r
 import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.common.dmaap.DmaapService;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
@@ -40,6 +39,7 @@ import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;
 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
 import org.onap.holmes.rulemgt.bolt.enginebolt.EngineWrapper;\r
 import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;\r
+import org.onap.holmes.rulemgt.send.Ip4AddingRule;\r
 \r
 \r
 @Service\r
@@ -47,6 +47,12 @@ import org.onap.holmes.rulemgt.db.CorrelationRuleQueryDao;
 @Slf4j\r
 public class RuleMgtWrapper {\r
 \r
+    @Inject\r
+    private Ip4AddingRule ip4AddingRule;\r
+\r
+    @Inject\r
+    private RuleQueryWrapper ruleQueryWrapper;\r
+\r
     @Inject\r
     private CorrelationRuleQueryDao correlationRuleQueryDao;\r
     @Inject\r
@@ -73,13 +79,20 @@ public class RuleMgtWrapper {
         if (ruleTemp != null) {\r
             throw new CorrelationException("A rule with the same name already exists.");\r
         }\r
-        String packageName = deployRule2Engine(correlationRule);\r
+        String ip ="";\r
+        try{\r
+            ip = ip4AddingRule.getEngineIp4AddRule();\r
+        }catch(Exception e){\r
+            log.error("When adding rules, can not get engine instance ip");\r
+        }\r
+        String packageName = deployRule2Engine(correlationRule, ip);\r
         correlationRule.setPackageName(packageName);\r
+        correlationRule.setEngineInstance(ip);\r
         CorrelationRule result = null;\r
         try {\r
             result = correlationRuleDao.saveRule(correlationRule);\r
         } catch (CorrelationException e) {\r
-            engineWarpper.deleteRuleFromEngine(packageName);\r
+            engineWarpper.deleteRuleFromEngine(packageName, ip);\r
             throw new CorrelationException(e.getMessage(), e);\r
         }\r
         RuleAddAndUpdateResponse ruleAddAndUpdateResponse = new RuleAddAndUpdateResponse();\r
@@ -96,18 +109,23 @@ public class RuleMgtWrapper {
         if (oldCorrelationRule == null) {\r
             throw new CorrelationException("You're trying to update a rule which does not exist in the system.");\r
         }\r
+        String updateIp = "";\r
+        updateIp = oldCorrelationRule.getEngineInstance();\r
         CorrelationRule newCorrelationRule = convertRuleUpdateRequest2CorrelationRule(modifier,\r
                 ruleUpdateRequest, oldCorrelationRule.getName());\r
+        newCorrelationRule.setEngineInstance(updateIp);\r
         checkCorrelation(newCorrelationRule);\r
         RuleAddAndUpdateResponse ruleChangeResponse = new RuleAddAndUpdateResponse();\r
         ruleChangeResponse.setRuleId(newCorrelationRule.getRid());\r
+\r
         if (!haveChange(newCorrelationRule, oldCorrelationRule)) {\r
             return ruleChangeResponse;\r
         }\r
         if (oldCorrelationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName());\r
+            String oldRuleEngineInstance = oldCorrelationRule.getEngineInstance();\r
+            engineWarpper.deleteRuleFromEngine(oldCorrelationRule.getPackageName(), oldRuleEngineInstance);\r
         }\r
-        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule));\r
+        newCorrelationRule.setPackageName(deployRule2Engine(newCorrelationRule, updateIp));\r
         correlationRuleDao.updateRule(newCorrelationRule);\r
         return ruleChangeResponse;\r
     }\r
@@ -155,7 +173,8 @@ public class RuleMgtWrapper {
             throw new CorrelationException("You're trying to delete a rule which does not exist in the system.");\r
         }\r
         if (correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN) {\r
-            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName());\r
+            String ip = correlationRule.getEngineInstance();\r
+            engineWarpper.deleteRuleFromEngine(correlationRule.getPackageName(), ip);\r
         }\r
         correlationRuleDao.deleteRule(correlationRule);\r
     }\r
@@ -200,11 +219,11 @@ public class RuleMgtWrapper {
         return correlationRule;\r
     }\r
 \r
-    private String deployRule2Engine(CorrelationRule correlationRule)\r
+    public String deployRule2Engine(CorrelationRule correlationRule, String ip)\r
             throws CorrelationException {\r
-        if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule)) && (\r
+        if (engineWarpper.checkRuleFromEngine(correlationRules2CheckRule(correlationRule), ip) && (\r
                 correlationRule.getEnabled() == RuleMgtConstant.STATUS_RULE_OPEN)) {\r
-            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule));\r
+            return engineWarpper.deployEngine(correlationRules2DeployRule(correlationRule), ip);\r
         }\r
         return "";\r
     }\r