2cb492cace665216a27d144e4a8b0f8aa168c3c6
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / ReconciliationJMXAgent.java
1 /*
2  * Copyright (c) 2020 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
9 package org.opendaylight.openflowplugin.applications.frm;
10
11 import java.lang.management.ManagementFactory;
12 import javax.inject.Inject;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.MBeanRegistrationException;
15 import javax.management.MBeanServer;
16 import javax.management.MalformedObjectNameException;
17 import javax.management.NotCompliantMBeanException;
18 import javax.management.ObjectName;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public final class ReconciliationJMXAgent {
23     private static final Logger LOG = LoggerFactory.getLogger(ReconciliationJMXAgent.class);
24     private static final String OF_RECONC_BEANNAME
25             = "org.opendaylight.openflowplugin.frm:type=ReconciliationState";
26     private MBeanServer mbs = null;
27     private ObjectName objectName = null;
28
29     @Inject
30     public ReconciliationJMXAgent(final ReconciliationJMXService reconciliationJMXService) {
31         mbs = ManagementFactory.getPlatformMBeanServer();
32         try {
33             objectName = new ObjectName(OF_RECONC_BEANNAME);
34             registerReconciliationMbean(reconciliationJMXService);
35         } catch (MalformedObjectNameException e) {
36             LOG.error("ObjectName instance creation failed for Mbean {} : ", OF_RECONC_BEANNAME, e);
37         }
38     }
39
40     public void registerReconciliationMbean(ReconciliationJMXService reconciliationJMXService) {
41         try {
42             // Uniquely identify the MBeans and register them with the platform MBeanServer
43             if (!mbs.isRegistered(objectName)) {
44                 mbs.registerMBean(reconciliationJMXService, objectName);
45                 LOG.debug("Registered Mbean {} successfully", OF_RECONC_BEANNAME);
46             }
47         } catch (MBeanRegistrationException | InstanceAlreadyExistsException | NotCompliantMBeanException e) {
48             LOG.error("Registeration failed for Mbean {} : ", OF_RECONC_BEANNAME , e);
49         }
50     }
51 }