Initial code drop of yang model driven configuration system
[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
12 import javax.management.Attribute;
13 import javax.management.AttributeNotFoundException;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.InstanceNotFoundException;
16 import javax.management.ObjectName;
17
18 import org.jolokia.client.request.J4pExecRequest;
19 import org.jolokia.client.request.J4pReadRequest;
20 import org.jolokia.client.request.J4pWriteRequest;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
25 import org.opendaylight.controller.config.util.AttributeEntry;
26 import org.opendaylight.controller.config.util.ConfigTransactionClient;
27
28 public class ConfigTransactionJolokiaClient extends ListableJolokiaClient
29         implements ConfigTransactionClient {
30
31     private final ConfigRegistryJolokiaClient configRegistryJolokiaClient;
32
33     public ConfigTransactionJolokiaClient(String url,
34             ObjectName transactionControllerON,
35             ConfigRegistryJolokiaClient configRegistryJolokiaClient) {
36         super(url, transactionControllerON);
37         this.configRegistryJolokiaClient = configRegistryJolokiaClient;
38     }
39
40     public ObjectName getTransactionON() {
41         return objectName;
42     }
43
44     @Override
45     public CommitStatus commit() throws ConflictingVersionException,
46             ValidationException {
47         return configRegistryJolokiaClient.commitConfig(objectName);
48     }
49
50     @Override
51     public ObjectName createModule(String moduleName, String instanceName)
52             throws InstanceAlreadyExistsException {
53         J4pExecRequest execReq = new J4pExecRequest(objectName, "createModule",
54                 moduleName, instanceName);
55         try {
56             return extractObjectName(execute(execReq));
57         } catch (RuntimeException e) {
58             if (e.getMessage() != null
59                     && e.getMessage().startsWith(
60                             InstanceAlreadyExistsException.class.getName()))
61                 throw new InstanceAlreadyExistsException();
62             throw e;
63         }
64     }
65
66     @Override
67     public void destroyModule(ObjectName configBeanON) {
68         J4pExecRequest execReq = new J4pExecRequest(objectName,
69                 "destroyModule(javax.management.ObjectName)", configBeanON);
70         execute(execReq);
71     }
72
73     @Override
74     public void destroyConfigBean(String moduleName, String instanceName)
75             throws InstanceNotFoundException {
76         destroyModule(ObjectNameUtil.createTransactionModuleON(
77                 getTransactionName(), moduleName, instanceName));
78     }
79
80     @Override
81     public void abortConfig() {
82         J4pExecRequest execReq = new J4pExecRequest(objectName, "abortConfig");
83         execute(execReq);
84     }
85
86     @Override
87     public void validateConfig() throws ValidationException {
88         J4pExecRequest execReq = new J4pExecRequest(objectName,
89                 "validateConfig");
90         execute(execReq);
91     }
92
93     @Override
94     public long getParentVersion() {
95         J4pReadRequest req = new J4pReadRequest(objectName, "ParentVersion");
96         return (Long) execute(req).getValue();
97     }
98
99     @Override
100     public long getVersion() {
101         J4pReadRequest req = new J4pReadRequest(objectName, "Version");
102         return (Long) execute(req).getValue();
103     }
104
105     public void setAttribute(ObjectName configBeanTransactionON, String key,
106             Object value) {
107         J4pWriteRequest req = new J4pWriteRequest(configBeanTransactionON, key,
108                 value);
109         try {
110             execute(req);
111         } catch (RuntimeException e) {
112             if (e.getMessage() != null
113                     && e.getMessage().startsWith(
114                             AttributeNotFoundException.class.getName())) {
115                 // try to fix wrong case
116                 Map<String, AttributeEntry> allAttributes = getAttributes(configBeanTransactionON);
117                 for (AttributeEntry attrib : allAttributes.values()) {
118                     if (attrib.getKey().equalsIgnoreCase(key)) {
119                         req = new J4pWriteRequest(configBeanTransactionON,
120                                 attrib.getKey(), value);
121                         execute(req);
122                         return;
123                     }
124                 }
125             }
126             throw e;
127         }
128     }
129
130     public Object getAttribute(ObjectName objectName, String key) {
131         return configRegistryJolokiaClient.getAttribute(objectName, key);
132     }
133
134     public ObjectName getAttributeON(ObjectName objectName, String key) {
135         return configRegistryJolokiaClient.getAttributeON(objectName, key);
136     }
137
138     @Override
139     public String getTransactionName() {
140         return ObjectNameUtil.getTransactionName(objectName);
141     }
142
143     @Override
144     public void validateBean(ObjectName rwON) throws ValidationException {
145         J4pExecRequest req = new J4pExecRequest(rwON, "validate", new Object[0]);
146         execute(req);
147     }
148
149     @Override
150     public void assertVersion(int expectedParentVersion,
151             int expectedCurrentVersion) {
152         if (expectedParentVersion != getParentVersion()) {
153             throw new IllegalStateException();
154         }
155         if (expectedCurrentVersion != getVersion()) {
156             throw new IllegalStateException();
157         }
158     }
159
160     @Override
161     public void setAttribute(ObjectName on, String jmxName, Attribute attribute) {
162         throw new UnsupportedOperationException();
163     }
164
165 }