2 * Copyright (c) 2016 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.genius.fcapsmanager.alarmmanager;
10 import java.lang.management.ManagementFactory;
12 import java.util.TreeSet;
13 import javax.management.AttributeChangeNotification;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.MBeanServer;
16 import javax.management.MBeanServerNotification;
17 import javax.management.MalformedObjectNameException;
18 import javax.management.Notification;
19 import javax.management.NotificationFilterSupport;
20 import javax.management.NotificationListener;
21 import javax.management.ObjectName;
22 import org.opendaylight.genius.fcapsmanager.AlarmServiceFacade;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class AlarmNotificationListeners implements Runnable {
29 static Logger s_logger = LoggerFactory.getLogger(AlarmNotificationListeners.class);
30 private MBeanServer mbs = null;
31 private static String DOMAIN = "SDNC.FM";
33 private final DelegateListener delegateListener = new DelegateListener();
34 private BundleContext context = null;
36 public AlarmNotificationListeners(BundleContext context) {
41 * Platform dependent bundle injects its handle and it is retrieved in the method
43 private AlarmServiceFacade getAlarmServiceSPI (){
44 AlarmServiceFacade service =null;
47 ServiceReference<?> serviceReference = context.
48 getServiceReference(AlarmServiceFacade.class.getName());
49 service = (AlarmServiceFacade) context.
50 getService(serviceReference);
51 }catch (NullPointerException ex){
54 s_logger.error("Exception {} occurred in getting AlarmServiceSPI",e);
61 * Gets register notification when a mbean is registered in platform Mbeanserver and checks if it is alarm mbean and add attribute notification listener to it.
62 * Gets attribute notification when alarm mbean is updated by the application.
64 public class DelegateListener implements NotificationListener {
65 public void handleNotification(Notification notification, Object obj) {
66 if (notification instanceof MBeanServerNotification) {
67 MBeanServerNotification msnotification =
68 (MBeanServerNotification) notification;
69 String nType = msnotification.getType();
70 ObjectName mbn = msnotification.getMBeanName();
72 if (nType.equals("JMX.mbean.registered")) {
73 if (mbn.toString().contains(DOMAIN)) {
74 s_logger.debug("Received registeration of Mbean "+mbn);
76 mbs.addNotificationListener(mbn,delegateListener, null, null);
77 s_logger.debug("Added attribute notification listener for Mbean "+ mbn);
78 } catch (InstanceNotFoundException e) {
79 s_logger.error("Exception while adding attribute notification of mbean {}", e);
84 if (nType.equals("JMX.mbean.unregistered")) {
85 if (mbn.toString().contains(DOMAIN)) {
86 s_logger.debug("Time: " + msnotification.getTimeStamp() + "MBean " + msnotification.getMBeanName()+" unregistered successfully");
90 else if (notification instanceof AttributeChangeNotification) {
91 AttributeChangeNotification acn =
92 (AttributeChangeNotification) notification;
94 s_logger.debug("Received attribute notification of Mbean: "
95 + notification.getSource()
96 + " for attribute:" + acn.getAttributeName() );
98 if(acn.getAttributeName().toString().equals("raiseAlarmObject")){
100 String value=acn.getNewValue().toString();
101 value = value.replace(value.charAt(0), ' ');
102 value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
104 String[] args =value.split(",");
105 s_logger.debug("Receive attribute value :"+args[0].trim()+args[1].trim()+args[2].trim());
106 if(getAlarmServiceSPI() != null ) {
107 getAlarmServiceSPI().raiseAlarm(args[0].trim(),args[1].trim(),args[2].trim());
109 s_logger.debug("Alarm service not available");
112 } else if(acn.getAttributeName().toString().equals("clearAlarmObject")){
114 String value=acn.getNewValue().toString();
115 value = value.replace(value.charAt(0), ' ');
116 value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
118 String[] args =value.split(",");
119 s_logger.debug("Receive attribute value :"+args[0].trim()+args[1].trim()+args[2].trim());
120 if(getAlarmServiceSPI() != null )
121 getAlarmServiceSPI().clearAlarm(args[0].trim(), args[1].trim(), args[2].trim());
123 s_logger.debug("Alarm service not available");
130 * Gets the platform MBeanServer instance and registers to get notification whenever alarm mbean is registered in the mbeanserver
134 mbs = ManagementFactory.getPlatformMBeanServer();
138 ObjectName delegate = null;
140 delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
141 } catch (MalformedObjectNameException e) {
144 NotificationFilterSupport filter = new NotificationFilterSupport();
145 filter.enableType("JMX.mbean.registered");
146 filter.enableType("JMX.mbean.unregistered");
149 mbs.addNotificationListener(delegate, delegateListener, filter, null);
150 s_logger.debug("Added registeration listener for Mbean {}",delegate);
151 } catch (InstanceNotFoundException e) {
152 s_logger.error("Failed to add registeration listener {}", e);
155 waitForNotification();
159 * Pre-provisioning case to handle all alarm mbeans which are registered before installation of framework bundle
160 * Queries the platform Mbeanserver to retrieve registered alarm mbean and add attribute notification listener to it
162 public void queryMbeans() {
164 Set<ObjectName> names =
165 new TreeSet<>(mbs.queryNames(null, null));
166 s_logger.debug("Queried MBeanServer for MBeans:");
167 for (ObjectName beanName : names) {
168 if(beanName.toString().contains(DOMAIN)){
170 mbs.addNotificationListener(beanName,delegateListener, null, null);
171 s_logger.debug("Added attribute notification listener for Mbean "+ beanName);
172 } catch (InstanceNotFoundException e) {
173 s_logger.error("Failed to add attribute notification for Mbean {}", e);
178 public void waitForNotification() {
184 ex.printStackTrace();