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 // if on is without transaction, add it. Reason is that when using getters on MXBeans the transaction name is stripped
47 on = ObjectNameUtil.withTransactionName(on, getTransactionName());
48 // if this is service reference and user requests for implementation, look it up
49 on = ConfigRegistryJMXClient.translateServiceRefIfPossible(on, clazz, configMBeanServer);
50 on = ObjectNameUtil.withTransactionName(on, getTransactionName());
51 return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
55 * Usage of this method indicates error as config JMX uses solely MXBeans.
56 * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
57 * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
58 * This method will be removed soon.
61 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
62 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
66 public CommitStatus commit() throws ConflictingVersionException,
68 return configRegistryMXBeanProxy
69 .commitConfig(configTransactionControllerON);
73 public void assertVersion(int expectedParentVersion,
74 int expectedCurrentVersion) {
75 if (expectedParentVersion != getParentVersion()) {
76 throw new IllegalStateException();
78 if (expectedCurrentVersion != getVersion()) {
79 throw new IllegalStateException();
83 // proxy around ConfigManagerMXBean
85 public ObjectName createModule(String moduleName, String instanceName)
86 throws InstanceAlreadyExistsException {
87 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
91 public void destroyModule(ObjectName objectName)
92 throws InstanceNotFoundException {
93 configTransactionControllerMXBeanProxy.destroyModule(objectName);
101 public void destroyConfigBean(String moduleName, String instanceName)
102 throws InstanceNotFoundException {
103 destroyModule(ObjectNameUtil.createTransactionModuleON(
104 getTransactionName(), moduleName, instanceName));
108 public void destroyModule(String moduleName, String instanceName)
109 throws InstanceNotFoundException {
110 destroyModule(ObjectNameUtil.createTransactionModuleON(
111 getTransactionName(), moduleName, instanceName));
115 public void abortConfig() {
116 configTransactionControllerMXBeanProxy.abortConfig();
120 public void validateConfig() throws ValidationException {
121 configTransactionControllerMXBeanProxy.validateConfig();
125 public long getParentVersion() {
127 return (Long) configMBeanServer.getAttribute(
128 configTransactionControllerON, "ParentVersion");
129 } catch (JMException e) {
130 throw new RuntimeException(e);
135 public long getVersion() {
137 return (Long) configMBeanServer.getAttribute(
138 configTransactionControllerON, "Version");
139 } catch (JMException e) {
140 throw new RuntimeException(e);
145 public String getTransactionName() {
146 return configTransactionControllerMXBeanProxy.getTransactionName();
150 public Set<String> getAvailableModuleNames() {
151 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
155 public ObjectName getObjectName() {
156 return configTransactionControllerON;
160 public Set<ObjectName> lookupConfigBeans() {
161 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
165 public Set<ObjectName> lookupConfigBeans(String moduleName) {
166 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
170 public ObjectName lookupConfigBean(String moduleName, String instanceName)
171 throws InstanceNotFoundException {
172 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
176 public Set<ObjectName> lookupConfigBeans(String moduleName,
177 String instanceName) {
178 return configTransactionControllerMXBeanProxy
179 .lookupConfigBeans(moduleName, instanceName);
183 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
184 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
188 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
189 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
193 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
194 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
198 public void removeAllServiceReferences() {
199 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
203 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
204 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
208 public Map<String, Map<String, ObjectName>> getServiceMapping() {
209 return configTransactionControllerMXBeanProxy.getServiceMapping();
213 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
214 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
218 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
219 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
223 public String getServiceInterfaceName(String namespace, String localName) {
224 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
228 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
229 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
233 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
234 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
238 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
239 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
243 public void validateBean(ObjectName configBeanON)
244 throws ValidationException {
246 configMBeanServer.invoke(configBeanON, "validate", null, null);
247 } catch (MBeanException e) {
248 Exception targetException = e.getTargetException();
249 if (targetException instanceof ValidationException){
250 throw (ValidationException) targetException;
252 throw new RuntimeException(e);
254 } catch (JMException e) {
255 throw new RuntimeException(e);
260 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
261 if (ObjectNameUtil.getTransactionName(on) == null)
262 throw new IllegalArgumentException("Not in transaction instance "
263 + on + ", no transaction name present");
266 configMBeanServer.setAttribute(on, attribute);
267 } catch (JMException e) {
268 throw new IllegalStateException("Unable to set attribute "
269 + attrName + " for " + on, e);
274 public Set<String> getAvailableModuleFactoryQNames() {
275 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();