Fixed netconf monitoring.
[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 java.util.Arrays;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.management.AttributeNotFoundException;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.JMException;
18 import javax.management.JMX;
19 import javax.management.MBeanException;
20 import javax.management.MBeanServer;
21 import javax.management.ObjectInstance;
22 import javax.management.ObjectName;
23 import javax.management.ReflectionException;
24
25 import org.opendaylight.controller.config.api.ConflictingVersionException;
26 import org.opendaylight.controller.config.api.ValidationException;
27 import org.opendaylight.controller.config.api.jmx.CommitStatus;
28 import org.opendaylight.controller.config.api.jmx.ConfigRegistryMXBean;
29 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
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     public <T> T newMBeanProxy(ObjectName on, Class<T> clazz) {
70         return JMX.newMBeanProxy(configMBeanServer, on, clazz);
71     }
72
73     public <T> T newMXBeanProxy(ObjectName on, Class<T> clazz) {
74         return JMX.newMXBeanProxy(configMBeanServer, on, clazz);
75     }
76
77     @Override
78     public ObjectName beginConfig() {
79         return configRegistryMXBeanProxy.beginConfig();
80     }
81
82     @Override
83     public CommitStatus commitConfig(ObjectName transactionControllerON)
84             throws ConflictingVersionException, ValidationException {
85         return configRegistryMXBeanProxy.commitConfig(transactionControllerON);
86     }
87
88     @Override
89     public List<ObjectName> getOpenConfigs() {
90         return configRegistryMXBeanProxy.getOpenConfigs();
91     }
92
93     @Override
94     public long getVersion() {
95         try {
96             return (Long) configMBeanServer.getAttribute(configRegistryON,
97                     "Version");
98         } catch (JMException e) {
99             throw new RuntimeException(e);
100         }
101     }
102
103     @Override
104     public Set<String> getAvailableModuleNames() {
105         return configRegistryMXBeanProxy.getAvailableModuleNames();
106     }
107
108     @Override
109     public boolean isHealthy() {
110         return configRegistryMXBeanProxy.isHealthy();
111     }
112
113     @Override
114     public Set<ObjectName> lookupConfigBeans() {
115         return configRegistryMXBeanProxy.lookupConfigBeans();
116     }
117
118     @Override
119     public Set<ObjectName> lookupConfigBeans(String moduleName) {
120         return configRegistryMXBeanProxy.lookupConfigBeans(moduleName);
121     }
122
123     @Override
124     public Set<ObjectName> lookupConfigBeans(String moduleName,
125             String instanceName) {
126         return configRegistryMXBeanProxy.lookupConfigBeans(moduleName, instanceName);
127     }
128
129     @Override
130     public ObjectName lookupConfigBean(String moduleName, String instanceName)
131             throws InstanceNotFoundException {
132         return configRegistryMXBeanProxy.lookupConfigBean(moduleName, instanceName);
133     }
134
135     @Override
136     public Set<ObjectName> lookupRuntimeBeans() {
137         return configRegistryMXBeanProxy.lookupRuntimeBeans();
138     }
139
140     @Override
141     public Set<ObjectName> lookupRuntimeBeans(String ifcName,
142             String instanceName) {
143         return configRegistryMXBeanProxy.lookupRuntimeBeans(ifcName, instanceName);
144     }
145
146     @Override
147     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
148         configRegistryMXBeanProxy.checkConfigBeanExists(objectName);
149     }
150
151     @Override
152     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceName, String refName) {
153         return configRegistryMXBeanProxy.lookupConfigBeanByServiceInterfaceName(serviceInterfaceName, refName);
154     }
155
156     @Override
157     public Map<String, Map<String, ObjectName>> getServiceMapping() {
158         return configRegistryMXBeanProxy.getServiceMapping();
159     }
160
161     @Override
162     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceName) {
163         return configRegistryMXBeanProxy.lookupServiceReferencesByServiceInterfaceName(serviceInterfaceName);
164     }
165
166     @Override
167     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
168         return configRegistryMXBeanProxy.lookupServiceInterfaceNames(objectName);
169     }
170
171     @Override
172     public String getServiceInterfaceName(String namespace, String localName) {
173         return configRegistryMXBeanProxy.getServiceInterfaceName(namespace, localName);
174     }
175
176     @Override
177     public Object invokeMethod(ObjectName on, String name, Object[] params,
178             String[] signature) {
179         try {
180             return configMBeanServer.invoke(on, name, params, signature);
181         } catch (InstanceNotFoundException | ReflectionException
182                 | MBeanException e) {
183             throw new RuntimeException("Unable to invoke operation " + name
184                     + " on " + on + " with attributes "
185                     + Arrays.toString(params) + " and signature "
186                     + Arrays.toString(signature), e);
187         }
188     }
189
190     @Override
191     public Object getAttributeCurrentValue(ObjectName on, String attributeName) {
192         try {
193             return configMBeanServer.getAttribute(on, attributeName);
194         } catch (AttributeNotFoundException | InstanceNotFoundException
195                 | MBeanException | ReflectionException e) {
196             throw new RuntimeException("Unable to get attribute "
197                     + attributeName + " for " + on, e);
198         }
199     }
200
201 }