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;
13 import javax.management.Attribute;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.JMException;
17 import javax.management.JMX;
18 import javax.management.MBeanException;
19 import javax.management.MBeanServer;
20 import javax.management.ObjectName;
22 import org.opendaylight.controller.config.api.ConflictingVersionException;
23 import org.opendaylight.controller.config.api.ValidationException;
24 import org.opendaylight.controller.config.api.jmx.CommitStatus;
25 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
26 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
29 public class ConfigTransactionJMXClient implements ConfigTransactionClient {
30 private final ConfigRegistryMXBean configRegistryMXBeanProxy;
31 private final ObjectName configTransactionControllerON;
32 private final ConfigTransactionControllerMXBean configTransactionControllerMXBeanProxy;
33 private final MBeanServer configMBeanServer;
35 public ConfigTransactionJMXClient(
36 ConfigRegistryMXBean configRegistryMXBeanProxy,
37 ObjectName configTransactionControllerON,
38 MBeanServer configMBeanServer) {
39 this.configMBeanServer = configMBeanServer;
40 this.configRegistryMXBeanProxy = configRegistryMXBeanProxy;
41 this.configTransactionControllerON = configTransactionControllerON;
42 this.configTransactionControllerMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer,
43 configTransactionControllerON,
44 ConfigTransactionControllerMXBean.class);
47 public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
48 ObjectName onName = on;
49 // if on is without transaction, add it. Reason is that when using getters on MXBeans the transaction name is stripped
50 onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
51 // if this is service reference and user requests for implementation, look it up
52 onName = ConfigRegistryJMXClient.translateServiceRefIfPossible(onName, clazz, configMBeanServer);
53 onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
54 return JMX.newMXBeanProxy(configMBeanServer, onName, clazz);
58 * Usage of this method indicates error as config JMX uses solely MXBeans.
59 * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
60 * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
61 * This method will be removed soon.
64 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
65 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
69 public CommitStatus commit() throws ConflictingVersionException,
71 return configRegistryMXBeanProxy
72 .commitConfig(configTransactionControllerON);
76 public void assertVersion(int expectedParentVersion,
77 int expectedCurrentVersion) {
78 if (expectedParentVersion != getParentVersion()) {
79 throw new IllegalStateException();
81 if (expectedCurrentVersion != getVersion()) {
82 throw new IllegalStateException();
86 // proxy around ConfigManagerMXBean
88 public ObjectName createModule(String moduleName, String instanceName)
89 throws InstanceAlreadyExistsException {
90 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
94 public void destroyModule(ObjectName objectName)
95 throws InstanceNotFoundException {
96 configTransactionControllerMXBeanProxy.destroyModule(objectName);
104 public void destroyConfigBean(String moduleName, String instanceName)
105 throws InstanceNotFoundException {
106 destroyModule(ObjectNameUtil.createTransactionModuleON(
107 getTransactionName(), moduleName, instanceName));
111 public void destroyModule(String moduleName, String instanceName)
112 throws InstanceNotFoundException {
113 destroyModule(ObjectNameUtil.createTransactionModuleON(
114 getTransactionName(), moduleName, instanceName));
118 public void abortConfig() {
119 configTransactionControllerMXBeanProxy.abortConfig();
123 public void validateConfig() throws ValidationException {
124 configTransactionControllerMXBeanProxy.validateConfig();
128 public long getParentVersion() {
130 return (Long) configMBeanServer.getAttribute(
131 configTransactionControllerON, "ParentVersion");
132 } catch (JMException e) {
133 throw new RuntimeException(e);
138 public long getVersion() {
140 return (Long) configMBeanServer.getAttribute(
141 configTransactionControllerON, "Version");
142 } catch (JMException e) {
143 throw new RuntimeException(e);
148 public String getTransactionName() {
149 return configTransactionControllerMXBeanProxy.getTransactionName();
153 public Set<String> getAvailableModuleNames() {
154 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
158 public ObjectName getObjectName() {
159 return configTransactionControllerON;
163 public Set<ObjectName> lookupConfigBeans() {
164 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
168 public Set<ObjectName> lookupConfigBeans(String moduleName) {
169 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
173 public ObjectName lookupConfigBean(String moduleName, String instanceName)
174 throws InstanceNotFoundException {
175 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
179 public Set<ObjectName> lookupConfigBeans(String moduleName,
180 String instanceName) {
181 return configTransactionControllerMXBeanProxy
182 .lookupConfigBeans(moduleName, instanceName);
186 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
187 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
191 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
192 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
196 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
197 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
201 public void removeAllServiceReferences() {
202 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
206 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
207 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
211 public Map<String, Map<String, ObjectName>> getServiceMapping() {
212 return configTransactionControllerMXBeanProxy.getServiceMapping();
216 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
217 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
221 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
222 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
226 public String getServiceInterfaceName(String namespace, String localName) {
227 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
231 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
232 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
236 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
237 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
241 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
242 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
246 public void validateBean(ObjectName configBeanON)
247 throws ValidationException {
249 configMBeanServer.invoke(configBeanON, "validate", null, null);
250 } catch (MBeanException e) {
251 Exception targetException = e.getTargetException();
252 if (targetException instanceof ValidationException){
253 throw (ValidationException) targetException;
255 throw new RuntimeException(e);
257 } catch (JMException e) {
258 throw new RuntimeException(e);
263 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
264 if (ObjectNameUtil.getTransactionName(on) == null) {
265 throw new IllegalArgumentException("Not in transaction instance "
266 + on + ", no transaction name present");
270 configMBeanServer.setAttribute(on, attribute);
271 } catch (JMException e) {
272 throw new IllegalStateException("Unable to set attribute "
273 + attrName + " for " + on, e);
278 public Attribute getAttribute(ObjectName on, String attrName) {
279 if (ObjectNameUtil.getTransactionName(on) == null) {
280 throw new IllegalArgumentException("Not in transaction instance "
281 + on + ", no transaction name present");
285 return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
286 } catch (JMException e) {
287 throw new IllegalStateException("Unable to get attribute "
288 + attrName + " for " + on, e);
293 public Set<String> getAvailableModuleFactoryQNames() {
294 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();