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 java.lang.reflect.Method;
12 import javax.annotation.concurrent.ThreadSafe;
13 import javax.management.Attribute;
14 import javax.management.AttributeList;
15 import javax.management.AttributeNotFoundException;
16 import javax.management.InstanceNotFoundException;
17 import javax.management.InvalidAttributeValueException;
18 import javax.management.MBeanException;
19 import javax.management.MBeanOperationInfo;
20 import javax.management.MBeanServer;
21 import javax.management.ObjectName;
22 import javax.management.ReflectionException;
24 import org.opendaylight.controller.config.api.ModuleIdentifier;
25 import org.opendaylight.controller.config.api.ValidationException;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
28 import org.opendaylight.controller.config.spi.Module;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Wraps {@link org.opendaylight.controller.config.spi.Module} instance in a
34 * {@link DynamicMBean} interface. Inspects dependency attributes, identified by
35 * ObjectName getter/setter and {@link RequireInterface} annotation. Used to
36 * simplify client calls - to set a dependency, only instance name is needed.
37 * This class creates new writable String attribute for each dependency with
38 * 'Name' suffix backed by the actual ObjectName attribute.
40 * Thread safety - setting attributes is synchronized on 'this'. Synchronization
41 * of {@link org.opendaylight.controller.config.spi.Module#validate()} and
42 * {@link org.opendaylight.controller.config.spi.Module#getInstance()} is also
44 * {@link org.opendaylight.controller.config.manager.impl.ConfigTransactionControllerInternal}
45 * so the actual {@link org.opendaylight.controller.config.spi.Module} needs not
50 public class DynamicWritableWrapper extends AbstractDynamicWrapper {
51 private static final Logger logger = LoggerFactory
52 .getLogger(DynamicWritableWrapper.class);
54 private final ReadOnlyAtomicBoolean configBeanModificationDisabled;
56 public DynamicWritableWrapper(Module module,
57 ModuleIdentifier moduleIdentifier,
58 TransactionIdentifier transactionIdentifier,
59 ReadOnlyAtomicBoolean configBeanModificationDisabled,
60 MBeanServer internalServer, MBeanServer configMBeanServer) {
61 super(module, true, moduleIdentifier, ObjectNameUtil
62 .createTransactionModuleON(transactionIdentifier.getName(), moduleIdentifier), getOperations(moduleIdentifier),
63 internalServer, configMBeanServer);
64 this.configBeanModificationDisabled = configBeanModificationDisabled;
67 private static MBeanOperationInfo[] getOperations(
68 ModuleIdentifier moduleIdentifier) {
69 Method validationMethod;
71 validationMethod = DynamicWritableWrapper.class.getMethod(
72 "validate", new Class<?>[0]);
73 } catch (NoSuchMethodException e) {
74 throw new IllegalStateException("No such method exception on "
75 + moduleIdentifier, e);
77 return new MBeanOperationInfo[] { new MBeanOperationInfo("Validation",
82 public synchronized void setAttribute(Attribute attribute)
83 throws AttributeNotFoundException, InvalidAttributeValueException,
84 MBeanException, ReflectionException {
85 if (configBeanModificationDisabled.get() == true)
86 throw new IllegalStateException("Operation is not allowed now");
88 if (attribute.getName().equals("Attribute")) {
89 setAttribute((Attribute) attribute.getValue());
94 if (attribute.getValue() instanceof ObjectName) {
95 AttributeHolder attributeHolder = attributeHolderMap
96 .get(attribute.getName());
97 if (attributeHolder.getRequireInterfaceOrNull() != null) {
98 attribute = new Attribute(attribute.getName(),
99 fixObjectName((ObjectName) attribute.getValue()));
101 attribute = new Attribute(attribute.getName(),
102 attribute.getValue());
105 internalServer.setAttribute(objectNameInternal, attribute);
106 } catch (InstanceNotFoundException e) {
107 throw new MBeanException(e);
113 public AttributeList setAttributes(AttributeList attributes) {
114 AttributeList result = new AttributeList();
115 for (Object attributeObject : attributes) {
116 Attribute attribute = (Attribute) attributeObject;
118 setAttribute(attribute);
119 result.add(attribute);
120 } catch (Exception e) {
121 logger.warn("Setting attribute {} failed on {}",
122 attribute.getName(), moduleIdentifier, e);
123 throw new IllegalArgumentException(
124 "Setting attribute failed - " + attribute.getName()
125 + " on " + moduleIdentifier, e);
132 public Object invoke(String actionName, Object[] params, String[] signature)
133 throws MBeanException, ReflectionException {
134 if ("validate".equals(actionName)
135 && (params == null || params.length == 0)
136 && (signature == null || signature.length == 0)) {
139 } catch (Exception e) {
140 throw ValidationException.createForSingleException(
141 moduleIdentifier, e);
145 return super.invoke(actionName, params, signature);
148 public void validate() {