Remove wildcard imports
[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     static Logger s_logger = LoggerFactory.getLogger(AlarmNotificationListeners.class);
30     private MBeanServer mbs = null;
31     private static String DOMAIN = "SDNC.FM";
32
33     private final DelegateListener delegateListener = new DelegateListener();
34     private BundleContext context = null;
35
36     public AlarmNotificationListeners(BundleContext context) {
37         this.context=context;
38     }
39
40     /**
41      * Platform dependent bundle injects its handle and it is retrieved in the method
42      */
43     private AlarmServiceFacade getAlarmServiceSPI (){
44         AlarmServiceFacade service =null;
45         if(context != null) {
46             try {
47                 ServiceReference<?> serviceReference = context.
48                         getServiceReference(AlarmServiceFacade.class.getName());
49                 service = (AlarmServiceFacade) context.
50                         getService(serviceReference);
51             }catch (NullPointerException ex){
52                 service = null;
53             }catch (Exception e){
54                 s_logger.error("Exception {} occurred in getting AlarmServiceSPI",e);
55             }
56         }
57         return service;
58     }
59
60     /**
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.
63      */
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();
71
72                 if (nType.equals("JMX.mbean.registered")) {
73                     if (mbn.toString().contains(DOMAIN)) {
74                         s_logger.debug("Received registeration of Mbean "+mbn);
75                         try {
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);
80                         }
81                     }
82                 }
83
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");
87                     }
88                 }
89             }
90             else if (notification instanceof AttributeChangeNotification) {
91                 AttributeChangeNotification acn =
92                         (AttributeChangeNotification) notification;
93
94                 s_logger.debug("Received attribute notification of Mbean: "
95                         + notification.getSource()
96                         + " for attribute:" + acn.getAttributeName() );
97
98                 if(acn.getAttributeName().toString().equals("raiseAlarmObject")){
99
100                     String value=acn.getNewValue().toString();
101                     value = value.replace(value.charAt(0), ' ');
102                     value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
103
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());
108                     } else {
109                         s_logger.debug("Alarm service not available");
110                     }
111
112                 } else if(acn.getAttributeName().toString().equals("clearAlarmObject")){
113
114                     String value=acn.getNewValue().toString();
115                     value = value.replace(value.charAt(0), ' ');
116                     value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
117
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());
122                     else
123                         s_logger.debug("Alarm service not available");
124                 }
125             }
126         }
127     }
128
129     /**
130      * Gets the platform MBeanServer instance and registers to get notification whenever alarm mbean is registered in the mbeanserver
131      */
132     @Override
133     public void run() {
134         mbs = ManagementFactory.getPlatformMBeanServer();
135
136         queryMbeans();
137
138         ObjectName delegate = null;
139         try {
140             delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
141         } catch (MalformedObjectNameException e) {
142             e.printStackTrace();
143         }
144         NotificationFilterSupport filter = new NotificationFilterSupport();
145         filter.enableType("JMX.mbean.registered");
146         filter.enableType("JMX.mbean.unregistered");
147
148         try {
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);
153         }
154
155         waitForNotification();
156     }
157
158     /**
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
161      */
162     public void queryMbeans() {
163
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)){
169                 try {
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);
174                 }
175             }
176         }
177     }
178     public void waitForNotification() {
179         while(true){
180             try {
181                 Thread.sleep(50);
182             }
183             catch(Exception ex){
184                 ex.printStackTrace();
185             }
186         }
187     }
188 }