Serialization/Deserialization and a host of other fixes
[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
38   private static final Logger LOG = LoggerFactory
39       .getLogger(AbstractBaseMBean.class);
40
41   MBeanServer server = ManagementFactory.getPlatformMBeanServer();
42   /**
43    * gets the MBean ObjectName
44    *
45    * @return Object name of the MBean
46    * @throws MalformedObjectNameException - The bean name does not have the right format.
47    * @throws NullPointerException - The bean name is null
48    */
49   protected ObjectName getMBeanObjectName()
50       throws MalformedObjectNameException, NullPointerException {
51     String name = BASE_JMX_PREFIX + "type="+getMBeanType()+",Category="+
52                                    getMBeanCategory() + ",name="+
53                                    getMBeanName();
54
55
56     return new ObjectName(name);
57   }
58
59   public boolean registerMBean() {
60     boolean registered = false;
61     try {
62       // Object to identify MBean
63       final ObjectName mbeanName = this.getMBeanObjectName();
64
65       Preconditions.checkArgument(mbeanName != null,
66           "Object name of the MBean cannot be null");
67
68       LOG.debug("Register MBean {}", mbeanName);
69
70       // unregistered if already registered
71       if (server.isRegistered(mbeanName)) {
72
73         LOG.debug("MBean {} found to be already registered", mbeanName);
74
75         try {
76           unregisterMBean(mbeanName);
77         } catch (Exception e) {
78
79           LOG.warn("unregister mbean {} resulted in exception {} ", mbeanName,
80               e);
81         }
82       }
83       server.registerMBean(this, mbeanName);
84
85       LOG.debug("MBean {} registered successfully",
86           mbeanName.getCanonicalName());
87       registered = true;
88     } catch (Exception e) {
89
90       LOG.error("registration failed {}", e);
91
92     }
93     return registered;
94   }
95
96
97   public boolean unregisterMBean() {
98     boolean unregister = false;
99     try {
100       ObjectName mbeanName = this.getMBeanObjectName();
101       unregister = true;
102       unregisterMBean(mbeanName);
103     } catch (Exception e) {
104
105       LOG.error("Failed when unregistering MBean {}", e);
106     }
107     return unregister;
108   }
109
110   private void unregisterMBean(ObjectName mbeanName)
111       throws MBeanRegistrationException, InstanceNotFoundException {
112
113     server.unregisterMBean(mbeanName);
114
115   }
116
117
118   /**
119    * @return name of bean
120    */
121   protected abstract String getMBeanName();
122
123   /**
124    * @return type of the MBean
125    */
126   protected abstract String getMBeanType();
127
128
129   /**
130    * @return Category name of teh bean
131    */
132   protected abstract String getMBeanCategory();
133
134   //require for test cases
135   public MBeanServer getMBeanServer() {
136     return server;
137   }
138 }