cfdbdb470e2cfb0f368e706ebb0bc662716e940f
[netvirt.git] / fcapsmanager / countermanager / src / main / java / org / opendaylight / vpnservice / fcapsmanager / countermanager / PMRegistrationListener.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.countermanager;
9
10 import java.lang.management.ManagementFactory;
11 import java.util.HashSet;
12 import java.util.Set;
13 import java.util.TreeSet;
14
15 import javax.management.InstanceNotFoundException;
16 import javax.management.MBeanServer;
17 import javax.management.MBeanServerNotification;
18 import javax.management.MalformedObjectNameException;
19 import javax.management.Notification;
20 import javax.management.NotificationFilterSupport;
21 import javax.management.NotificationListener;
22 import javax.management.ObjectName;
23
24 import org.osgi.framework.BundleContext;
25 import org.slf4j.LoggerFactory;
26
27 public class PMRegistrationListener implements Runnable {
28     private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(PMRegistrationListener.class);
29     static MBeanServer mbs = null;
30
31     private static String DOMAIN = "SDNC.PM";
32     public static HashSet<ObjectName> beanNames = new HashSet<ObjectName>();
33     private BundleContext context = null;
34
35     public PMRegistrationListener(BundleContext context){
36         this.context=context;
37     }
38
39     /**
40      * Gets register notification when a mbean is registered in platform Mbeanserver and checks if it is counter mbean and add it to the map.
41      */
42     public static class DelegateListener implements NotificationListener {
43         public void handleNotification(Notification notification, Object obj) {
44             if (notification instanceof MBeanServerNotification) {
45                 MBeanServerNotification msnotification =
46                         (MBeanServerNotification) notification;
47                 String nType = msnotification.getType();
48                 ObjectName mbn = msnotification.getMBeanName();
49                 if (nType.equals("JMX.mbean.registered")) {
50                     String mbean = mbn.toString();
51                     if(mbean.contains(DOMAIN)) {
52                         beanNames.add(mbn);
53                         LOG.debug("Beans are " +beanNames);
54                     }
55                 }
56                 if (nType.equals("JMX.mbean.unregistered")) {
57                     if(mbn.toString().contains(DOMAIN)) {
58                         beanNames.remove(mbn);
59                         LOG.debug(mbn +" MBean has been unregistered");
60                     }
61                 }
62             }
63         }
64     }
65
66     @Override
67     public void run(){
68         mbs = ManagementFactory.getPlatformMBeanServer();
69         queryMbeans();
70         DelegateListener delegateListener = new DelegateListener();
71         ObjectName delegate = null;
72         try {
73             delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
74         } catch (MalformedObjectNameException e) {
75             e.printStackTrace();
76         }
77         NotificationFilterSupport filter = new NotificationFilterSupport();
78
79         filter.enableType("JMX.mbean.registered");
80         filter.enableType("JMX.mbean.unregistered");
81
82         LOG.debug("Add PM Registeration Notification Listener");
83         try {
84             mbs.addNotificationListener(delegate, delegateListener, filter,null);
85         }catch (InstanceNotFoundException e) {
86             e.printStackTrace();
87         }
88         Poller poller = new Poller(this.context);
89         poller.polling();
90         waitforNotification();
91     }
92
93     /**
94      * Prepovising case to handle all counter mbeans which are registered before the installation of framework bundle
95      * Queries the platform Mbeanserver to retrieve registered counter mbean and add it to the map
96      */
97     public void queryMbeans() {
98         Set<ObjectName> names =
99                 new TreeSet<ObjectName>(mbs.queryNames(null, null));
100         LOG.debug("\nQueried MBeanServer for MBeans:");
101         for (ObjectName name : names) {
102             if(name.toString().contains(DOMAIN)){
103                 beanNames.add(name);
104             }
105         }
106     }
107
108     private void waitforNotification() {
109         while(true){
110             try {
111                 Thread.sleep(50);
112             } catch (InterruptedException e) {
113                 e.printStackTrace();
114             }
115         }
116     }
117 }