Change HTTP Requests into HTTPS Ones
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / EngineResources.java
index 8f9a271..b9875da 100644 (file)
@@ -20,6 +20,9 @@ import com.codahale.metrics.annotation.Timed;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
 import java.util.Locale;
 import javax.inject.Inject;
 import javax.servlet.http.HttpServletRequest;
@@ -33,6 +36,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import lombok.extern.slf4j.Slf4j;
 import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.common.dmaap.DmaapService;
 import org.onap.holmes.common.exception.CorrelationException;
 import org.onap.holmes.common.utils.ExceptionUtil;
 import org.onap.holmes.common.utils.LanguageUtil;
@@ -56,23 +60,34 @@ public class EngineResources {
     @Produces(MediaType.APPLICATION_JSON)
     @Timed
     public CorrelationRuleResponse deployRule(
-            @ApiParam(value = "The request entity of the HTTP call, which comprises two "
-                    + "fields: \"content\" and \"engineid\". "
-                    + "The \"content\" should be a valid Drools rule string and the \"engineid\" "
+            @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\" "
                     + "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());
+            DmaapService.loopControlNames
+                    .put(packageName, deployRuleRequest.getLoopControlName());
+            String packageNameRet = droolsEngine.deployRule(deployRuleRequest, locale);
+            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: " + packageName);
             crResponse.setPackageName(packageName);
 
         } 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;
@@ -89,9 +104,8 @@ public class EngineResources {
         Locale locale = LanguageUtil.getLocale(httpRequest);
 
         try {
-
             droolsEngine.undeployRule(packageName, locale);
-
+            DmaapService.loopControlNames.remove(packageName);
         } catch (CorrelationException correlationException) {
             log.error(correlationException.getMessage(), correlationException);
             throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
@@ -117,4 +131,10 @@ public class EngineResources {
         }
         return true;
     }
+
+    private String getPackageName(String contents){
+        String ret = contents.trim();
+        ret = ret.substring(7, ret.indexOf("import")).trim();
+        return ret.endsWith(";") ? ret.substring(0, ret.length() - 1) : ret;
+    }
 }