26ca1391ad4bf32eea9708e2db47f21cc33bdcfa
[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.Map;
11 import java.util.Set;
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;
26
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;
32
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);
43     }
44
45     public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
46         ObjectName onName = on;
47         // if on is without transaction, add it. Reason is that when using getters on MXBeans the transaction name is stripped
48         onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
49         // if this is service reference and user requests for implementation, look it up
50         onName = ConfigRegistryJMXClient.translateServiceRefIfPossible(onName, clazz, configMBeanServer);
51         onName = ObjectNameUtil.withTransactionName(onName, getTransactionName());
52         return JMX.newMXBeanProxy(configMBeanServer, onName, clazz);
53     }
54
55     /**
56      * Usage of this method indicates error as config JMX uses solely MXBeans.
57      * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
58      * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
59      * This method will be removed soon.
60      */
61     @Deprecated
62     public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
63         return JMX.newMBeanProxy(configMBeanServer, on, clazz);
64     }
65
66     @Override
67     public CommitStatus commit() throws ConflictingVersionException,
68             ValidationException {
69         return configRegistryMXBeanProxy
70                 .commitConfig(configTransactionControllerON);
71     }
72
73     @Override
74     public void assertVersion(int expectedParentVersion,
75             int expectedCurrentVersion) {
76         if (expectedParentVersion != getParentVersion()) {
77             throw new IllegalStateException();
78         }
79         if (expectedCurrentVersion != getVersion()) {
80             throw new IllegalStateException();
81         }
82     }
83
84     // proxy around ConfigManagerMXBean
85     @Override
86     public ObjectName createModule(String moduleName, String instanceName)
87             throws InstanceAlreadyExistsException {
88         return configTransactionControllerMXBeanProxy.createModule(moduleName, instanceName);
89     }
90
91     @Override
92     public void destroyModule(ObjectName objectName)
93             throws InstanceNotFoundException {
94         configTransactionControllerMXBeanProxy.destroyModule(objectName);
95     }
96
97     @Override
98     @Deprecated
99     /**
100      * {@inheritDoc}
101      */
102     public void destroyConfigBean(String moduleName, String instanceName)
103             throws InstanceNotFoundException {
104         destroyModule(ObjectNameUtil.createTransactionModuleON(
105                 getTransactionName(), moduleName, instanceName));
106     }
107
108     @Override
109     public void destroyModule(String moduleName, String instanceName)
110             throws InstanceNotFoundException {
111         destroyModule(ObjectNameUtil.createTransactionModuleON(
112                 getTransactionName(), moduleName, instanceName));
113     }
114
115     @Override
116     public void abortConfig() {
117         configTransactionControllerMXBeanProxy.abortConfig();
118     }
119
120     @Override
121     public void validateConfig() throws ValidationException {
122         configTransactionControllerMXBeanProxy.validateConfig();
123     }
124
125     @Override
126     public long getParentVersion() {
127         try {
128             return (Long) configMBeanServer.getAttribute(
129                     configTransactionControllerON, "ParentVersion");
130         } catch (JMException e) {
131             throw new RuntimeException(e);
132         }
133     }
134
135     @Override
136     public long getVersion() {
137         try {
138             return (Long) configMBeanServer.getAttribute(
139                     configTransactionControllerON, "Version");
140         } catch (JMException e) {
141             throw new RuntimeException(e);
142         }
143     }
144
145     @Override
146     public String getTransactionName() {
147         return configTransactionControllerMXBeanProxy.getTransactionName();
148     }
149
150     @Override
151     public Set<String> getAvailableModuleNames() {
152         return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
153     }
154
155     @Override
156     public ObjectName getObjectName() {
157         return configTransactionControllerON;
158     }
159
160     @Override
161     public Set<ObjectName> lookupConfigBeans() {
162         return configTransactionControllerMXBeanProxy.lookupConfigBeans();
163     }
164
165     @Override
166     public Set<ObjectName> lookupConfigBeans(String moduleName) {
167         return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
168     }
169
170     @Override
171     public ObjectName lookupConfigBean(String moduleName, String instanceName)
172             throws InstanceNotFoundException {
173         return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
174     }
175
176     @Override
177     public Set<ObjectName> lookupConfigBeans(String moduleName,
178             String instanceName) {
179         return configTransactionControllerMXBeanProxy
180                 .lookupConfigBeans(moduleName, instanceName);
181     }
182
183     @Override
184     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
185         configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
186     }
187
188     @Override
189     public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
190         return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
191     }
192
193     @Override
194     public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
195         configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
196     }
197
198     @Override
199     public void removeAllServiceReferences() {
200         configTransactionControllerMXBeanProxy.removeAllServiceReferences();
201     }
202
203     @Override
204     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
205         return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
206     }
207
208     @Override
209     public Map<String, Map<String, ObjectName>> getServiceMapping() {
210         return configTransactionControllerMXBeanProxy.getServiceMapping();
211     }
212
213     @Override
214     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
215         return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
216     }
217
218     @Override
219     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
220         return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
221     }
222
223     @Override
224     public String getServiceInterfaceName(String namespace, String localName) {
225         return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
226     }
227
228     @Override
229     public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
230         return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
231     }
232
233     @Override
234     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
235         return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
236     }
237
238     @Override
239     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
240         configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
241     }
242
243     @Override
244     public Attribute getAttribute(ObjectName on, String attrName) {
245         if (ObjectNameUtil.getTransactionName(on) == null) {
246             throw new IllegalArgumentException("Not in transaction instance "
247                     + on + ", no transaction name present");
248         }
249
250         try {
251             return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
252         } catch (JMException e) {
253             throw new IllegalStateException("Unable to get attribute "
254                     + attrName + " for " + on, e);
255         }
256     }
257
258     @Override
259     public Object getAttributeCurrentValue(ObjectName on, String attrName) {
260         return getAttribute(on, attrName).getValue();
261     }
262
263     @Override
264     public void validateBean(ObjectName configBeanON)
265             throws ValidationException {
266         try {
267             configMBeanServer.invoke(configBeanON, "validate", null, null);
268         } catch (MBeanException e) {
269             Exception targetException = e.getTargetException();
270             if (targetException instanceof ValidationException){
271                 throw (ValidationException) targetException;
272             } else {
273                 throw new RuntimeException(e);
274             }
275         } catch (JMException e) {
276             throw new RuntimeException(e);
277         }
278     }
279
280     @Override
281     public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
282         if (ObjectNameUtil.getTransactionName(on) == null) {
283             throw new IllegalArgumentException("Not in transaction instance "
284                     + on + ", no transaction name present");
285         }
286
287         try {
288             configMBeanServer.setAttribute(on, attribute);
289         } catch (JMException e) {
290             throw new IllegalStateException("Unable to set attribute "
291                     + attrName + " for " + on, e);
292         }
293     }
294
295     @Override
296     public Set<String> getAvailableModuleFactoryQNames() {
297         return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();
298     }
299
300     @Override
301     public Set<ObjectName> lookupRuntimeBeans() {
302         return configTransactionControllerMXBeanProxy.lookupRuntimeBeans();
303     }
304
305     @Override
306     public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
307         return configTransactionControllerMXBeanProxy.lookupRuntimeBeans(moduleName, instanceName);
308     }
309 }