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