From 8da3962249701031b715edf9003e4120aea46799 Mon Sep 17 00:00:00 2001 From: FengLiang Date: Tue, 21 Feb 2017 15:38:26 +0800 Subject: Modify package name Change-Id: I1341290d6e5375967ec12b5cd578c3cab684cd78 Issue-ID:HOLMES-19 Signed-off-by: FengLiang --- engine-d/pom.xml | 6 +- .../org/openo/holmes/engine/EngineDActiveApp.java | 25 ++ .../org/openo/holmes/engine/EngineDAppConfig.java | 67 +++++ .../openo/holmes/engine/db/CorrelationRuleDao.java | 37 +++ .../engine/db/mapper/CorrelationRuleMapper.java | 50 ++++ .../openo/holmes/engine/manager/DroolsEngine.java | 293 ++++++++++++++++++++ .../holmes/engine/request/CompileRuleRequest.java | 30 +++ .../holmes/engine/request/DeployRuleRequest.java | 33 +++ .../holmes/engine/resources/EngineResources.java | 114 ++++++++ .../engine/response/CorrelationRuleResponse.java | 28 ++ .../org/openo/holmes/engine/utils/AlarmUtil.java | 99 +++++++ .../holmes/engine/wrapper/RuleMgtWrapper.java | 47 ++++ .../openo/holmes/enginemgt/EngineActiveApp.java | 25 -- .../openo/holmes/enginemgt/EngineAppConfig.java | 68 ----- .../holmes/enginemgt/db/CorrelationRuleDao.java | 37 --- .../enginemgt/db/mapper/CorrelationRuleMapper.java | 50 ---- .../holmes/enginemgt/manager/DroolsEngine.java | 294 --------------------- .../enginemgt/request/CompileRuleRequest.java | 30 --- .../enginemgt/request/DeployRuleRequest.java | 33 --- .../enginemgt/resources/EngineResources.java | 117 -------- .../response/CorrelationRuleResponse.java | 28 -- .../openo/holmes/enginemgt/utils/AlarmUtil.java | 99 ------- .../holmes/enginemgt/wrapper/RuleMgtWrapper.java | 47 ---- 23 files changed, 826 insertions(+), 831 deletions(-) create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.java create mode 100644 engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/db/CorrelationRuleDao.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/request/CompileRuleRequest.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/request/DeployRuleRequest.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/resources/EngineResources.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/utils/AlarmUtil.java delete mode 100644 engine-d/src/main/java/org/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java diff --git a/engine-d/pom.xml b/engine-d/pom.xml index d7ff763..bb055bc 100644 --- a/engine-d/pom.xml +++ b/engine-d/pom.xml @@ -25,8 +25,8 @@ 1.1.0-SNAPSHOT - holmes-engine - holmes-enginemgt-service + holmes-engine-d + holmes-engine-d-service jar @@ -163,7 +163,7 @@ implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> - org.openo.holmes.enginemgt.EngineActiveApp + org.openo.holmes.engine.EngineDActiveApp diff --git a/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java b/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.java new file mode 100644 index 0000000..24bff21 --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/EngineDActiveApp.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.engine; + +import org.openo.dropwizard.ioc.bundle.IOCApplication; + +public class EngineDActiveApp extends IOCApplication { + + public static void main(String[] args) throws Exception { + new EngineDActiveApp().run(args); + } +} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java b/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java new file mode 100644 index 0000000..8b20ac1 --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/EngineDAppConfig.java @@ -0,0 +1,67 @@ +/** + * 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.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.openo.holmes.common.config.MQConfig; + +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/openo/holmes/engine/db/CorrelationRuleDao.java b/engine-d/src/main/java/org/openo/holmes/engine/db/CorrelationRuleDao.java new file mode 100644 index 0000000..4370054 --- /dev/null +++ b/engine-d/src/main/java/org/openo/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.openo.holmes.engine.db; + + +import java.util.List; +import org.openo.holmes.common.api.entity.CorrelationRule; +import org.openo.holmes.engine.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 queryRuleByEnable(@Bind("enable") int enable); + + public List queryRuleByRuleEnable(int enable) { + return queryRuleByEnable(enable); + } +} + diff --git a/engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java b/engine-d/src/main/java/org/openo/holmes/engine/db/mapper/CorrelationRuleMapper.java new file mode 100644 index 0000000..4af2980 --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/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.engine.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 { + + 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/engine/manager/DroolsEngine.java b/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java new file mode 100644 index 0000000..473b929 --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/manager/DroolsEngine.java @@ -0,0 +1,293 @@ +/** + * 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.engine.manager; + + +import java.io.StringReader; +import java.util.List; +import java.util.Locale; +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.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.openo.holmes.common.api.entity.CorrelationRule; +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.common.exception.CorrelationException; +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.engine.request.DeployRuleRequest; +import org.openo.holmes.engine.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; + + private KnowledgeBase kbase; + + private KnowledgeBaseConfiguration kconf; + + private StatefulKnowledgeSession ksession; + + private KnowledgeBuilder kbuilder; + + @Inject + private IterableProvider mqConfigProvider; + + private ConnectionFactory connectionFactory; + + @PostConstruct + private void init() { + try { + // 1. start engine + start(); + // 2. start mq listener + registerAlarmTopicListener(); + } catch (Exception e) { + log.error("Start service failed: " + e.getMessage()); + throw ExceptionUtil.buildExceptionResponse("Start service failed!"); + } + } + + private void registerAlarmTopicListener() { + String brokerURL = + "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort; + connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername, + mqConfigProvider.get().brokerPassword, brokerURL); + + Thread thread = new Thread(new AlarmMqMessageListener()); + 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 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 CorrelationException { + StringReader reader = new StringReader(rule.getContent()); + Resource res = ResourceFactory.newReaderResource(reader); + + kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + + kbuilder.add(res, ResourceType.DRL); + + if (kbuilder.hasErrors()) { + + String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, + I18nProxy.ENGINE_CONTENT_ILLEGALITY, + new String[]{kbuilder.getErrors().toString()}); + throw new CorrelationException(errorMsg); + } + + String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); + + if (kbase.getKnowledgePackages().contains(packageName)) { + + String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, + I18nProxy.ENGINE_CONTENT_ILLEGALITY,new String[]{ + I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE)}); + + throw new CorrelationException(errorMsg); + } + try { + + kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); + } catch (Exception e) { + + String errorMsg = + I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED); + throw new CorrelationException(errorMsg, e); + } + + kbuilder = null; + + ksession.fireAllRules(); + return packageName; + } + + + public synchronized void undeployRule(String packageName, Locale locale) + throws CorrelationException { + + KnowledgePackage pkg = kbase.getKnowledgePackage(packageName); + + if (null == pkg) { + String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, + I18nProxy.ENGINE_DELETE_RULE_NULL, + new String[]{packageName}); + throw new CorrelationException(errorMsg); + } + + try { + + kbase.removeKnowledgePackage(pkg.getName()); + } catch (Exception e) { + String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, + I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName}); + throw new CorrelationException(errorMsg, e); + } + } + + public void compileRule(String content, Locale locale) + throws CorrelationException { + StringReader reader = new StringReader(content); + Resource res = ResourceFactory.newReaderResource(reader); + + kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); + + kbuilder.add(res, ResourceType.DRL); + + if (kbuilder.hasErrors()) { + String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, + I18nProxy.ENGINE_CONTENT_ILLEGALITY, + new String[]{kbuilder.getErrors().toString()}); + log.error(errorMsg); + throw new CorrelationException(errorMsg); + } + 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(); + } + + class AlarmMqMessageListener implements Runnable { + + 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) { + putRaisedIntoStream((Alarm) objMessage.getObject()); + } else { + break; + } + } + } catch (JMSException e) { + log.error("connection mq service Failed: " + e.getMessage()); + } + + } + } + +} diff --git a/engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/engine/request/CompileRuleRequest.java new file mode 100644 index 0000000..692deb5 --- /dev/null +++ b/engine-d/src/main/java/org/openo/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.openo.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/openo/holmes/engine/request/DeployRuleRequest.java b/engine-d/src/main/java/org/openo/holmes/engine/request/DeployRuleRequest.java new file mode 100644 index 0000000..979c5bf --- /dev/null +++ b/engine-d/src/main/java/org/openo/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.openo.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/openo/holmes/engine/resources/EngineResources.java b/engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java new file mode 100644 index 0000000..94fc040 --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/resources/EngineResources.java @@ -0,0 +1,114 @@ +/** + * 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.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.openo.holmes.common.exception.CorrelationException; +import org.openo.holmes.common.utils.ExceptionUtil; +import org.openo.holmes.common.utils.LanguageUtil; +import org.openo.holmes.engine.manager.DroolsEngine; +import org.openo.holmes.engine.request.CompileRuleRequest; +import org.openo.holmes.engine.request.DeployRuleRequest; +import org.openo.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); + 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/openo/holmes/engine/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/openo/holmes/engine/response/CorrelationRuleResponse.java new file mode 100644 index 0000000..f033a1b --- /dev/null +++ b/engine-d/src/main/java/org/openo/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.openo.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/openo/holmes/engine/utils/AlarmUtil.java b/engine-d/src/main/java/org/openo/holmes/engine/utils/AlarmUtil.java new file mode 100644 index 0000000..558d97b --- /dev/null +++ b/engine-d/src/main/java/org/openo/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.openo.holmes.engine.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> + */ + private final Map> rootPriorityMap = + new HashMap>(); + /** + * Map + */ + private final Map saveRuleMsg = new HashMap(); + + 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 map = new HashMap(); + 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/engine/wrapper/RuleMgtWrapper.java b/engine-d/src/main/java/org/openo/holmes/engine/wrapper/RuleMgtWrapper.java new file mode 100644 index 0000000..34dab1f --- /dev/null +++ b/engine-d/src/main/java/org/openo/holmes/engine/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.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.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.engine.db.CorrelationRuleDao; + + +@Service +@Singleton +@Slf4j +public class RuleMgtWrapper { + + @Inject + private DbDaoUtil daoUtil; + + public List queryRuleByEnable(int enable) throws DbException { + + List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) + .queryRuleByRuleEnable(enable); + if (ruleTemp != null) { + throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME); + } + return ruleTemp; + } +} 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 deleted file mode 100644 index ddaed97..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineActiveApp.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * 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 { - - 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 deleted file mode 100644 index b01d173..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/EngineAppConfig.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * 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 deleted file mode 100644 index 2393417..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/db/CorrelationRuleDao.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * 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 queryRuleByEnable(@Bind("enable") int enable); - - public List 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 deleted file mode 100644 index 7d9ed97..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/db/mapper/CorrelationRuleMapper.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * 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 { - - 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/manager/DroolsEngine.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java deleted file mode 100644 index ae2ba11..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/manager/DroolsEngine.java +++ /dev/null @@ -1,294 +0,0 @@ -/** - * 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 javax.inject.Singleton; -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.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.openo.holmes.common.api.entity.CorrelationRule; -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.common.exception.CorrelationException; -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.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; - - private KnowledgeBase kbase; - - private KnowledgeBaseConfiguration kconf; - - private StatefulKnowledgeSession ksession; - - private KnowledgeBuilder kbuilder; - - @Inject - private IterableProvider mqConfigProvider; - - private ConnectionFactory connectionFactory; - - @PostConstruct - private void init() { - try { - // 1. start engine - start(); - // 2. start mq listener - registerAlarmTopicListener(); - } catch (Exception e) { - log.error("Start service failed: " + e.getMessage()); - throw ExceptionUtil.buildExceptionResponse("Start service failed!"); - } - } - - private void registerAlarmTopicListener() { - String brokerURL = - "tcp://" + mqConfigProvider.get().brokerIp + ":" + mqConfigProvider.get().brokerPort; - connectionFactory = new ActiveMQConnectionFactory(mqConfigProvider.get().brokerUsername, - mqConfigProvider.get().brokerPassword, brokerURL); - - Thread thread = new Thread(new AlarmMqMessageListener()); - 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 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 CorrelationException { - StringReader reader = new StringReader(rule.getContent()); - Resource res = ResourceFactory.newReaderResource(reader); - - kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add(res, ResourceType.DRL); - - if (kbuilder.hasErrors()) { - - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_CONTENT_ILLEGALITY, - new String[]{kbuilder.getErrors().toString()}); - throw new CorrelationException(errorMsg); - } - - String packageName = kbuilder.getKnowledgePackages().iterator().next().getName(); - - if (kbase.getKnowledgePackages().contains(packageName)) { - - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_CONTENT_ILLEGALITY,new String[]{ - I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_CONTAINS_PACKAGE)}); - - throw new CorrelationException(errorMsg); - } - try { - - kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); - } catch (Exception e) { - - String errorMsg = - I18nProxy.getInstance().getValue(locale, I18nProxy.ENGINE_DEPLOY_RULE_FAILED); - throw new CorrelationException(errorMsg, e); - } - - kbuilder = null; - - ksession.fireAllRules(); - return packageName; - } - - - public synchronized void undeployRule(String packageName, Locale locale) - throws CorrelationException { - - KnowledgePackage pkg = kbase.getKnowledgePackage(packageName); - - if (null == pkg) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_DELETE_RULE_NULL, - new String[]{packageName}); - throw new CorrelationException(errorMsg); - } - - try { - - kbase.removeKnowledgePackage(pkg.getName()); - } catch (Exception e) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_DELETE_RULE_FAILED, new String[]{packageName}); - throw new CorrelationException(errorMsg, e); - } - } - - public void compileRule(String content, Locale locale) - throws CorrelationException { - StringReader reader = new StringReader(content); - Resource res = ResourceFactory.newReaderResource(reader); - - kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - - kbuilder.add(res, ResourceType.DRL); - - if (kbuilder.hasErrors()) { - String errorMsg = I18nProxy.getInstance().getValueByArgs(locale, - I18nProxy.ENGINE_CONTENT_ILLEGALITY, - new String[]{kbuilder.getErrors().toString()}); - log.error(errorMsg); - throw new CorrelationException(errorMsg); - } - 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(); - } - - class AlarmMqMessageListener implements Runnable { - - 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) { - putRaisedIntoStream((Alarm) objMessage.getObject()); - } else { - break; - } - } - } catch (JMSException e) { - log.error("connection mq service Failed: " + e.getMessage()); - } - - } - } - -} 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 deleted file mode 100644 index bf056be..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/request/CompileRuleRequest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * 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 deleted file mode 100644 index b1d9346..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/request/DeployRuleRequest.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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 deleted file mode 100644 index 054ce52..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/resources/EngineResources.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * 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.CorrelationException; -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 (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/openo/holmes/enginemgt/response/CorrelationRuleResponse.java b/engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java deleted file mode 100644 index 6c9842c..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/response/CorrelationRuleResponse.java +++ /dev/null @@ -1,28 +0,0 @@ -/** - * 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 deleted file mode 100644 index 2d5b5ca..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/utils/AlarmUtil.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * 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> - */ - private final Map> rootPriorityMap = - new HashMap>(); - /** - * Map - */ - private final Map saveRuleMsg = new HashMap(); - - 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 map = new HashMap(); - 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 deleted file mode 100644 index 35890ca..0000000 --- a/engine-d/src/main/java/org/openo/holmes/enginemgt/wrapper/RuleMgtWrapper.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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 queryRuleByEnable(int enable) throws DbException { - - List ruleTemp = daoUtil.getJdbiDaoByOnDemand(CorrelationRuleDao.class) - .queryRuleByRuleEnable(enable); - if (ruleTemp != null) { - throw new DbException(I18nProxy.RULE_MANAGEMENT_REPEAT_RULE_NAME); - } - return ruleTemp; - } -} -- cgit 1.2.3-korg