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.MBeanServer;
19 import javax.management.ObjectName;
20 import javax.management.RuntimeMBeanException;
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 return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
51 public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
52 return JMX.newMBeanProxy(configMBeanServer, on, clazz);
56 public CommitStatus commit() throws ConflictingVersionException,
58 return configRegistryMXBeanProxy
59 .commitConfig(configTransactionControllerON);
63 public void assertVersion(int expectedParentVersion,
64 int expectedCurrentVersion) {
65 if (expectedParentVersion != getParentVersion()) {
66 throw new IllegalStateException();
68 if (expectedCurrentVersion != getVersion()) {
69 throw new IllegalStateException();
73 // proxy around ConfigManagerMXBean
75 public ObjectName createModule(String moduleName, String instanceName)
76 throws InstanceAlreadyExistsException {
77 return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
81 public void destroyModule(ObjectName objectName)
82 throws InstanceNotFoundException {
83 configTransactionControllerMXBeanProxy.destroyModule(objectName);
91 public void destroyConfigBean(String moduleName, String instanceName)
92 throws InstanceNotFoundException {
93 destroyModule(ObjectNameUtil.createTransactionModuleON(
94 getTransactionName(), moduleName, instanceName));
98 public void destroyModule(String moduleName, String instanceName)
99 throws InstanceNotFoundException {
100 destroyModule(ObjectNameUtil.createTransactionModuleON(
101 getTransactionName(), moduleName, instanceName));
105 public void abortConfig() {
106 configTransactionControllerMXBeanProxy.abortConfig();
110 public void validateConfig() throws ValidationException {
111 configTransactionControllerMXBeanProxy.validateConfig();
115 public long getParentVersion() {
117 return (Long) configMBeanServer.getAttribute(
118 configTransactionControllerON, "ParentVersion");
119 } catch (JMException e) {
120 throw new RuntimeException(e);
125 public long getVersion() {
127 return (Long) configMBeanServer.getAttribute(
128 configTransactionControllerON, "Version");
129 } catch (JMException e) {
130 throw new RuntimeException(e);
135 public String getTransactionName() {
136 return configTransactionControllerMXBeanProxy.getTransactionName();
140 public Set<String> getAvailableModuleNames() {
141 return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
145 public ObjectName getObjectName() {
146 return configTransactionControllerON;
150 public Set<ObjectName> lookupConfigBeans() {
151 return configTransactionControllerMXBeanProxy.lookupConfigBeans();
155 public Set<ObjectName> lookupConfigBeans(String moduleName) {
156 return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
160 public ObjectName lookupConfigBean(String moduleName, String instanceName)
161 throws InstanceNotFoundException {
162 return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
166 public Set<ObjectName> lookupConfigBeans(String moduleName,
167 String instanceName) {
168 return configTransactionControllerMXBeanProxy
169 .lookupConfigBeans(moduleName, instanceName);
173 public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
174 configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
178 public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
179 return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
183 public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
184 configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
188 public void removeAllServiceReferences() {
189 configTransactionControllerMXBeanProxy.removeAllServiceReferences();
193 public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
194 return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
198 public Map<String, Map<String, ObjectName>> getServiceMapping() {
199 return configTransactionControllerMXBeanProxy.getServiceMapping();
203 public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
204 return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
208 public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
209 return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
213 public String getServiceInterfaceName(String namespace, String localName) {
214 return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
218 public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
219 return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
223 public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
224 return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
228 public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
229 configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
233 public void validateBean(ObjectName configBeanON)
234 throws ValidationException {
236 configMBeanServer.invoke(configBeanON, "validate", null, null);
237 } catch (JMException e) {
238 throw new RuntimeException(e);
239 } catch (RuntimeMBeanException e) {
240 throw e.getTargetException();
245 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
246 if (ObjectNameUtil.getTransactionName(on) == null)
247 throw new IllegalArgumentException("Not in transaction instance "
248 + on + ", no transaction name present");
251 configMBeanServer.setAttribute(on, attribute);
252 } catch (JMException e) {
253 throw new IllegalStateException("Unable to set attribute "
254 + attrName + " for " + on, e);
259 public Set<String> getAvailableModuleFactoryQNames() {
260 return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();