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.MBeanServer;
18 import javax.management.ObjectName;
19 import javax.management.RuntimeMBeanException;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
25 import org.opendaylight.controller.config.api.jmx.ConfigTransactionControllerMXBean;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 public class ConfigTransactionJMXClient implements ConfigTransactionClient {
29 private final ConfigRegistryMXBean configTransactionManagerProxy;
30 private final ObjectName configTransactionControllerON;
31 private final ConfigTransactionControllerMXBean configControllerProxy;
32 private final MBeanServer configMBeanServer;
34 public ConfigTransactionJMXClient(
35 ConfigRegistryMXBean configTransactionManagerProxy,
36 ObjectName configTransactionControllerON,
37 MBeanServer configMBeanServer) {
38 this.configMBeanServer = configMBeanServer;
39 this.configTransactionManagerProxy = configTransactionManagerProxy;
40 this.configTransactionControllerON = configTransactionControllerON;
41 this.configControllerProxy = 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 configTransactionManagerProxy
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 configControllerProxy.createModule(moduleName, instanceName);
80 public void destroyModule(ObjectName objectName)
81 throws InstanceNotFoundException {
82 configControllerProxy.destroyModule(objectName);
86 public void destroyConfigBean(String moduleName, String instanceName)
87 throws InstanceNotFoundException {
88 destroyModule(ObjectNameUtil.createTransactionModuleON(
89 getTransactionName(), moduleName, instanceName));
93 public void abortConfig() {
94 configControllerProxy.abortConfig();
98 public void validateConfig() throws ValidationException {
99 configControllerProxy.validateConfig();
103 public long getParentVersion() {
105 return (Long) configMBeanServer.getAttribute(
106 configTransactionControllerON, "ParentVersion");
107 } catch (JMException e) {
108 throw new RuntimeException(e);
113 public long getVersion() {
115 return (Long) configMBeanServer.getAttribute(
116 configTransactionControllerON, "Version");
117 } catch (JMException e) {
118 throw new RuntimeException(e);
123 public String getTransactionName() {
124 return configControllerProxy.getTransactionName();
128 public Set<String> getAvailableModuleNames() {
129 return configControllerProxy.getAvailableModuleNames();
133 public ObjectName getObjectName() {
134 return configTransactionControllerON;
138 public Set<ObjectName> lookupConfigBeans() {
139 return configControllerProxy.lookupConfigBeans();
143 public Set<ObjectName> lookupConfigBeans(String moduleName) {
144 return configControllerProxy.lookupConfigBeans(moduleName);
148 public ObjectName lookupConfigBean(String moduleName, String instanceName)
149 throws InstanceNotFoundException {
150 return configControllerProxy.lookupConfigBean(moduleName, instanceName);
154 public Set<ObjectName> lookupConfigBeans(String moduleName,
155 String instanceName) {
156 return configControllerProxy
157 .lookupConfigBeans(moduleName, instanceName);
161 public void validateBean(ObjectName configBeanON)
162 throws ValidationException {
164 configMBeanServer.invoke(configBeanON, "validate", null, null);
165 } catch (JMException e) {
166 throw new RuntimeException(e);
167 } catch (RuntimeMBeanException e) {
168 throw e.getTargetException();
173 public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
174 if (ObjectNameUtil.getTransactionName(on) == null)
175 throw new IllegalArgumentException("Not in transaction instance "
176 + on + ", no transaction name present");
179 configMBeanServer.setAttribute(on, attribute);
180 } catch (JMException e) {
181 throw new IllegalStateException("Unable to set attribute "
182 + attrName + " for " + on, e);