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

©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.