e683d915baea604db86b891e81dc9192b203c4aa
[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     public void destroyConfigBean(String moduleName, String instanceName)
88             throws InstanceNotFoundException {
89         destroyModule(ObjectNameUtil.createTransactionModuleON(
90                 getTransactionName(), moduleName, instanceName));
91     }
92
93     @Override
94     public void abortConfig() {
95         configTransactionControllerMXBeanProxy.abortConfig();
96     }
97
98     @Override
99     public void validateConfig() throws ValidationException {
100         configTransactionControllerMXBeanProxy.validateConfig();
101     }
102
103     @Override
104     public long getParentVersion() {
105         try {
106             return (Long) configMBeanServer.getAttribute(
107                     configTransactionControllerON, "ParentVersion");
108         } catch (JMException e) {
109             throw new RuntimeException(e);
110         }
111     }
112
113     @Override
114     public long getVersion() {
115         try {
116             return (Long) configMBeanServer.getAttribute(
117                     configTransactionControllerON, "Version");
118         } catch (JMException e) {
119             throw new RuntimeException(e);
120         }
121     }
122
123     @Override
124     public String getTransactionName() {
125         return configTransactionControllerMXBeanProxy.getTransactionName();
126     }
127
128     @Override
129     public Set<String> getAvailableModuleNames() {
130         return configTransactionControllerMXBeanProxy.getAvailableModuleNames();
131     }
132
133     @Override
134     public ObjectName getObjectName() {
135         return configTransactionControllerON;
136     }
137
138     @Override
139     public Set<ObjectName> lookupConfigBeans() {
140         return configTransactionControllerMXBeanProxy.lookupConfigBeans();
141     }
142
143     @Override
144     public Set<ObjectName> lookupConfigBeans(String moduleName) {
145         return configTransactionControllerMXBeanProxy.lookupConfigBeans(moduleName);
146     }
147
148     @Override
149     public ObjectName lookupConfigBean(String moduleName, String instanceName)
150             throws InstanceNotFoundException {
151         return configTransactionControllerMXBeanProxy.lookupConfigBean(moduleName, instanceName);
152     }
153
154     @Override
155     public Set<ObjectName> lookupConfigBeans(String moduleName,
156             String instanceName) {
157         return configTransactionControllerMXBeanProxy
158                 .lookupConfigBeans(moduleName, instanceName);
159     }
160
161     @Override
162     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
163         configTransactionControllerMXBeanProxy.checkConfigBeanExists(objectName);
164     }
165
166     @Override
167     public ObjectName saveServiceReference(String serviceInterfaceName, String refName, ObjectName moduleON) throws InstanceNotFoundException {
168         return configTransactionControllerMXBeanProxy.saveServiceReference(serviceInterfaceName,refName, moduleON);
169     }
170
171     @Override
172     public void removeServiceReference(String serviceInterfaceName, String refName) throws InstanceNotFoundException{
173         configTransactionControllerMXBeanProxy.removeServiceReference(serviceInterfaceName, refName);
174     }
175
176     @Override
177     public void removeAllServiceReferences() {
178         configTransactionControllerMXBeanProxy.removeAllServiceReferences();
179     }
180
181     @Override
182     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
183         return configTransactionControllerMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
184     }
185
186     @Override
187     public Map<String, Map<String, ObjectName>> getServiceMapping() {
188         return configTransactionControllerMXBeanProxy.getServiceMapping();
189     }
190
191     @Override
192     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
193         return configTransactionControllerMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
194     }
195
196     @Override
197     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
198         return configTransactionControllerMXBeanProxy.lookupServiceInterfaceNames(objectName);
199     }
200
201     @Override
202     public String getServiceInterfaceName(String namespace, String localName) {
203         return configTransactionControllerMXBeanProxy.getServiceInterfaceName(namespace, localName);
204     }
205
206     @Override
207     public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
208         return configTransactionControllerMXBeanProxy.removeServiceReferences(objectName);
209     }
210
211     @Override
212     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
213         return configTransactionControllerMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
214     }
215
216     @Override
217     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
218         configTransactionControllerMXBeanProxy.checkServiceReferenceExists(objectName);
219     }
220
221     @Override
222     public void validateBean(ObjectName configBeanON)
223             throws ValidationException {
224         try {
225             configMBeanServer.invoke(configBeanON, "validate", null, null);
226         } catch (JMException e) {
227             throw new RuntimeException(e);
228         } catch (RuntimeMBeanException e) {
229             throw e.getTargetException();
230         }
231     }
232
233     @Override
234     public void setAttribute(ObjectName on, String attrName, Attribute attribute) {
235         if (ObjectNameUtil.getTransactionName(on) == null)
236             throw new IllegalArgumentException("Not in transaction instance "
237                     + on + ", no transaction name present");
238
239         try {
240             configMBeanServer.setAttribute(on, attribute);
241         } catch (JMException e) {
242             throw new IllegalStateException("Unable to set attribute "
243                     + attrName + " for " + on, e);
244         }
245     }
246
247     @Override
248     public Set<String> getAvailableModuleFactoryQNames() {
249         return configTransactionControllerMXBeanProxy.getAvailableModuleFactoryQNames();
250     }
251 }

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