Move TransactionChainManagerTest
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / alarm / AlarmAgent.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.applications.southboundcli.alarm;
9
10 import java.lang.management.ManagementFactory;
11 import javax.annotation.PostConstruct;
12 import javax.inject.Singleton;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.MBeanException;
16 import javax.management.MBeanRegistrationException;
17 import javax.management.MBeanServer;
18 import javax.management.MalformedObjectNameException;
19 import javax.management.NotCompliantMBeanException;
20 import javax.management.ObjectName;
21 import javax.management.ReflectionException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Singleton
26 public class AlarmAgent {
27     private static final Logger LOG = LoggerFactory.getLogger(AlarmAgent.class);
28     private static final String BEAN_NAME = "SDNC.FM:name=NodeReconciliationOperationOngoingBean";
29     private final MBeanServer mbs;
30     private final NodeReconciliationAlarm alarmBean = new NodeReconciliationAlarm();
31     private ObjectName alarmName;
32
33     /**
34      * constructor get the instance of platform MBeanServer.
35      */
36     public AlarmAgent() {
37         mbs = ManagementFactory.getPlatformMBeanServer();
38     }
39
40     @PostConstruct
41     public void start() {
42         try {
43             alarmName = new ObjectName(BEAN_NAME);
44             if (!mbs.isRegistered(alarmName)) {
45                 mbs.registerMBean(alarmBean, alarmName);
46                 LOG.info("Registered Mbean {} successfully", alarmName);
47             }
48         } catch (MalformedObjectNameException e) {
49             LOG.error("ObjectName instance creation failed for bean {}", BEAN_NAME, e);
50         } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
51             LOG.error("Registeration failed for Mbean {}", alarmName, e);
52         }
53     }
54
55     /**
56      * Method invoke raise alarm JMX API in platform MbeanServer with alarm
57      * details.
58      *
59      * @param alarmId alarm to be raised
60      * @param text Additional details describing about the alarm on which dpnId
61      * @param src Source of the alarm ex: dpnId=openflow:1 the source node that
62      *            caused this alarm
63      */
64     public void invokeFMRaiseMethod(final String alarmId, final String text, final String src) {
65         try {
66             mbs.invoke(alarmName, "raiseAlarm", new Object[] { alarmId, text, src },
67                     new String[] { String.class.getName(), String.class.getName(), String.class.getName() });
68             LOG.debug("Invoked raiseAlarm function for Mbean {} with source {}", BEAN_NAME, src);
69         } catch (InstanceNotFoundException | ReflectionException | MBeanException e) {
70             LOG.error("Invoking raiseAlarm function failed for Mbean {}", alarmName, e);
71         }
72     }
73
74     /**
75      * Method invoke clear alarm JMX API in platform MbeanServer with alarm
76      * details.
77      *
78      * @param alarmId alarm to be cleared
79      * @param text Additional details describing about the alarm on which dpnId
80      * @param src Source of the alarm ex: dpn=openflow:1 the source node that
81      *            caused this alarm
82      */
83     public void invokeFMClearMethod(final String alarmId, final String text, final String src) {
84         try {
85             mbs.invoke(alarmName, "clearAlarm", new Object[] { alarmId, text, src },
86                     new String[] { String.class.getName(), String.class.getName(), String.class.getName() });
87             LOG.debug("Invoked clearAlarm function for Mbean {} with source {}", BEAN_NAME, src);
88         } catch (InstanceNotFoundException | ReflectionException | MBeanException e) {
89             LOG.error("Invoking clearAlarm method failed for Mbean {}", alarmName, e);
90         }
91     }
92
93     /**
94      * Method gets the alarm details to be raised and construct the alarm
95      * objects.
96      *
97      * @param nodeId Source of the alarm dpnId
98      */
99     public void raiseNodeReconciliationAlarm(final Long nodeId) {
100         String alarmText = getAlarmText(nodeId,  " started reconciliation");
101         String source = getSourceText(nodeId);
102
103         LOG.debug("Raising NodeReconciliationOperationOngoing alarm, alarmText {} source {}", alarmText, source);
104         // Invokes JMX raiseAlarm method
105         invokeFMRaiseMethod("NodeReconciliationOperationOngoing", alarmText, source);
106     }
107
108     /**
109      * Method gets the alarm details to be cleared and construct the alarm
110      * objects.
111      *
112      * @param nodeId Source of the alarm dpnId
113      */
114     public void clearNodeReconciliationAlarm(final Long nodeId) {
115         String alarmText = getAlarmText(nodeId, " finished reconciliation");
116         String source = getSourceText(nodeId);
117
118         LOG.debug("Clearing NodeReconciliationOperationOngoing alarm of source {}", source);
119         // Invokes JMX clearAlarm method
120         invokeFMClearMethod("NodeReconciliationOperationOngoing", alarmText, source);
121     }
122
123     /**
124      * Method gets the alarm text for the nodeId.
125      *
126      * @param nodeId Source of the alarm nodeId
127      * @param event reason for alarm invoke/clear
128      */
129     private static String getAlarmText(final Long nodeId, final String event) {
130         return new StringBuilder("OF Switch ").append(nodeId).append(event).toString();
131     }
132
133     /**
134      * Method gets the source text for the nodeId.
135      *
136      * @param nodeId Source of the alarm nodeId
137      */
138     private static String getSourceText(final Long nodeId) {
139         return "Dpn=" + nodeId;
140     }
141 }