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;
12 import javax.management.Attribute;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.JMException;
16 import javax.management.JMX;
17 import javax.management.MBeanException;
18 import javax.management.MBeanServer;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.ConflictingVersionException;
21 import org.opendaylight.controller.config.api.ValidationException;
22 import org.opendaylight.controller.config.api.jmx.CommitStatus;
23 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
24 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 public class ConfigTransactionJMXClient implements ConfigTransactionClient {
28 private final ConfigRegistryMXBean configRegistryMXBeanProxy;
29 private final ObjectName configTransactionControllerON;
30 private final ConfigTransactionControllerMXBean configTransactionControllerMXBeanProxy;
31 private final MBeanServer configMBeanServer;
33 public ConfigTransactionJMXClient(
34 final ConfigRegistryMXBean configRegistryMXBeanProxy,
35 final ObjectName configTransactionControllerON,
36 final MBeanServer configMBeanServer) {
37 this.configMBeanServer = configMBeanServer;
38 this.configRegistryMXBeanProxy = configRegistryMXBeanProxy;
39 this.configTransactionControllerON = configTransactionControllerON;
40 this.configTransactionControllerMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer,
41 configTransactionControllerON,
42 ConfigTransactionControllerMXBean.class);
45 public <T> T newMXBeanProxy(final ObjectName on, final Class<T> clazz) {
46 ObjectName onName = on;
47 // if on is without transaction, add it. Reason is that when using getters on MXBeans the transaction name is stripped
48 onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
49 // if this is service reference and user requests for implementation, look it up
50 onName = ConfigRegistryJMXClient.translateServiceRefIfPossible(onName, clazz, configMBeanServer);
51 onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
52 return JMX.newMXBeanProxy(configMBeanServer, onName, clazz);
56 * Usage of this method indicates error as config JMX uses solely MXBeans.
57 * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
58 * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
59 * This method will be removed soon.
62 public <T> T newMBeanProxy(final ObjectName on, final Class<T> clazz) {
63 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
67 public CommitStatus commit() throws ConflictingVersionException,
69 return configRegistryMXBeanProxy
70 .commitConfig(configTransactionControllerON);
74 public void assertVersion(final int expectedParentVersion,
75 final int expectedCurrentVersion) {
76 if (expectedParentVersion != getParentVersion()) {
77 throw new IllegalStateException();
79 if (expectedCurrentVersion != getVersion()) {
80 throw new IllegalStateException();
84 // proxy around ConfigManagerMXBean
86 public ObjectName createModule(final String moduleName, final String instanceName)
87 throws InstanceAlreadyExistsException {
88 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
92 public void reCreateModule(final ObjectName objectName) throws InstanceNotFoundException {
93 configTransactionControllerMXBeanProxy.reCreateModule(objectName);
97 public void destroyModule(final ObjectName objectName)
98 throws InstanceNotFoundException {
99 configTransactionControllerMXBeanProxy.destroyModule(objectName);
107 public void destroyConfigBean(final String moduleName, final String instanceName)
108 throws InstanceNotFoundException {
109 destroyModule(ObjectNameUtil.createTransactionModuleON(
110 getTransactionName(), moduleName, instanceName));
114 public void destroyModule(final String moduleName, final String instanceName)
115 throws InstanceNotFoundException {
116 destroyModule(ObjectNameUtil.createTransactionModuleON(
117 getTransactionName(), moduleName, instanceName));
121 public void abortConfig() {
122 configTransactionControllerMXBeanProxy.abortConfig();
126 public void validateConfig() throws ValidationException {
127 configTransactionControllerMXBeanProxy.validateConfig();
131 public long getParentVersion() {
133 return (Long) configMBeanServer.getAttribute(
134 configTransactionControllerON, "ParentVersion");
135 } catch (final JMException e) {
136 throw new RuntimeException(e);
141 public long getVersion() {
143 return (Long) configMBeanServer.getAttribute(
144 configTransactionControllerON, "Version");
145 } catch (final JMException e) {
146 throw new RuntimeException(e);
151 public String getTransactionName() {
152 return configTransactionControllerMXBeanProxy.getTransactionName();
156 public Set<String> getAvailableModuleNames() {
157 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
161 public ObjectName getObjectName() {
162 return configTransactionControllerON;
166 public Set<ObjectName> lookupConfigBeans() {
167 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
171 public Set<ObjectName> lookupConfigBeans(final String moduleName) {
172 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
176 public ObjectName lookupConfigBean(final String moduleName, final String instanceName)
177 throws InstanceNotFoundException {
178 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
182 public Set<ObjectName> lookupConfigBeans(final String moduleName,
183 final String instanceName) {
184 return configTransactionControllerMXBeanProxy
185 .lookupConfigBeans(moduleName, instanceName);
189 public void checkConfigBeanExists(final ObjectName objectName) throws InstanceNotFoundException {
190 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
194 public ObjectName saveServiceReference(final String serviceInterfaceName, final String refName, final ObjectName moduleON) throws InstanceNotFoundException {
195 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
199 public void removeServiceReference(final String serviceInterfaceName, final String refName) throws InstanceNotFoundException{
200 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
204 public void removeAllServiceReferences() {
205 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
209 public ObjectName lookupConfigBeanByServiceInterfaceName(final String serviceInterfaceQName, final String refName) {
210 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
214 public Map<String, Map<String, ObjectName>> getServiceMapping() {
215 return configTransactionControllerMXBeanProxy.getServiceMapping();
219 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(final String serviceInterfaceQName) {
220 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
224 public Set<String> lookupServiceInterfaceNames(final ObjectName objectName) throws InstanceNotFoundException {
225 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
229 public String getServiceInterfaceName(final String namespace, final String localName) {
230 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
234 public boolean removeServiceReferences(final ObjectName objectName) throws InstanceNotFoundException {
235 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
239 public ObjectName getServiceReference(final String serviceInterfaceQName, final String refName) throws InstanceNotFoundException {
240 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
244 public void checkServiceReferenceExists(final ObjectName objectName) throws InstanceNotFoundException {
245 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
249 public Attribute getAttribute(final ObjectName on, final String attrName) {
250 if (ObjectNameUtil.getTransactionName(on) == null) {
251 throw new IllegalArgumentException("Not in transaction instance "
252 + on + ", no transaction name present");
256 return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
257 } catch (final JMException e) {
258 throw new IllegalStateException("Unable to get attribute "
259 + attrName + " for " + on, e);
264 public Object getAttributeCurrentValue(final ObjectName on, final String attrName) {
265 return getAttribute(on, attrName).getValue();
269 public void validateBean(final ObjectName configBeanON)
270 throws ValidationException {
272 configMBeanServer.invoke(configBeanON, "validate", null, null);
273 } catch (final MBeanException e) {
274 Exception targetException = e.getTargetException();
275 if (targetException instanceof ValidationException){
276 throw (ValidationException) targetException;
278 throw new RuntimeException(e);
280 } catch (final JMException e) {
281 throw new RuntimeException(e);
286 public void setAttribute(final ObjectName on, final String attrName, final Attribute attribute) {
287 if (ObjectNameUtil.getTransactionName(on) == null) {
288 throw new IllegalArgumentException("Not in transaction instance "
289 + on + ", no transaction name present");
293 configMBeanServer.setAttribute(on, attribute);
294 } catch (final JMException e) {
295 throw new IllegalStateException("Unable to set attribute "
296 + attrName + " for " + on, e);
301 public Set<String> getAvailableModuleFactoryQNames() {
302 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();
306 public Set<ObjectName> lookupRuntimeBeans() {
307 return configTransactionControllerMXBeanProxy.lookupRuntimeBeans();
311 public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
312 return configTransactionControllerMXBeanProxy.lookupRuntimeBeans(moduleName, instanceName);