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

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