ad6f8502d82381f23e0c88c0fb9e230d89969a97
[vpnservice.git] / fcapsmanager / countermanager / src / main / java / org / opendaylight / vpnservice / fcapsmanager / countermanager / Poller.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 org.osgi.framework.BundleContext;
12 import org.osgi.framework.ServiceReference;
13 import org.slf4j.LoggerFactory;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.TimeUnit;
19 import javax.management.MBeanServer;
20 import javax.management.ObjectName;
21 import org.opendaylight.vpnservice.fcapsmanager.PMServiceFacade;
22
23 public class Poller {
24     private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(Poller.class);
25     private static BundleContext context = null;
26     public Poller(){
27     }
28     public Poller(BundleContext bundleContext) {
29         context = bundleContext;
30     }
31     //This method do the Polling every 5 second and retrieves the the counter details
32     //@Override
33     public void polling() {
34         LOG.debug("Poller Polling Mbean List and the content is " + PMRegistrationListener.beanNames);
35         ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
36         service.scheduleAtFixedRate(new Pollerthread(), 0, 5, TimeUnit.SECONDS);
37     }
38
39     /**
40      * Platform dependent bundle injects its handle and it is retrieved in the method
41      */
42     protected PMServiceFacade getPMServiceSPI (){
43         PMServiceFacade service =null;
44         if(context != null) {
45             try {
46                 ServiceReference<?> serviceReference = context.
47                         getServiceReference(PMServiceFacade.class.getName());
48                 service = (PMServiceFacade) context.
49                         getService(serviceReference);
50             }catch(NullPointerException ex){
51                 service = null;
52             }catch (Exception e){
53                 LOG.error("Exception {} occurred in getting PMServiceSPI",e);
54             }
55         }
56         return service;
57     }
58 }
59
60 class Pollerthread implements Runnable {
61     private final static org.slf4j.Logger LOG = LoggerFactory.getLogger(Pollerthread.class);
62     MBeanServer mbs = null;
63     Map<String,String> getCounter = new HashMap<String,String>();
64     Poller poller = new Poller();
65
66     /**
67      * Retrieve countermap from each counter mbean and send to platform
68      */
69     @SuppressWarnings("unchecked")
70     @Override
71     public void run() {
72         try {
73             mbs = ManagementFactory.getPlatformMBeanServer();
74             for (ObjectName objectName : PMRegistrationListener.beanNames) {
75                 getCounter=(Map<String, String>) mbs.invoke(objectName, "retrieveCounterMap",null,null);
76                 if(poller.getPMServiceSPI() != null)
77                     poller.getPMServiceSPI().connectToPMFactory(getCounter);
78                 else
79                     LOG.debug("PM service not available");
80             }
81         } catch (Exception e) {
82             LOG.error("Exception caught {} ", e);
83         }
84     }
85 }