2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.util.jolokia;
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;
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;
28 public class ConfigTransactionJolokiaClient extends ListableJolokiaClient
29 implements ConfigTransactionClient {
31 private final ConfigRegistryJolokiaClient configRegistryJolokiaClient;
33 public ConfigTransactionJolokiaClient(String url,
34 ObjectName transactionControllerON,
35 ConfigRegistryJolokiaClient configRegistryJolokiaClient) {
36 super(url, transactionControllerON);
37 this.configRegistryJolokiaClient = configRegistryJolokiaClient;
40 public ObjectName getTransactionON() {
45 public CommitStatus commit() throws ConflictingVersionException,
47 return configRegistryJolokiaClient.commitConfig(objectName);
51 public ObjectName createModule(String moduleName, String instanceName)
52 throws InstanceAlreadyExistsException {
53 J4pExecRequest execReq = new J4pExecRequest(objectName, "createModule",
54 moduleName, instanceName);
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();
67 public void destroyModule(ObjectName configBeanON) {
68 J4pExecRequest execReq = new J4pExecRequest(objectName,
69 "destroyModule(javax.management.ObjectName)", configBeanON);
74 public void destroyConfigBean(String moduleName, String instanceName)
75 throws InstanceNotFoundException {
76 destroyModule(ObjectNameUtil.createTransactionModuleON(
77 getTransactionName(), moduleName, instanceName));
81 public void abortConfig() {
82 J4pExecRequest execReq = new J4pExecRequest(objectName, "abortConfig");
87 public void validateConfig() throws ValidationException {
88 J4pExecRequest execReq = new J4pExecRequest(objectName,
94 public long getParentVersion() {
95 J4pReadRequest req = new J4pReadRequest(objectName, "ParentVersion");
96 return (Long) execute(req).getValue();
100 public long getVersion() {
101 J4pReadRequest req = new J4pReadRequest(objectName, "Version");
102 return (Long) execute(req).getValue();
105 public void setAttribute(ObjectName configBeanTransactionON, String key,
107 J4pWriteRequest req = new J4pWriteRequest(configBeanTransactionON, key,
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);
130 public Object getAttribute(ObjectName objectName, String key) {
131 return configRegistryJolokiaClient.getAttribute(objectName, key);
134 public ObjectName getAttributeON(ObjectName objectName, String key) {
135 return configRegistryJolokiaClient.getAttributeON(objectName, key);
139 public String getTransactionName() {
140 return ObjectNameUtil.getTransactionName(objectName);
144 public void validateBean(ObjectName rwON) throws ValidationException {
145 J4pExecRequest req = new J4pExecRequest(rwON, "validate", new Object[0]);
150 public void assertVersion(int expectedParentVersion,
151 int expectedCurrentVersion) {
152 if (expectedParentVersion != getParentVersion()) {
153 throw new IllegalStateException();
155 if (expectedCurrentVersion != getVersion()) {
156 throw new IllegalStateException();
161 public void setAttribute(ObjectName on, String jmxName, Attribute attribute) {
162 throw new UnsupportedOperationException();