summaryrefslogtreecommitdiffstats
path: root/engine-d/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'engine-d/src/main/java')
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java25
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java68
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/db/CorrelationRuleDao.java37
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java50
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/listener/AlarmMqMessageListener.java83
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java215
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/request/CompileRuleRequest.java30
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/request/DeployRuleRequest.java33
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/resources/EngineResources.java140
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java28
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/utils/AlarmUtil.java99
-rw-r--r--engine-d/src/main/java/org/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java47
12 files changed, 855 insertions, 0 deletions
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java
new file mode 100644
index 0000000..ddaed97
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java
@@ -0,0 +1,25 @@
+/**
+ * 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.openo.holmes.enginemgt;
+
+import org.openo.dropwizard.ioc.bundle.IOCApplication;
+
+public class EngineActiveApp extends IOCApplication<EngineAppConfig> {
+
+ public static void main(String[] args) throws Exception {
+ new EngineActiveApp().run(args);
+ }
+}
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java
new file mode 100644
index 0000000..b01d173
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java
@@ -0,0 +1,68 @@
+/**
+ * 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.openo.holmes.enginemgt;
+
+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 lombok.Getter;
+import org.hibernate.validator.constraints.NotEmpty;
+import org.openo.holmes.common.config.MQConfig;
+
+public class EngineAppConfig 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/openo/holmes/enginemgt/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/db/CorrelationRuleDao.java
new file mode 100644
index 0000000..2393417
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/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.openo.holmes.enginemgt.db;
+
+
+import java.util.List;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.enginemgt.db.mapper.CorrelationRuleMapper;
+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/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java
new file mode 100644
index 0000000..7d9ed97
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java
@@ -0,0 +1,50 @@
+/**
+ * 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.openo.holmes.enginemgt.db.mapper;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Properties;
+import org.openo.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.setDomain(resultSet.getString("domain"));
+ correlationRule.setContent(resultSet.getString("content"));
+ correlationRule.setIsManual(resultSet.getInt("isManual"));
+ correlationRule.setVendor(resultSet.getString("vendor"));
+ correlationRule.setPackageName(resultSet.getString("package"));
+ return correlationRule;
+ }
+
+}
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/listener/AlarmMqMessageListener.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/listener/AlarmMqMessageListener.java
new file mode 100644
index 0000000..f1bc622
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/listener/AlarmMqMessageListener.java
@@ -0,0 +1,83 @@
+/**
+ * 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.openo.holmes.enginemgt.listener;
+
+
+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.MessageConsumer;
+import javax.jms.ObjectMessage;
+import javax.jms.Session;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.glassfish.hk2.api.IterableProvider;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.api.stat.Alarm;
+import org.openo.holmes.common.config.MQConfig;
+import org.openo.holmes.common.constant.AlarmConst;
+import org.openo.holmes.enginemgt.manager.DroolsEngine;
+
+@Service
+@Slf4j
+public class AlarmMqMessageListener implements Runnable {
+
+ @Inject
+ private static IterableProvider<MQConfig> mqConfigProvider;
+ @Inject
+ DroolsEngine droolsEngine;
+ private ConnectionFactory connectionFactory;
+
+ @PostConstruct
+ public void init() {
+
+ String brokerURL =
+ "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort;
+ connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername,
+ mqConfigProvider.get().brokerPassword, brokerURL);
+ }
+
+
+ public void run() {
+ Connection connection;
+ Session session;
+ Destination destination;
+ MessageConsumer messageConsumer;
+
+ try {
+ connection = connectionFactory.createConnection();
+ connection.start();
+ session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM);
+ messageConsumer = session.createConsumer(destination);
+
+ while (true) {
+ ObjectMessage objMessage = (ObjectMessage) messageConsumer.receive(100000);
+ if (objMessage != null) {
+ droolsEngine.putRaisedIntoStream((Alarm) objMessage.getObject());
+ } else {
+ break;
+ }
+ }
+ } catch (JMSException e) {
+ log.debug("Receive alarm failure" + e.getMessage());
+ }
+
+ }
+}
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java
new file mode 100644
index 0000000..49b2042
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java
@@ -0,0 +1,215 @@
+/**
+ * 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.openo.holmes.enginemgt.manager;
+
+
+import java.io.StringReader;
+import java.util.List;
+import java.util.Locale;
+import javax.annotation.PostConstruct;
+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.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.api.stat.Alarm;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.exception.EngineException;
+import org.openo.holmes.common.exception.RuleIllegalityException;
+import org.openo.holmes.common.utils.ExceptionUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.enginemgt.listener.AlarmMqMessageListener;
+import org.openo.holmes.enginemgt.request.DeployRuleRequest;
+import org.openo.holmes.enginemgt.wrapper.RuleMgtWrapper;
+
+@Slf4j
+@Service
+public class DroolsEngine {
+
+ private final static String CORRELATION_RULE = "CORRELATION_RULE";
+ private final static String CORRELATION_ALARM = "CORRELATION_ALARM";
+ private final static int ENABLE = 1;
+ @Inject
+ private RuleMgtWrapper ruleMgtWrapper;
+ @Inject
+ private AlarmMqMessageListener mqRegister;
+ private KnowledgeBase kbase;
+ private KnowledgeBaseConfiguration kconf;
+ private StatefulKnowledgeSession ksession;
+ private KnowledgeBuilder kbuilder;
+
+ @PostConstruct
+ private void init() {
+ registerAlarmTopicListener();
+ try {
+ start();
+ } catch (Exception e) {
+ log.error("Start service failed: " + e.getMessage());
+ throw ExceptionUtil.buildExceptionResponse("Start service failed!");
+ }
+ }
+
+ private void registerAlarmTopicListener() {
+ Thread thread = new Thread(mqRegister);
+ thread.start();
+ }
+
+
+ private void start() throws EngineException, RuleIllegalityException, DbException {
+ log.info("Drools Egine Initialize Begining ... ");
+
+ initEngineParameter();
+ initDeployRule();
+
+ log.info("Business Rule Egine Initialize Successfully ");
+ }
+
+ public void stop() throws Exception {
+ this.ksession.dispose();
+ }
+
+ private void initEngineParameter() throws EngineException {
+ this.kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
+
+ this.kconf.setOption(EventProcessingOption.STREAM);
+
+ this.kconf.setProperty("drools.assertBehaviour", "equality");
+
+ this.kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ this.kbase = KnowledgeBaseFactory.newKnowledgeBase("D-ENGINE", this.kconf);
+
+ this.ksession = kbase.newStatefulKnowledgeSession();
+ }
+
+ private void initDeployRule() throws RuleIllegalityException, EngineException, DbException {
+ List<CorrelationRule> rules = ruleMgtWrapper.queryRuleByEnable(ENABLE);
+
+ if (rules.size() > 0) {
+ for (CorrelationRule rule : rules) {
+ if (rule.getContent() != null) {
+ deployRuleFromCache(rule.getContent());
+ }
+ }
+ }
+ }
+
+ private void deployRuleFromCache(String ruleContent) throws EngineException {
+ StringReader reader = new StringReader(ruleContent);
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ try {
+
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ } catch (Exception e) {
+ throw new EngineException(e);
+ }
+
+ kbuilder = null;
+
+ ksession.fireAllRules();
+ }
+
+ public synchronized String deployRule(DeployRuleRequest rule, Locale locale)
+ throws RuleIllegalityException, EngineException {
+ StringReader reader = new StringReader(rule.getContent());
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ if (kbuilder.hasErrors()) {
+ throw new RuleIllegalityException(kbuilder.getErrors().toString());
+ }
+
+ String packageName = kbuilder.getKnowledgePackages().iterator().next().getName();
+
+ if (kbase.getKnowledgePackages().contains(packageName)) {
+ throw new RuleIllegalityException(
+ I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE));
+ }
+ try {
+
+ kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
+ } catch (Exception e) {
+ throw new EngineException(e);
+ }
+
+ kbuilder = null;
+
+ ksession.fireAllRules();
+ return packageName;
+ }
+
+
+ public synchronized void undeployRule(String packageName)
+ throws RuleIllegalityException, EngineException {
+
+ KnowledgePackage pkg = kbase.getKnowledgePackage(packageName);
+
+ if (null == pkg) {
+ throw new RuleIllegalityException(packageName);
+ }
+
+ try {
+
+ kbase.removeKnowledgePackage(pkg.getName());
+ } catch (Exception e) {
+
+ throw new EngineException(e);
+ }
+ }
+
+ public void compileRule(String content, Locale locale)
+ throws RuleIllegalityException {
+ StringReader reader = new StringReader(content);
+ Resource res = ResourceFactory.newReaderResource(reader);
+
+ kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ kbuilder.add(res, ResourceType.DRL);
+
+ if (kbuilder.hasErrors()) {
+ throw new RuleIllegalityException(kbuilder.getErrors().toString());
+ }
+ kbuilder = null;
+ }
+
+ 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();
+ }
+}
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/request/CompileRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/request/CompileRuleRequest.java
new file mode 100644
index 0000000..bf056be
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/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.openo.holmes.enginemgt.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/openo/holmes/enginemgt/request/DeployRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/request/DeployRuleRequest.java
new file mode 100644
index 0000000..b1d9346
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/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.openo.holmes.enginemgt.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/openo/holmes/enginemgt/resources/EngineResources.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/resources/EngineResources.java
new file mode 100644
index 0000000..59fb9e3
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/resources/EngineResources.java
@@ -0,0 +1,140 @@
+/**
+ * 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.openo.holmes.enginemgt.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.openo.holmes.common.exception.EngineException;
+import org.openo.holmes.common.exception.RuleIllegalityException;
+import org.openo.holmes.common.utils.ExceptionUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.common.utils.LanguageUtil;
+import org.openo.holmes.enginemgt.manager.DroolsEngine;
+import org.openo.holmes.enginemgt.request.CompileRuleRequest;
+import org.openo.holmes.enginemgt.request.DeployRuleRequest;
+import org.openo.holmes.enginemgt.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);
+ crResponse.setPackageName(packageName);
+
+ } catch (RuleIllegalityException ruleIllegalityException) {
+
+ String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
+ I18nProxy.ENGINE_CONTENT_ILLEGALITY,
+ new String[]{ruleIllegalityException.getMessage()});
+ log.error(errorMsg);
+ throw ExceptionUtil.buildExceptionResponse(errorMsg);
+ } catch (EngineException e) {
+
+ String errorMsg =
+ I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED);
+ log.error(errorMsg + ":" + e.getMessage());
+ throw ExceptionUtil.buildExceptionResponse(errorMsg);
+ }
+
+ 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);
+
+ } catch (RuleIllegalityException ruleIllegalityException) {
+
+ String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
+ I18nProxy.ENGINE_DELETE_RULE_NULL,
+ new String[]{ruleIllegalityException.getMessage()});
+ log.error(errorMsg);
+ throw ExceptionUtil.buildExceptionResponse(errorMsg);
+ } catch (EngineException e) {
+
+ String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
+ I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName});
+ log.error(errorMsg + e.getMessage());
+ throw ExceptionUtil.buildExceptionResponse(errorMsg);
+ }
+ 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 (RuleIllegalityException ruleIllegalityException) {
+
+ String errorMsg = I18nProxy.getInstance().getValueByArgs(locale,
+ I18nProxy.ENGINE_CONTENT_ILLEGALITY,
+ new String[]{ruleIllegalityException.getMessage()});
+ log.error(errorMsg);
+ throw ExceptionUtil.buildExceptionResponse(errorMsg);
+ }
+ return true;
+ }
+}
diff --git a/engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java
new file mode 100644
index 0000000..6c9842c
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/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.openo.holmes.enginemgt.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/openo/holmes/enginemgt/utils/AlarmUtil.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/utils/AlarmUtil.java
new file mode 100644
index 0000000..2d5b5ca
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/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.openo.holmes.enginemgt.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.holmes.common.api.stat.Alarm;
+import org.openo.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/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java
new file mode 100644
index 0000000..35890ca
--- /dev/null
+++ b/engine-d/src/main/java/org/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java
@@ -0,0 +1,47 @@
+/**
+ * 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.openo.holmes.enginemgt.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.openo.holmes.common.api.entity.CorrelationRule;
+import org.openo.holmes.common.exception.DbException;
+import org.openo.holmes.common.utils.DbDaoUtil;
+import org.openo.holmes.common.utils.I18nProxy;
+import org.openo.holmes.enginemgt.db.CorrelationRuleDao;
+
+
+@Service
+@Singleton
+@Slf4j
+public class RuleMgtWrapper {
+
+ @Inject
+ private DbDaoUtil daoUtil;
+
+ public List<CorrelationRule> queryRuleByEnable(int enable) throws DbException {
+
+ List<CorrelationRule> ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class)
+ .queryRuleByRuleEnable(enable);
+ if (ruleTemp != null) {
+ throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME);
+ }
+ return ruleTemp;
+ }
+}