Merge "Integration of Fcaps framework"
[vpnservice.git] / fcapsmanager / alarmmanager / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.fcapsmanager.alarmmanager;
9
10 import javax.management.*;
11 import java.lang.management.ManagementFactory;
12 import java.util.*;
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;
18
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";
23
24     private final DelegateListener delegateListener = new DelegateListener();
25     private BundleContext context = null;
26
27     public AlarmNotificationListeners(BundleContext context) {
28         this.context=context;
29     }
30
31     /**
32      * Platform dependent bundle injects its handle and it is retrieved in the method
33      */
34     private AlarmServiceFacade getAlarmServiceSPI (){
35         AlarmServiceFacade service =null;
36         if(context != null) {
37             try {
38                 ServiceReference<?> serviceReference = context.
39                         getServiceReference(AlarmServiceFacade.class.getName());
40                 service = (AlarmServiceFacade) context.
41                         getService(serviceReference);
42             }catch (NullPointerException ex){
43                 service = null;
44             }catch (Exception e){
45                 s_logger.error("Exception {} occurred in getting AlarmServiceSPI",e);
46             }
47         }
48         return service;
49     }
50
51     /**
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.
54      */
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();
62
63                 if (nType.equals("JMX.mbean.registered")) {
64                     if (mbn.toString().contains(DOMAIN)) {
65                         s_logger.debug("Received registeration of Mbean "+mbn);
66                         try {
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);
71                         }
72                     }
73                 }
74
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");
78                     }
79                 }
80             }
81             else if (notification instanceof AttributeChangeNotification) {
82                 AttributeChangeNotification acn =
83                         (AttributeChangeNotification) notification;
84
85                 s_logger.debug("Received attribute notification of Mbean: "
86                         + notification.getSource()
87                         + " for attribute:" + acn.getAttributeName() );
88
89                 if(acn.getAttributeName().toString().equals("raiseAlarmObject")){
90
91                     String value=acn.getNewValue().toString();
92                     value = value.replace(value.charAt(0), ' ');
93                     value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
94
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());
99                     } else {
100                         s_logger.debug("Alarm service not available");
101                     }
102
103                 } else if(acn.getAttributeName().toString().equals("clearAlarmObject")){
104
105                     String value=acn.getNewValue().toString();
106                     value = value.replace(value.charAt(0), ' ');
107                     value = value.replace(value.charAt(value.lastIndexOf("]")), ' ');
108
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());
113                     else
114                         s_logger.debug("Alarm service not available");
115                 }
116             }
117         }
118     }
119
120     /**
121      * Gets the platform MBeanServer instance and registers to get notification whenever alarm mbean is registered in the mbeanserver
122      */
123     @Override
124     public void run() {
125         mbs = ManagementFactory.getPlatformMBeanServer();
126
127         queryMbeans();
128
129         ObjectName delegate = null;
130         try {
131             delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
132         } catch (MalformedObjectNameException e) {
133             e.printStackTrace();
134         }
135         NotificationFilterSupport filter = new NotificationFilterSupport();
136         filter.enableType("JMX.mbean.registered");
137         filter.enableType("JMX.mbean.unregistered");
138
139         try {
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);
144         }
145
146         waitForNotification();
147     }
148
149     /**
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
152      */
153     public void queryMbeans() {
154
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)){
160                 try {
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);
165                 }
166             }
167         }
168     }
169     public void waitForNotification() {
170         while(true){
171             try {
172                 Thread.sleep(50);
173             }
174             catch(Exception ex){
175                 ex.printStackTrace();
176             }
177         }
178     }
179 }