X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fdependencyresolver%2FDependencyResolverImpl.java;h=4f60a673f5f711ce17815061318f2641bbda0091;hb=b692b07e81933ff8c85a936a90a934cf778282e2;hp=925a57b044f44f87c31bf1aa9e8d60d7ebf210c9;hpb=c541f7868e6e2d654b8080b5426bb12a39bddf11;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dependencyresolver/DependencyResolverImpl.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dependencyresolver/DependencyResolverImpl.java index 925a57b044..4f60a673f5 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dependencyresolver/DependencyResolverImpl.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/dependencyresolver/DependencyResolverImpl.java @@ -7,7 +7,21 @@ */ package org.opendaylight.controller.config.manager.impl.dependencyresolver; +import static java.lang.String.format; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; +import javax.annotation.concurrent.GuardedBy; +import javax.management.AttributeNotFoundException; +import javax.management.InstanceNotFoundException; +import javax.management.JMX; +import javax.management.MBeanException; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import javax.management.ReflectionException; import org.opendaylight.controller.config.api.DependencyResolver; +import org.opendaylight.controller.config.api.IdentityAttributeRef; import org.opendaylight.controller.config.api.JmxAttribute; import org.opendaylight.controller.config.api.JmxAttributeValidationException; import org.opendaylight.controller.config.api.ModuleIdentifier; @@ -17,14 +31,12 @@ import org.opendaylight.controller.config.api.jmx.ObjectNameUtil; import org.opendaylight.controller.config.manager.impl.TransactionStatus; import org.opendaylight.controller.config.spi.Module; import org.opendaylight.controller.config.spi.ModuleFactory; - -import javax.annotation.concurrent.GuardedBy; -import javax.management.ObjectName; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.Set; - -import static java.lang.String.format; +import org.opendaylight.yangtools.yang.binding.BaseIdentity; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry; +import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Protect {@link org.opendaylight.controller.config.spi.Module#getInstance()} @@ -32,22 +44,30 @@ import static java.lang.String.format; * during validation. Tracks dependencies for ordering purposes. */ final class DependencyResolverImpl implements DependencyResolver, - Comparable { + Comparable { + private static final Logger logger = LoggerFactory.getLogger(DependencyResolverImpl.class); + private final ModulesHolder modulesHolder; private final ModuleIdentifier name; private final TransactionStatus transactionStatus; @GuardedBy("this") private final Set dependencies = new HashSet<>(); private final ServiceReferenceReadableRegistry readableRegistry; + private final CodecRegistry codecRegistry; + private final String transactionName; + private final MBeanServer mBeanServer; DependencyResolverImpl(ModuleIdentifier currentModule, - TransactionStatus transactionStatus, ModulesHolder modulesHolder, - ServiceReferenceReadableRegistry readableRegistry) { - + TransactionStatus transactionStatus, ModulesHolder modulesHolder, + ServiceReferenceReadableRegistry readableRegistry, CodecRegistry codecRegistry, + String transactionName, MBeanServer mBeanServer) { + this.codecRegistry = codecRegistry; this.name = currentModule; this.transactionStatus = transactionStatus; this.modulesHolder = modulesHolder; this.readableRegistry = readableRegistry; + this.transactionName = transactionName; + this.mBeanServer = mBeanServer; } /** @@ -64,13 +84,14 @@ final class DependencyResolverImpl implements DependencyResolver, throw new NullPointerException( "Parameter 'expectedServiceInterface' is null"); } - if (jmxAttribute == null) + if (jmxAttribute == null) { throw new NullPointerException("Parameter 'jmxAttribute' is null"); + } JmxAttributeValidationException.checkNotNull(dependentReadOnlyON, - "is null, " + "expected dependency implementing " - + expectedServiceInterface, jmxAttribute); - + "is null, expected dependency implementing " + + expectedServiceInterface, jmxAttribute + ); // check that objectName belongs to this transaction - this should be @@ -81,8 +102,10 @@ final class DependencyResolverImpl implements DependencyResolver, JmxAttributeValidationException.checkCondition( hasTransaction == false, format("ObjectName should not contain " - + "transaction name. %s set to %s. ", jmxAttribute, - dependentReadOnlyON), jmxAttribute); + + "transaction name. %s set to %s. ", jmxAttribute, + dependentReadOnlyON + ), jmxAttribute + ); dependentReadOnlyON = translateServiceRefIfPossible(dependentReadOnlyON); @@ -100,7 +123,8 @@ final class DependencyResolverImpl implements DependencyResolver, + "attribute %s", foundFactory.getImplementationName(), foundFactory, expectedServiceInterface, dependentReadOnlyON, - jmxAttribute); + jmxAttribute + ); throw new JmxAttributeValidationException(message, jmxAttribute); } synchronized (this) { @@ -108,7 +132,7 @@ final class DependencyResolverImpl implements DependencyResolver, } } - // transalate from serviceref to module ON + // translate from serviceref to module ON private ObjectName translateServiceRefIfPossible(ObjectName dependentReadOnlyON) { if (ObjectNameUtil.isServiceReference(dependentReadOnlyON)) { String serviceQName = ObjectNameUtil.getServiceQName(dependentReadOnlyON); @@ -125,10 +149,10 @@ final class DependencyResolverImpl implements DependencyResolver, //TODO: check for cycles @Override public T resolveInstance(Class expectedType, ObjectName dependentReadOnlyON, - JmxAttribute jmxAttribute) { + JmxAttribute jmxAttribute) { if (expectedType == null || dependentReadOnlyON == null || jmxAttribute == null) { throw new IllegalArgumentException(format( - "Null parameters not allowed, got {} {} {}", expectedType, + "Null parameters not allowed, got %s %s %s", expectedType, dependentReadOnlyON, jmxAttribute)); } dependentReadOnlyON = translateServiceRefIfPossible(dependentReadOnlyON); @@ -147,21 +171,49 @@ final class DependencyResolverImpl implements DependencyResolver, String message = format( "Error while %s resolving instance %s. getInstance() returned null. " + "Expected type %s , attribute %s", name, - dependentModuleIdentifier, expectedType, jmxAttribute); + dependentModuleIdentifier, expectedType, jmxAttribute + ); throw new JmxAttributeValidationException(message, jmxAttribute); } try { - T result = expectedType.cast(instance); - return result; + return expectedType.cast(instance); } catch (ClassCastException e) { String message = format( "Instance cannot be cast to expected type. Instance class is %s , " + "expected type %s , attribute %s", - instance.getClass(), expectedType, jmxAttribute); + instance.getClass(), expectedType, jmxAttribute + ); throw new JmxAttributeValidationException(message, e, jmxAttribute); } } + @Override + public Class resolveIdentity(IdentityAttributeRef identityRef, Class expectedBaseClass) { + final QName qName = QName.create(identityRef.getqNameOfIdentity()); + IdentityCodec identityCodec = codecRegistry.getIdentityCodec(); + Class deserialized = identityCodec.deserialize(qName); + if (deserialized == null) { + throw new IllegalStateException("Unable to retrieve identity class for " + qName + ", null response from " + + codecRegistry); + } + if (expectedBaseClass.isAssignableFrom(deserialized)) { + return (Class) deserialized; + } else { + logger.error("Cannot resolve class of identity {} : deserialized class {} is not a subclass of {}.", + identityRef, deserialized, expectedBaseClass); + throw new IllegalArgumentException("Deserialized identity " + deserialized + " cannot be cast to " + expectedBaseClass); + } + } + + @Override + public void validateIdentity(IdentityAttributeRef identityRef, Class expectedBaseClass, JmxAttribute jmxAttribute) { + try { + resolveIdentity(identityRef, expectedBaseClass); + } catch (Exception e) { + throw JmxAttributeValidationException.wrap(e, jmxAttribute); + } + } + @Override public int compareTo(DependencyResolverImpl o) { transactionStatus.checkCommitted(); @@ -178,7 +230,7 @@ final class DependencyResolverImpl implements DependencyResolver, return maxDependencyDepth; } - public void countMaxDependencyDepth(DependencyResolverManager manager) { + void countMaxDependencyDepth(DependencyResolverManager manager) { transactionStatus.checkCommitted(); if (maxDependencyDepth == null) { maxDependencyDepth = getMaxDepth(this, manager, @@ -187,8 +239,8 @@ final class DependencyResolverImpl implements DependencyResolver, } private static int getMaxDepth(DependencyResolverImpl impl, - DependencyResolverManager manager, - LinkedHashSet chainForDetectingCycles) { + DependencyResolverManager manager, + LinkedHashSet chainForDetectingCycles) { int maxDepth = 0; LinkedHashSet chainForDetectingCycles2 = new LinkedHashSet<>( chainForDetectingCycles); @@ -198,7 +250,7 @@ final class DependencyResolverImpl implements DependencyResolver, .getOrCreate(dependencyName); if (chainForDetectingCycles2.contains(dependencyName)) { throw new IllegalStateException(format( - "Cycle detected, {} contains {}", + "Cycle detected, %s contains %s", chainForDetectingCycles2, dependencyName)); } int subDepth; @@ -221,4 +273,21 @@ final class DependencyResolverImpl implements DependencyResolver, public ModuleIdentifier getIdentifier() { return name; } + + @Override + public Object getAttribute(ObjectName name, String attribute) + throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException { + name = translateServiceRefIfPossible(name); + // add transaction name + name = ObjectNameUtil.withTransactionName(name, transactionName); + return mBeanServer.getAttribute(name, attribute); + } + + @Override + public T newMXBeanProxy(ObjectName name, Class interfaceClass) { + name = translateServiceRefIfPossible(name); + // add transaction name + name = ObjectNameUtil.withTransactionName(name, transactionName); + return JMX.newMXBeanProxy(mBeanServer, name, interfaceClass); + } }