Move ServiceReferenceMXBean to config-api
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / ConfigRegistryJMXClient.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.ObjectNameUtil;
15
16 import javax.management.AttributeNotFoundException;
17 import javax.management.InstanceNotFoundException;
18 import javax.management.JMException;
19 import javax.management.JMX;
20 import javax.management.MBeanException;
21 import javax.management.MBeanServer;
22 import javax.management.ObjectInstance;
23 import javax.management.ObjectName;
24 import javax.management.ReflectionException;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import org.opendaylight.controller.config.api.jmx.ServiceReferenceMXBean;
30
31 public class ConfigRegistryJMXClient implements ConfigRegistryClient {
32     private final ConfigRegistryMXBean configRegistryMXBeanProxy;
33     private final ObjectName configRegistryON;
34     private final MBeanServer configMBeanServer;
35
36     public ConfigRegistryJMXClient(MBeanServer configMBeanServer) {
37         this.configMBeanServer = configMBeanServer;
38         configRegistryON = OBJECT_NAME;
39         Set<ObjectInstance> searchResult = configMBeanServer.queryMBeans(
40                 configRegistryON, null);
41         if (!(searchResult.size() == 1)) {
42             throw new IllegalStateException("Config registry not found");
43         }
44         configRegistryMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer, configRegistryON, ConfigRegistryMXBean.class,
45                 false);
46     }
47
48     @Override
49     public ConfigTransactionJMXClient createTransaction() {
50         ObjectName configTransactionControllerON = beginConfig();
51         return getConfigTransactionClient(configTransactionControllerON);
52     }
53
54     @Override
55     public ConfigTransactionJMXClient getConfigTransactionClient(
56             String transactionName) {
57         ObjectName objectName = ObjectNameUtil
58                 .createTransactionControllerON(transactionName);
59         return getConfigTransactionClient(objectName);
60     }
61
62     @Override
63     public ConfigTransactionJMXClient getConfigTransactionClient(
64             ObjectName objectName) {
65         return new ConfigTransactionJMXClient(configRegistryMXBeanProxy, objectName,
66                 configMBeanServer);
67     }
68
69     /**
70      * Usage of this method indicates error as config JMX uses solely MXBeans.
71      * Use {@link #newMXBeanProxy(javax.management.ObjectName, Class)}
72      * or {@link JMX#newMBeanProxy(javax.management.MBeanServerConnection, javax.management.ObjectName, Class)}
73      * This method will be removed soon.
74      */
75     @Deprecated
76     public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
77         on = translateServiceRefIfPossible(on, clazz, configMBeanServer);
78         return JMX.newMBeanProxy(configMBeanServer, on, clazz);
79     }
80
81     static  ObjectName translateServiceRefIfPossible(ObjectName on, Class<?> clazz, MBeanServer configMBeanServer) {
82         if (ObjectNameUtil.isServiceReference(on) && clazz.equals(ServiceReferenceMXBean.class) == false) {
83             ServiceReferenceMXBean proxy = JMX.newMXBeanProxy(configMBeanServer, on, ServiceReferenceMXBean.class);
84             on = proxy.getCurrentImplementation();
85         }
86         return on;
87     }
88
89
90     public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
91         return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
92     }
93
94     @Override
95     public ObjectName beginConfig() {
96         return configRegistryMXBeanProxy.beginConfig();
97     }
98
99     @Override
100     public CommitStatus commitConfig(ObjectName transactionControllerON)
101             throws ConflictingVersionException, ValidationException {
102         return configRegistryMXBeanProxy.commitConfig(transactionControllerON);
103     }
104
105     @Override
106     public List<ObjectName> getOpenConfigs() {
107         return configRegistryMXBeanProxy.getOpenConfigs();
108     }
109
110     @Override
111     public long getVersion() {
112         try {
113             return (Long) configMBeanServer.getAttribute(configRegistryON,
114                     "Version");
115         } catch (JMException e) {
116             throw new RuntimeException(e);
117         }
118     }
119
120     @Override
121     public Set<String> getAvailableModuleNames() {
122         return configRegistryMXBeanProxy.getAvailableModuleNames();
123     }
124
125     @Override
126     public boolean isHealthy() {
127         return configRegistryMXBeanProxy.isHealthy();
128     }
129
130     @Override
131     public Set<ObjectName> lookupConfigBeans() {
132         return configRegistryMXBeanProxy.lookupConfigBeans();
133     }
134
135     @Override
136     public Set<ObjectName> lookupConfigBeans(String moduleName) {
137         return configRegistryMXBeanProxy.lookupConfigBeans(moduleName);
138     }
139
140     @Override
141     public Set<ObjectName> lookupConfigBeans(String moduleName,
142             String instanceName) {
143         return configRegistryMXBeanProxy.lookupConfigBeans(moduleName, instanceName);
144     }
145
146     @Override
147     public ObjectName lookupConfigBean(String moduleName, String instanceName)
148             throws InstanceNotFoundException {
149         return configRegistryMXBeanProxy.lookupConfigBean(moduleName, instanceName);
150     }
151
152     @Override
153     public Set<ObjectName> lookupRuntimeBeans() {
154         return configRegistryMXBeanProxy.lookupRuntimeBeans();
155     }
156
157     @Override
158     public Set<ObjectName> lookupRuntimeBeans(String ifcName,
159             String instanceName) {
160         return configRegistryMXBeanProxy.lookupRuntimeBeans(ifcName, instanceName);
161     }
162
163     @Override
164     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
165         configRegistryMXBeanProxy.checkConfigBeanExists(objectName);
166     }
167
168     @Override
169     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceQName, String refName) {
170         return configRegistryMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceQName, refName);
171     }
172
173     @Override
174     public Map<String, Map<String, ObjectName>> getServiceMapping() {
175         return configRegistryMXBeanProxy.getServiceMapping();
176     }
177
178     @Override
179     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceQName) {
180         return configRegistryMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceQName);
181     }
182
183     @Override
184     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
185         return configRegistryMXBeanProxy.lookupServiceInterfaceNames(objectName);
186     }
187
188     @Override
189     public String getServiceInterfaceName(String namespace, String localName) {
190         return configRegistryMXBeanProxy.getServiceInterfaceName(namespace, localName);
191     }
192
193     @Override
194     public Object invokeMethod(ObjectName on, String name, Object[] params,
195             String[] signature) {
196         try {
197             return configMBeanServer.invoke(on, name, params, signature);
198         } catch (InstanceNotFoundException | ReflectionException
199                 | MBeanException e) {
200             throw new RuntimeException("Unable to invoke operation " + name
201                     + " on " + on + " with attributes "
202                     + Arrays.toString(params) + " and signature "
203                     + Arrays.toString(signature), e);
204         }
205     }
206
207     @Override
208     public Object getAttributeCurrentValue(ObjectName on, String attributeName) {
209         try {
210             return configMBeanServer.getAttribute(on, attributeName);
211         } catch (AttributeNotFoundException | InstanceNotFoundException
212                 | MBeanException | ReflectionException e) {
213             throw new RuntimeException("Unable to get attribute "
214                     + attributeName + " for " + on, e);
215         }
216     }
217
218     @Override
219     public Set<String> getAvailableModuleFactoryQNames() {
220         return configRegistryMXBeanProxy.getAvailableModuleFactoryQNames();
221     }
222
223     @Override
224     public ObjectName getServiceReference(String serviceInterfaceQName, String refName) throws InstanceNotFoundException {
225         return configRegistryMXBeanProxy.getServiceReference(serviceInterfaceQName, refName);
226     }
227
228     @Override
229     public void checkServiceReferenceExists(ObjectName objectName) throws InstanceNotFoundException {
230         configRegistryMXBeanProxy.checkServiceReferenceExists(objectName);
231     }
232 }

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