Merge "Bug 116 - Revisit SanityTest"
[controller.git] / opendaylight / config / config-util / src / main / java / org / opendaylight / controller / config / util / jolokia / ConfigTransactionJolokiaClient.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.jolokia;
9
10 import java.util.Map;
11 import java.util.Set;
12
13 import javax.management.Attribute;
14 import javax.management.AttributeNotFoundException;
15 import javax.management.InstanceAlreadyExistsException;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.ObjectName;
18
19 import org.jolokia.client.request.J4pExecRequest;
20 import org.jolokia.client.request.J4pReadRequest;
21 import org.jolokia.client.request.J4pWriteRequest;
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.ObjectNameUtil;
26 import org.opendaylight.controller.config.util.AttributeEntry;
27 import org.opendaylight.controller.config.util.ConfigTransactionClient;
28
29 @Deprecated
30 public class ConfigTransactionJolokiaClient extends ListableJolokiaClient
31         implements ConfigTransactionClient {
32
33     private final ConfigRegistryJolokiaClient configRegistryJolokiaClient;
34
35     public ConfigTransactionJolokiaClient(String url,
36             ObjectName transactionControllerON,
37             ConfigRegistryJolokiaClient configRegistryJolokiaClient) {
38         super(url, transactionControllerON);
39         this.configRegistryJolokiaClient = configRegistryJolokiaClient;
40     }
41
42     public ObjectName getTransactionON() {
43         return objectName;
44     }
45
46     @Override
47     public CommitStatus commit() throws ConflictingVersionException,
48             ValidationException {
49         return configRegistryJolokiaClient.commitConfig(objectName);
50     }
51
52     @Override
53     public ObjectName createModule(String moduleName, String instanceName)
54             throws InstanceAlreadyExistsException {
55         J4pExecRequest execReq = new J4pExecRequest(objectName, "createModule",
56                 moduleName, instanceName);
57         try {
58             return extractObjectName(execute(execReq));
59         } catch (RuntimeException e) {
60             if (e.getMessage() != null
61                     && e.getMessage().startsWith(
62                             InstanceAlreadyExistsException.class.getName()))
63                 throw new InstanceAlreadyExistsException();
64             throw e;
65         }
66     }
67
68     @Override
69     public void destroyModule(ObjectName configBeanON) {
70         J4pExecRequest execReq = new J4pExecRequest(objectName,
71                 "destroyModule(javax.management.ObjectName)", configBeanON);
72         execute(execReq);
73     }
74
75     @Override
76     public void destroyConfigBean(String moduleName, String instanceName)
77             throws InstanceNotFoundException {
78         destroyModule(ObjectNameUtil.createTransactionModuleON(
79                 getTransactionName(), moduleName, instanceName));
80     }
81
82     @Override
83     public void abortConfig() {
84         J4pExecRequest execReq = new J4pExecRequest(objectName, "abortConfig");
85         execute(execReq);
86     }
87
88     @Override
89     public void validateConfig() throws ValidationException {
90         J4pExecRequest execReq = new J4pExecRequest(objectName,
91                 "validateConfig");
92         execute(execReq);
93     }
94
95     @Override
96     public long getParentVersion() {
97         J4pReadRequest req = new J4pReadRequest(objectName, "ParentVersion");
98         return (Long) execute(req).getValue();
99     }
100
101     @Override
102     public long getVersion() {
103         J4pReadRequest req = new J4pReadRequest(objectName, "Version");
104         return (Long) execute(req).getValue();
105     }
106
107     public void setAttribute(ObjectName configBeanTransactionON, String key,
108             Object value) {
109         J4pWriteRequest req = new J4pWriteRequest(configBeanTransactionON, key,
110                 value);
111         try {
112             execute(req);
113         } catch (RuntimeException e) {
114             if (e.getMessage() != null
115                     && e.getMessage().startsWith(
116                             AttributeNotFoundException.class.getName())) {
117                 // try to fix wrong case
118                 Map<String, AttributeEntry> allAttributes = getAttributes(configBeanTransactionON);
119                 for (AttributeEntry attrib : allAttributes.values()) {
120                     if (attrib.getKey().equalsIgnoreCase(key)) {
121                         req = new J4pWriteRequest(configBeanTransactionON,
122                                 attrib.getKey(), value);
123                         execute(req);
124                         return;
125                     }
126                 }
127             }
128             throw e;
129         }
130     }
131
132     public Object getAttribute(ObjectName objectName, String key) {
133         return configRegistryJolokiaClient.getAttribute(objectName, key);
134     }
135
136     public ObjectName getAttributeON(ObjectName objectName, String key) {
137         return configRegistryJolokiaClient.getAttributeON(objectName, key);
138     }
139
140     @Override
141     public String getTransactionName() {
142         return ObjectNameUtil.getTransactionName(objectName);
143     }
144
145     @Override
146     public void validateBean(ObjectName rwON) throws ValidationException {
147         J4pExecRequest req = new J4pExecRequest(rwON, "validate", new Object[0]);
148         execute(req);
149     }
150
151     @Override
152     public void assertVersion(int expectedParentVersion,
153             int expectedCurrentVersion) {
154         if (expectedParentVersion != getParentVersion()) {
155             throw new IllegalStateException();
156         }
157         if (expectedCurrentVersion != getVersion()) {
158             throw new IllegalStateException();
159         }
160     }
161
162     @Override
163     public void setAttribute(ObjectName on, String jmxName, Attribute attribute) {
164         throw new UnsupportedOperationException();
165     }
166
167     // TODO: implement or deprecate
168     @Override
169     public void checkConfigBeanExists(ObjectName objectName) throws InstanceNotFoundException {
170         throw new UnsupportedOperationException();
171     }
172
173     @Override
174     public void saveServiceReference(String serviceInterfaceName, String refName, ObjectName objectName) throws InstanceNotFoundException {
175         throw new UnsupportedOperationException();
176     }
177
178     @Override
179     public boolean removeServiceReference(String serviceInterfaceName, String refName) {
180         throw new UnsupportedOperationException();
181     }
182
183     @Override
184     public void removeAllServiceReferences() {
185         throw new UnsupportedOperationException();
186     }
187
188     @Override
189     public ObjectName lookupConfigBeanByServiceInterfaceName(String serviceInterfaceName, String refName) {
190         throw new UnsupportedOperationException();
191     }
192
193     @Override
194     public Map<String, Map<String, ObjectName>> getServiceMapping() {
195         throw new UnsupportedOperationException();
196     }
197
198     @Override
199     public Map<String, ObjectName> lookupServiceReferencesByServiceInterfaceName(String serviceInterfaceName) {
200         throw new UnsupportedOperationException();
201     }
202
203     @Override
204     public Set<String> lookupServiceInterfaceNames(ObjectName objectName) throws InstanceNotFoundException {
205         throw new UnsupportedOperationException();
206     }
207
208     @Override
209     public String getServiceInterfaceName(String namespace, String localName) {
210         throw new UnsupportedOperationException();
211     }
212
213     @Override
214     public boolean removeServiceReferences(ObjectName objectName) throws InstanceNotFoundException {
215         throw new UnsupportedOperationException();
216     }
217 }