Minor cleanup and formatting
[genius.git] / fcapsmanager / alarmmanager / src / main / java / org / opendaylight / genius / fcapsmanager / alarmmanager / AlarmNotificationListeners.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 package org.opendaylight.genius.fcapsmanager.alarmmanager;
9
10 import java.lang.management.ManagementFactory;
11 import java.util.Set;
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;
27
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);
35
36
37     public AlarmNotificationListeners(BundleContext context) {
38         this.context = context;
39     }
40
41     public void setShouldContinue(boolean shouldContinue) {
42         this.shouldContinue = shouldContinue;
43     }
44
45     /**
46      * Platform dependent bundle injects its handle and it is retrieved in the
47      * method.
48      */
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);
55             }
56         }
57         return service;
58     }
59
60     /**
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.
65      */
66     public class DelegateListener implements NotificationListener {
67
68         @Override
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();
74
75                 if ("JMX.mbean.registered".equals(notificationType) && mbn.toString().contains(DOMAIN)) {
76                     LOG.debug("Received registeration of Mbean " + mbn);
77                     try {
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);
82                     }
83                 }
84
85                 if ("JMX.mbean.unregistered".equals(notificationType) && mbn.toString().contains(DOMAIN)) {
86                     LOG.debug("Time: " + msnotification.getTimeStamp() + "MBean " + msnotification.getMBeanName()
87                             + " unregistered successfully");
88                 }
89             } else if (notification instanceof AttributeChangeNotification) {
90                 AttributeChangeNotification acn = (AttributeChangeNotification) notification;
91
92                 LOG.debug("Received attribute notification of Mbean: " + notification.getSource() + " for attribute:"
93                         + acn.getAttributeName());
94
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(']')), ' ');
99
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());
104                     } else {
105                         LOG.debug("Alarm service not available");
106                     }
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(']')), ' ');
111
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());
116                     } else {
117                         LOG.debug("Alarm service not available");
118                     }
119                 }
120             }
121         }
122     }
123
124     /**
125      * Gets the platform MBeanServer instance and registers to get notification
126      * whenever alarm mbean is registered in the mbeanserver.
127      */
128     @Override
129     public void run() {
130         mbs = ManagementFactory.getPlatformMBeanServer();
131
132         queryMbeans();
133
134         ObjectName delegate = null;
135         try {
136             delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
137         } catch (MalformedObjectNameException e) {
138             LOG.error("Failed to create JMX ObjectName", e);
139             return;
140         }
141         NotificationFilterSupport filter = new NotificationFilterSupport();
142         filter.enableType("JMX.mbean.registered");
143         filter.enableType("JMX.mbean.unregistered");
144
145         try {
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);
150             return;
151         }
152
153         while (shouldContinue) {
154             try {
155                 Thread.sleep(50);
156             } catch (InterruptedException ex) {
157                 shouldContinue = false;
158             }
159         }
160     }
161
162     /**
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
166      * listener to it.
167      */
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)) {
173                 try {
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);
178                 }
179             }
180         }
181     }
182 }