From 5feac31a11a337a0c840f73c5c4612a6c997fa2a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 26 Jul 2014 04:17:52 +0200 Subject: [PATCH] Degrade DataNodeContainer.getChildNodes() from Set to Collection Datastore-geared workload shows that around 1% of CPU is being used by AbstractDocumentedDataNodeContainer.getChildNodes(). Of that, around 72% is spent in ImmutableSet.copyOf(). We could perform that copy in the constructor, but we can actually do better. As it turns out, there is already a childNodes map, which is immutable and indexed via QName present. That indexing guarantees that the values compare as distinct, so we can reuse childNodes.values(), except for the contract. None of the callers care if the return is a Collection, so downgrade the return, such that we can elide copying the stuff completely. Also fixup all the callers. Requires https://git.opendaylight.org/gerrit/#/c/9340/ to fix controller-based users. Change-Id: I310b351d80955a37f6672064dd238bb9cb1e16b1 Signed-off-by: Robert Varga --- .../generator/impl/BindingGeneratorImpl.java | 24 +++---- .../impl/LazyGeneratedCodecRegistry.java | 7 +- .../GroupingDefinitionDependencySort.java | 11 ++-- .../unified/doc/generator/GeneratorImpl.xtend | 8 +-- .../tests/MultipleRevisionsSupportTest.java | 29 ++++----- .../data/impl/codec/xml/XmlDocumentUtils.java | 30 ++++----- .../yang/data/impl/schema/SchemaUtils.java | 15 ++--- .../schema/NormalizedDataBuilderTest.java | 65 ++++++++++--------- .../yang/model/api/DataNodeContainer.java | 5 +- .../yang/model/util/DataNodeIterator.java | 5 +- .../parser/builder/impl/BuilderUtils.java | 5 +- .../yang/parser/impl/AugmentTest.java | 18 ++--- .../yang/parser/impl/GroupingTest.java | 7 +- .../yang/parser/impl/OrderingTest.java | 11 ++-- .../yang/parser/impl/UsesAugmentTest.java | 5 +- 15 files changed, 127 insertions(+), 118 deletions(-) diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index 15fa0902d0..d448db7659 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -30,6 +30,7 @@ import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findP import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -38,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; + import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil; import org.opendaylight.yangtools.binding.generator.util.BindingTypes; import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl; @@ -525,8 +527,8 @@ public class BindingGeneratorImpl implements BindingGenerator { notification.getChildNodes()); listenerInterface.addMethod("on" + notificationInterface.getName()) - .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification") - .setComment(notification.getDescription()).setReturnType(Types.VOID); + .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification") + .setComment(notification.getDescription()).setReturnType(Types.VOID); } } @@ -594,7 +596,7 @@ public class BindingGeneratorImpl implements BindingGenerator { .getQNameModule()); final String returnTypeName = BindingMapping.getClassName(baseIdentity.getQName()); final GeneratedTransferObject gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName) - .toInstance(); + .toInstance(); newType.setExtendsType(gto); } newType.setAbstract(true); @@ -641,7 +643,7 @@ public class BindingGeneratorImpl implements BindingGenerator { private void groupingsToGenTypes(final Module module, final Collection groupings) { final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule()); final List groupingsSortedByDependencies = new GroupingDefinitionDependencySort() - .sort(groupings); + .sort(groupings); for (GroupingDefinition grouping : groupingsSortedByDependencies) { groupingToGenType(basePackageName, grouping, module); } @@ -996,8 +998,8 @@ public class BindingGeneratorImpl implements BindingGenerator { * added to it. */ private GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName, - final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Set schemaNodes) { - if ((schemaNodes != null) && (parent != null)) { + final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Iterable schemaNodes) { + if (schemaNodes != null && parent != null) { for (DataSchemaNode schemaNode : schemaNodes) { if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) { addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module); @@ -1031,7 +1033,7 @@ public class BindingGeneratorImpl implements BindingGenerator { */ private GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName, final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, - final Set schemaNodes) { + final Iterable schemaNodes) { if ((schemaNodes != null) && (typeBuilder != null)) { for (DataSchemaNode schemaNode : schemaNodes) { if (!schemaNode.isAugmenting()) { @@ -1164,7 +1166,7 @@ public class BindingGeneratorImpl implements BindingGenerator { caseTypeBuilder.addImplementsType(refChoiceType); genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder); genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode); - final Set caseChildNodes = caseNode.getChildNodes(); + final Iterable caseChildNodes = caseNode.getChildNodes(); if (caseChildNodes != null) { Object parentNode = null; final SchemaPath nodeSp = choiceNode.getPath(); @@ -1183,7 +1185,7 @@ public class BindingGeneratorImpl implements BindingGenerator { if (targetSchemaNode == null) { throw new IllegalStateException( "Failed to find target node from grouping for augmentation " + augSchema - + " in module " + module.getName()); + + " in module " + module.getName()); } } parent = targetSchemaNode; @@ -1231,7 +1233,7 @@ public class BindingGeneratorImpl implements BindingGenerator { * */ private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName, - final Type targetType, final ChoiceNode targetNode, final Set augmentedNodes) { + final Type targetType, final ChoiceNode targetNode, final Iterable augmentedNodes) { checkArgument(basePackageName != null, "Base Package Name cannot be NULL."); checkArgument(targetType != null, "Referenced Choice Type cannot be NULL."); checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL."); @@ -1267,7 +1269,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } else { node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName()); } - final Set childNodes = node.getChildNodes(); + final Iterable childNodes = node.getChildNodes(); if (childNodes != null) { resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes); } diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java index e87683db07..86f0cdf881 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/LazyGeneratedCodecRegistry.java @@ -311,8 +311,7 @@ class LazyGeneratedCodecRegistry implements CodecRegistry, SchemaContextListener } private DataSchemaNode searchInChoices(final DataNodeContainer node, final QName arg) { - Set children = node.getChildNodes(); - for (DataSchemaNode child : children) { + for (DataSchemaNode child : node.getChildNodes()) { if (child instanceof ChoiceNode) { ChoiceNode choiceNode = (ChoiceNode) child; DataSchemaNode potential = searchInCases(choiceNode, arg); @@ -425,8 +424,8 @@ class LazyGeneratedCodecRegistry implements CodecRegistry, SchemaContextListener for (Map.Entry entry : bimap.entrySet()) { Type key = entry.getKey(); AugmentationSchema value = entry.getValue(); - Set augmentedNodes = value.getChildNodes(); - if (augmentedNodes != null && !(augmentedNodes.isEmpty())) { + Collection augmentedNodes = value.getChildNodes(); + if (augmentedNodes != null && !augmentedNodes.isEmpty()) { typeToAugment.put(key, value); } } diff --git a/code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/GroupingDefinitionDependencySort.java b/code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/GroupingDefinitionDependencySort.java index 1b6ffd9822..b697bf9912 100644 --- a/code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/GroupingDefinitionDependencySort.java +++ b/code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/GroupingDefinitionDependencySort.java @@ -7,6 +7,9 @@ */ package org.opendaylight.yangtools.sal.binding.yang.types; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -25,9 +28,6 @@ import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.opendaylight.yangtools.yang.parser.util.TopologicalSort; import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - public class GroupingDefinitionDependencySort { /** @@ -132,7 +132,7 @@ public class GroupingDefinitionDependencySort { * data node container which can contain some uses of grouping * @return set of uses nodes which were find in container. */ - private Set getAllUsesNodes(DataNodeContainer container) { + private Set getAllUsesNodes(final DataNodeContainer container) { Set ret = new HashSet<>(); Set usesNodes = container.getUses(); ret.addAll(usesNodes); @@ -146,8 +146,7 @@ public class GroupingDefinitionDependencySort { for (GroupingDefinition groupingDefinition : groupings) { ret.addAll(getAllUsesNodes(groupingDefinition)); } - Set childNodes = container.getChildNodes(); - for (DataSchemaNode childNode : childNodes) { + for (DataSchemaNode childNode : container.getChildNodes()) { if (childNode instanceof DataNodeContainer) { ret.addAll(getAllUsesNodes((DataNodeContainer) childNode)); } else if (childNode instanceof ChoiceNode) { diff --git a/code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend b/code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend index 8acc2ad111..bc4e267b0c 100644 --- a/code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend +++ b/code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend @@ -367,7 +367,7 @@ class GeneratorImpl { return targetPathNodes } - private def DataSchemaNode findNodeInChildNodes(QName findingNode, Set childNodes) { + private def DataSchemaNode findNodeInChildNodes(QName findingNode, Iterable childNodes) { for(child : childNodes) { if (child.QName.equals(findingNode)) return child; @@ -950,7 +950,7 @@ class GeneratorImpl { ''' } - def CharSequence printChildren(Set nodes, int level, InstanceIdentifier path) { + def CharSequence printChildren(Iterable nodes, int level, InstanceIdentifier path) { val anyxmlNodes = nodes.filter(AnyXmlSchemaNode) val leafNodes = nodes.filter(LeafSchemaNode) val leafListNodes = nodes.filter(LeafListSchemaNode) @@ -1000,13 +1000,13 @@ class GeneratorImpl { ''' } - def CharSequence xmlExample(Set nodes, QName name,InstanceIdentifier path) ''' + def CharSequence xmlExample(Iterable nodes, QName name,InstanceIdentifier path) '''
         «xmlExampleTag(name,nodes.xmplExampleTags(path))»
     
''' - def CharSequence xmplExampleTags(Set nodes, InstanceIdentifier identifier) ''' + def CharSequence xmplExampleTags(Iterable nodes, InstanceIdentifier identifier) ''' «FOR node : nodes» diff --git a/integration-test/yang-runtime-tests/src/test/java/org/opendaylight/yangtools/it/yang/runtime/tests/MultipleRevisionsSupportTest.java b/integration-test/yang-runtime-tests/src/test/java/org/opendaylight/yangtools/it/yang/runtime/tests/MultipleRevisionsSupportTest.java index 16f408c243..d75c5657bf 100644 --- a/integration-test/yang-runtime-tests/src/test/java/org/opendaylight/yangtools/it/yang/runtime/tests/MultipleRevisionsSupportTest.java +++ b/integration-test/yang-runtime-tests/src/test/java/org/opendaylight/yangtools/it/yang/runtime/tests/MultipleRevisionsSupportTest.java @@ -11,6 +11,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.common.collect.ImmutableSet; + import java.io.InputStream; import java.net.URI; import java.util.List; @@ -30,10 +34,6 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; -import com.google.common.collect.ImmutableSet; - public class MultipleRevisionsSupportTest { public static final YangModuleInfo TOPOLOGY_OLD_MODULE = org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev130712.$YangModuleInfoImpl @@ -117,7 +117,7 @@ public class MultipleRevisionsSupportTest { verifyBindingDifference(schemaContext, TOPOLOGY_OLD_MODULE, TOPOLOGY_NEW_MODULE); } - private SchemaContext contextVerified(List streams) { + private SchemaContext contextVerified(final List streams) { YangParserImpl parser = new YangParserImpl(); Set modules = parser.parseYangModelsFromStreams(streams); assertNotNull(modules); @@ -127,19 +127,19 @@ public class MultipleRevisionsSupportTest { return context; } - private void verifyBindingDifference(SchemaContext schemaContext, YangModuleInfo oldModule, YangModuleInfo newModule) { + private void verifyBindingDifference(final SchemaContext schemaContext, final YangModuleInfo oldModule, final YangModuleInfo newModule) { generatedTypesVerified(schemaContext, oldModule, newModule); } - private Map generatedTypesVerified(SchemaContext schemaContext, YangModuleInfo oldModule, - YangModuleInfo newModule) { + private Map generatedTypesVerified(final SchemaContext schemaContext, final YangModuleInfo oldModule, + final YangModuleInfo newModule) { BindingGeneratorImpl generator = new BindingGeneratorImpl(); generator.generateTypes(schemaContext); return generator.getModuleContexts(); } - private void verifySchemaDifference(SchemaContext context, YangModuleInfo topologyOldModule, - YangModuleInfo topologyNewModule) { + private void verifySchemaDifference(final SchemaContext context, final YangModuleInfo topologyOldModule, + final YangModuleInfo topologyNewModule) { Module oldModel = context.findModuleByNamespaceAndRevision(// URI.create(TOPOLOGY_OLD_MODULE.getNamespace()), QName.parseRevision(TOPOLOGY_OLD_MODULE.getRevision())); @@ -156,7 +156,7 @@ public class MultipleRevisionsSupportTest { assertDeepRevision(TOPOLOGY_NEW_MODULE.getRevision(), newNode); } - private static void assertDeepRevision(String revision, SchemaNode node) { + private static void assertDeepRevision(final String revision, final SchemaNode node) { assertEquals("Wrong revision: " + node.getPath(), revision, node.getQName().getFormattedRevision()); if (node instanceof DataNodeContainer) { for (DataSchemaNode child : ((DataNodeContainer) node).getChildNodes()) { @@ -169,7 +169,7 @@ public class MultipleRevisionsSupportTest { } } - private static final SchemaNode findSchemaNode(DataNodeContainer container, String... pathArgs) { + private static final SchemaNode findSchemaNode(final DataNodeContainer container, final String... pathArgs) { DataNodeContainer previous = container; SchemaNode result = (container instanceof SchemaNode) ? (SchemaNode) container : null; @@ -177,8 +177,7 @@ public class MultipleRevisionsSupportTest { if (previous == null) { return null; } - Set childs = previous.getChildNodes(); - for (DataSchemaNode child : childs) { + for (DataSchemaNode child : previous.getChildNodes()) { if (child.getQName().getLocalName().equals(arg)) { if (child instanceof DataNodeContainer) { previous = (DataNodeContainer) child; @@ -193,7 +192,7 @@ public class MultipleRevisionsSupportTest { return result; } - private static final Iterable toInputStreams(Set moduleInfos) + private static final Iterable toInputStreams(final Set moduleInfos) throws Exception { Builder streams = ImmutableList. builder(); for (YangModuleInfo yangModuleInfo : moduleInfos) { diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java index 2da30e54bd..262a48a933 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java @@ -9,6 +9,13 @@ package org.opendaylight.yangtools.yang.data.impl.codec.xml; import static com.google.common.base.Preconditions.checkState; +import com.google.common.base.Function; +import com.google.common.base.Objects; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; + import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -54,13 +61,6 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; -import com.google.common.base.Function; -import com.google.common.base.Objects; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; - public class XmlDocumentUtils { private static class ElementWithSchemaContext { Element element; @@ -278,8 +278,8 @@ public class XmlDocumentUtils { checkState(qName.getLocalName().equals(xmlElement.getLocalName())); } - public static final Optional findFirstSchema(final QName qname, final Set dataSchemaNode) { - if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) { + public static final Optional findFirstSchema(final QName qname, final Iterable dataSchemaNode) { + if (dataSchemaNode != null && qname != null) { for (DataSchemaNode dsn : dataSchemaNode) { if (qname.isEqualWithoutRevision(dsn.getQName())) { return Optional. of(dsn); @@ -326,7 +326,7 @@ public class XmlDocumentUtils { return ImmutableCompositeNode.create(qname, values.build()); } - public static List> toDomNodes(final Element element, final Optional> context,final SchemaContext schemaCtx) { + public static List> toDomNodes(final Element element, final Optional> context, final SchemaContext schemaCtx) { return forEachChild(element.getChildNodes(),schemaCtx, new Function>>() { @Override @@ -335,7 +335,7 @@ public class XmlDocumentUtils { QName partialQName = qNameFromElement(input.getElement()); Optional schemaNode = findFirstSchema(partialQName, context.get()); if (schemaNode.isPresent()) { - return Optional.> fromNullable(// + return Optional.> fromNullable( toNodeWithSchema(input.getElement(), schemaNode.get(), XmlUtils.DEFAULT_XML_CODEC_PROVIDER, input.getSchemaContext())); } } @@ -346,8 +346,8 @@ public class XmlDocumentUtils { } - public static List> toDomNodes(final Element element, final Optional> context) { - return toDomNodes(element,context,null); + public static List> toDomNodes(final Element element, final Optional> context) { + return toDomNodes(element, context, null); } /** @@ -383,9 +383,9 @@ public class XmlDocumentUtils { final Optional notificationDef = findNotification(partialQName, notifications.get()); if (notificationDef.isPresent()) { - final Set dataNodes = notificationDef.get().getChildNodes(); + final Iterable dataNodes = notificationDef.get().getChildNodes(); final List> domNodes = toDomNodes(childElement, - Optional.> fromNullable(dataNodes),schemaCtx); + Optional.> fromNullable(dataNodes),schemaCtx); return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes); } } diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java index fe5892a3f6..bafeeaeb8a 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaUtils.java @@ -36,8 +36,8 @@ public final class SchemaUtils { private SchemaUtils() { } - public static final Optional findFirstSchema(final QName qname, final Set dataSchemaNode) { - if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) { + public static final Optional findFirstSchema(final QName qname, final Iterable dataSchemaNode) { + if (dataSchemaNode != null && qname != null) { for (DataSchemaNode dsn : dataSchemaNode) { if (qname.isEqualWithoutRevision(dsn.getQName())) { return Optional. of(dsn); @@ -55,11 +55,10 @@ public final class SchemaUtils { } public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) { - Set childNodes = schema.getChildNodes(); - return findSchemaForChild(schema, qname, childNodes); + return findSchemaForChild(schema, qname, schema.getChildNodes()); } - public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Set childNodes) { + public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable childNodes) { Optional childSchema = findFirstSchema(qname, childNodes); Preconditions.checkState(childSchema.isPresent(), "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema); @@ -125,12 +124,10 @@ public final class SchemaUtils { * @return Map with all child nodes, to their most top augmentation */ public static Map mapChildElementsFromChoices(final DataNodeContainer schema) { - Set childNodes = schema.getChildNodes(); - - return mapChildElementsFromChoices(schema, childNodes); + return mapChildElementsFromChoices(schema, schema.getChildNodes()); } - private static Map mapChildElementsFromChoices(final DataNodeContainer schema, final Set childNodes) { + private static Map mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable childNodes) { Map mappedChoices = Maps.newLinkedHashMap(); for (final DataSchemaNode childSchema : childNodes) { diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java index 6fcc0f345d..d15c6339c7 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedDataBuilderTest.java @@ -7,6 +7,12 @@ */ package org.opendaylight.yangtools.yang.data.impl.schema; +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + import java.io.InputStream; import java.net.URI; import java.util.Collections; @@ -38,34 +44,28 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; - public class NormalizedDataBuilderTest { private ContainerSchemaNode containerNode; private SchemaContext schema; - SchemaContext parseTestSchema(String... yangPath) { + SchemaContext parseTestSchema(final String... yangPath) { YangParserImpl yangParserImpl = new YangParserImpl(); Set modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath)); return yangParserImpl.resolveSchemaContext(modules); } - List getTestYangs(String... yangPaths) { + List getTestYangs(final String... yangPaths) { return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths), new Function() { - @Override - public InputStream apply(String input) { - InputStream resourceAsStream = getClass().getResourceAsStream(input); - Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream); - return resourceAsStream; - } - })); + @Override + public InputStream apply(final String input) { + InputStream resourceAsStream = getClass().getResourceAsStream(input); + Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream); + return resourceAsStream; + } + })); } @Before @@ -92,8 +92,8 @@ public class NormalizedDataBuilderTest { .withChildValue(1) .withChild( Builders. leafSetEntryBuilder() - .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build()) - .build(); + .withNodeIdentifier(getNodeWithValueIdentifier("leaf", 3)).withValue(3).build()) + .build(); builder.withChild(leafList); // list @@ -101,12 +101,12 @@ public class NormalizedDataBuilderTest { .mapEntryBuilder() .withChild( Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList")) - .withValue(1).build()) - .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build()) - .withNodeIdentifier( - new InstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(), - Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1))) - .build(); + .withValue(1).build()) + .withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("containerInList")).build()) + .withNodeIdentifier( + new InstanceIdentifier.NodeIdentifierWithPredicates(getNodeIdentifier("list").getNodeType(), + Collections.singletonMap(getNodeIdentifier("uint32InList").getNodeType(), (Object) 1))) + .build(); MapNode list = Builders.mapBuilder().withChild(listChild1).withNodeIdentifier(getNodeIdentifier("list")) .build(); @@ -116,8 +116,8 @@ public class NormalizedDataBuilderTest { .augmentationBuilder() .withNodeIdentifier( new InstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(getQName("augmentUint32")))) - .withChild( - Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) + .withChild( + Builders. leafBuilder().withNodeIdentifier(getNodeIdentifier("augmentUint32")) .withValue(11).build()).build(); builder.withChild(augmentation); @@ -186,7 +186,7 @@ public class NormalizedDataBuilderTest { // .build()); } - private AugmentationSchema getAugmentationSchemaForChild(ContainerSchemaNode containerNode, QName qName) { + private AugmentationSchema getAugmentationSchemaForChild(final ContainerSchemaNode containerNode, final QName qName) { for (AugmentationSchema augmentationSchema : containerNode.getAvailableAugmentations()) { if (augmentationSchema.getDataChildByName(qName) != null) { return augmentationSchema; @@ -195,20 +195,20 @@ public class NormalizedDataBuilderTest { throw new IllegalStateException("Unable to find child augmentation in " + containerNode); } - private InstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(String localName, Object value) { + private InstanceIdentifier.NodeWithValue getNodeWithValueIdentifier(final String localName, final Object value) { return new InstanceIdentifier.NodeWithValue(getQName(localName), value); } - private QName getQName(String localName) { + private QName getQName(final String localName) { String namespace = "namespace"; return new QName(URI.create(namespace), localName); } - private InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) { + private InstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) { return new InstanceIdentifier.NodeIdentifier(getQName(localName)); } - public static DataSchemaNode getSchemaNode(SchemaContext context, String moduleName, String childNodeName) { + public static DataSchemaNode getSchemaNode(final SchemaContext context, final String moduleName, final String childNodeName) { for (Module module : context.getModules()) { if (module.getName().equals(moduleName)) { DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName); @@ -219,12 +219,13 @@ public class NormalizedDataBuilderTest { throw new IllegalStateException("Unable to find child node " + childNodeName); } - static DataSchemaNode findChildNode(Set children, String name) { + private static DataSchemaNode findChildNode(final Iterable children, final String name) { List containers = Lists.newArrayList(); for (DataSchemaNode dataSchemaNode : children) { - if (dataSchemaNode.getQName().getLocalName().equals(name)) + if (dataSchemaNode.getQName().getLocalName().equals(name)) { return dataSchemaNode; + } if (dataSchemaNode instanceof DataNodeContainer) { containers.add((DataNodeContainer) dataSchemaNode); } else if (dataSchemaNode instanceof ChoiceNode) { diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/DataNodeContainer.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/DataNodeContainer.java index 8b20b2b72f..8b15c979a2 100644 --- a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/DataNodeContainer.java +++ b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/DataNodeContainer.java @@ -7,6 +7,7 @@ */ package org.opendaylight.yangtools.yang.model.api; +import java.util.Collection; import java.util.Set; import org.opendaylight.yangtools.yang.common.QName; @@ -25,10 +26,12 @@ public interface DataNodeContainer { /** * Returns set of all child nodes defined within this DataNodeContainer. + * Although the return type is a collection, each node is guaranteed to + * be present at most once. * * @return child nodes in lexicographical order */ - Set getChildNodes(); + Collection getChildNodes(); /** * Returns set of all groupings defined within this DataNodeContainer. diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/DataNodeIterator.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/DataNodeIterator.java index 850b51de87..025c11c1e2 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/DataNodeIterator.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/DataNodeIterator.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.model.util; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -109,7 +110,7 @@ public class DataNodeIterator implements Iterator { return; } - final Set childNodes = dataNode.getChildNodes(); + final Iterable childNodes = dataNode.getChildNodes(); if (childNodes != null) { for (DataSchemaNode childNode : childNodes) { if (childNode.isAugmenting()) { @@ -183,7 +184,7 @@ public class DataNodeIterator implements Iterator { @Override public boolean hasNext() { if (container.getChildNodes() != null) { - final Set childNodes = container.getChildNodes(); + final Collection childNodes = container.getChildNodes(); if ((childNodes != null) && !childNodes.isEmpty()) { return childNodes.iterator().hasNext(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java index b227759c6e..2ce662647a 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/BuilderUtils.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; + import org.apache.commons.io.IOUtils; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; @@ -662,8 +663,8 @@ public final class BuilderUtils { } public static Set wrapChildNodes(final String moduleName, final int line, - final Set nodes, final SchemaPath parentPath, final QName parentQName) { - Set result = new LinkedHashSet<>(); + final Collection nodes, final SchemaPath parentPath, final QName parentQName) { + Set result = new LinkedHashSet<>(nodes.size()); for (DataSchemaNode node : nodes) { QName qname = QName.create(parentQName, node.getQName().getLocalName()); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java index 30c46efc75..b20c978087 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java @@ -19,10 +19,12 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Set; + import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -86,7 +88,7 @@ public class AugmentTest { expectedSchemaPath = SchemaPath.create(qnames, true); assertEquals(expectedSchemaPath, augment.getTargetPath()); - Set augmentChildren = augment.getChildNodes(); + Collection augmentChildren = augment.getChildNodes(); assertEquals(4, augmentChildren.size()); for (DataSchemaNode dsn : augmentChildren) { TestUtils.checkIsAugmenting(dsn, false); @@ -285,7 +287,7 @@ public class AugmentTest { qnames.add(qname); expectedPath = SchemaPath.create(qnames, true); assertEquals(expectedPath, id.getPath()); - Set idChildren = id.getChildNodes(); + Collection idChildren = id.getChildNodes(); assertEquals(1, idChildren.size()); // case node1 @@ -294,7 +296,7 @@ public class AugmentTest { qnames.set(4, qname); expectedPath = SchemaPath.create(qnames, true); assertEquals(expectedPath, node1.getPath()); - Set node1Children = node1.getChildNodes(); + Collection node1Children = node1.getChildNodes(); assertTrue(node1Children.isEmpty()); // case node2 @@ -303,7 +305,7 @@ public class AugmentTest { qnames.set(4, qname); expectedPath = SchemaPath.create(qnames, true); assertEquals(expectedPath, node2.getPath()); - Set node2Children = node2.getChildNodes(); + Collection node2Children = node2.getChildNodes(); assertTrue(node2Children.isEmpty()); // case node3 @@ -312,7 +314,7 @@ public class AugmentTest { qnames.set(4, qname); expectedPath = SchemaPath.create(qnames, true); assertEquals(expectedPath, node3.getPath()); - Set node3Children = node3.getChildNodes(); + Collection node3Children = node3.getChildNodes(); assertEquals(1, node3Children.size()); // test cases @@ -402,7 +404,7 @@ public class AugmentTest { assertEquals(qnames[3], attach.getQName()); expectedPath = SchemaPath.create(Arrays.asList(qnames), true); assertEquals(expectedPath, attach.getPath()); - Set attachChildren = attach.getChildNodes(); + Collection attachChildren = attach.getChildNodes(); assertEquals(1, attachChildren.size()); // case create @@ -410,7 +412,7 @@ public class AugmentTest { assertEquals(qnames[3], create.getQName()); expectedPath = SchemaPath.create(Arrays.asList(qnames), true); assertEquals(expectedPath, create.getPath()); - Set createChildren = create.getChildNodes(); + Collection createChildren = create.getChildNodes(); assertEquals(1, createChildren.size()); // case attach @@ -418,7 +420,7 @@ public class AugmentTest { assertEquals(qnames[3], destroy.getQName()); expectedPath = SchemaPath.create(Arrays.asList(qnames), true); assertEquals(expectedPath, destroy.getPath()); - Set destroyChildren = destroy.getChildNodes(); + Collection destroyChildren = destroy.getChildNodes(); assertEquals(1, destroyChildren.size()); } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java index 043c3c7f29..03dc0663de 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/GroupingTest.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; @@ -122,7 +123,7 @@ public class GroupingTest { Set groupings = testModule.getGroupings(); assertEquals(1, groupings.size()); GroupingDefinition grouping = groupings.iterator().next(); - Set children = grouping.getChildNodes(); + Collection children = grouping.getChildNodes(); assertEquals(5, children.size()); } @@ -352,7 +353,7 @@ public class GroupingTest { assertEquals(1, usesAugments.size()); AugmentationSchema augment = usesAugments.iterator().next(); assertEquals("inner augment", augment.getDescription()); - Set children = augment.getChildNodes(); + Collection children = augment.getChildNodes(); assertEquals(1, children.size()); DataSchemaNode leaf = children.iterator().next(); assertTrue(leaf instanceof LeafSchemaNode); @@ -411,7 +412,7 @@ public class GroupingTest { SchemaPath expectedPath; // grouping-U - Set childNodes = gu.getChildNodes(); + Collection childNodes = gu.getChildNodes(); assertEquals(7, childNodes.size()); LeafSchemaNode leafGroupingU = (LeafSchemaNode) gu.getDataChildByName("leaf-grouping-U"); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/OrderingTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/OrderingTest.java index dbdde51e26..0ea41a0744 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/OrderingTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/OrderingTest.java @@ -13,7 +13,9 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.net.URISyntaxException; +import java.util.Collection; import java.util.Set; + import org.junit.Test; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; @@ -71,7 +73,7 @@ public class OrderingTest { Set modules = TestUtils.loadModules(getClass().getResource("/model").toURI()); Module foo = TestUtils.findModule(modules, "foo"); - Set childNodes = foo.getChildNodes(); + Collection childNodes = foo.getChildNodes(); String[] expectedOrder = new String[] { "int32-leaf", "string-leaf", "length-leaf", "decimal-leaf", "decimal-leaf2", "ext", "union-leaf", "custom-union-leaf", "transfer", "datas", "mycont", "data", "how", "address", "port", "addresses", "peer" }; @@ -93,7 +95,7 @@ public class OrderingTest { assertEquals(1, groupings.size()); GroupingDefinition target = groupings.iterator().next(); - Set childNodes = target.getChildNodes(); + Collection childNodes = target.getChildNodes(); String[] expectedOrder = new String[] { "data", "how", "address", "port", "addresses" }; String[] actualOrder = new String[childNodes.size()]; @@ -109,10 +111,9 @@ public class OrderingTest { public void testOrderingNestedChildNodes3() throws Exception { Module baz = TestUtils.loadModule(getClass().getResourceAsStream("/ordering/foo.yang")); ContainerSchemaNode x = (ContainerSchemaNode) baz.getDataChildByName("x"); - Set childNodes = x.getChildNodes(); + Collection childNodes = x.getChildNodes(); - String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", - "ax5" }; + String[] expectedOrder = new String[] { "x15", "x10", "x5", "x1", "a5", "a1", "x2", "b5", "b1", "x3", "ax15", "ax5" }; String[] actualOrder = new String[childNodes.size()]; int i = 0; diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/UsesAugmentTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/UsesAugmentTest.java index 79136aa5fc..010570dc10 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/UsesAugmentTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/UsesAugmentTest.java @@ -13,6 +13,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.Lists; + import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; @@ -20,10 +21,12 @@ import java.net.URISyntaxException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Set; + import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -130,7 +133,7 @@ public class UsesAugmentTest { path.offer(expectedQName); SchemaPath expectedPath = SchemaPath.create(path, true); assertEquals(expectedPath, pcreq.getPath()); - Set childNodes = pcreq.getChildNodes(); + Collection childNodes = pcreq.getChildNodes(); assertEquals(4, childNodes.size()); // * |-- leaf version LeafSchemaNode version = (LeafSchemaNode) pcreq.getDataChildByName("version"); -- 2.36.6