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.countermanager;
10 import java.lang.management.ManagementFactory;
11 import java.util.HashSet;
13 import java.util.TreeSet;
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;
24 import org.osgi.framework.BundleContext;
25 import org.slf4j.LoggerFactory;
27 public class PMRegistrationListener implements Runnable {
28 private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(PMRegistrationListener.class);
29 static MBeanServer mbs = null;
31 private static String DOMAIN = "SDNC.PM";
32 public static HashSet<ObjectName> beanNames = new HashSet<ObjectName>();
33 private BundleContext context = null;
35 public PMRegistrationListener(BundleContext context){
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.
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)) {
53 LOG.debug("Beans are " +beanNames);
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");
68 mbs = ManagementFactory.getPlatformMBeanServer();
70 DelegateListener delegateListener = new DelegateListener();
71 ObjectName delegate = null;
73 delegate = new ObjectName("JMImplementation:type=MBeanServerDelegate");
74 } catch (MalformedObjectNameException e) {
77 NotificationFilterSupport filter = new NotificationFilterSupport();
79 filter.enableType("JMX.mbean.registered");
80 filter.enableType("JMX.mbean.unregistered");
82 LOG.debug("Add PM Registeration Notification Listener");
84 mbs.addNotificationListener(delegate, delegateListener, filter,null);
85 }catch (InstanceNotFoundException e) {
88 Poller poller = new Poller(this.context);
90 waitforNotification();
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
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)){
108 private void waitforNotification() {
112 } catch (InterruptedException e) {