Imported vpnservice as a subtree
[netvirt.git] / vpnservice / fcapsapplication / fcapsapplication-impl / src / main / java / org / opendaylight / vpnservice / fcapsapp / alarm / AlarmAgent.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.vpnservice.fcapsapp.alarm;
10
11 import org.opendaylight.vpnservice.fcapsappjmx.ControlPathFailureAlarm;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import javax.management.MBeanServer;
16 import javax.management.MalformedObjectNameException;
17 import javax.management.ObjectName;
18 import java.lang.management.ManagementFactory;
19
20 public class AlarmAgent {
21     static Logger s_logger = LoggerFactory.getLogger(AlarmAgent.class);
22     private MBeanServer mbs = null;
23     private ObjectName alarmName = null;
24     private static final String BEANNAME = "SDNC.FM:name=ControlPathFailureAlarmBean";
25     private static ControlPathFailureAlarm alarmBean = new ControlPathFailureAlarm();
26
27     /**
28      * constructor get the instance of platform MBeanServer
29      */
30     public AlarmAgent() {
31         mbs = ManagementFactory.getPlatformMBeanServer();
32         try {
33             alarmName = new ObjectName(BEANNAME);
34         } catch (MalformedObjectNameException e) {
35             s_logger.error("ObjectName instance creation failed for BEANAME {} : {}",BEANNAME, e);
36         }
37     }
38
39     /**
40      * Method registers alarm mbean in platform MbeanServer
41      */
42     public void registerAlarmMbean() {
43         try {
44             if (!mbs.isRegistered(alarmName)) {
45                 mbs.registerMBean(alarmBean, alarmName);
46                 s_logger.info("Registered Mbean {} successfully", alarmName);
47             }
48         } catch (Exception e) {
49             s_logger.error("Registeration failed for Mbean {} :{}", alarmName,e);
50         }
51     }
52
53     /**
54      * Method invoke raise alarm JMX API in platform MbeanServer with alarm details
55      * @param alarmId
56      *          alarm to be raised
57      * @param text
58      *          Additional details describing about the alarm on which dpnId and hostname
59      * @param src
60      *         Source of the alarm ex: dpnId=openflow:1
61      *            the source node that caused this alarm
62      */
63     public void invokeFMraisemethod(String alarmId,String text,String src) {
64         try {
65             mbs.invoke(alarmName, "raiseAlarm", new Object[]{alarmId, text, src},
66                     new String[]{String.class.getName(), String.class.getName(), String.class.getName()});
67             s_logger.debug("Invoked raiseAlarm function for Mbean {} with source {}", BEANNAME, src);
68         } catch (Exception e) {
69             s_logger.error("Invoking raiseAlarm method failed for Mbean {} :{}", alarmName,e);
70         }
71     }
72
73     /**
74      * Method invoke clear alarm JMX API in platform MbeanServer with alarm details
75      * @param alarmId
76      *          alarm to be cleared
77      * @param text
78      *          Additional details describing about the alarm on which dpnId and hostname
79      * @param src
80      *         Source of the alarm ex: dpn=openflow:1
81      *            the source node that caused this alarm
82      */
83     public void invokeFMclearmethod(String alarmId,String text,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             s_logger.debug("Invoked clearAlarm function for Mbean {} with source {}",BEANNAME,src);
88         } catch (Exception e) {
89             s_logger.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 objects
95      * @param nodeId
96      *         Source of the alarm dpnId
97      * @param host
98      *         Controller hostname
99      */
100     public void raiseControlPathAlarm(String nodeId,String host) {
101         StringBuilder alarmText = new StringBuilder();
102         StringBuilder source = new StringBuilder();
103
104         if (host != null) {
105             try {
106                 alarmText.append("OF Switch ").append(nodeId).append(" lost heart beat communication with controller ")
107                         .append(host);
108                 source.append("Dpn=").append(nodeId);
109
110                 s_logger.debug("Raising ControlPathConnectionFailure alarm... alarmText {} source {} ", alarmText, source);
111                 //Invokes JMX raiseAlarm method
112                 invokeFMraisemethod("ControlPathConnectionFailure", alarmText.toString(), source.toString());
113             } catch (Exception e) {
114                 s_logger.error("Exception before invoking raise method in jmx {}", e);
115             }
116         } else {
117             s_logger.error("Received hostname is null");
118         }
119     }
120
121     /**
122      * Method gets the alarm details to be cleared and construct the alarm objects
123      * @param nodeId
124      *         Source of the alarm dpnId
125      */
126     public void clearControlPathAlarm(String nodeId) {
127         StringBuilder source = new StringBuilder();
128
129         try {
130             source.append("Dpn=").append(nodeId);
131             s_logger.debug("Clearing ControlPathConnectionFailure alarm of source {} ", source);
132             //Invokes JMX clearAlarm method
133             invokeFMclearmethod("ControlPathConnectionFailure", "OF Switch gained communication with controller",
134                     source.toString());
135         } catch (Exception e) {
136             s_logger.error("Exception before invoking clear method jmx {}", e);
137         }
138     }
139 }