80065f0c279e70687be77d9ed1868fcfdc6f1b70
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / snd / ITMStatusMonitor.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.genius.itm.snd;
9
10 import java.lang.management.ManagementFactory;
11 import javax.management.InstanceAlreadyExistsException;
12 import javax.management.InstanceNotFoundException;
13 import javax.management.MBeanRegistrationException;
14 import javax.management.MBeanServer;
15 import javax.management.MalformedObjectNameException;
16 import javax.management.NotCompliantMBeanException;
17 import javax.management.ObjectName;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class ITMStatusMonitor implements ITMStatusMonitorMBean {
22
23     private String serviceStatus;
24     private static ITMStatusMonitor itmStatusMonitor = new ITMStatusMonitor();
25     private static final String JMX_ITM_OBJ_NAME = "com.ericsson.sdncp.services.status:type=SvcItmService";
26     private static final Logger LOG = LoggerFactory.getLogger(ITMStatusMonitor.class);
27
28     private ITMStatusMonitor() {
29     }
30
31     public void registerMbean() {
32         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
33         try {
34             ObjectName objName = new ObjectName(JMX_ITM_OBJ_NAME);
35             mbs.registerMBean(itmStatusMonitor, objName);
36             LOG.info("itm MXBean registration SUCCESSFUL!!! {}", JMX_ITM_OBJ_NAME);
37         } catch (InstanceAlreadyExistsException iaeEx) {
38             LOG.error("itm MXBean registration FAILED with InstanceAlreadyExistsException", iaeEx);
39         } catch (MBeanRegistrationException mbrEx) {
40             LOG.error("itm MXBean registration FAILED with MBeanRegistrationException", mbrEx);
41         } catch (NotCompliantMBeanException ncmbEx) {
42             LOG.error("itm MXBean registration FAILED with NotCompliantMBeanException", ncmbEx);
43         } catch (MalformedObjectNameException monEx) {
44             LOG.error("itm MXBean registration FAILED with MalformedObjectNameException", monEx);
45         }
46     }
47
48     public void unregisterMbean() {
49         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
50         try {
51             ObjectName objName = new ObjectName(JMX_ITM_OBJ_NAME);
52             mbs.unregisterMBean(objName);
53             LOG.info("itm MXBean un-registration SUCCESSFUL!!! {}", JMX_ITM_OBJ_NAME);
54         } catch (MalformedObjectNameException e) {
55             LOG.error("itm MXBean un-registration FAILED with MalformedObjectNameException", e);
56         } catch (MBeanRegistrationException e) {
57             LOG.warn("itm MXBean un-registration FAILED with MBeanRegistrationException", e);
58         } catch (InstanceNotFoundException e) {
59             LOG.debug("itm MXBean un-registration FAILED with InstanceNotFoundException", e);
60         }
61     }
62
63     public static ITMStatusMonitor getInstance() {
64         return itmStatusMonitor;
65     }
66
67     @Override
68     public String acquireServiceStatus() {
69         return serviceStatus;
70     }
71
72     public void reportStatus(@SuppressWarnings("hiding") String serviceStatus) {
73         this.serviceStatus = serviceStatus;
74     }
75 }