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 private static final String DOMAIN = "SDNC.FM";
30 private boolean shouldContinue = true;
31 private final DelegateListener delegateListener = new DelegateListener();
32 private final BundleContext context;
33 private MBeanServer mbs;
34 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListeners.class);
37 public AlarmNotificationListeners(BundleContext context) {
38 this.context = context;
41 public void setShouldContinue(boolean shouldContinue) {
42 this.shouldContinue = shouldContinue;
46 * Platform dependent bundle injects its handle and it is retrieved in the
49 private AlarmServiceFacade getAlarmServiceSPI() {
50 AlarmServiceFacade service = null;
51 if (context != null) {
52 ServiceReference<?> serviceReference = context.getServiceReference(AlarmServiceFacade.class.getName());
53 if (serviceReference != null) {
54 service = (AlarmServiceFacade) context.getService(serviceReference);
61 * Gets register notification when a mbean is registered in platform
62 * Mbeanserver and checks if it is alarm mbean and add attribute
63 * notification listener to it. Gets attribute notification when alarm mbean
64 * is updated by the application.
66 public class DelegateListener implements NotificationListener {
69 public void handleNotification(Notification notification, Object obj) {
70 if (notification instanceof MBeanServerNotification) {
71 MBeanServerNotification msnotification = (MBeanServerNotification) notification;
72 String notificationType = msnotification.getType();
73 ObjectName mbn = msnotification.getMBeanName();
75 if ("JMX.mbean.registered".equals(notificationType) && mbn.toString().contains(DOMAIN)) {
76 LOG.debug("Received registeration of Mbean " + mbn);
78 mbs.addNotificationListener(mbn, delegateListener, null, null);
79 LOG.debug("Added attribute notification listener for Mbean " + mbn);
80 } catch (InstanceNotFoundException e) {
81 LOG.error("Exception while adding attribute notification of mbean {}", e);
85 if ("JMX.mbean.unregistered".equals(notificationType) && mbn.toString().contains(DOMAIN)) {
86 LOG.debug("Time: " + msnotification.getTimeStamp() + "MBean " + msnotification.getMBeanName()
87 + " unregistered successfully");
89 } else if (notification instanceof AttributeChangeNotification) {
90 AttributeChangeNotification acn = (AttributeChangeNotification) notification;
92 LOG.debug("Received attribute notification of Mbean: " + notification.getSource() + " for attribute:"
93 + acn.getAttributeName());
95 if ("raiseAlarmObject".equals(acn.getAttributeName())) {
96 String value = acn.getNewValue().toString();
97 value = value.replace(value.charAt(0), ' ');
98 value = value.replace(value.charAt(value.lastIndexOf(']')), ' ');
100 String[] args = value.split(",");
101 LOG.debug("Receive attribute value :" + args[0].trim() + args[1].trim() + args[2].trim());
102 if (getAlarmServiceSPI() != null) {
103 getAlarmServiceSPI().raiseAlarm(args[0].trim(), args[1].trim(), args[2].trim());
105 LOG.debug("Alarm service not available");
107 } else if ("clearAlarmObject".equals(acn.getAttributeName())) {
108 String value = acn.getNewValue().toString();
109 value = value.replace(value.charAt(0), ' ');
110 value = value.replace(value.charAt(value.lastIndexOf(']')), ' ');
112 String[] args = value.split(",");
113 LOG.debug("Receive attribute value :" + args[0].trim() + args[1].trim() + args[2].trim());
114 if (getAlarmServiceSPI() != null) {
115 getAlarmServiceSPI().clearAlarm(args[0].trim(), args[1].trim(), args[2].trim());
117 LOG.debug("Alarm service not available");
125 * Gets the platform MBeanServer instance and registers to get notification
126 * whenever alarm mbean is registered in the mbeanserver.
130 mbs = ManagementFactory.getPlatformMBeanServer();
134 ObjectName delegate = null;
136 delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
137 } catch (MalformedObjectNameException e) {
138 LOG.error("Failed to create JMX ObjectName", e);
141 NotificationFilterSupport filter = new NotificationFilterSupport();
142 filter.enableType("JMX.mbean.registered");
143 filter.enableType("JMX.mbean.unregistered");
146 mbs.addNotificationListener(delegate, delegateListener, filter, null);
147 LOG.debug("Added registeration listener for Mbean {}", delegate);
148 } catch (InstanceNotFoundException e) {
149 LOG.error("Failed to add registeration listener {}", e);
153 while (shouldContinue) {
156 } catch (InterruptedException ex) {
157 shouldContinue = false;
163 * Pre-provisioning case to handle all alarm mbeans which are registered
164 * before installation of framework bundle. Queries the platform Mbeanserver
165 * to retrieve registered alarm mbean and add attribute notification
168 public void queryMbeans() {
169 Set<ObjectName> names = new TreeSet<>(mbs.queryNames(null, null));
170 LOG.debug("Queried MBeanServer for MBeans:");
171 for (ObjectName beanName : names) {
172 if (beanName.toString().contains(DOMAIN)) {
174 mbs.addNotificationListener(beanName, delegateListener, null, null);
175 LOG.debug("Added attribute notification listener for Mbean " + beanName);
176 } catch (InstanceNotFoundException e) {
177 LOG.error("Failed to add attribute notification for Mbean {}", e);