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;
10 import org.opendaylight.controller.config.api.ConflictingVersionException;
11 import org.opendaylight.controller.config.api.ValidationException;
12 import org.opendaylight.controller.config.api.jmx.CommitStatus;
13 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
14 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import javax.management.Attribute;
18 import javax.management.InstanceAlreadyExistsException;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.JMException;
21 import javax.management.JMX;
22 import javax.management.MBeanException;
23 import javax.management.MBeanServer;
24 import javax.management.ObjectName;
28 public class ConfigTransactionJMXClient implements ConfigTransactionClient {
29 private final ConfigRegistryMXBean configRegistryMXBeanProxy;
30 private final ObjectName configTransactionControllerON;
31 private final ConfigTransactionControllerMXBean configTransactionControllerMXBeanProxy;
32 private final MBeanServer configMBeanServer;
34 public ConfigTransactionJMXClient(
35 ConfigRegistryMXBean configRegistryMXBeanProxy,
36 ObjectName configTransactionControllerON,
37 MBeanServer configMBeanServer) {
38 this.configMBeanServer = configMBeanServer;
39 this.configRegistryMXBeanProxy = configRegistryMXBeanProxy;
40 this.configTransactionControllerON = configTransactionControllerON;
41 this.configTransactionControllerMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer,
42 configTransactionControllerON,
43 ConfigTransactionControllerMXBean.class);
46 public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
47 return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
50 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
51 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
55 public CommitStatus commit() throws ConflictingVersionException,
57 return configRegistryMXBeanProxy
58 .commitConfig(configTransactionControllerON);
62 public void assertVersion(int expectedParentVersion,
63 int expectedCurrentVersion) {
64 if (expectedParentVersion != getParentVersion()) {
65 throw new IllegalStateException();
67 if (expectedCurrentVersion != getVersion()) {
68 throw new IllegalStateException();
72 // proxy around ConfigManagerMXBean
74 public ObjectName createModule(String moduleName, String instanceName)
75 throws InstanceAlreadyExistsException {
76 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
80 public void destroyModule(ObjectName objectName)
81 throws InstanceNotFoundException {
82 configTransactionControllerMXBeanProxy.destroyModule(objectName);
90 public void destroyConfigBean(String moduleName, String instanceName)
91 throws InstanceNotFoundException {
92 destroyModule(ObjectNameUtil.createTransactionModuleON(
93 getTransactionName(), moduleName, instanceName));
97 public void destroyModule(String moduleName, String instanceName)
98 throws InstanceNotFoundException {
99 destroyModule(ObjectNameUtil.createTransactionModuleON(
100 getTransactionName(), moduleName, instanceName));
104 public void abortConfig() {
105 configTransactionControllerMXBeanProxy.abortConfig();
109 public void validateConfig() throws ValidationException {
110 configTransactionControllerMXBeanProxy.validateConfig();
114 public long getParentVersion() {
116 return (Long) configMBeanServer.getAttribute(
117 configTransactionControllerON, "ParentVersion");
118 } catch (JMException e) {
119 throw new RuntimeException(e);
124 public long getVersion() {
126 return (Long) configMBeanServer.getAttribute(
127 configTransactionControllerON, "Version");
128 } catch (JMException e) {
129 throw new RuntimeException(e);
134 public String getTransactionName() {
135 return configTransactionControllerMXBeanProxy.getTransactionName();
139 public Set<String> getAvailableModuleNames() {
140 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
144 public ObjectName getObjectName() {
145 return configTransactionControllerON;
149 public Set<ObjectName> lookupConfigBeans() {
150 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
154 public Set<ObjectName> lookupConfigBeans(String moduleName) {
155 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
159 public ObjectName lookupConfigBean(String moduleName, String instanceName)
160 throws InstanceNotFoundException {
161 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
165 public Set<ObjectName> lookupConfigBeans(String moduleName,
166 String instanceName) {
167 return configTransactionControllerMXBeanProxy
168 .lookupConfigBeans(moduleName, instanceName);
172 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
173 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
177 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
178 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
182 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
183 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
187 public void removeAllServiceReferences() {
188 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
192 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
193 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
197 public Map<String, Map<String, ObjectName>> getServiceMapping() {
198 return configTransactionControllerMXBeanProxy.getServiceMapping();
202 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
203 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
207 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
208 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
212 public String getServiceInterfaceName(String namespace, String localName) {
213 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
217 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
218 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
222 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
223 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
227 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
228 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
232 public void validateBean(ObjectName configBeanON)
233 throws ValidationException {
235 configMBeanServer.invoke(configBeanON, "validate", null, null);
236 } catch (MBeanException e) {
237 Exception targetException = e.getTargetException();
238 if (targetException instanceof ValidationException){
239 throw (ValidationException) targetException;
241 throw new RuntimeException(e);
243 } catch (JMException e) {
244 throw new RuntimeException(e);
249 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
250 if (ObjectNameUtil.getTransactionName(on) == null)
251 throw new IllegalArgumentException("Not in transaction instance "
252 + on + ", no transaction name present");
255 configMBeanServer.setAttribute(on, attribute);
256 } catch (JMException e) {
257 throw new IllegalStateException("Unable to set attribute "
258 + attrName + " for " + on, e);
263 public Set<String> getAvailableModuleFactoryQNames() {
264 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();