Add mq
authorFengLiang <feng.liang1@zte.com.cn>
Thu, 16 Feb 2017 08:46:11 +0000 (16:46 +0800)
committerFengLiang <feng.liang1@zte.com.cn>
Thu, 16 Feb 2017 11:16:57 +0000 (19:16 +0800)
add mq function

Change-Id: Ida02db81314d203f0e9b7b780f2848ac8dbdc4c8
Issue-ID:HOLMES-19
Signed-off-by: FengLiang <feng.liang1@zte.com.cn>
holmes-actions/pom.xml
holmes-actions/src/main/java/org/openo/holmes/common/api/entity/CorrelationResult.java
holmes-actions/src/main/java/org/openo/holmes/common/api/stat/Alarm.java
holmes-actions/src/main/java/org/openo/holmes/common/config/MQConfig.java [new file with mode: 0644]
holmes-actions/src/main/java/org/openo/holmes/common/constant/AlarmConst.java
holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java [new file with mode: 0644]
pom.xml

index 756a068..3a3f827 100644 (file)
     <packaging>jar</packaging>\r
     <artifactId>holmes-actions</artifactId>\r
     <dependencies>\r
+        <dependency>\r
+            <groupId>org.apache.geronimo.specs</groupId>\r
+            <artifactId>geronimo-jms_1.1_spec</artifactId>\r
+        </dependency>\r
+        <dependency>\r
+            <groupId>org.apache.activemq</groupId>\r
+            <artifactId>activemq-client</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.openo.common-services.common-utilities</groupId>\r
             <artifactId>baseservice-i18n</artifactId>\r
index da6924b..d78619d 100644 (file)
@@ -16,6 +16,7 @@
 package org.openo.holmes.common.api.entity;\r
 \r
 import com.fasterxml.jackson.annotation.JsonProperty;\r
+import java.io.Serializable;\r
 import org.openo.holmes.common.api.stat.Alarm;\r
 \r
 import lombok.Getter;\r
@@ -27,7 +28,7 @@ import lombok.ToString;
 @Getter\r
 @NoArgsConstructor\r
 @ToString\r
-public class CorrelationResult {\r
+public class CorrelationResult implements Serializable{\r
 \r
   @JsonProperty\r
   private String ruleId;\r
index 71a5d00..3cd6093 100644 (file)
@@ -16,6 +16,7 @@
 \r
 package org.openo.holmes.common.api.stat;\r
 \r
+import java.io.Serializable;\r
 import java.io.StringReader;\r
 import java.lang.reflect.Field;\r
 import java.util.Date;\r
@@ -34,7 +35,7 @@ import org.jdom.output.XMLOutputter;
 \r
 @Getter\r
 @Setter\r
-public class Alarm implements AplusData, Cloneable {\r
+public class Alarm implements AplusData, Cloneable, Serializable {\r
 \r
     public static final byte EVENT_CLEARED_ALARM = 3;\r
 \r
diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/config/MQConfig.java b/holmes-actions/src/main/java/org/openo/holmes/common/config/MQConfig.java
new file mode 100644 (file)
index 0000000..d225dae
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * 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.common.config;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.HashMap;
+import java.util.Map;
+import javax.validation.constraints.NotNull;
+
+public class MQConfig {
+
+    @JsonProperty
+    @NotNull
+    public String brokerIp = "localhost";
+
+    @JsonProperty
+    @NotNull
+    public int brokerPort = 5672;
+
+
+    @JsonProperty
+    public String brokerUsername;
+
+    @JsonProperty
+    public String brokerPassword;
+
+    @JsonProperty
+    public boolean autoDiscover = false;
+
+    @JsonProperty
+    public String mqServiceName = "mqService";
+
+    @JsonProperty
+    public String mqServiceVersion = "v1";
+
+
+    @JsonProperty
+    public long healthCheckMillisecondsToWait = 2000; // 2 seconds
+
+    @JsonProperty
+    public int shutdownWaitInSeconds = 20;
+
+    @JsonProperty
+    public int timeToLiveInSeconds = -1; // Default no TTL. Jackson does not support java.util.Optional yet.
+
+    @JsonProperty
+    public Map<String, String> extConsumerConfMap = new HashMap<>();
+
+    @JsonProperty
+    public Map<String, String> extProducerConfMap = new HashMap<>();
+
+
+    @Override
+    public String toString() {
+        return "MQConfig [brokerIp=" + brokerIp + ", brokerPort=" + brokerPort + ", brokerUsername="
+            + brokerUsername + ", brokerPassword=" + brokerPassword + ", autoDiscover="
+            + autoDiscover + ", mqServiceName=" + mqServiceName + ", mqServiceVersion="
+            + mqServiceVersion + ", healthCheckMillisecondsToWait=" + healthCheckMillisecondsToWait
+            + ", shutdownWaitInSeconds=" + shutdownWaitInSeconds + ", timeToLiveInSeconds="
+            + timeToLiveInSeconds + "]";
+    }
+
+
+}
index b0d0b48..d941172 100644 (file)
-/**\r
- * Copyright 2017 ZTE Corporation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.openo.holmes.common.constant;\r
-\r
-public interface AlarmConst {\r
-\r
-    public static final String MQ_QUEUE_NAME_EMF_UP = "queue://zenap/fm/emf_up";\r
-\r
-    public static final String MQ_TOPIC_NAME_EMF_DOWN = "topic://zenap/fm/emf_down";\r
-\r
-    public static final String MQ_TOPIC_NAME_NORTH_UP = "topic://zenap/fm/north_up";\r
-\r
-    public static final String MQ_TOPIC_NAME_ALARM_RULE = "topic://zenap/fm/alarm_rule";\r
-\r
-    public static final String INTERNAL_MQ_QUEUE_NAME_HISTORY_ALARM_2_DB = "queue://zenap/fm/historyalarm2DB";\r
-\r
-    public static final String MQ_SELECTOR_KEY = "nf";\r
-\r
-    public static final String MQ_EMF_DOWN_MSG_FILTER_KEY = "type";\r
-\r
-    public static final String MQ_EMF_UP_MSG_FILTER_KEY = "type";\r
-\r
-    public static final String INTERNAL_MQ_EMF_UP_ACKALARM_MSG_FILTER_VALUE = "ACK";\r
-\r
-    public static final String MQ_EMF_DOWN_CLEARALARM_MSG_FILTER_VALUE = "CLEAR";\r
-\r
-    public static final String MQ_EMF_UP_CLEARALARM_MSG_FILTER_VALUE = "CLEAR";\r
-\r
-    public static final String MQ_EMF_UP_CHANGEALARM_MSG_FILTER_VALUE = "CHANGE";\r
-\r
-    public static final String MQ_EMF_UP_RAISEALARM_MSG_FILTER_VALUE = "RAISE";\r
-\r
-    public static final String INTERNAL_MQ_CLEARALARM_2_DB_FILTER_VALUE = "CLEAR";\r
-\r
-    public static final String COMETD_MESSAGE_TOPIC = "cometd2Client";\r
-\r
-    public static final String COMETD_MESSAGE_CHANEL = "/broadcast_channel/alarm_cometd_chanel";\r
-\r
-    public static final String COMETD_NF_COUNTER_CHANEL = "/nf_counter_cometd_chanel";\r
-\r
-    public static final String COMETD_PROMPTING_RULE_CHANEL = "/prompting_rule_cometd_chanel";\r
-\r
-    public static final String COMETD_MASK_RULE_MESSAGE_FILTER_KEY = "mask_rule";\r
-\r
-    public static final String COMETD_CLEAR_ALARM_MESSAGE_FILTER_KEY = "clear_alarm";\r
-\r
-    public static final String COMETD_ACK_ALARM_MESSAGE_FILTER_KEY = "ack_alarm";\r
-\r
-    public static final String COMETD_HISTORY_ALARM_ACK_STATE_MESSAGE_FILTER_KEY = "history_alarm_ack";\r
-\r
-    public static final String CACHE_ACTIVE_ALARM_KEY = "TAG_ACTIVE_ALARM";\r
-\r
-    public static final String CACHE_ALARM_CODE_KEY = "TAG_ALARM_CODE";\r
-\r
-    public static final String CACHE_ALARM_RESTYPE_AND_RESVERSION_KEY = "TAG_ALARM_RESTYPE_AND_RESVERSION";\r
-\r
-    public static final String CACHE_ALARM_REASON_KEY = "TAG_ALARM_REASON";\r
-\r
-    public static final String CACHE_ALARM_MAIN_KEY = "TAG_ALARM_MAIN";\r
-\r
-    public static final short STATUS_ENABLE = 0;\r
-\r
-    public static final short STATUS_DISABLE = 1;\r
-\r
-    public static final short STATUS_DELETED = 2;\r
-\r
-    public static final short UNDEFINE_LEVEL = 0;\r
-\r
-    public static final short CRITICA_LEVEL = 1;\r
-\r
-    public static final short MAJOR_LEVEL = 2;\r
-\r
-    public static final short MINOR_LEVEL = 3;\r
-\r
-    public static final short WARNING_LEVEL = 4;\r
-\r
-    public static final String I18N_EN = "en";\r
-\r
-    public static final String I18N_ZH = "zh";\r
-\r
-    public static final String ZH_CN = "zh_CN";\r
-\r
-    public static final String EN_US = "en_US";\r
-\r
-    public static final String EXECUTE_TIMER = "0 0 0 */1 * ? ";\r
-\r
-    public static final String COMMON_EM_RESTYPE = "common_em";\r
-\r
-    public static final String EM_LOCATION = "em";\r
-\r
-    public static final String BASE_MOC = "em";\r
-\r
-    public static final String SYSTEM_ID = "SystemId";\r
-\r
-    public static final String ADMIN = "admin";\r
-\r
-    public static final long UNDEFINE_ALARM_CODE = -1;\r
-\r
-    // for rule code\r
-    public static final long FORWARD_FAILED_ALARM_CODE = 1028L;\r
-\r
-    public static long ACTIVE_PERSISTING_RULE_CODE = 1017L;\r
-    public static long UNACKNOWLEDGED_PERSISTING_RULE_CODE = 1018L;\r
-}\r
+/**
+ * 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.common.constant;
+
+public interface AlarmConst {
+
+    public static final String MQ_TOPIC_NAME_ALARMS_CORRELATION = "topic://voss/fm/alarms_correlation";
+
+    public static final String MQ_TOPIC_NAME_ALARM = "topic://voss/fm/alarm";
+
+    public static final String I18N_EN = "en";
+
+    public static final String I18N_ZH = "zh";
+
+    public static final String ZH_CN = "zh_CN";
+
+    public static final String ADMIN = "admin";
+}
diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java b/holmes-actions/src/main/java/org/openo/holmes/common/producer/MQProducer.java
new file mode 100644 (file)
index 0000000..a9a869c
--- /dev/null
@@ -0,0 +1,122 @@
+/**
+ * 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.common.producer;
+
+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.MessageProducer;
+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.entity.CorrelationResult;
+import org.openo.holmes.common.api.stat.Alarm;
+import org.openo.holmes.common.api.stat.AplusResult;
+import org.openo.holmes.common.config.MQConfig;
+import org.openo.holmes.common.constant.AlarmConst;
+
+@Service
+@Slf4j
+public class MQProducer {
+
+    @Inject
+    private static IterableProvider<MQConfig> mqConfigProvider;
+    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 sendAlarmMQTopicMsg(Alarm alarm) {
+
+        Connection connection = null;
+        Session session;
+        Destination destination;
+        MessageProducer messageProducer;
+
+        try {
+
+            connection = connectionFactory.createConnection();
+            connection.start();
+            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
+            destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM);
+            messageProducer = session.createProducer(destination);
+            ObjectMessage message = session.createObjectMessage(alarm);
+            messageProducer.send(message);
+            session.commit();
+
+        } catch (Exception e) {
+            log.error("Failed send alarm." + e.getMessage(), e);
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (JMSException e) {
+                    log.error("Failed close connection." + e.getMessage(), e);
+                }
+            }
+        }
+    }
+
+    public void sendCorrelationMQTopicMsg(String ruleId, long createTimeL, Alarm parentAlarm,
+        Alarm childAlarm) {
+
+        CorrelationResult correlationResult = new CorrelationResult();
+        correlationResult.setRuleId(ruleId);
+        correlationResult.setCreateTimeL(createTimeL);
+        correlationResult.setResultType(AplusResult.APLUS_CORRELATION);
+        correlationResult.setAffectedAlarms(new Alarm[]{parentAlarm, childAlarm});
+
+        Connection connection = null;
+        Session session;
+        Destination destination;
+        MessageProducer messageProducer;
+
+        try {
+
+            connection = connectionFactory.createConnection();
+            connection.start();
+            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
+            destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARMS_CORRELATION);
+            messageProducer = session.createProducer(destination);
+            ObjectMessage message = session.createObjectMessage(correlationResult);
+            messageProducer.send(message);
+            session.commit();
+
+        } catch (Exception e) {
+            log.error("Failed send correlation." + e.getMessage(), e);
+        } finally {
+            if (connection != null) {
+                try {
+                    connection.close();
+                } catch (JMSException e) {
+                    log.error("Failed close connection." + e.getMessage(), e);
+                }
+            }
+        }
+    }
+}
diff --git a/pom.xml b/pom.xml
index bd4427f..f0720ab 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                 <version>1.6.4</version>\r
                 <scope>test</scope>\r
             </dependency>\r
+\r
+            <dependency>\r
+                <groupId>org.apache.geronimo.specs</groupId>\r
+                <artifactId>geronimo-jms_1.1_spec</artifactId>\r
+                <version>1.1.1</version>\r
+            </dependency>\r
+\r
+            <dependency>\r
+                <groupId>org.apache.activemq</groupId>\r
+                <artifactId>activemq-client</artifactId>\r
+                <version>5.13.4</version>\r
+            </dependency>\r
         </dependencies>\r
     </dependencyManagement>\r
 \r