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

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