*/
package org.opendaylight.controller.config.manager.impl.dynamicmbean;
-import static java.lang.String.format;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+import org.opendaylight.controller.config.api.annotations.Description;
+import org.opendaylight.controller.config.api.annotations.RequireInterface;
+import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
+import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper;
+import org.opendaylight.controller.config.spi.Module;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.ReflectionException;
+import java.lang.reflect.Array;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
-import org.opendaylight.controller.config.api.ModuleIdentifier;
-import org.opendaylight.controller.config.api.annotations.Description;
-import org.opendaylight.controller.config.api.annotations.RequireInterface;
-import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper;
-import org.opendaylight.controller.config.spi.Module;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import static java.lang.String.format;
/**
* Contains common code for readable/rw dynamic mbean wrappers. Routes all
} catch (InstanceNotFoundException e) {
new MBeanException(e);
}
+
if (obj instanceof ObjectName) {
AttributeHolder attributeHolder = attributeHolderMap
.get(attributeName);
}
return obj;
}
+
+
+ if(isDependencyListAttr(attributeName, obj)) {
+ obj = fixDependencyListAttribute(obj);
+ }
+
return obj;
+ }
+
+ private Object fixDependencyListAttribute(Object attribute) {
+ if(attribute.getClass().isArray() == false)
+ throw new IllegalArgumentException("Unexpected attribute type, should be an array, but was " + attribute.getClass());
+
+ for (int i = 0; i < Array.getLength(attribute); i++) {
+
+ Object on = Array.get(attribute, i);
+ if(on instanceof ObjectName == false)
+ throw new IllegalArgumentException("Unexpected attribute type, should be an ObjectName, but was " + on.getClass());
+ on = fixObjectName((ObjectName) on);
+
+ Array.set(attribute, i, on);
+ }
+
+ return attribute;
+ }
+
+ private boolean isDependencyListAttr(String attributeName, Object attribute) {
+ if (attributeHolderMap.containsKey(attributeName) == false)
+ return false;
+
+ AttributeHolder attributeHolder = attributeHolderMap.get(attributeName);
+ boolean isDepList = true;
+ isDepList &= attributeHolder.getRequireInterfaceOrNull() != null;
+ isDepList &= attribute instanceof ObjectName[];
+ return isDepList;
}
protected ObjectName fixObjectName(ObjectName on) {
*/
package org.opendaylight.controller.config.manager.impl.dynamicmbean;
-import java.lang.reflect.Method;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import org.opendaylight.controller.config.api.annotations.Description;
+import org.opendaylight.controller.config.api.annotations.RequireInterface;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.management.MBeanAttributeInfo;
import javax.management.ObjectName;
-
-import org.opendaylight.controller.config.api.annotations.Description;
-import org.opendaylight.controller.config.api.annotations.RequireInterface;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
@Immutable
class AttributeHolder {
private final RequireInterface requireInterfaceAnnotation;
private final String attributeType;
+ public static final Set<Class<?>> PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER = new HashSet<>();
+
+ static {
+ PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(ObjectName.class);
+ PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(ObjectName[].class);
+ PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.add(List.class);
+ }
+
public AttributeHolder(String name, Object object, String returnType,
boolean writable,
@Nullable RequireInterface requireInterfaceAnnotation,
static RequireInterface findRequireInterfaceAnnotation(final Method setter,
Set<Class<?>> inspectedInterfaces) {
- // only allow setX(ObjectName y) or setX(ObjectName[] y) to continue
- if (setter.getParameterTypes().length != 1
- || (setter.getParameterTypes()[0].equals(ObjectName.class) == false && setter
- .getParameterTypes()[0].equals(ObjectName[].class) == false)) {
+ // only allow setX(ObjectName y) or setX(ObjectName[] y) or setX(List<ObjectName> y) to continue
+
+ if (setter.getParameterTypes().length > 1)
+ return null;
+ if(PERMITTED_PARAMETER_TYPES_FOR_DEPENDENCY_SETTER.contains(setter.getParameterTypes()[0]) == false)
return null;
- }
List<RequireInterface> foundRequireInterfaces = AnnotationsHelper
.findMethodAnnotationInSuperClassesAndIfcs(setter, RequireInterface.class, inspectedInterfaces);
*/
package org.opendaylight.controller.config.manager.impl.dynamicmbean;
-import java.lang.reflect.Method;
+import org.opendaylight.controller.config.api.ModuleIdentifier;
+import org.opendaylight.controller.config.api.ValidationException;
+import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
+import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
+import org.opendaylight.controller.config.spi.Module;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.ThreadSafe;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;
-
-import org.opendaylight.controller.config.api.ModuleIdentifier;
-import org.opendaylight.controller.config.api.ValidationException;
-import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
-import org.opendaylight.controller.config.manager.impl.TransactionIdentifier;
-import org.opendaylight.controller.config.spi.Module;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.lang.reflect.Method;
/**
* Wraps {@link org.opendaylight.controller.config.spi.Module} instance in a
try {
if (attribute.getValue() instanceof ObjectName) {
- AttributeHolder attributeHolder = attributeHolderMap
- .get(attribute.getName());
- if (attributeHolder.getRequireInterfaceOrNull() != null) {
- attribute = new Attribute(attribute.getName(),
- fixObjectName((ObjectName) attribute.getValue()));
- } else {
- attribute = new Attribute(attribute.getName(),
- attribute.getValue());
- }
+ attribute = fixDependencyAttribute(attribute);
+ } else if(attribute.getValue() instanceof ObjectName[]) {
+ attribute = fixDependencyListAttribute(attribute);
}
+
internalServer.setAttribute(objectNameInternal, attribute);
} catch (InstanceNotFoundException e) {
throw new MBeanException(e);
}
+ private Attribute fixDependencyListAttribute(Attribute attribute) {
+ AttributeHolder attributeHolder = attributeHolderMap
+ .get(attribute.getName());
+ if (attributeHolder.getRequireInterfaceOrNull() != null) {
+ attribute = new Attribute(attribute.getName(),
+ fixObjectNames((ObjectName[]) attribute.getValue()));
+ }
+ return attribute;
+ }
+
+ private Attribute fixDependencyAttribute(Attribute attribute) {
+ AttributeHolder attributeHolder = attributeHolderMap
+ .get(attribute.getName());
+ if (attributeHolder.getRequireInterfaceOrNull() != null) {
+ attribute = new Attribute(attribute.getName(),
+ fixObjectName((ObjectName) attribute.getValue()));
+ } else {
+ attribute = new Attribute(attribute.getName(),
+ attribute.getValue());
+ }
+ return attribute;
+ }
+
+ private ObjectName[] fixObjectNames(ObjectName[] dependencies) {
+ int i = 0;
+
+ for (ObjectName dependencyOn : dependencies) {
+ dependencies[i++] = fixObjectName(dependencyOn);
+ }
+
+ return dependencies;
+ }
+
@Override
public AttributeList setAttributes(AttributeList attributes) {
AttributeList result = new AttributeList();
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
- <groupId>org.opendaylight.yangtools.model</groupId>
- <artifactId>ietf-inet-types</artifactId>
+ <groupId>org.opendaylight.yangtools.model</groupId>
+ <artifactId>ietf-inet-types</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.bgpcep</groupId>
+ <artifactId>mockito-configuration</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-manager</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>config-manager</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>config-util</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
</dependency>
</dependencies>
--- /dev/null
+package org.opendaylight.controller.config.yang.test.impl;
+
+import com.google.common.collect.Lists;
+import junit.framework.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.config.api.ConflictingVersionException;
+import org.opendaylight.controller.config.api.ValidationException;
+import org.opendaylight.controller.config.api.jmx.CommitStatus;
+import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
+import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
+import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
+import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.ObjectName;
+import java.util.ArrayList;
+import java.util.List;
+
+public class NetconfTestImplModuleTest extends AbstractConfigTest {
+
+ public static final String TESTING_DEP_PREFIX = "testing-dep";
+ private NetconfTestImplModuleFactory factory;
+ private final String instanceName = "n1";
+
+ @Before
+ public void setUp() {
+
+ factory = new NetconfTestImplModuleFactory();
+ super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(factory,
+ new DepTestImplModuleFactory()));
+ }
+
+ @Test
+ public void testDependencyList() throws InstanceAlreadyExistsException, ValidationException,
+ ConflictingVersionException {
+ ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+
+ ObjectName on = createInstance(transaction, instanceName, 4);
+ transaction.validateConfig();
+ CommitStatus status = transaction.commit();
+
+ assertBeanCount(1, factory.getImplementationName());
+ assertBeanCount(4 + 1, DepTestImplModuleFactory.NAME);
+ assertStatus(status, 1 + 4 + 1, 0, 0);
+
+ transaction = configRegistryClient.createTransaction();
+
+ NetconfTestImplModuleMXBean proxy = transaction.newMXBeanProxy(ObjectNameUtil.withoutTransactionName(on),
+ NetconfTestImplModuleMXBean.class);
+ proxy.getComplexList();
+ List<ObjectName> testingDeps = proxy.getTestingDeps();
+ ObjectName testingDep = proxy.getTestingDep();
+
+ Assert.assertEquals(TESTING_DEP_PREFIX, ObjectNameUtil.getInstanceName(testingDep));
+ assertTestingDeps(testingDeps, 4);
+
+ transaction.abortConfig();
+ }
+
+ private void assertTestingDeps(List<ObjectName> testingDeps, int i) {
+ Assert.assertEquals(i, testingDeps.size());
+
+ int c = 1;
+ for (ObjectName testingDep : testingDeps) {
+ Assert.assertEquals(TESTING_DEP_PREFIX + Integer.toString(c++), ObjectNameUtil.getInstanceName(testingDep));
+ }
+ }
+
+
+ private ObjectName createInstance(ConfigTransactionJMXClient transaction, String instanceName, int depsCount)
+ throws InstanceAlreadyExistsException {
+ ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
+ NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NetconfTestImplModuleMXBean.class);
+
+ ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX);
+ mxBean.setTestingDep(dep);
+
+ ArrayList<ObjectName> testingDeps = Lists.newArrayList();
+ for (int i = 0; i < depsCount; i++) {
+ dep = transaction.createModule(DepTestImplModuleFactory.NAME, TESTING_DEP_PREFIX + Integer.toString(i + 1));
+ testingDeps.add(dep);
+ }
+ mxBean.setTestingDeps(testingDeps);
+
+ return nameCreated;
+ }
+
+}