Bug 116 - Revisit SanityTest
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / ConfigTransactionJMXClient.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.util;
9
10 import java.util.Set;
11
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;
20
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;
27
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;
33
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);
44     }
45
46     public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
47         return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
48     }
49
50     public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
51         return JMX.newMBeanProxy(configMBeanServer, on, clazz);
52     }
53
54     @Override
55     public CommitStatus commit() throws ConflictingVersionException,
56             ValidationException {
57         return configTransactionManagerProxy
58                 .commitConfig(configTransactionControllerON);
59     }
60
61     @Override
62     public void assertVersion(int expectedParentVersion,
63             int expectedCurrentVersion) {
64         if (expectedParentVersion != getParentVersion()) {
65             throw new IllegalStateException();
66         }
67         if (expectedCurrentVersion != getVersion()) {
68             throw new IllegalStateException();
69         }
70     }
71
72     // proxy around ConfigManagerMXBean
73     @Override
74     public ObjectName createModule(String moduleName, String instanceName)
75             throws InstanceAlreadyExistsException {
76         return configControllerProxy.createModule(moduleName, instanceName);
77     }
78
79     @Override
80     public void destroyModule(ObjectName objectName)
81             throws InstanceNotFoundException {
82         configControllerProxy.destroyModule(objectName);
83     }
84
85     @Override
86     public void destroyConfigBean(String moduleName, String instanceName)
87             throws InstanceNotFoundException {
88         destroyModule(ObjectNameUtil.createTransactionModuleON(
89                 getTransactionName(), moduleName, instanceName));
90     }
91
92     @Override
93     public void abortConfig() {
94         configControllerProxy.abortConfig();
95     }
96
97     @Override
98     public void validateConfig() throws ValidationException {
99         configControllerProxy.validateConfig();
100     }
101
102     @Override
103     public long getParentVersion() {
104         try {
105             return (Long) configMBeanServer.getAttribute(
106                     configTransactionControllerON, "ParentVersion");
107         } catch (JMException e) {
108             throw new RuntimeException(e);
109         }
110     }
111
112     @Override
113     public long getVersion() {
114         try {
115             return (Long) configMBeanServer.getAttribute(
116                     configTransactionControllerON, "Version");
117         } catch (JMException e) {
118             throw new RuntimeException(e);
119         }
120     }
121
122     @Override
123     public String getTransactionName() {
124         return configControllerProxy.getTransactionName();
125     }
126
127     @Override
128     public Set<String> getAvailableModuleNames() {
129         return configControllerProxy.getAvailableModuleNames();
130     }
131
132     @Override
133     public ObjectName getObjectName() {
134         return configTransactionControllerON;
135     }
136
137     @Override
138     public Set<ObjectName> lookupConfigBeans() {
139         return configControllerProxy.lookupConfigBeans();
140     }
141
142     @Override
143     public Set<ObjectName> lookupConfigBeans(String moduleName) {
144         return configControllerProxy.lookupConfigBeans(moduleName);
145     }
146
147     @Override
148     public ObjectName lookupConfigBean(String moduleName, String instanceName)
149             throws InstanceNotFoundException {
150         return configControllerProxy.lookupConfigBean(moduleName, instanceName);
151     }
152
153     @Override
154     public Set<ObjectName> lookupConfigBeans(String moduleName,
155             String instanceName) {
156         return configControllerProxy
157                 .lookupConfigBeans(moduleName, instanceName);
158     }
159
160     @Override
161     public void validateBean(ObjectName configBeanON)
162             throws ValidationException {
163         try {
164             configMBeanServer.invoke(configBeanON, "validate", null, null);
165         } catch (JMException e) {
166             throw new RuntimeException(e);
167         } catch (RuntimeMBeanException e) {
168             throw e.getTargetException();
169         }
170     }
171
172     @Override
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");
177
178         try {
179             configMBeanServer.setAttribute(on, attribute);
180         } catch (JMException e) {
181             throw new IllegalStateException("Unable to set attribute "
182                     + attrName + " for " + on, e);
183         }
184     }
185 }