summaryrefslogtreecommitdiffstats
path: root/engine-d/src/main/java/org/onap
diff options
context:
space:
mode:
Diffstat (limited to 'engine-d/src/main/java/org/onap')
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java54
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java69
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java37
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java48
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java285
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java30
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java33
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java115
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java28
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java99
-rw-r--r--engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java43
11 files changed, 841 insertions, 0 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
new file mode 100644
index 0000000..ef671e3
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDActiveApp.java
@@ -0,0 +1,54 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine;
+
+import io.dropwizard.setup.Environment;
+import java.io.IOException;
+import lombok.extern.slf4j.Slf4j;
+import org.onap.holmes.common.dropwizard.ioc.bundle.IOCApplication;
+import org.onap.holmes.common.api.entity.ServiceRegisterEntity;
+import org.onap.holmes.common.config.MicroServiceConfig;
+import org.onap.holmes.common.utils.MSBRegisterUtil;
+
+@Slf4j
+public class EngineDActiveApp extends IOCApplication<EngineDAppConfig> {
+
+ public static void main(String[] args) throws Exception {
+ new EngineDActiveApp().run(args);
+ }
+
+ @Override
+ public void run(EngineDAppConfig configuration, Environment environment) throws Exception {
+ super.run(configuration, environment);
+
+ try {
+ new MSBRegisterUtil().register(initServiceEntity());
+ } catch (IOException e) {
+ log.warn("Micro service registry httpclient close failure", e);
+ }
+ }
+
+ private ServiceRegisterEntity initServiceEntity() {
+ ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();
+ serviceRegisterEntity.setServiceName("holmes-engine-mgmt");
+ serviceRegisterEntity.setProtocol("REST");
+ serviceRegisterEntity.setVersion("v1");
+ serviceRegisterEntity.setUrl("/onapapi/holmes-engine-mgmt/v1");
+ serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9102", 0);
+ serviceRegisterEntity.setVisualRange("1|0");
+ return serviceRegisterEntity;
+ }
+}
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
new file mode 100644
index 0000000..c42e9d9
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/EngineDAppConfig.java
@@ -0,0 +1,69 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+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
+ @NotNull
+ @Valid
+ private MQConfig mqConfig;
+ @Valid
+ @NotNull
+ private DataSourceFactory database = new DataSourceFactory();
+
+ public MQConfig getMqConfig() {
+ return mqConfig;
+ }
+
+ public void setMqConfig(MQConfig mqConfig) {
+ this.mqConfig = mqConfig;
+ }
+
+ @JsonProperty("database")
+ public DataSourceFactory getDataSourceFactory() {
+ return database;
+ }
+
+ @JsonProperty("database")
+ public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
+ this.database = dataSourceFactory;
+ }
+
+ public String getApidescription() {
+ return apidescription;
+ }
+
+ public void setApidescription(String apidescription) {
+ this.apidescription = apidescription;
+ }
+}
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java
new file mode 100644
index 0000000..4e01778
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/db/CorrelationRuleDao.java
@@ -0,0 +1,37 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.db;
+
+
+import java.util.List;
+import org.onap.holmes.engine.db.mapper.CorrelationRuleMapper;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.skife.jdbi.v2.sqlobject.Bind;
+import org.skife.jdbi.v2.sqlobject.SqlQuery;
+import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
+
+@RegisterMapper(CorrelationRuleMapper.class)
+public abstract class CorrelationRuleDao {
+
+
+ @SqlQuery("SELECT * FROM APLUS_RULE WHERE enable=:enable")
+ public abstract List<CorrelationRule> queryRuleByEnable(@Bind("enable") int enable);
+
+ public List<CorrelationRule> queryRuleByRuleEnable(int enable) {
+ return queryRuleByEnable(enable);
+ }
+}
+
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java b/engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java
new file mode 100644
index 0000000..a682824
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/db/mapper/CorrelationRuleMapper.java
@@ -0,0 +1,48 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.db.mapper;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.skife.jdbi.v2.StatementContext;
+import org.skife.jdbi.v2.tweak.ResultSetMapper;
+
+public class CorrelationRuleMapper implements ResultSetMapper<CorrelationRule> {
+
+ public CorrelationRule map(int i, ResultSet resultSet, StatementContext statementContext)
+ throws SQLException {
+ CorrelationRule correlationRule = new CorrelationRule();
+ correlationRule.setName(resultSet.getString("name"));
+ correlationRule.setRid(resultSet.getString("rid"));
+ correlationRule.setDescription(resultSet.getString("description"));
+ correlationRule.setEnabled(resultSet.getInt("enable"));
+ correlationRule.setTemplateID(resultSet.getInt("templateID"));
+ correlationRule.setEngineID(resultSet.getString("engineID"));
+ correlationRule.setEngineType(resultSet.getString("engineType"));
+ correlationRule.setCreator(resultSet.getString("creator"));
+ correlationRule.setCreateTime(resultSet.getDate("createTime"));
+ correlationRule.setModifier(resultSet.getString("updator"));
+ correlationRule.setUpdateTime(resultSet.getDate("updateTime"));
+ correlationRule.setParams((Properties) resultSet.getObject("params"));
+ correlationRule.setContent(resultSet.getString("content"));
+ correlationRule.setVendor(resultSet.getString("vendor"));
+ correlationRule.setPackageName(resultSet.getString("package"));
+ return correlationRule;
+ }
+
+}
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
new file mode 100644
index 0000000..c8835cc
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/manager/DroolsEngine.java
@@ -0,0 +1,285 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.manager;
+
+
+import java.io.Serializable;
+import java.io.StringReader;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.Session;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.command.ActiveMQObjectMessage;
+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.glassfish.hk2.api.IterableProvider;
+import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.engine.request.DeployRuleRequest;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.api.stat.Alarm;
+import org.onap.holmes.common.config.MQConfig;
+import org.onap.holmes.common.constant.AlarmConst;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.ExceptionUtil;
+import org.onap.holmes.engine.wrapper.RuleMgtWrapper;
+
+@Slf4j
+@Service
+public class DroolsEngine {
+
+ private final static int ENABLE = 1;
+ private final Set<String> packageNames = new HashSet<String>();
+ @Inject
+ private RuleMgtWrapper ruleMgtWrapper;
+ private KnowledgeBase kbase;
+ private KnowledgeBaseConfiguration kconf;
+ private StatefulKnowledgeSession ksession;
+ @Inject
+ private IterableProvider<MQConfig> mqConfigProvider;
+ private ConnectionFactory connectionFactory;
+
+ @PostConstruct
+ private void init() {
+ try {
+ // 1. start engine
+ start();
+ // 2. start mq listener
+ registerAlarmTopicListener();
+ } catch (Exception e) {
+ log.error("Failed to start the service: " + e.getMessage(), e);
+ throw ExceptionUtil.buildExceptionResponse("Failed to start the drools engine!");
+ }
+ }
+
+ private void registerAlarmTopicListener() {
+ String brokerURL =
+ "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort;
+ connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername,
+ mqConfigProvider.get().brokerPassword, brokerURL);
+
+ AlarmMqMessageListener listener = new AlarmMqMessageListener();
+ listener.receive();
+ }
+
+
+ private void start() throws CorrelationException {
+ log.info("Drools Engine Initialize Beginning...");
+
+ initEngineParameter();
+ initDeployRule();
+
+ log.info("Business Rule Engine Initialize Successfully.");
+ }
+
+ public void stop() {
+ this.ksession.dispose();
+ }
+
+ private void initEngineParameter() {
+ this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
+
+ this.kconf.setOption(EventProcessingOption.STREAM);
+
+ this.kconf.setProperty("drools.assertBehaviour", "equality");
+
+ this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
+
+ this.ksession = kbase.newStatefulKnowledgeSession();
+ }
+
+ private void initDeployRule() throws CorrelationException {
+ List<CorrelationRule> rules = ruleMgtWrapper.queryRuleByEnable(ENABLE);
+
+ if (rules.isEmpty()) {
+ return;
+ }
+ for (CorrelationRule rule : rules) {
+ if (rule.getContent() != null) {
+ deployRuleFromDB(rule.getContent());
+ }
+ }
+ }
+
+ private void deployRuleFromDB(String ruleContent) throws CorrelationException {
+ StringReader reader = new StringReader(ruleContent);
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ try {
+
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ } catch (Exception e) {
+ throw new CorrelationException(e.getMessage(), e);
+ }
+ ksession.fireAllRules();
+ }
+
+ public synchronized String deployRule(DeployRuleRequest rule, Locale locale)
+ throws CorrelationException {
+ StringReader reader = new StringReader(rule.getContent());
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ judgeRuleContent(locale, kbuilder, true);
+
+ String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
+ try {
+ packageNames.add(packageName);
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to deploy the rule.", e);
+ }
+
+ ksession.fireAllRules();
+ return packageName;
+ }
+
+ public synchronized void undeployRule(String packageName, Locale locale)
+ throws CorrelationException {
+
+ KnowledgePackage pkg = kbase.getKnowledgePackage(packageName);
+
+ if (null == pkg) {
+ throw new CorrelationException("The rule " + packageName + " does not exist!");
+ }
+
+ try {
+ kbase.removeKnowledgePackage(pkg.getName());
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to delete the rule: " + packageName, e);
+ }
+ }
+
+ public void compileRule(String content, Locale locale)
+ throws CorrelationException {
+ StringReader reader = new StringReader(content);
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ judgeRuleContent(locale, kbuilder, false);
+ }
+
+ private void judgeRuleContent(Locale locale, KnowledgeBuilder kbuilder, boolean judgePackageName)
+ throws CorrelationException {
+ if (kbuilder.hasErrors()) {
+ String errorMsg = "There are errors in the rule: " + kbuilder.getErrors().toString();
+ log.error(errorMsg);
+ throw new CorrelationException(errorMsg);
+ }
+
+ String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
+
+ if (packageNames.contains(packageName) && judgePackageName) {
+ throw new CorrelationException("The rule " + packageName + " already exists in the drools engine.");
+ }
+ }
+
+ public void putRaisedIntoStream(Alarm raiseAlarm) {
+ FactHandle factHandle = this.ksession.getFactHandle(raiseAlarm);
+ if (factHandle != null) {
+ this.ksession.retract(factHandle);
+ }
+ this.ksession.insert(raiseAlarm);
+ this.ksession.fireAllRules();
+ }
+
+ class AlarmMqMessageListener implements MessageListener {
+
+ private Connection connection = null;
+ private Session session = null;
+ private Destination destination = null;
+ private MessageConsumer consumer = null;
+
+ private void initialize() throws JMSException {
+ connection = connectionFactory.createConnection();
+ session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM);
+ consumer = session.createConsumer(destination);
+ connection.start();
+ }
+
+ public void receive() {
+ try {
+ initialize();
+ consumer.setMessageListener(this);
+ } catch (JMSException e) {
+ log.error("Failed to connect to the MQ service : " + e.getMessage(), e);
+ try {
+ close();
+ } catch (JMSException e1) {
+ log.error("Failed close connection " + e1.getMessage(), e1);
+ }
+ }
+ }
+
+ public void onMessage(Message arg0) {
+ ActiveMQObjectMessage objectMessage = (ActiveMQObjectMessage) arg0;
+ try {
+ Serializable object = objectMessage.getObject();
+
+ if (object instanceof Alarm) {
+ Alarm alarm = (Alarm) object;
+ putRaisedIntoStream(alarm);
+ }
+ } catch (JMSException e) {
+ log.error("Failed get object : " + e.getMessage(), e);
+ }
+ }
+
+ private void close() throws JMSException {
+ if (consumer != null) {
+ consumer.close();
+ }
+ if (session != null) {
+ session.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ }
+ }
+}
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
new file mode 100644
index 0000000..6e0da48
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/request/CompileRuleRequest.java
@@ -0,0 +1,30 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.request;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class CompileRuleRequest {
+
+ @JsonProperty(value = "content")
+ @NotNull
+ private String content;
+} \ No newline at end of file
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java
new file mode 100644
index 0000000..de9d773
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/request/DeployRuleRequest.java
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.request;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import javax.validation.constraints.NotNull;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class DeployRuleRequest {
+
+ @JsonProperty(value = "content")
+ @NotNull
+ private String content;
+
+ @JsonProperty(value = "engineid")
+ private String engineId;
+}
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
new file mode 100644
index 0000000..3480dbb
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/resources/EngineResources.java
@@ -0,0 +1,115 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.resources;
+
+
+import com.codahale.metrics.annotation.Timed;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import java.util.Locale;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+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.exception.CorrelationException;
+import org.onap.holmes.common.utils.ExceptionUtil;
+import org.onap.holmes.common.utils.LanguageUtil;
+import org.onap.holmes.engine.manager.DroolsEngine;
+import org.onap.holmes.engine.request.CompileRuleRequest;
+import org.onap.holmes.engine.request.DeployRuleRequest;
+import org.onap.holmes.engine.response.CorrelationRuleResponse;
+
+@Service
+@Path("/rule")
+@Api(tags = {"Engine Manager"})
+@Produces(MediaType.APPLICATION_JSON)
+@Slf4j
+public class EngineResources {
+
+ @Inject
+ DroolsEngine droolsEngine;
+
+ @PUT
+ @ApiOperation(value = "Add rule to Engine and Cache", response = CorrelationRuleResponse.class)
+ @Produces(MediaType.APPLICATION_JSON)
+ @Timed
+ public CorrelationRuleResponse deployRule(DeployRuleRequest deployRuleRequest,
+ @Context HttpServletRequest httpRequest) {
+
+ CorrelationRuleResponse crResponse = new CorrelationRuleResponse();
+ Locale locale = LanguageUtil.getLocale(httpRequest);
+ try {
+
+ String packageName = droolsEngine.deployRule(deployRuleRequest, locale);
+ log.info("Rule deployed. Package name: " + packageName);
+ crResponse.setPackageName(packageName);
+
+ } catch (CorrelationException correlationException) {
+ log.error(correlationException.getMessage(), correlationException);
+ throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
+ }
+
+ return crResponse;
+ }
+
+ @DELETE
+ @ApiOperation(value = "delete rule")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Timed
+ @Path("/{packageName}")
+ public boolean undeployRule(@PathParam("packageName") String packageName,
+ @Context HttpServletRequest httpRequest) {
+
+ Locale locale = LanguageUtil.getLocale(httpRequest);
+
+ try {
+
+ droolsEngine.undeployRule(packageName, locale);
+
+ } catch (CorrelationException correlationException) {
+ log.error(correlationException.getMessage(), correlationException);
+ throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
+ }
+ return true;
+ }
+
+
+ @POST
+ @ApiOperation(value = "compile rule")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Timed
+ public boolean compileRule(CompileRuleRequest compileRuleRequest,
+ @Context HttpServletRequest httpRequest) {
+
+ Locale locale = LanguageUtil.getLocale(httpRequest);
+
+ try {
+ droolsEngine.compileRule(compileRuleRequest.getContent(), locale);
+ } catch (CorrelationException correlationException) {
+ log.error(correlationException.getMessage(), correlationException);
+ throw ExceptionUtil.buildExceptionResponse(correlationException.getMessage());
+ }
+ return true;
+ }
+}
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java
new file mode 100644
index 0000000..b234f91
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/response/CorrelationRuleResponse.java
@@ -0,0 +1,28 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.response;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class CorrelationRuleResponse {
+
+ @JsonProperty(value = "package")
+ private String packageName;
+}
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java
new file mode 100644
index 0000000..ca93066
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/utils/AlarmUtil.java
@@ -0,0 +1,99 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.common.api.stat.Alarm;
+import org.onap.holmes.common.producer.MQProducer;
+
+@Service
+public class AlarmUtil {
+
+ private final static AlarmUtil alarmUtil = new AlarmUtil();
+ /**
+ * Map<ruleId, <ProbableCause-EquipType, priority>>
+ */
+ private final Map<String, Map<String, Integer>> rootPriorityMap =
+ new HashMap<String, Map<String, Integer>>();
+ /**
+ * Map<rule, ProbableCause+EquipType+priority>
+ */
+ private final Map<String, String> saveRuleMsg = new HashMap<String, String>();
+
+ private AlarmUtil() {
+ }
+
+ public static AlarmUtil getInstance() {
+ return alarmUtil;
+ }
+
+ public boolean equipTypeFilter(String probableCauseStr, String equipType, Alarm alarm) {
+ if ("null".equals(probableCauseStr)) {
+ return true;
+ }
+ String[] equipTypes = equipType.replace(" ", "").split(",");
+ String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(",");
+ for (int i = 0; i < probableCauseStrs.length; i++) {
+ if (alarm.getProbableCause() == Long.parseLong(probableCauseStrs[i])
+ && alarm.getEquipType().equals(equipTypes[i])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public Integer getPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr,
+ String equipTypeStr, Alarm alarm) {
+ if (rootPriorityMap.containsKey(ruleId)) {
+ if (!saveRuleMsg.get(ruleId)
+ .equals(probableCauseStr + equipTypeStr + rootAlarmFeatureStr)) {
+ setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr);
+ }
+ } else {
+ setPriority(ruleId, probableCauseStr, rootAlarmFeatureStr, equipTypeStr);
+ }
+
+ Integer priority =
+ rootPriorityMap.get(ruleId).get(alarm.getProbableCause() + "-" + alarm.getEquipType());
+ if (priority == null) {
+ priority = 0;
+ }
+ return priority;
+ }
+
+ private void setPriority(String ruleId, String probableCauseStr, String rootAlarmFeatureStr,
+ String equipTypeStr) {
+ saveRuleMsg.put(ruleId, probableCauseStr + equipTypeStr + rootAlarmFeatureStr);
+
+ Map<String, Integer> map = new HashMap<String, Integer>();
+ String[] probableCauseStrs = probableCauseStr.replace(" ", "").split(",");
+ String[] rootAlarmFeatureStrs = rootAlarmFeatureStr.replace(" ", "").split(",");
+ String[] equipTypes = equipTypeStr.replace(" ", "").split(",");
+ for (int i = 0; i < rootAlarmFeatureStrs.length; i++) {
+ map.put(probableCauseStrs[i] + "-" + equipTypes[i],
+ Integer.parseInt(rootAlarmFeatureStrs[i]));
+ }
+
+ rootPriorityMap.put(ruleId, map);
+ }
+
+ public MQProducer getMqProducer() {
+ MQProducer mqProducer = new MQProducer();
+ return mqProducer;
+ }
+}
diff --git a/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java
new file mode 100644
index 0000000..1fbbfe7
--- /dev/null
+++ b/engine-d/src/main/java/org/onap/holmes/engine/wrapper/RuleMgtWrapper.java
@@ -0,0 +1,43 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.engine.wrapper;
+
+import java.util.List;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import lombok.extern.slf4j.Slf4j;
+import org.jvnet.hk2.annotations.Service;
+import org.onap.holmes.engine.db.CorrelationRuleDao;
+import org.onap.holmes.common.api.entity.CorrelationRule;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.DbDaoUtil;
+
+
+@Service
+@Singleton
+@Slf4j
+public class RuleMgtWrapper {
+
+ @Inject
+ private DbDaoUtil daoUtil;
+
+ public List<CorrelationRule> queryRuleByEnable(int enable) throws CorrelationException {
+
+ List<CorrelationRule> ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .queryRuleByRuleEnable(enable);
+ return ruleTemp;
+ }
+}