diff options
Diffstat (limited to 'engine-d/src/main/java')
-rw-r--r-- | engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java | 3 | ||||
-rw-r--r-- | engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java | 193 |
2 files changed, 125 insertions, 71 deletions
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 c42e9d9..c748f85 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 @@ -20,17 +20,14 @@ import io.dropwizard.Configuration; import io.dropwizard.db.DataSourceFactory; import javax.validation.Valid; import javax.validation.constraints.NotNull; -import org.hibernate.validator.constraints.NotEmpty; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.config.MQConfig; @Service public class EngineDAppConfig extends Configuration { - @NotEmpty private String defaultName = "Correlation-Rule"; - @NotEmpty private String apidescription = "Holmes rule management rest API"; @JsonProperty 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 2ecea5e..d790ee7 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 @@ -14,29 +14,37 @@ * limitations under the License.
*/
package org.onap.holmes.engine.manager;
-
-
import java.io.StringReader;
+import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
-import org.drools.KnowledgeBase;
-import org.drools.KnowledgeBaseConfiguration;
-import org.drools.KnowledgeBaseFactory;
-import org.drools.builder.KnowledgeBuilder;
-import org.drools.builder.KnowledgeBuilderFactory;
-import org.drools.builder.ResourceType;
-import org.drools.conf.EventProcessingOption;
-import org.drools.definition.KnowledgePackage;
-import org.drools.io.Resource;
-import org.drools.io.ResourceFactory;
-import org.drools.runtime.StatefulKnowledgeSession;
-import org.drools.runtime.rule.FactHandle;
+import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.jvnet.hk2.annotations.Service;
+import org.kie.api.KieBase;
+import org.kie.api.KieServices;
+import org.kie.api.builder.KieBuilder;
+import org.kie.api.builder.KieFileSystem;
+import org.kie.api.builder.KieRepository;
+import org.kie.api.builder.Message;
+import org.kie.api.builder.Message.Level;
+import org.kie.api.builder.model.KieBaseModel;
+import org.kie.api.builder.model.KieModuleModel;
+import org.kie.api.builder.model.KieSessionModel;
+import org.kie.api.conf.EqualityBehaviorOption;
+import org.kie.api.conf.EventProcessingOption;
+import org.kie.api.definition.KiePackage;
+import org.kie.api.io.KieResources;
+import org.kie.api.io.ResourceType;
+import org.kie.api.runtime.KieContainer;
+import org.kie.api.runtime.KieSession;
+import org.kie.api.runtime.conf.ClockTypeOption;
+import org.kie.api.runtime.rule.FactHandle;
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.dmaap.DmaapService;
import org.onap.holmes.engine.request.DeployRuleRequest;
@@ -53,9 +61,15 @@ public class DroolsEngine { private final Set<String> packageNames = new HashSet<String>();
@Inject
private RuleMgtWrapper ruleMgtWrapper;
- private KnowledgeBase kbase;
- private KnowledgeBaseConfiguration kconf;
- private StatefulKnowledgeSession ksession;
+
+ private KieBase kieBase;
+ private KieSession kieSession;
+ private KieContainer kieContainer;
+ private KieFileSystem kfs;
+ private KieServices ks;
+ private KieBuilder kieBuilder;
+ private KieResources resources;
+ private KieRepository kieRepository;
@PostConstruct
private void init() {
@@ -78,19 +92,20 @@ public class DroolsEngine { }
public void stop() {
- this.ksession.dispose();
+ this.kieSession.dispose();
}
- private void initEngineParameter() {
- this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
-
- this.kconf.setOption(EventProcessingOption.STREAM);
+ public void initEngineParameter() {
+ this.ks = KieServices.Factory.get();
+ this.resources = ks.getResources();
+ this.kieRepository = ks.getRepository();
+ this.kfs = createKieFileSystemWithKProject(ks);
- this.kconf.setProperty("drools.assertBehaviour", "equality");
+ this.kieBuilder = ks.newKieBuilder(kfs).buildAll();
+ this.kieContainer = ks.newKieContainer(kieRepository.getDefaultReleaseId());
- this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
-
- this.ksession = kbase.newStatefulKnowledgeSession();
+ this.kieBase = kieContainer.getKieBase();
+ this.kieSession = kieContainer.newKieSession();
}
private void initDeployRule() throws CorrelationException {
@@ -108,100 +123,142 @@ public class DroolsEngine { }
private void deployRuleFromDB(String ruleContent) throws CorrelationException {
+ avoidDeployBug();
StringReader reader = new StringReader(ruleContent);
- Resource res = ResourceFactory.newReaderResource(reader);
-
- KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
- kbuilder.add(res, ResourceType.DRL);
-
+ kfs.write("src/main/resources/rules/rule.drl",
+ this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
+ kieBuilder = ks.newKieBuilder(kfs).buildAll();
try {
-
- kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();
+ kieContainer.updateToVersion(internalKieModule.getReleaseId());
} catch (Exception e) {
throw new CorrelationException(e.getMessage(), e);
}
- ksession.fireAllRules();
+ kieSession.fireAllRules();
}
public synchronized String deployRule(DeployRuleRequest rule, Locale locale)
throws CorrelationException {
+ avoidDeployBug();
StringReader reader = new StringReader(rule.getContent());
- Resource res = ResourceFactory.newReaderResource(reader);
-
- KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
-
- kbuilder.add(res, ResourceType.DRL);
+ kfs.write("src/main/resources/rules/rule.drl",
+ this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
+ kieBuilder = ks.newKieBuilder(kfs).buildAll();
- judgeRuleContent(locale, kbuilder, true);
+ judgeRuleContent(locale, kieBuilder, true);
- String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
+ InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();;
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
try {
- packageNames.add(packageName);
- kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ kieContainer.updateToVersion(internalKieModule.getReleaseId());
} catch (Exception e) {
throw new CorrelationException("Failed to deploy the rule.", e);
}
-
- ksession.fireAllRules();
+ packageNames.add(packageName);
+ kieSession.fireAllRules();
return packageName;
}
public synchronized void undeployRule(String packageName, Locale locale)
throws CorrelationException {
-
- KnowledgePackage pkg = kbase.getKnowledgePackage(packageName);
-
- if (null == pkg) {
+ KiePackage kiePackage = kieBase.getKiePackage(packageName);
+ if (null == kiePackage) {
throw new CorrelationException("The rule " + packageName + " does not exist!");
}
-
try {
- kbase.removeKnowledgePackage(pkg.getName());
+ kieBase.removeKiePackage(kiePackage.getName());
} catch (Exception e) {
throw new CorrelationException("Failed to delete the rule: " + packageName, e);
}
- packageNames.remove(pkg.getName());
+ packageNames.remove(kiePackage.getName());
}
public void compileRule(String content, Locale locale)
throws CorrelationException {
StringReader reader = new StringReader(content);
- Resource res = ResourceFactory.newReaderResource(reader);
- KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ kfs.write("src/main/resources/rules/rule.drl",
+ this.resources.newReaderResource(reader,"UTF-8").setResourceType(ResourceType.DRL));
- kbuilder.add(res, ResourceType.DRL);
+ kieBuilder = ks.newKieBuilder(kfs).buildAll();
- judgeRuleContent(locale, kbuilder, false);
+ judgeRuleContent(locale, kieBuilder, false);
}
- private void judgeRuleContent(Locale locale, KnowledgeBuilder kbuilder, boolean judgePackageName)
+ private void judgeRuleContent(Locale locale, KieBuilder kbuilder, boolean judgePackageName)
throws CorrelationException {
- if (kbuilder.hasErrors()) {
- String errorMsg = "There are errors in the rule: " + kbuilder.getErrors().toString();
+ if (kbuilder.getResults().hasMessages(Message.Level.ERROR)) {
+ String errorMsg = "There are errors in the rule: " + kbuilder.getResults()
+ .getMessages(Level.ERROR).toString();
log.error(errorMsg);
throw new CorrelationException(errorMsg);
}
+ InternalKieModule internalKieModule = null;
+ try {
+ internalKieModule = (InternalKieModule) kbuilder.getKieModule();
+ } catch (Exception e) {
+ throw new CorrelationException("There are errors in the rule!" + e.getMessage(), e);
+ }
+ if (internalKieModule == null) {
+ throw new CorrelationException("There are errors in the rule!");
+ }
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
- String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
-
- if (packageNames.contains(packageName) && judgePackageName) {
+ if (queryAllPackage().contains(packageName) && judgePackageName) {
throw new CorrelationException("The rule " + packageName + " already exists in the drools engine.");
}
}
public void putRaisedIntoStream(VesAlarm raiseAlarm) {
- FactHandle factHandle = this.ksession.getFactHandle(raiseAlarm);
+ FactHandle factHandle = this.kieSession.getFactHandle(raiseAlarm);
if (factHandle != null) {
- Object obj = this.ksession.getObject(factHandle);
+ Object obj = this.kieSession.getObject(factHandle);
if (obj != null && obj instanceof VesAlarm) {
raiseAlarm.setRootFlag(((VesAlarm) obj).getRootFlag());
}
- this.ksession.retract(factHandle);
+ this.kieSession.delete(factHandle);
}
- this.ksession.insert(raiseAlarm);
- this.ksession.fireAllRules();
+ this.kieSession.insert(raiseAlarm);
+ this.kieSession.fireAllRules();
+ }
+
+ public List<String> queryAllPackage() {
+ List<KiePackage> kiePackages = (List<KiePackage>)kieBase.getKiePackages();
+ List<String> list = new ArrayList<>();
+ for(KiePackage kiePackage : kiePackages) {
+ list.add(kiePackage.getName());
+ }
+ return list;
+ }
+
+ private KieFileSystem createKieFileSystemWithKProject(KieServices ks) {
+ KieModuleModel kieModuleModel = ks.newKieModuleModel();
+ KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("KBase")
+ .addPackage("rules")
+ .setDefault(true)
+ .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
+ .setEventProcessingMode(EventProcessingOption.STREAM);
+ KieSessionModel kieSessionModel = kieBaseModel.newKieSessionModel("KSession")
+ .setDefault( true )
+ .setType( KieSessionModel.KieSessionType.STATEFUL )
+ .setClockType( ClockTypeOption.get("realtime") );
+ KieFileSystem kfs = ks.newKieFileSystem();
+ kfs.writeKModuleXML(kieModuleModel.toXML());
+ return kfs;
+ }
+
+ private void avoidDeployBug() {
+ String tmp = Math.random() + "";
+ String rule = "package justInOrderToAvoidDeployBug" + tmp.substring(2);
+ kfs.write("src/main/resources/rules/rule.drl", rule);
+ kieBuilder = ks.newKieBuilder(kfs).buildAll();
+ InternalKieModule internalKieModule = (InternalKieModule)kieBuilder.getKieModule();
+ String packageName = internalKieModule.getKnowledgePackagesForKieBase("KBase").iterator().next().getName();
+ kieRepository.addKieModule(internalKieModule);
+ kieContainer.updateToVersion(internalKieModule.getReleaseId());
+
+ KiePackage kiePackage = kieBase.getKiePackage(packageName);
+ kieBase.removeKiePackage(kiePackage.getName());
}
}
|