diff options
7 files changed, 48 insertions, 43 deletions
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java index 19a9153..7ac4642 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java @@ -43,6 +43,7 @@ public class EngineDActiveApp extends IOCApplication<EngineDAppConfig> { new EngineDActiveApp().run(args); } + @Override public void run(EngineDAppConfig configuration, Environment environment) throws Exception { super.run(configuration, environment); diff --git a/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java index c748f85..71a58f9 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java @@ -26,8 +26,6 @@ import org.onap.holmes.common.config.MQConfig; @Service public class EngineDAppConfig extends Configuration { - private String defaultName = "Correlation-Rule"; - private String apidescription = "Holmes rule management rest API"; @JsonProperty diff --git a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java index b02cbe4..c96b813 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/dmaap/SubscriberAction.java @@ -18,7 +18,6 @@ package org.onap.holmes.engine.dmaap; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import javax.annotation.PreDestroy; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; diff --git a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java index dcc40fd..bb3795d 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java @@ -63,8 +63,12 @@ import org.onap.holmes.engine.wrapper.RuleMgtWrapper; @Service
public class DroolsEngine {
- private final static int ENABLE = 1;
+ private static final int ENABLE = 1;
+ public static final String UTF_8 = "UTF-8";
+ public static final String K_BASE = "KBase";
+ private static final String RULES_FILE_NAME = "src/main/resources/rules/rule.drl";
private final Set<String> packageNames = new HashSet<String>();
+
@Inject
private RuleMgtWrapper ruleMgtWrapper;
@@ -139,8 +143,8 @@ public class DroolsEngine { private void deployRuleFromDB(String ruleContent) throws CorrelationException {
avoidDeployBug();
StringReader reader = new StringReader(ruleContent);
- kfs.write("src/main/resources/rules/rule.drl",
- this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
+ kfs.write(RULES_FILE_NAME,
+ this.resources.newReaderResource(reader, UTF_8).setResourceType(ResourceType.DRL));
kieBuilder = ks.newKieBuilder(kfs).buildAll();
try {
InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();
@@ -155,14 +159,14 @@ public class DroolsEngine { throws CorrelationException {
avoidDeployBug();
StringReader reader = new StringReader(rule.getContent());
- kfs.write("src/main/resources/rules/rule.drl",
- this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
+ kfs.write(RULES_FILE_NAME,
+ this.resources.newReaderResource(reader, UTF_8).setResourceType(ResourceType.DRL));
kieBuilder = ks.newKieBuilder(kfs).buildAll();
judgeRuleContent(locale, kieBuilder, true);
InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();;
- String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase(K_BASE).iterator().next().getName();
try {
kieContainer.updateToVersion(internalKieModule.getReleaseId());
} catch (Exception e) {
@@ -191,8 +195,8 @@ public class DroolsEngine { throws CorrelationException {
StringReader reader = new StringReader(content);
- kfs.write("src/main/resources/rules/rule.drl",
- this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
+ kfs.write(RULES_FILE_NAME,
+ this.resources.newReaderResource(reader, UTF_8).setResourceType(ResourceType.DRL));
kieBuilder = ks.newKieBuilder(kfs).buildAll();
@@ -216,7 +220,7 @@ public class DroolsEngine { if (internalKieModule == null) {
throw new CorrelationException("There are errors in the rule!");
}
- String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase(K_BASE).iterator().next().getName();
if (queryAllPackage().contains(packageName) && judgePackageName) {
throw new CorrelationException("The rule " + packageName + " already exists in the drools engine.");
@@ -254,7 +258,7 @@ public class DroolsEngine { private KieFileSystem createKieFileSystemWithKProject(KieServices ks) {
KieModuleModel kieModuleModel = ks.newKieModuleModel();
- KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("KBase")
+ KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel(K_BASE)
.addPackage("rules")
.setDefault(true)
.setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
@@ -271,10 +275,10 @@ public class DroolsEngine { private void avoidDeployBug() {
String tmp = Math.random() + "";
String rule = "package justInOrderToAvoidDeployBug" + tmp.substring(2);
- kfs.write("src/main/resources/rules/rule.drl", rule);
+ kfs.write(RULES_FILE_NAME, rule);
kieBuilder = ks.newKieBuilder(kfs).buildAll();
InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();
- String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase(K_BASE).iterator().next().getName();
kieRepository.addKieModule(internalKieModule);
kieContainer.updateToVersion(internalKieModule.getReleaseId());
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java index 8af9778..b38ee5f 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java @@ -15,7 +15,6 @@ */ package org.onap.holmes.engine.request; -import com.fasterxml.jackson.annotation.JsonProperty; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java index b9875da..4088d72 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java @@ -20,10 +20,10 @@ 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 java.util.regex.Matcher; +import java.util.regex.Pattern; + import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; @@ -51,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; @@ -70,6 +71,10 @@ public class EngineResources { Locale locale = LanguageUtil.getLocale(httpRequest); try { 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()); String packageNameRet = droolsEngine.deployRule(deployRuleRequest, locale); @@ -79,8 +84,8 @@ public class EngineResources { DmaapService.loopControlNames .put(packageNameRet, deployRuleRequest.getLoopControlName()); } - log.info("Rule deployed. Package name: " + packageName); - crResponse.setPackageName(packageName); + log.info("Rule deployed. Package name: " + packageNameRet); + crResponse.setPackageName(packageNameRet); } catch (CorrelationException correlationException) { log.error(correlationException.getMessage(), correlationException); @@ -131,10 +136,14 @@ 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; + Matcher m = packagePattern.matcher(contents); + + if (m.find( )) { + return m.group(1); + }else { + return null; + } } } diff --git a/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java b/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java index a20b578..6743b8e 100644 --- a/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java +++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/SwaggerResource.java @@ -16,14 +16,10 @@ package org.onap.holmes.engine.resources; -import io.swagger.annotations.Api; -import io.swagger.annotations.SwaggerDefinition; import java.io.BufferedReader; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import javax.ws.rs.GET; @@ -44,30 +40,29 @@ public class SwaggerResource { public String getSwaggerJson() { URL url = SwaggerResource.class.getResource("/swagger.json"); String ret = "{}"; - BufferedReader br = null; + File file = null; + try { System.out.println(URLDecoder.decode(url.getPath(), "UTF-8")); - File file = new File(URLDecoder.decode(url.getPath(), "UTF-8")); + file = new File(URLDecoder.decode(url.getPath(), "UTF-8")); + } catch (IOException e) { + log.warn("An error occurred while decoding url"); + } - br = new BufferedReader(new FileReader(file)); + if (file == null) { + log.warn("Unable to get Swagger Json since API description file could not be read"); + return ret; + } + + try(BufferedReader br = new BufferedReader(new FileReader(file))) { StringBuffer buffer = new StringBuffer(); String line = " "; while ((line = br.readLine()) != null) { buffer.append(line); } ret = buffer.toString(); - } catch (FileNotFoundException e) { - log.warn("Failed to read the API description file."); } catch (IOException e) { log.warn("An error occurred while reading swagger.json."); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException e) { - log.warn("Failed to close the file reader. This may cause memory leak."); - } - } } return ret; } |