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.vpnservice.fcapsmanager.alarmmanager;
10 import javax.management.*;
11 import java.lang.management.ManagementFactory;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.ServiceReference;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 import org.opendaylight.vpnservice.fcapsmanager.AlarmServiceFacade;
19 public class AlarmNotificationListeners implements Runnable {
20 static Logger s_logger = LoggerFactory.getLogger(AlarmNotificationListeners.class);
21 private MBeanServer mbs = null;
22 private static String DOMAIN = "SDNC.FM";
24 private final DelegateListener delegateListener = new DelegateListener();
25 private BundleContext context = null;
27 public AlarmNotificationListeners(BundleContext context) {
32 * Platform dependent bundle injects its handle and it is retrieved in the method
34 private AlarmServiceFacade getAlarmServiceSPI (){
35 AlarmServiceFacade service =null;
38 ServiceReference<?> serviceReference = context.
39 getServiceReference(AlarmServiceFacade.class.getName());
40 service = (AlarmServiceFacade) context.
41 getService(serviceReference);
42 }catch (NullPointerException ex){
45 s_logger.error("Exception {} occurred in getting AlarmServiceSPI",e);
52 * 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.
53 * Gets attribute notification when alarm mbean is updated by the application.
55 public class DelegateListener implements NotificationListener {
56 public void handleNotification(Notification notification, Object obj) {
57 if (notification instanceof MBeanServerNotification) {
58 MBeanServerNotification msnotification =
59 (MBeanServerNotification) notification;
60 String nType = msnotification.getType();
61 ObjectName mbn = msnotification.getMBeanName();
63 if (nType.equals("JMX.mbean.registered")) {
64 if (mbn.toString().contains(DOMAIN)) {
65 s_logger.debug("Received registeration of Mbean "+mbn);
67 mbs.addNotificationListener(mbn,delegateListener, null, null);
68 s_logger.debug("Added attribute notification listener for Mbean "+ mbn);
69 } catch (InstanceNotFoundException e) {
70 s_logger.error("Exception while adding attribute notification of mbean {}", e);
75 if (nType.equals("JMX.mbean.unregistered")) {
76 if (mbn.toString().contains(DOMAIN)) {
77 s_logger.debug("Time: " + msnotification.getTimeStamp() + "MBean " + msnotification.getMBeanName()+" unregistered successfully");
81 else if (notification instanceof AttributeChangeNotification) {
82 AttributeChangeNotification acn =
83 (AttributeChangeNotification) notification;
85 s_logger.debug("Received attribute notification of Mbean: "
86 + notification.getSource()
87 + " for attribute:" + acn.getAttributeName() );
89 if(acn.getAttributeName().toString().equals("raiseAlarmObject")){
91 String value=acn.getNewValue().toString();
92 value = value.replace(value.charAt(0), ' ');
93 value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
95 String[] args =value.split(",");
96 s_logger.debug("Receive attribute value :"+args[0].trim()+args[1].trim()+args[2].trim());
97 if(getAlarmServiceSPI() != null ) {
98 getAlarmServiceSPI().raiseAlarm(args[0].trim(),args[1].trim(),args[2].trim());
100 s_logger.debug("Alarm service not available");
103 } else if(acn.getAttributeName().toString().equals("clearAlarmObject")){
105 String value=acn.getNewValue().toString();
106 value = value.replace(value.charAt(0), ' ');
107 value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
109 String[] args =value.split(",");
110 s_logger.debug("Receive attribute value :"+args[0].trim()+args[1].trim()+args[2].trim());
111 if(getAlarmServiceSPI() != null )
112 getAlarmServiceSPI().clearAlarm(args[0].trim(), args[1].trim(), args[2].trim());
114 s_logger.debug("Alarm service not available");
121 * Gets the platform MBeanServer instance and registers to get notification whenever alarm mbean is registered in the mbeanserver
125 mbs = ManagementFactory.getPlatformMBeanServer();
129 ObjectName delegate = null;
131 delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
132 } catch (MalformedObjectNameException e) {
135 NotificationFilterSupport filter = new NotificationFilterSupport();
136 filter.enableType("JMX.mbean.registered");
137 filter.enableType("JMX.mbean.unregistered");
140 mbs.addNotificationListener(delegate, delegateListener, filter, null);
141 s_logger.debug("Added registeration listener for Mbean {}",delegate);
142 } catch (InstanceNotFoundException e) {
143 s_logger.error("Failed to add registeration listener {}", e);
146 waitForNotification();
150 * Pre-provisioning case to handle all alarm mbeans which are registered before installation of framework bundle
151 * Queries the platform Mbeanserver to retrieve registered alarm mbean and add attribute notification listener to it
153 public void queryMbeans() {
155 Set<ObjectName> names =
156 new TreeSet<ObjectName>(mbs.queryNames(null, null));
157 s_logger.debug("Queried MBeanServer for MBeans:");
158 for (ObjectName beanName : names) {
159 if(beanName.toString().contains(DOMAIN)){
161 mbs.addNotificationListener(beanName,delegateListener, null, null);
162 s_logger.debug("Added attribute notification listener for Mbean "+ beanName);
163 } catch (InstanceNotFoundException e) {
164 s_logger.error("Failed to add attribute notification for Mbean {}", e);
169 public void waitForNotification() {
175 ex.printStackTrace();