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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.