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(
71 "validate", new Class<?>[0]);
72 } catch (NoSuchMethodException e) {
73 throw new IllegalStateException("No such method exception on "
74 + moduleIdentifier, e);
76 return new MBeanOperationInfo[] { new MBeanOperationInfo("Validation",
81 public synchronized void setAttribute(Attribute attribute)
82 throws AttributeNotFoundException, InvalidAttributeValueException,
83 MBeanException, ReflectionException {
84 if (configBeanModificationDisabled.get() == true)
85 throw new IllegalStateException("Operation is not allowed now");
87 if (attribute.getName().equals("Attribute")) {
88 setAttribute((Attribute) attribute.getValue());
93 if (attribute.getValue() instanceof ObjectName) {
94 attribute = fixDependencyAttribute(attribute);
95 } else if(attribute.getValue() instanceof ObjectName[]) {
96 attribute = fixDependencyListAttribute(attribute);
99 internalServer.setAttribute(objectNameInternal, attribute);
100 } catch (InstanceNotFoundException e) {
101 throw new MBeanException(e);
106 private Attribute fixDependencyListAttribute(Attribute attribute) {
107 AttributeHolder attributeHolder = attributeHolderMap
108 .get(attribute.getName());
109 if (attributeHolder.getRequireInterfaceOrNull() != null) {
110 attribute = new Attribute(attribute.getName(),
111 fixObjectNames((ObjectName[]) attribute.getValue()));
116 private Attribute fixDependencyAttribute(Attribute attribute) {
117 AttributeHolder attributeHolder = attributeHolderMap
118 .get(attribute.getName());
119 if (attributeHolder.getRequireInterfaceOrNull() != null) {
120 attribute = new Attribute(attribute.getName(),
121 fixObjectName((ObjectName) attribute.getValue()));
123 attribute = new Attribute(attribute.getName(),
124 attribute.getValue());
129 private ObjectName[] fixObjectNames(ObjectName[] dependencies) {
132 for (ObjectName dependencyOn : dependencies) {
133 dependencies[i++] = fixObjectName(dependencyOn);
140 public AttributeList setAttributes(AttributeList attributes) {
141 AttributeList result = new AttributeList();
142 for (Object attributeObject : attributes) {
143 Attribute attribute = (Attribute) attributeObject;
145 setAttribute(attribute);
146 result.add(attribute);
147 } catch (Exception e) {
148 logger.warn("Setting attribute {} failed on {}",
149 attribute.getName(), moduleIdentifier, e);
150 throw new IllegalArgumentException(
151 "Setting attribute failed - " + attribute.getName()
152 + " on " + moduleIdentifier, e);
159 public Object invoke(String actionName, Object[] params, String[] signature)
160 throws MBeanException, ReflectionException {
161 if ("validate".equals(actionName)
162 && (params == null || params.length == 0)
163 && (signature == null || signature.length == 0)) {
166 } catch (Exception e) {
167 throw ValidationException.createForSingleException(
168 moduleIdentifier, e);
172 return super.invoke(actionName, params, signature);
175 public void validate() {