2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.southboundcli.alarm;
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;
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;
34 * constructor get the instance of platform MBeanServer.
37 mbs = ManagementFactory.getPlatformMBeanServer();
43 alarmName = new ObjectName(BEAN_NAME);
44 if (!mbs.isRegistered(alarmName)) {
45 mbs.registerMBean(alarmBean, alarmName);
46 LOG.info("Registered Mbean {} successfully", alarmName);
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);
56 * Method invoke raise alarm JMX API in platform MbeanServer with alarm
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
64 public void invokeFMRaiseMethod(final String alarmId, final String text, final String src) {
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);
75 * Method invoke clear alarm JMX API in platform MbeanServer with alarm
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
83 public void invokeFMClearMethod(final String alarmId, final String text, final String src) {
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);
94 * Method gets the alarm details to be raised and construct the alarm
97 * @param nodeId Source of the alarm dpnId
99 public void raiseNodeReconciliationAlarm(final Long nodeId) {
100 String alarmText = getAlarmText(nodeId, " started reconciliation");
101 String source = getSourceText(nodeId);
103 LOG.debug("Raising NodeReconciliationOperationOngoing alarm, alarmText {} source {}", alarmText, source);
104 // Invokes JMX raiseAlarm method
105 invokeFMRaiseMethod("NodeReconciliationOperationOngoing", alarmText, source);
109 * Method gets the alarm details to be cleared and construct the alarm
112 * @param nodeId Source of the alarm dpnId
114 public void clearNodeReconciliationAlarm(final Long nodeId) {
115 String alarmText = getAlarmText(nodeId, " finished reconciliation");
116 String source = getSourceText(nodeId);
118 LOG.debug("Clearing NodeReconciliationOperationOngoing alarm of source {}", source);
119 // Invokes JMX clearAlarm method
120 invokeFMClearMethod("NodeReconciliationOperationOngoing", alarmText, source);
124 * Method gets the alarm text for the nodeId.
126 * @param nodeId Source of the alarm nodeId
127 * @param event reason for alarm invoke/clear
129 private static String getAlarmText(final Long nodeId, final String event) {
130 return new StringBuilder("OF Switch ").append(nodeId).append(event).toString();
134 * Method gets the source text for the nodeId.
136 * @param nodeId Source of the alarm nodeId
138 private static String getSourceText(final Long nodeId) {
139 return "Dpn=" + nodeId;