Optimized Rule Deployment Logic
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / EngineResources.java
index 9724355..0d26b6e 100644 (file)
@@ -21,6 +21,9 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
 import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import javax.inject.Inject;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.DELETE;
@@ -48,7 +51,8 @@ import org.onap.holmes.engine.response.CorrelationRuleResponse;
 @Produces(MediaType.APPLICATION_JSON)
 @Slf4j
 public class EngineResources {
-
+       private Pattern packagePattern = Pattern.compile("package[\\s]+([^;]+)[;\\s]*");
+    
     @Inject
     DroolsEngine droolsEngine;
 
@@ -58,23 +62,37 @@ public class EngineResources {
     @Timed
     public CorrelationRuleResponse deployRule(
             @ApiParam(value = "The request entity of the HTTP call, which comprises three "
-                    + "fields: \"content\" , \"loopcontrolname\" and \"engineid\". "
-                    + "The \"content\" should be a valid Drools rule string and the \"engineid\" "
+                    + "fields: \"content\" , \"loopControlName\" and \"engineId\". "
+                    + "The \"content\" should be a valid Drools rule string and the \"engineId\" "
                     + "has to be \"engine-d\" in the Amsterdam release.", required = true) DeployRuleRequest deployRuleRequest,
             @Context HttpServletRequest httpRequest) {
 
         CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
         Locale locale = LanguageUtil.getLocale(httpRequest);
         try {
-            String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
+            String packageName = getPackageName(deployRuleRequest.getContent());
+            if(packageName == null) {
+               throw new CorrelationException("Could not find package name in rule: "+deployRuleRequest.getContent());
+            }
+            
             DmaapService.loopControlNames
                     .put(packageName, deployRuleRequest.getLoopControlName());
-            log.info("Rule deployed. Package name: " + packageName);
-            crResponse.setPackageName(packageName);
+            String packageNameRet = droolsEngine.deployRule(deployRuleRequest);
+            if (!packageName.equals(packageNameRet)) {
+                log.info("The parsed package name is different from that returned by the engine.");
+                DmaapService.loopControlNames.remove(packageName);
+                DmaapService.loopControlNames
+                        .put(packageNameRet, deployRuleRequest.getLoopControlName());
+            }
+            log.info("Rule deployed. Package name: " + packageNameRet);
+            crResponse.setPackageName(packageNameRet);
 
         } catch (CorrelationException correlationException) {
             log.error(correlationException.getMessage(), correlationException);
             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
+        } catch (RuntimeException e) {
+            log.error(e.getMessage(), e);
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
         }
 
         return crResponse;
@@ -91,13 +109,13 @@ public class EngineResources {
         Locale locale = LanguageUtil.getLocale(httpRequest);
 
         try {
-
-            droolsEngine.undeployRule(packageName, locale);
-
+            droolsEngine.undeployRule(packageName);
+            DmaapService.loopControlNames.remove(packageName);
         } catch (CorrelationException correlationException) {
             log.error(correlationException.getMessage(), correlationException);
             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
         }
+
         return true;
     }
 
@@ -112,11 +130,21 @@ public class EngineResources {
         Locale locale = LanguageUtil.getLocale(httpRequest);
 
         try {
-            droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
+            droolsEngine.compileRule(compileRuleRequest.getContent());
         } catch (CorrelationException correlationException) {
             log.error(correlationException.getMessage(), correlationException);
             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
         }
         return true;
     }
+    
+    private String getPackageName(String contents){
+        Matcher m = packagePattern.matcher(contents);
+        
+        if (m.find( )) {
+           return m.group(1);
+        }else {
+           return null;
+        }
+    }
 }