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 ConfigRegistryMXBean configRegistryMXBeanProxy,
35 ObjectName configTransactionControllerON,
36 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(ObjectName on, 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(ObjectName on, 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(int expectedParentVersion,
75 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(String moduleName, String instanceName)
87 throws InstanceAlreadyExistsException {
88 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
92 public void destroyModule(ObjectName objectName)
93 throws InstanceNotFoundException {
94 configTransactionControllerMXBeanProxy.destroyModule(objectName);
102 public void destroyConfigBean(String moduleName, String instanceName)
103 throws InstanceNotFoundException {
104 destroyModule(ObjectNameUtil.createTransactionModuleON(
105 getTransactionName(), moduleName, instanceName));
109 public void destroyModule(String moduleName, String instanceName)
110 throws InstanceNotFoundException {
111 destroyModule(ObjectNameUtil.createTransactionModuleON(
112 getTransactionName(), moduleName, instanceName));
116 public void abortConfig() {
117 configTransactionControllerMXBeanProxy.abortConfig();
121 public void validateConfig() throws ValidationException {
122 configTransactionControllerMXBeanProxy.validateConfig();
126 public long getParentVersion() {
128 return (Long) configMBeanServer.getAttribute(
129 configTransactionControllerON, "ParentVersion");
130 } catch (JMException e) {
131 throw new RuntimeException(e);
136 public long getVersion() {
138 return (Long) configMBeanServer.getAttribute(
139 configTransactionControllerON, "Version");
140 } catch (JMException e) {
141 throw new RuntimeException(e);
146 public String getTransactionName() {
147 return configTransactionControllerMXBeanProxy.getTransactionName();
151 public Set<String> getAvailableModuleNames() {
152 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
156 public ObjectName getObjectName() {
157 return configTransactionControllerON;
161 public Set<ObjectName> lookupConfigBeans() {
162 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
166 public Set<ObjectName> lookupConfigBeans(String moduleName) {
167 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
171 public ObjectName lookupConfigBean(String moduleName, String instanceName)
172 throws InstanceNotFoundException {
173 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
177 public Set<ObjectName> lookupConfigBeans(String moduleName,
178 String instanceName) {
179 return configTransactionControllerMXBeanProxy
180 .lookupConfigBeans(moduleName, instanceName);
184 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
185 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
189 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
190 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
194 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
195 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
199 public void removeAllServiceReferences() {
200 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
204 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
205 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
209 public Map<String, Map<String, ObjectName>> getServiceMapping() {
210 return configTransactionControllerMXBeanProxy.getServiceMapping();
214 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
215 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
219 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
220 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
224 public String getServiceInterfaceName(String namespace, String localName) {
225 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
229 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
230 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
234 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
235 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
239 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
240 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
244 public void validateBean(ObjectName configBeanON)
245 throws ValidationException {
247 configMBeanServer.invoke(configBeanON, "validate", null, null);
248 } catch (MBeanException e) {
249 Exception targetException = e.getTargetException();
250 if (targetException instanceof ValidationException){
251 throw (ValidationException) targetException;
253 throw new RuntimeException(e);
255 } catch (JMException e) {
256 throw new RuntimeException(e);
261 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
262 if (ObjectNameUtil.getTransactionName(on) == null) {
263 throw new IllegalArgumentException("Not in transaction instance "
264 + 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");
283 return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
284 } catch (JMException e) {
285 throw new IllegalStateException("Unable to get attribute "
286 + attrName + " for " + on, e);
291 public Set<String> getAvailableModuleFactoryQNames() {
292 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();