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.manager.impl.dynamicmbean;
10 import org.opendaylight.controller.config.api.ModuleIdentifier;
11 import org.opendaylight.controller.config.api.ValidationException;
12 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
13 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
14 import org.opendaylight.controller.config.spi.Module;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 import javax.annotation.concurrent.ThreadSafe;
19 import javax.management.Attribute;
20 import javax.management.AttributeList;
21 import javax.management.AttributeNotFoundException;
22 import javax.management.InstanceNotFoundException;
23 import javax.management.InvalidAttributeValueException;
24 import javax.management.MBeanException;
25 import javax.management.MBeanOperationInfo;
26 import javax.management.MBeanServer;
27 import javax.management.ObjectName;
28 import javax.management.ReflectionException;
29 import java.lang.reflect.Method;
32 * Wraps {@link org.opendaylight.controller.config.spi.Module} instance in a
33 * {@link DynamicMBean} interface. Inspects dependency attributes, identified by
34 * ObjectName getter/setter and {@link RequireInterface} annotation. Used to
35 * simplify client calls - to set a dependency, only instance name is needed.
36 * This class creates new writable String attribute for each dependency with
37 * 'Name' suffix backed by the actual ObjectName attribute.
39 * Thread safety - setting attributes is synchronized on 'this'. Synchronization
40 * of {@link org.opendaylight.controller.config.spi.Module#validate()} and
41 * {@link org.opendaylight.controller.config.spi.Module#getInstance()} is also
43 * {@link org.opendaylight.controller.config.manager.impl.ConfigTransactionControllerInternal}
44 * so the actual {@link org.opendaylight.controller.config.spi.Module} needs not
49 public class DynamicWritableWrapper extends AbstractDynamicWrapper {
50 private static final Logger logger = LoggerFactory
51 .getLogger(DynamicWritableWrapper.class);
53 private final ReadOnlyAtomicBoolean configBeanModificationDisabled;
55 public DynamicWritableWrapper(Module module,
56 ModuleIdentifier moduleIdentifier,
57 TransactionIdentifier transactionIdentifier,
58 ReadOnlyAtomicBoolean configBeanModificationDisabled,
59 MBeanServer internalServer, MBeanServer configMBeanServer) {
60 super(module, true, moduleIdentifier, ObjectNameUtil
61 .createTransactionModuleON(transactionIdentifier.getName(), moduleIdentifier), getOperations(moduleIdentifier),
62 internalServer, configMBeanServer);
63 this.configBeanModificationDisabled = configBeanModificationDisabled;
66 private static MBeanOperationInfo[] getOperations(
67 ModuleIdentifier moduleIdentifier) {
68 Method validationMethod;
70 validationMethod = DynamicWritableWrapper.class.getMethod("validate");
71 } catch (NoSuchMethodException e) {
72 throw new IllegalStateException("No such method exception on " + moduleIdentifier, e);
74 return new MBeanOperationInfo[]{new MBeanOperationInfo("Validation", validationMethod)};
78 public synchronized void setAttribute(Attribute attribute)
79 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
80 if (configBeanModificationDisabled.get() == true) {
81 throw new IllegalStateException("Operation is not allowed now");
84 if (attribute.getName().equals("Attribute")) {
85 setAttribute((Attribute) attribute.getValue());
90 if (attribute.getValue() instanceof ObjectName) {
91 attribute = fixDependencyAttribute(attribute);
92 } else if (attribute.getValue() instanceof ObjectName[]) {
93 attribute = fixDependencyListAttribute(attribute);
96 internalServer.setAttribute(objectNameInternal, attribute);
97 } catch (InstanceNotFoundException e) {
98 throw new MBeanException(e);
103 private Attribute fixDependencyListAttribute(Attribute attribute) {
104 AttributeHolder attributeHolder = attributeHolderMap.get(attribute.getName());
105 if (attributeHolder.getRequireInterfaceOrNull() != null) {
106 attribute = new Attribute(attribute.getName(), fixObjectNames((ObjectName[]) attribute.getValue()));
111 private Attribute fixDependencyAttribute(Attribute attribute) {
112 AttributeHolder attributeHolder = attributeHolderMap.get(attribute.getName());
113 if (attributeHolder.getRequireInterfaceOrNull() != null) {
114 attribute = new Attribute(attribute.getName(), fixObjectName((ObjectName) attribute.getValue()));
116 attribute = new Attribute(attribute.getName(), attribute.getValue());
121 private ObjectName[] fixObjectNames(ObjectName[] dependencies) {
124 for (ObjectName dependencyOn : dependencies) {
125 dependencies[i++] = fixObjectName(dependencyOn);
132 public AttributeList setAttributes(AttributeList attributes) {
133 AttributeList result = new AttributeList();
134 for (Object attributeObject : attributes) {
135 Attribute attribute = (Attribute) attributeObject;
137 setAttribute(attribute);
138 result.add(attribute);
139 } catch (Exception e) {
140 logger.warn("Setting attribute {} failed on {}", attribute.getName(), moduleIdentifier, e);
141 throw new IllegalArgumentException(
142 "Setting attribute failed - " + attribute.getName()
143 + " on " + moduleIdentifier, e);
150 public Object invoke(String actionName, Object[] params, String[] signature)
151 throws MBeanException, ReflectionException {
152 if ("validate".equals(actionName)
153 && (params == null || params.length == 0)
154 && (signature == null || signature.length == 0)) {
157 } catch (Exception e) {
159 throw new MBeanException(ValidationException.createForSingleException(
160 moduleIdentifier, e));
164 return super.invoke(actionName, params, signature);
167 public void validate() {