Introducing simple merge strategy for config subsystem
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / DynamicWritableWrapper.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.manager.impl.dynamicmbean;
9
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;
17
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;
30
31 /**
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.
38  * <p>
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
42  * guaranteed by
43  * {@link org.opendaylight.controller.config.manager.impl.ConfigTransactionControllerInternal}
44  * so the actual {@link org.opendaylight.controller.config.spi.Module} needs not
45  * to be thread safe.
46  * </p>
47  */
48 @ThreadSafe
49 public class DynamicWritableWrapper extends AbstractDynamicWrapper {
50     private static final Logger logger = LoggerFactory
51             .getLogger(DynamicWritableWrapper.class);
52
53     private final ReadOnlyAtomicBoolean configBeanModificationDisabled;
54
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;
64     }
65
66     private static MBeanOperationInfo[] getOperations(
67             ModuleIdentifier moduleIdentifier) {
68         Method validationMethod;
69         try {
70             validationMethod = DynamicWritableWrapper.class.getMethod("validate");
71         } catch (NoSuchMethodException e) {
72             throw new IllegalStateException("No such method exception on " + moduleIdentifier, e);
73         }
74         return new MBeanOperationInfo[]{new MBeanOperationInfo("Validation", validationMethod)};
75     }
76
77     @Override
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");
82         }
83
84         if (attribute.getName().equals("Attribute")) {
85             setAttribute((Attribute) attribute.getValue());
86             return;
87         }
88
89         try {
90             if (attribute.getValue() instanceof ObjectName) {
91                 attribute = fixDependencyAttribute(attribute);
92             } else if (attribute.getValue() instanceof ObjectName[]) {
93                 attribute = fixDependencyListAttribute(attribute);
94             }
95
96             internalServer.setAttribute(objectNameInternal, attribute);
97         } catch (InstanceNotFoundException e) {
98             throw new MBeanException(e);
99         }
100
101     }
102
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()));
107         }
108         return attribute;
109     }
110
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()));
115         } else {
116             attribute = new Attribute(attribute.getName(), attribute.getValue());
117         }
118         return attribute;
119     }
120
121     private ObjectName[] fixObjectNames(ObjectName[] dependencies) {
122         int i = 0;
123
124         for (ObjectName dependencyOn : dependencies) {
125             dependencies[i++] = fixObjectName(dependencyOn);
126         }
127
128         return dependencies;
129     }
130
131     @Override
132     public AttributeList setAttributes(AttributeList attributes) {
133         AttributeList result = new AttributeList();
134         for (Object attributeObject : attributes) {
135             Attribute attribute = (Attribute) attributeObject;
136             try {
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);
144             }
145         }
146         return result;
147     }
148
149     @Override
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)) {
155             try {
156                 validate();
157             } catch (Exception e) {
158
159                 throw new MBeanException(ValidationException.createForSingleException(
160                         moduleIdentifier, e));
161             }
162             return Void.TYPE;
163         }
164         return super.invoke(actionName, params, signature);
165     }
166
167     public void validate() {
168         module.validate();
169     }
170 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.