Optimizations, Monitoring and Logging
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / jmx / mbeans / AbstractBaseMBean.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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
10 package org.opendaylight.controller.cluster.datastore.jmx.mbeans;
11
12
13 import com.google.common.base.Preconditions;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import javax.management.InstanceNotFoundException;
18 import javax.management.MBeanRegistrationException;
19 import javax.management.MBeanServer;
20 import javax.management.MalformedObjectNameException;
21 import javax.management.ObjectName;
22 import java.lang.management.ManagementFactory;
23
24 /**
25  * All MBeans should extend this class that help in registering and
26  * unregistering the MBeans.
27  *
28  */
29
30
31 public abstract class AbstractBaseMBean {
32
33
34   public static String BASE_JMX_PREFIX = "org.opendaylight.controller:";
35   public static String JMX_TYPE_DISTRIBUTED_DATASTORE = "DistributedDatastore";
36   public static String JMX_CATEGORY_SHARD = "Shard";
37   public static String JMX_CATEGORY_SHARD_MANAGER = "ShardManager";
38
39   private static final Logger LOG = LoggerFactory
40       .getLogger(AbstractBaseMBean.class);
41
42   MBeanServer server = ManagementFactory.getPlatformMBeanServer();
43   /**
44    * gets the MBean ObjectName
45    *
46    * @return Object name of the MBean
47    * @throws MalformedObjectNameException - The bean name does not have the right format.
48    * @throws NullPointerException - The bean name is null
49    */
50   protected ObjectName getMBeanObjectName()
51       throws MalformedObjectNameException, NullPointerException {
52     String name = BASE_JMX_PREFIX + "type="+getMBeanType()+",Category="+
53                                    getMBeanCategory() + ",name="+
54                                    getMBeanName();
55
56
57     return new ObjectName(name);
58   }
59
60   public boolean registerMBean() {
61     boolean registered = false;
62     try {
63       // Object to identify MBean
64       final ObjectName mbeanName = this.getMBeanObjectName();
65
66       Preconditions.checkArgument(mbeanName != null,
67           "Object name of the MBean cannot be null");
68
69       LOG.debug("Register MBean {}", mbeanName);
70
71       // unregistered if already registered
72       if (server.isRegistered(mbeanName)) {
73
74         LOG.debug("MBean {} found to be already registered", mbeanName);
75
76         try {
77           unregisterMBean(mbeanName);
78         } catch (Exception e) {
79
80           LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName,
81               e);
82         }
83       }
84       server.registerMBean(this, mbeanName);
85
86       LOG.debug("MBean {} registered successfully",
87           mbeanName.getCanonicalName());
88       registered = true;
89     } catch (Exception e) {
90
91       LOG.error("registration failed {}", e);
92
93     }
94     return registered;
95   }
96
97
98   public boolean unregisterMBean() {
99     boolean unregister = false;
100     try {
101       ObjectName mbeanName = this.getMBeanObjectName();
102       unregister = true;
103       unregisterMBean(mbeanName);
104     } catch (Exception e) {
105
106       LOG.error("Failed when unregistering MBean {}", e);
107     }
108     return unregister;
109   }
110
111   private void unregisterMBean(ObjectName mbeanName)
112       throws MBeanRegistrationException, InstanceNotFoundException {
113
114     server.unregisterMBean(mbeanName);
115
116   }
117
118
119   /**
120    * @return name of bean
121    */
122   protected abstract String getMBeanName();
123
124   /**
125    * @return type of the MBean
126    */
127   protected abstract String getMBeanType();
128
129
130   /**
131    * @return Category name of teh bean
132    */
133   protected abstract String getMBeanCategory();
134
135   //require for test cases
136   public MBeanServer getMBeanServer() {
137     return server;
138   }
139 }