2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.util;
10 import org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.ValidationException;
12 import org.opendaylight.controller.config.api.jmx.CommitStatus;
13 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import javax.management.AttributeNotFoundException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.JMException;
19 import javax.management.JMX;
20 import javax.management.MBeanException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectInstance;
23 import javax.management.ObjectName;
24 import javax.management.ReflectionException;
25 import java.util.Arrays;
26 import java.util.List;
29 import org.opendaylight.controller.config.api.jmx.ServiceReferenceMXBean;
31 public class ConfigRegistryJMXClient implements ConfigRegistryClient {
32 private final ConfigRegistryMXBean configRegistryMXBeanProxy;
33 private final ObjectName configRegistryON;
34 private final MBeanServer configMBeanServer;
36 public ConfigRegistryJMXClient(MBeanServer configMBeanServer) {
37 this.configMBeanServer = configMBeanServer;
38 configRegistryON = OBJECT_NAME;
39 Set<ObjectInstance> searchResult = configMBeanServer.queryMBeans(
40 configRegistryON, null);
41 if (!(searchResult.size() == 1)) {
42 throw new IllegalStateException("Config registry not found");
44 configRegistryMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer, configRegistryON, ConfigRegistryMXBean.class,
49 public ConfigTransactionJMXClient createTransaction() {
50 ObjectName configTransactionControllerON = beginConfig();
51 return getConfigTransactionClient(configTransactionControllerON);
55 public ConfigTransactionJMXClient getConfigTransactionClient(
56 String transactionName) {
57 ObjectName objectName = ObjectNameUtil
58 .createTransactionControllerON(transactionName);
59 return getConfigTransactionClient(objectName);
63 public ConfigTransactionJMXClient getConfigTransactionClient(
64 ObjectName objectName) {
65 return new ConfigTransactionJMXClient(configRegistryMXBeanProxy, objectName,
70 * Usage of this method indicates error as config JMX uses solely MXBeans.
71 * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
72 * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
73 * This method will be removed soon.
76 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
77 on = translateServiceRefIfPossible(on, clazz, configMBeanServer);
78 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
81 static ObjectName translateServiceRefIfPossible(ObjectName on, Class<?> clazz, MBeanServer configMBeanServer) {
82 if (ObjectNameUtil.isServiceReference(on) && clazz.equals(ServiceReferenceMXBean.class) == false) {
83 ServiceReferenceMXBean proxy = JMX.newMXBeanProxy(configMBeanServer, on, ServiceReferenceMXBean.class);
84 on = proxy.getCurrentImplementation();
90 public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
91 return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
95 public ObjectName beginConfig() {
96 return configRegistryMXBeanProxy.beginConfig();
100 public CommitStatus commitConfig(ObjectName transactionControllerON)
101 throws ConflictingVersionException, ValidationException {
102 return configRegistryMXBeanProxy.commitConfig(transactionControllerON);
106 public List<ObjectName> getOpenConfigs() {
107 return configRegistryMXBeanProxy.getOpenConfigs();
111 public long getVersion() {
113 return (Long) configMBeanServer.getAttribute(configRegistryON,
115 } catch (JMException e) {
116 throw new RuntimeException(e);
121 public Set<String> getAvailableModuleNames() {
122 return configRegistryMXBeanProxy.getAvailableModuleNames();
126 public boolean isHealthy() {
127 return configRegistryMXBeanProxy.isHealthy();
131 public Set<ObjectName> lookupConfigBeans() {
132 return configRegistryMXBeanProxy.lookupConfigBeans();
136 public Set<ObjectName> lookupConfigBeans(String moduleName) {
137 return configRegistryMXBeanProxy.lookupConfigBeans(moduleName);
141 public Set<ObjectName> lookupConfigBeans(String moduleName,
142 String instanceName) {
143 return configRegistryMXBeanProxy.lookupConfigBeans(moduleName, instanceName);
147 public ObjectName lookupConfigBean(String moduleName, String instanceName)
148 throws InstanceNotFoundException {
149 return configRegistryMXBeanProxy.lookupConfigBean(moduleName, instanceName);
153 public Set<ObjectName> lookupRuntimeBeans() {
154 return configRegistryMXBeanProxy.lookupRuntimeBeans();
158 public Set<ObjectName> lookupRuntimeBeans(String ifcName,
159 String instanceName) {
160 return configRegistryMXBeanProxy.lookupRuntimeBeans(ifcName, instanceName);
164 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
165 configRegistryMXBeanProxy.checkConfigBeanExists(objectName);
169 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
170 return configRegistryMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
174 public Map<String, Map<String, ObjectName>> getServiceMapping() {
175 return configRegistryMXBeanProxy.getServiceMapping();
179 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
180 return configRegistryMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
184 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
185 return configRegistryMXBeanProxy.lookupServiceInterfaceNames(objectName);
189 public String getServiceInterfaceName(String namespace, String localName) {
190 return configRegistryMXBeanProxy.getServiceInterfaceName(namespace, localName);
194 public Object invokeMethod(ObjectName on, String name, Object[] params,
195 String[] signature) {
197 return configMBeanServer.invoke(on, name, params, signature);
198 } catch (InstanceNotFoundException | ReflectionException
199 | MBeanException e) {
200 throw new RuntimeException("Unable to invoke operation " + name
201 + " on " + on + " with attributes "
202 + Arrays.toString(params) + " and signature "
203 + Arrays.toString(signature), e);
208 public Object getAttributeCurrentValue(ObjectName on, String attributeName) {
210 return configMBeanServer.getAttribute(on, attributeName);
211 } catch (AttributeNotFoundException | InstanceNotFoundException
212 | MBeanException | ReflectionException e) {
213 throw new RuntimeException("Unable to get attribute "
214 + attributeName + " for " + on, e);
219 public Set<String> getAvailableModuleFactoryQNames() {
220 return configRegistryMXBeanProxy.getAvailableModuleFactoryQNames();
224 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
225 return configRegistryMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
229 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
230 configRegistryMXBeanProxy.checkServiceReferenceExists(objectName);