Add config system API to recreate a module instance
[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 reCreateModule(ObjectName objectName) throws InstanceNotFoundException {
93         configTransactionControllerMXBeanProxy.reCreateModule(objectName);
94     }
95
96     @Override
97     public void destroyModule(ObjectName objectName)
98             throws InstanceNotFoundException {
99         configTransactionControllerMXBeanProxy.destroyModule(objectName);
100     }
101
102     @Override
103     @Deprecated
104     /**
105      * {@inheritDoc}
106      */
107     public void destroyConfigBean(String moduleName, String instanceName)
108             throws InstanceNotFoundException {
109         destroyModule(ObjectNameUtil.createTransactionModuleON(
110                 getTransactionName(), moduleName, instanceName));
111     }
112
113     @Override
114     public void destroyModule(String moduleName, String instanceName)
115             throws InstanceNotFoundException {
116         destroyModule(ObjectNameUtil.createTransactionModuleON(
117                 getTransactionName(), moduleName, instanceName));
118     }
119
120     @Override
121     public void abortConfig() {
122         configTransactionControllerMXBeanProxy.abortConfig();
123     }
124
125     @Override
126     public void validateConfig() throws ValidationException {
127         configTransactionControllerMXBeanProxy.validateConfig();
128     }
129
130     @Override
131     public long getParentVersion() {
132         try {
133             return (Long) configMBeanServer.getAttribute(
134                     configTransactionControllerON, "ParentVersion");
135         } catch (JMException e) {
136             throw new RuntimeException(e);
137         }
138     }
139
140     @Override
141     public long getVersion() {
142         try {
143             return (Long) configMBeanServer.getAttribute(
144                     configTransactionControllerON, "Version");
145         } catch (JMException e) {
146             throw new RuntimeException(e);
147         }
148     }
149
150     @Override
151     public String getTransactionName() {
152         return configTransactionControllerMXBeanProxy.getTransactionName();
153     }
154
155     @Override
156     public Set<String> getAvailableModuleNames() {
157         return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
158     }
159
160     @Override
161     public ObjectName getObjectName() {
162         return configTransactionControllerON;
163     }
164
165     @Override
166     public Set<ObjectName> lookupConfigBeans() {
167         return configTransactionControllerMXBeanProxy.lookupConfigBeans();
168     }
169
170     @Override
171     public Set<ObjectName> lookupConfigBeans(String moduleName) {
172         return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
173     }
174
175     @Override
176     public ObjectName lookupConfigBean(String moduleName, String instanceName)
177             throws InstanceNotFoundException {
178         return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
179     }
180
181     @Override
182     public Set<ObjectName> lookupConfigBeans(String moduleName,
183             String instanceName) {
184         return configTransactionControllerMXBeanProxy
185                 .lookupConfigBeans(moduleName, instanceName);
186     }
187
188     @Override
189     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
190         configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
191     }
192
193     @Override
194     public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
195         return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
196     }
197
198     @Override
199     public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
200         configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
201     }
202
203     @Override
204     public void removeAllServiceReferences() {
205         configTransactionControllerMXBeanProxy.removeAllServiceReferences();
206     }
207
208     @Override
209     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
210         return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
211     }
212
213     @Override
214     public Map<String, Map<String, ObjectName>> getServiceMapping() {
215         return configTransactionControllerMXBeanProxy.getServiceMapping();
216     }
217
218     @Override
219     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
220         return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
221     }
222
223     @Override
224     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
225         return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
226     }
227
228     @Override
229     public String getServiceInterfaceName(String namespace, String localName) {
230         return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
231     }
232
233     @Override
234     public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
235         return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
236     }
237
238     @Override
239     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
240         return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
241     }
242
243     @Override
244     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
245         configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
246     }
247
248     @Override
249     public Attribute getAttribute(ObjectName on, String attrName) {
250         if (ObjectNameUtil.getTransactionName(on) == null) {
251             throw new IllegalArgumentException("Not in transaction instance "
252                     + on + ", no transaction name present");
253         }
254
255         try {
256             return new Attribute(attrName, configMBeanServer.getAttribute(on,attrName));
257         } catch (JMException e) {
258             throw new IllegalStateException("Unable to get attribute "
259                     + attrName + " for " + on, e);
260         }
261     }
262
263     @Override
264     public Object getAttributeCurrentValue(ObjectName on, String attrName) {
265         return getAttribute(on, attrName).getValue();
266     }
267
268     @Override
269     public void validateBean(ObjectName configBeanON)
270             throws ValidationException {
271         try {
272             configMBeanServer.invoke(configBeanON, "validate", null, null);
273         } catch (MBeanException e) {
274             Exception targetException = e.getTargetException();
275             if (targetException instanceof ValidationException){
276                 throw (ValidationException) targetException;
277             } else {
278                 throw new RuntimeException(e);
279             }
280         } catch (JMException e) {
281             throw new RuntimeException(e);
282         }
283     }
284
285     @Override
286     public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
287         if (ObjectNameUtil.getTransactionName(on) == null) {
288             throw new IllegalArgumentException("Not in transaction instance "
289                     + on + ", no transaction name present");
290         }
291
292         try {
293             configMBeanServer.setAttribute(on, attribute);
294         } catch (JMException e) {
295             throw new IllegalStateException("Unable to set attribute "
296                     + attrName + " for " + on, e);
297         }
298     }
299
300     @Override
301     public Set<String> getAvailableModuleFactoryQNames() {
302         return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();
303     }
304
305     @Override
306     public Set<ObjectName> lookupRuntimeBeans() {
307         return configTransactionControllerMXBeanProxy.lookupRuntimeBeans();
308     }
309
310     @Override
311     public Set<ObjectName> lookupRuntimeBeans(final String moduleName, final String instanceName) {
312         return configTransactionControllerMXBeanProxy.lookupRuntimeBeans(moduleName, instanceName);
313     }
314 }