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