X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2FRuntimeBeanEntry.java;h=23b071c817022ac0103f7cc01c4cc5f5482ba995;hp=d07e20bebba013c82e32ab0ba1b3c988550f0ecb;hb=d5702150e612565cd091f184064f017bc4cc3971;hpb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9 diff --git a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntry.java b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntry.java index d07e20bebb..23b071c817 100644 --- a/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntry.java +++ b/opendaylight/config/yang-jmx-generator/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/RuntimeBeanEntry.java @@ -7,22 +7,46 @@ */ package org.opendaylight.controller.config.yangjmxgenerator; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc; import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute; import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute; +import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute; import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.FullyQualifiedNameHelper; import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.NameConflictException; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.*; - -import java.util.*; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkState; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.RpcDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.api.UsesNode; /** * Holds information about runtime bean to be generated. There are two kinds of @@ -43,11 +67,11 @@ public class RuntimeBeanEntry { private final Set rpcs; @VisibleForTesting - public RuntimeBeanEntry(String packageName, - DataSchemaNode nodeForReporting, String yangName, - String javaNamePrefix, boolean isRoot, - Optional keyYangName, List attributes, - List children, Set rpcs) { + RuntimeBeanEntry(final String packageName, + final DataNodeContainer nodeForReporting, final String yangName, + final String javaNamePrefix, final boolean isRoot, + final Optional keyYangName, final List attributes, + final List children, final Set rpcs) { checkArgument(isRoot == false || keyYangName.isPresent() == false, "Root RuntimeBeanEntry must not have key " + "set"); @@ -64,7 +88,7 @@ public class RuntimeBeanEntry { for (AttributeIfc a : attributes) { checkState(map.containsKey(a.getAttributeYangName()) == false, "Attribute already defined: " + a.getAttributeYangName() - + " in " + nodeForReporting); + + " in " + nodeForReporting); map.put(a.getAttributeYangName(), a); } @@ -88,9 +112,9 @@ public class RuntimeBeanEntry { * not contain special configuration for it. */ public static Map extractClassNameToRuntimeBeanMap( - String packageName, ChoiceCaseNode container, - String moduleYangName, TypeProviderWrapper typeProviderWrapper, - String javaNamePrefix, Module currentModule) { + final String packageName, final DataNodeContainer container, + final String moduleYangName, final TypeProviderWrapper typeProviderWrapper, + final String javaNamePrefix, final Module currentModule) { Map> identitiesToRpcs = getIdentitiesToRpcs(currentModule); @@ -129,7 +153,7 @@ public class RuntimeBeanEntry { } private static Map> getIdentitiesToRpcs( - Module currentModule) { + final Module currentModule) { // currently only looks for local identities (found in currentModule) Map> result = new HashMap<>(); for (IdentitySchemaNode identity : currentModule.getIdentities()) { @@ -139,41 +163,44 @@ public class RuntimeBeanEntry { for (RpcDefinition rpc : currentModule.getRpcs()) { ContainerSchemaNode input = rpc.getInput(); - for (UsesNode uses : input.getUses()) { - - if (uses.getGroupingPath().getPath().size() != 1) - continue; - - // check grouping path - QName qname = uses.getGroupingPath().getPath().get(0); - if (false == qname - .equals(ConfigConstants.RPC_CONTEXT_REF_GROUPING_QNAME)) - continue; - - for (SchemaNode refinedNode : uses.getRefines().values()) { - - for (UnknownSchemaNode unknownSchemaNode : refinedNode - .getUnknownSchemaNodes()) { - if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME - .equals(unknownSchemaNode.getNodeType())) { - String localIdentityName = unknownSchemaNode - .getNodeParameter(); - QName identityQName = new QName( - currentModule.getNamespace(), - currentModule.getRevision(), - localIdentityName); - Set rpcDefinitions = result - .get(identityQName); - if (rpcDefinitions == null) { - throw new IllegalArgumentException( - "Identity referenced by rpc not found. Identity:" - + localIdentityName + " , rpc " - + rpc); + if (input != null) { + for (UsesNode uses : input.getUses()) { + + if (uses.getGroupingPath().getPath().size() != 1) { + continue; + } + + // check grouping path + QName qname = uses.getGroupingPath().getPath().get(0); + if (false == qname + .equals(ConfigConstants.RPC_CONTEXT_REF_GROUPING_QNAME)) { + continue; + } + + for (SchemaNode refinedNode : uses.getRefines().values()) { + + for (UnknownSchemaNode unknownSchemaNode : refinedNode + .getUnknownSchemaNodes()) { + if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME + .equals(unknownSchemaNode.getNodeType())) { + String localIdentityName = unknownSchemaNode + .getNodeParameter(); + QName identityQName = QName.create( + currentModule.getNamespace(), + currentModule.getRevision(), + localIdentityName); + Set rpcDefinitions = result + .get(identityQName); + if (rpcDefinitions == null) { + throw new IllegalArgumentException( + "Identity referenced by rpc not found. Identity:" + + localIdentityName + " , rpc " + + rpc); + } + rpcDefinitions.add(rpc); } - rpcDefinitions.add(rpc); } } - } } } @@ -185,9 +212,9 @@ public class RuntimeBeanEntry { * in subtree. */ private static AttributesRpcsAndRuntimeBeans extractSubtree( - String packageName, DataNodeContainer subtree, - TypeProviderWrapper typeProviderWrapper, Module currentModule, - Map> identitiesToRpcs) { + final String packageName, final DataNodeContainer subtree, + final TypeProviderWrapper typeProviderWrapper, final Module currentModule, + final Map> identitiesToRpcs) { List attributes = Lists.newArrayList(); // List javaAttributes = new ArrayList<>(); @@ -204,7 +231,7 @@ public class RuntimeBeanEntry { ContainerSchemaNode container = (ContainerSchemaNode) child; // this can be either TO or hierarchical RB TOAttribute toAttribute = TOAttribute.create(container, - typeProviderWrapper); + typeProviderWrapper, packageName); attributes.add(toAttribute); } else if (child instanceof ListSchemaNode) { if (isInnerStateBean(child)) { @@ -215,13 +242,17 @@ public class RuntimeBeanEntry { runtimeBeanEntries.add(hierarchicalChild); } else /* ordinary list attribute */{ ListAttribute listAttribute = ListAttribute.create( - (ListSchemaNode) child, typeProviderWrapper); + (ListSchemaNode) child, typeProviderWrapper, packageName); attributes.add(listAttribute); } + } else if (child instanceof LeafListSchemaNode) { + ListAttribute listAttribute = ListAttribute.create( + (LeafListSchemaNode) child, typeProviderWrapper); + attributes.add(listAttribute); } else { - throw new IllegalStateException("Unknown running-data node " - + child + " , " + "" + "expected leaf or container"); + throw new IllegalStateException("Unexpected running-data node " + + child); } } Set rpcs = new HashSet<>(); @@ -231,7 +262,7 @@ public class RuntimeBeanEntry { if (ConfigConstants.RPC_CONTEXT_INSTANCE_EXTENSION_QNAME .equals(unknownSchemaNode.getNodeType())) { String localIdentityName = unknownSchemaNode.getNodeParameter(); - QName identityQName = new QName(currentModule.getNamespace(), + QName identityQName = QName.create(currentModule.getNamespace(), currentModule.getRevision(), localIdentityName); Set rpcDefinitions = identitiesToRpcs .get(identityQName); @@ -243,31 +274,17 @@ public class RuntimeBeanEntry { } // convert RpcDefinition to Rpc for (RpcDefinition rpcDefinition : rpcDefinitions) { - String name = ModuleMXBeanEntry + String name = TypeProviderWrapper .findJavaParameter(rpcDefinition); - String returnType; + AttributeIfc returnType; if (rpcDefinition.getOutput() == null || rpcDefinition.getOutput().getChildNodes().size() == 0) { - returnType = "void"; + returnType = VoidAttribute.getInstance(); } else if (rpcDefinition.getOutput().getChildNodes().size() == 1) { DataSchemaNode returnDSN = rpcDefinition.getOutput() .getChildNodes().iterator().next(); - checkArgument( - returnDSN instanceof LeafSchemaNode, - "Unexpected type of rpc return type. " - + "Currently only leafs and empty output nodes are supported, got " - + returnDSN); - LeafSchemaNode returnLeaf = (LeafSchemaNode) returnDSN; - // We currently expect leaf defined in output element in yang to be named result - // FIXME: value of result is fully qualified name - should be extended to accept TOs - String localName = returnLeaf.getQName().getLocalName(); - checkArgument( - localName.equals("result"), - "Unexpected name of leaf in output element, expected leaf named result, was %s at %s", - localName, currentModule.getName()); - - returnType = typeProviderWrapper.getType(returnLeaf) - .getFullyQualifiedName(); + returnType = getReturnTypeAttribute(returnDSN, typeProviderWrapper, packageName); + } else { throw new IllegalArgumentException( "More than one child node in rpc output is not supported. " @@ -277,8 +294,8 @@ public class RuntimeBeanEntry { for (DataSchemaNode childNode : sortAttributes(rpcDefinition.getInput() .getChildNodes())) { if (childNode.isAddedByUses() == false) { // skip - // refined - // context-instance + // refined + // context-instance checkArgument(childNode instanceof LeafSchemaNode, "Unexpected type of rpc input type. " + "Currently only leafs and empty output nodes are supported, got " + childNode); JavaAttribute javaAttribute = new JavaAttribute( @@ -297,10 +314,28 @@ public class RuntimeBeanEntry { attributes, rpcs); } - private static Collection sortAttributes(Set childNodes) { + private static AttributeIfc getReturnTypeAttribute(final DataSchemaNode child, final TypeProviderWrapper typeProviderWrapper, + final String packageName) { + if (child instanceof LeafSchemaNode) { + LeafSchemaNode leaf = (LeafSchemaNode) child; + return new JavaAttribute(leaf, typeProviderWrapper); + } else if (child instanceof ContainerSchemaNode) { + ContainerSchemaNode container = (ContainerSchemaNode) child; + TOAttribute toAttribute = TOAttribute.create(container, typeProviderWrapper, packageName); + return toAttribute; + } else if (child instanceof ListSchemaNode) { + return ListAttribute.create((ListSchemaNode) child, typeProviderWrapper, packageName); + } else if (child instanceof LeafListSchemaNode) { + return ListAttribute.create((LeafListSchemaNode) child, typeProviderWrapper); + } else { + throw new IllegalStateException("Unknown output data node " + child + " for rpc"); + } + } + + private static Collection sortAttributes(final Set childNodes) { final TreeSet dataSchemaNodes = new TreeSet<>(new Comparator() { @Override - public int compare(DataSchemaNode o1, DataSchemaNode o2) { + public int compare(final DataSchemaNode o1, final DataSchemaNode o2) { return o1.getQName().getLocalName().compareTo(o2.getQName().getLocalName()); } }); @@ -308,20 +343,21 @@ public class RuntimeBeanEntry { return dataSchemaNodes; } - private static boolean isInnerStateBean(DataSchemaNode child) { + private static boolean isInnerStateBean(final DataSchemaNode child) { for (UnknownSchemaNode unknownSchemaNode : child .getUnknownSchemaNodes()) { if (unknownSchemaNode.getNodeType().equals( - ConfigConstants.INNER_STATE_BEAN_EXTENSION_QNAME)) + ConfigConstants.INNER_STATE_BEAN_EXTENSION_QNAME)) { return true; + } } return false; } - private static RuntimeBeanEntry createHierarchical(String packageName, - ListSchemaNode listSchemaNode, - TypeProviderWrapper typeProviderWrapper, Module currentModule, - Map> identitiesToRpcs) { + private static RuntimeBeanEntry createHierarchical(final String packageName, + final ListSchemaNode listSchemaNode, + final TypeProviderWrapper typeProviderWrapper, final Module currentModule, + final Map> identitiesToRpcs) { // supported are numeric types, strings, enums // get all attributes @@ -343,7 +379,7 @@ public class RuntimeBeanEntry { "More than one key is not supported in " + listSchemaNode); } - String javaNamePrefix = ModuleMXBeanEntry + String javaNamePrefix = TypeProviderWrapper .findJavaNamePrefix(listSchemaNode); RuntimeBeanEntry rbFromAttributes = new RuntimeBeanEntry(packageName, @@ -356,10 +392,10 @@ public class RuntimeBeanEntry { return rbFromAttributes; } - private static RuntimeBeanEntry createRoot(String packageName, - DataSchemaNode nodeForReporting, String attributeYangName, - List attributes, String javaNamePrefix, - List children, Set rpcs) { + private static RuntimeBeanEntry createRoot(final String packageName, + final DataNodeContainer nodeForReporting, final String attributeYangName, + final List attributes, final String javaNamePrefix, + final List children, final Set rpcs) { return new RuntimeBeanEntry(packageName, nodeForReporting, attributeYangName, javaNamePrefix, true, Optional. absent(), attributes, children, rpcs); @@ -411,8 +447,8 @@ public class RuntimeBeanEntry { private final Set rpcs; public AttributesRpcsAndRuntimeBeans( - List runtimeBeanEntries, - List attributes, Set rpcs) { + final List runtimeBeanEntries, + final List attributes, final Set rpcs) { this.runtimeBeanEntries = runtimeBeanEntries; this.attributes = attributes; this.rpcs = rpcs; @@ -438,11 +474,11 @@ public class RuntimeBeanEntry { public static class Rpc { private final String name; private final List parameters; - private final String returnType; + private final AttributeIfc returnType; private final String yangName; - Rpc(String returnType, String name, String yangName, - List parameters) { + Rpc(final AttributeIfc returnType, final String name, final String yangName, + final List parameters) { this.returnType = returnType; this.name = name; this.parameters = parameters; @@ -461,7 +497,7 @@ public class RuntimeBeanEntry { return parameters; } - public String getReturnType() { + public AttributeIfc getReturnType() { return returnType; } } @@ -472,12 +508,12 @@ public class RuntimeBeanEntry { return getJavaNameOfRuntimeMXBean(javaNamePrefix); } - public String getFullyQualifiedName(String typeName) { + public String getFullyQualifiedName(final String typeName) { return FullyQualifiedNameHelper.getFullyQualifiedName(packageName, typeName); } - private static String getJavaNameOfRuntimeMXBean(String javaNamePrefix) { + private static String getJavaNameOfRuntimeMXBean(final String javaNamePrefix) { return javaNamePrefix + MXBEAN_SUFFIX; }