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 // if on is without transaction, add it. Reason is that when using getters on MXBeans the transaction name is stripped
49 on = ObjectNameUtil.withTransactionName(on, getTransactionName());
50 // if this is service reference and user requests for implementation, look it up
51 on = ConfigRegistryJMXClient.translateServiceRefIfPossible(on, clazz, configMBeanServer);
52 on = ObjectNameUtil.withTransactionName(on, getTransactionName());
53 return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
57 * Usage of this method indicates error as config JMX uses solely MXBeans.
58 * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
59 * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
60 * This method will be removed soon.
63 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
64 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
68 public CommitStatus commit() throws ConflictingVersionException,
70 return configRegistryMXBeanProxy
71 .commitConfig(configTransactionControllerON);
75 public void assertVersion(int expectedParentVersion,
76 int expectedCurrentVersion) {
77 if (expectedParentVersion != getParentVersion()) {
78 throw new IllegalStateException();
80 if (expectedCurrentVersion != getVersion()) {
81 throw new IllegalStateException();
85 // proxy around ConfigManagerMXBean
87 public ObjectName createModule(String moduleName, String instanceName)
88 throws InstanceAlreadyExistsException {
89 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
93 public void destroyModule(ObjectName objectName)
94 throws InstanceNotFoundException {
95 configTransactionControllerMXBeanProxy.destroyModule(objectName);
103 public void destroyConfigBean(String moduleName, String instanceName)
104 throws InstanceNotFoundException {
105 destroyModule(ObjectNameUtil.createTransactionModuleON(
106 getTransactionName(), moduleName, instanceName));
110 public void destroyModule(String moduleName, String instanceName)
111 throws InstanceNotFoundException {
112 destroyModule(ObjectNameUtil.createTransactionModuleON(
113 getTransactionName(), moduleName, instanceName));
117 public void abortConfig() {
118 configTransactionControllerMXBeanProxy.abortConfig();
122 public void validateConfig() throws ValidationException {
123 configTransactionControllerMXBeanProxy.validateConfig();
127 public long getParentVersion() {
129 return (Long) configMBeanServer.getAttribute(
130 configTransactionControllerON, "ParentVersion");
131 } catch (JMException e) {
132 throw new RuntimeException(e);
137 public long getVersion() {
139 return (Long) configMBeanServer.getAttribute(
140 configTransactionControllerON, "Version");
141 } catch (JMException e) {
142 throw new RuntimeException(e);
147 public String getTransactionName() {
148 return configTransactionControllerMXBeanProxy.getTransactionName();
152 public Set<String> getAvailableModuleNames() {
153 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
157 public ObjectName getObjectName() {
158 return configTransactionControllerON;
162 public Set<ObjectName> lookupConfigBeans() {
163 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
167 public Set<ObjectName> lookupConfigBeans(String moduleName) {
168 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
172 public ObjectName lookupConfigBean(String moduleName, String instanceName)
173 throws InstanceNotFoundException {
174 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
178 public Set<ObjectName> lookupConfigBeans(String moduleName,
179 String instanceName) {
180 return configTransactionControllerMXBeanProxy
181 .lookupConfigBeans(moduleName, instanceName);
185 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
186 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
190 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
191 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
195 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
196 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
200 public void removeAllServiceReferences() {
201 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
205 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
206 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
210 public Map<String, Map<String, ObjectName>> getServiceMapping() {
211 return configTransactionControllerMXBeanProxy.getServiceMapping();
215 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
216 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
220 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
221 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
225 public String getServiceInterfaceName(String namespace, String localName) {
226 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
230 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
231 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
235 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
236 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
240 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
241 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
245 public void validateBean(ObjectName configBeanON)
246 throws ValidationException {
248 configMBeanServer.invoke(configBeanON, "validate", null, null);
249 } catch (MBeanException e) {
250 Exception targetException = e.getTargetException();
251 if (targetException instanceof ValidationException){
252 throw (ValidationException) targetException;
254 throw new RuntimeException(e);
256 } catch (JMException e) {
257 throw new RuntimeException(e);
262 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
263 if (ObjectNameUtil.getTransactionName(on) == null)
264 throw new IllegalArgumentException("Not in transaction instance "
265 + on + ", no transaction name present");
268 configMBeanServer.setAttribute(on, attribute);
269 } catch (JMException e) {
270 throw new IllegalStateException("Unable to set attribute "
271 + attrName + " for " + on, e);
276 public Attribute getAttribute(ObjectName on, String attrName) {
277 if (ObjectNameUtil.getTransactionName(on) == null)
278 throw new IllegalArgumentException("Not in transaction instance "
279 + on + ", no transaction name present");
282 return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
283 } catch (JMException e) {
284 throw new IllegalStateException("Unable to get attribute "
285 + attrName + " for " + on, e);
290 public Set<String> getAvailableModuleFactoryQNames() {
291 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();