From: Martin Vitez Date: Mon, 19 Aug 2013 11:01:10 +0000 (+0200) Subject: Fixed bug in uses statement resolving. X-Git-Tag: yangtools-0.1.0~67^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=c147a8fc1d4e445d08a5aa1994863e87c9472a08;p=yangtools.git Fixed bug in uses statement resolving. Signed-off-by: Martin Vitez --- diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/IdentityrefType.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/IdentityrefType.java index a1b399e295..f498608c0f 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/IdentityrefType.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/IdentityrefType.java @@ -1,10 +1,10 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.yangtools.yang.model.util; import java.util.Collections; @@ -17,7 +17,8 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; /** - * The default implementation of Identityref Type Definition interface. + * The default implementation of Identityref Type Definition + * interface. * * @see IdentityrefTypeDefinition */ @@ -86,4 +87,9 @@ public final class IdentityrefType implements IdentityrefTypeDefinition { return baseType; } + @Override + public String toString() { + return "identityref " + identity.getLocalName(); + } + } diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/YangTypesConverter.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/YangTypesConverter.java index cf486fd91b..f7ea048a07 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/YangTypesConverter.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/YangTypesConverter.java @@ -7,14 +7,9 @@ */ package org.opendaylight.yangtools.yang.model.util; -import java.net.URI; -import java.util.ArrayList; -import java.util.Date; import java.util.HashSet; -import java.util.List; import java.util.Set; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; @@ -47,12 +42,9 @@ public final class YangTypesConverter { return baseYangTypes.contains(type); } - public static TypeDefinition javaTypeForBaseYangType( - List actualPath, URI namespace, Date revision, - String typeName) { + public static TypeDefinition javaTypeForBaseYangType(SchemaPath path, String typeName) { TypeDefinition type = null; - SchemaPath path = createSchemaPath(actualPath, namespace, revision, typeName); if (typeName.startsWith("int")) { if ("int8".equals(typeName)) { type = new Int8(path); @@ -88,19 +80,4 @@ public final class YangTypesConverter { return type; } - private static SchemaPath createSchemaPath(List actualPath, URI namespace, Date revision, String typeName) { - List correctPath = new ArrayList(actualPath); - // remove module name - correctPath.remove(0); - - List path = new ArrayList(); - for(String element : correctPath) { - path.add(new QName(namespace, revision, element)); - } - // add type qname - QName typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName); - path.add(typeQName); - return new SchemaPath(path, true); - } - } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/UsesNodeBuilder.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/UsesNodeBuilder.java index 1808197de5..0965f64213 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/UsesNodeBuilder.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/UsesNodeBuilder.java @@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.parser.builder.api; import java.util.List; import java.util.Set; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder; @@ -28,6 +29,14 @@ public interface UsesNodeBuilder extends GroupingMember, Builder { void setGroupingPath(SchemaPath groupingPath); + GroupingDefinition getGroupingDefinition(); + + void setGroupingDefinition(GroupingDefinition groupingDefinition); + + GroupingBuilder getGroupingBuilder(); + + void setGrouping(GroupingBuilder grouping); + Set getAugmentations(); void addAugment(AugmentationSchemaBuilder builder); @@ -46,34 +55,28 @@ public interface UsesNodeBuilder extends GroupingMember, Builder { UsesNode build(); - Set getFinalChildren(); - Set getTargetChildren(); void setTargetChildren(Set targetChildren); - Set getFinalGroupings(); - Set getTargetGroupings(); void setTargetGroupings(Set targetGroupings); - Set getFinalTypedefs(); - Set getTargetTypedefs(); void setTargetTypedefs(Set targetTypedefs); - List getFinalUnknownNodes(); - List getTargetUnknownNodes(); void setTargetUnknownNodes(List targetUnknownNodes); - List getTargetGroupingUses(); + boolean isDataCollected(); + + void setDataCollected(boolean dataCollected); - boolean isLoadDone(); + boolean isParentUpdated(); - void setLoadDone(boolean loadDone); + void setParentUpdated(boolean parentUpdated); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java index df3bb5910a..67471068db 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java @@ -187,9 +187,9 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder { return instance; } - public boolean allUsesLoadDone() { + public boolean isAllUsesDataCollected() { for(UsesNodeBuilder usesNode : allUsesNodes) { - if(!usesNode.isLoadDone()) { + if(!usesNode.isDataCollected()) { return false; } } @@ -739,7 +739,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder { return builder; } - public IdentitySchemaNodeBuilder addIdentity(final QName qname, final List parentPath, final int line) { + public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) { Builder parent = getActualNode(); if (!(parent.equals(this))) { throw new YangParseException(name, line, "identity can be defined only in module or submodule"); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeDefinitionBuilderImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeDefinitionBuilderImpl.java index 3fae6d9f0c..75afe1062a 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeDefinitionBuilderImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeDefinitionBuilderImpl.java @@ -205,15 +205,7 @@ public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder im @Override public String toString() { - final StringBuilder result = new StringBuilder("TypedefBuilder[" + qname.getLocalName()); - result.append(", type="); - if (type == null) { - result.append(typedef); - } else { - result.append(type); - } - result.append("]"); - return result.toString(); + return "typedef " + qname.getLocalName(); } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UnionTypeBuilder.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UnionTypeBuilder.java index 49f39d1dfc..29598ce893 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UnionTypeBuilder.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UnionTypeBuilder.java @@ -56,7 +56,7 @@ public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements } public List getTypedefs() { - return Collections.unmodifiableList(typedefs); + return typedefs; } @Override @@ -214,7 +214,7 @@ public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements @Override public String toString() { final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName() + "["); - result.append(", types=" + types); + result.append("types=" + types); result.append(", typedefs=" + typedefs); result.append("]"); return result.toString(); diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UsesNodeBuilderImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UsesNodeBuilderImpl.java index 8eb3a9e81e..7da9c5edff 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UsesNodeBuilderImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UsesNodeBuilderImpl.java @@ -16,6 +16,7 @@ import java.util.Map; import java.util.Set; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; @@ -38,34 +39,40 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo private DataNodeContainerBuilder parent; private final String groupingName; private SchemaPath groupingPath; + private GroupingDefinition groupingDefinition; + private GroupingBuilder groupingBuilder; private boolean augmenting; private boolean addedByUses; private final Set addedAugments = new HashSet(); private final List refineBuilders = new ArrayList(); private final List refines = new ArrayList(); - private final Set finalChildren = new HashSet<>(); - private Set targetChildren; + private Set targetChildren = new HashSet<>(); + private Set targetGroupings = new HashSet<>(); + private Set targetTypedefs = new HashSet<>(); + private List targetUnknownNodes = new ArrayList<>(); - private final Set finalGroupings = new HashSet<>(); - private Set targetGroupings; + private boolean dataCollected; + private boolean parentUpdated; - private final Set finalTypedefs = new HashSet<>(); - private Set targetTypedefs; - - private final List finalUnknownNodes = new ArrayList<>(); - private List targetUnknownNodes; - - private final List targetGroupingUses = new ArrayList<>(); + @Override + public boolean isDataCollected() { + return dataCollected; + } - boolean loadDone; + @Override + public void setDataCollected(boolean dataCollected) { + this.dataCollected = dataCollected; + } - public boolean isLoadDone() { - return loadDone; + @Override + public boolean isParentUpdated() { + return parentUpdated; } - public void setLoadDone(boolean loadDone) { - this.loadDone = loadDone; + @Override + public void setParentUpdated(boolean parentUpdated) { + this.parentUpdated = parentUpdated; } public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) { @@ -131,6 +138,26 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo this.groupingPath = groupingPath; } + @Override + public GroupingDefinition getGroupingDefinition() { + return groupingDefinition; + } + + @Override + public void setGroupingDefinition(GroupingDefinition groupingDefinition) { + this.groupingDefinition = groupingDefinition; + } + + @Override + public GroupingBuilder getGroupingBuilder() { + return groupingBuilder; + } + + @Override + public void setGrouping(GroupingBuilder grouping) { + this.groupingBuilder = grouping; + } + @Override public String getGroupingName() { return groupingName; @@ -186,11 +213,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo refines.add(refine); } - @Override - public Set getFinalChildren() { - return finalChildren; - } - @Override public Set getTargetChildren() { return targetChildren; @@ -201,11 +223,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo this.targetChildren = targetChildren; } - @Override - public Set getFinalGroupings() { - return finalGroupings; - } - @Override public Set getTargetGroupings() { return targetGroupings; @@ -216,11 +233,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo this.targetGroupings = targetGroupings; } - @Override - public Set getFinalTypedefs() { - return finalTypedefs; - } - @Override public Set getTargetTypedefs() { return targetTypedefs; @@ -231,11 +243,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo this.targetTypedefs = targetTypedefs; } - @Override - public List getFinalUnknownNodes() { - return finalUnknownNodes; - } - @Override public List getTargetUnknownNodes() { return targetUnknownNodes; @@ -246,11 +253,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo this.targetUnknownNodes = targetUnknownNodes; } - @Override - public List getTargetGroupingUses() { - return targetGroupingUses; - } - @Override public int hashCode() { final int prime = 31; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java index 7a97c53356..2eff93d472 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java @@ -15,7 +15,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -35,31 +34,20 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.opendaylight.yangtools.antlrv4.code.gen.YangLexer; import org.opendaylight.yangtools.antlrv4.code.gen.YangParser; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; -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.GroupingDefinition; 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.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.IdentityrefType; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder; -import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder; -import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember; import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder; @@ -70,6 +58,7 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilde import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.util.CopyUtils; import org.opendaylight.yangtools.yang.parser.util.GroupingUtils; import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort; import org.opendaylight.yangtools.yang.parser.util.ParserUtils; @@ -296,6 +285,8 @@ public final class YangParserImpl implements YangModelParser { } private Map build(final Map> modules) { + findUsesTargets(modules, null); + // fix unresolved nodes for (Map.Entry> entry : modules.entrySet()) { for (Map.Entry childEntry : entry.getValue().entrySet()) { @@ -303,8 +294,10 @@ public final class YangParserImpl implements YangModelParser { fixUnresolvedNodes(modules, moduleBuilder); } } + + finishResolveDirtyNodes(modules); resolveAugments(modules); - finishResolvingUses(modules); + resolveUses(modules); resolveDeviations(modules); // build @@ -323,6 +316,8 @@ public final class YangParserImpl implements YangModelParser { private Map buildWithContext(final Map> modules, SchemaContext context) { + findUsesTargets(modules, context); + // fix unresolved nodes for (Map.Entry> entry : modules.entrySet()) { for (Map.Entry childEntry : entry.getValue().entrySet()) { @@ -330,7 +325,9 @@ public final class YangParserImpl implements YangModelParser { fixUnresolvedNodesWithContext(modules, moduleBuilder, context); } } + // TODO finishResolveDirtyNodesWithContext(modules, context) resolveAugmentsWithContext(modules, context); + resolveUsesWithContext(modules, context); resolveDeviationsWithContext(modules, context); // build @@ -350,7 +347,6 @@ public final class YangParserImpl implements YangModelParser { private void fixUnresolvedNodes(final Map> modules, final ModuleBuilder builder) { resolveDirtyNodes(modules, builder); resolveIdentities(modules, builder); - resolveUsesNodes(modules, builder); resolveUnknownNodes(modules, builder); } @@ -358,7 +354,6 @@ public final class YangParserImpl implements YangModelParser { final ModuleBuilder builder, final SchemaContext context) { resolveDirtyNodesWithContext(modules, builder, context); resolveIdentitiesWithContext(modules, builder, context); - resolveUsesNodesWithContext(modules, builder, context); resolveUnknownNodesWithContext(modules, builder, context); } @@ -389,6 +384,39 @@ public final class YangParserImpl implements YangModelParser { } } + private void finishResolveDirtyNodes(final Map> modules) { + final Set dirtyNodes = new HashSet<>(); + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + dirtyNodes.addAll(inner.getValue().getDirtyNodes()); + } + } + + if (!dirtyNodes.isEmpty()) { + for (TypeAwareBuilder nodeToResolve : dirtyNodes) { + if (nodeToResolve instanceof UnionTypeBuilder) { + List newTypes = new ArrayList<>(); + List oldTypes = ((UnionTypeBuilder) nodeToResolve).getTypedefs(); + for (TypeDefinitionBuilder tdb : oldTypes) { + TypeDefinitionBuilder newType = CopyUtils.copy(tdb, nodeToResolve, false); + ParserUtils.correctTypeAwareNodePath(newType); + newTypes.add(newType); + } + oldTypes.clear(); + oldTypes.addAll(newTypes); + } else if (nodeToResolve.getType() instanceof IdentityrefType) { + TypeDefinition idRef = ParserUtils.createCorrectTypeDefinition(nodeToResolve.getPath(), + nodeToResolve.getType()); + nodeToResolve.setType(idRef); + } else { + TypeDefinitionBuilder tdb = CopyUtils.copy(nodeToResolve.getTypedef(), nodeToResolve, false); + ParserUtils.correctTypeAwareNodePath(tdb); + nodeToResolve.setTypedef(tdb); + } + } + } + } + private void resolveDirtyNodesWithContext(final Map> modules, final ModuleBuilder module, SchemaContext context) { final Set dirtyNodes = module.getDirtyNodes(); @@ -470,7 +498,6 @@ public final class YangParserImpl implements YangModelParser { ModuleBuilder module = getParentModule(augmentBuilder); List path = augmentBuilder.getTargetPath().getPath(); Builder augmentParent = augmentBuilder.getParent(); - boolean isUsesAugment = false; Builder firstNodeParent = null; if (augmentParent instanceof ModuleBuilder) { @@ -483,9 +510,6 @@ public final class YangParserImpl implements YangModelParser { } firstNodeParent = findDependentModuleBuilder(modules, module, prefix, line); } else if (augmentParent instanceof UsesNodeBuilder) { - // if augment is defined under uses, parent of first node is uses - // parent - isUsesAugment = true; firstNodeParent = augmentParent.getParent(); } else { // augment can be defined only under module or uses @@ -493,7 +517,7 @@ public final class YangParserImpl implements YangModelParser { "Failed to parse augment: Unresolved parent of augment: " + augmentParent); } - return processAugmentation(augmentBuilder, firstNodeParent, path, isUsesAugment); + return processAugmentation(augmentBuilder, firstNodeParent, path); } /** @@ -564,14 +588,11 @@ public final class YangParserImpl implements YangModelParser { } Builder augmentParent = augmentBuilder.getParent(); Builder currentParent = null; - boolean isUsesAugment = false; if (augmentParent instanceof ModuleBuilder) { // if augment is defined under module, first parent is target module currentParent = findDependentModuleBuilder(modules, module, prefix, line); } else if (augmentParent instanceof UsesNodeBuilder) { - // if augment is defined under uses, first parent is uses parent - isUsesAugment = true; currentParent = augmentParent.getParent(); } else { // augment can be defined only under module or uses @@ -582,7 +603,7 @@ public final class YangParserImpl implements YangModelParser { if (currentParent == null) { return processAugmentationOnContext(augmentBuilder, path, module, prefix, context); } else { - return processAugmentation(augmentBuilder, currentParent, path, isUsesAugment); + return processAugmentation(augmentBuilder, currentParent, path); } } @@ -677,179 +698,130 @@ public final class YangParserImpl implements YangModelParser { } /** - * Go through uses statements defined in current module and resolve their - * refine statements. + * Find target grouping for all uses nodes. * * @param modules - * all modules - * @param module - * module being resolved + * all loaded modules */ - private void resolveUsesNodes(final Map> modules, final ModuleBuilder module) { - final List allModuleUses = module.getAllUsesNodes(); - List collection = new ArrayList<>(module.getAllUsesNodes()); - boolean usesDataLoaded = module.allUsesLoadDone(); - while (!usesDataLoaded) { - for (UsesNodeBuilder usesNode : collection) { - if (!usesNode.isLoadDone()) { - final GroupingBuilder targetGrouping = GroupingUtils.getTargetGroupingFromModules(usesNode, - modules, module); - usesNode.setGroupingPath(targetGrouping.getPath()); - // load uses target nodes in uses - GroupingUtils.loadTargetGroupingData(usesNode, targetGrouping); - } + private void findUsesTargets(final Map> modules, final SchemaContext context) { + final List allUses = new ArrayList<>(); + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + allUses.addAll(inner.getValue().getAllUsesNodes()); } - collection = new ArrayList<>(module.getAllUsesNodes()); - usesDataLoaded = module.allUsesLoadDone(); } - - for (UsesNodeBuilder usesNode : allModuleUses) { - final GroupingBuilder targetGrouping = GroupingUtils - .getTargetGroupingFromModules(usesNode, modules, module); - // load uses target uses nodes in uses - GroupingUtils.loadTargetGroupingUses(usesNode, targetGrouping); + for (UsesNodeBuilder usesNode : allUses) { + ModuleBuilder module = ParserUtils.getParentModule(usesNode); + final GroupingBuilder targetGroupingBuilder = GroupingUtils.getTargetGroupingFromModules(usesNode, modules, + module); + if (targetGroupingBuilder == null) { + if (context == null) { + throw new YangParseException(module.getName(), usesNode.getLine(), "Referenced grouping '" + + usesNode.getGroupingName() + "' not found."); + } else { + GroupingDefinition targetGroupingDefinition = GroupingUtils.getTargetGroupingFromContext(usesNode, + module, context); + usesNode.setGroupingDefinition(targetGroupingDefinition); + usesNode.setGroupingPath(targetGroupingDefinition.getPath()); + } + } else { + usesNode.setGrouping(targetGroupingBuilder); + usesNode.setGroupingPath(targetGroupingBuilder.getPath()); + } } } - private void finishResolvingUses(final Map> modules) { - final List alluses = new ArrayList<>(); + /** + * Copy data from uses target, update uses parent and perform refinement. + * Augmentations have to be resolved already. + * + * @param modules + * all loaded modules + */ + private void resolveUses(final Map> modules) { for (Map.Entry> entry : modules.entrySet()) { for (Map.Entry inner : entry.getValue().entrySet()) { - alluses.addAll(inner.getValue().getAllUsesNodes()); + ModuleBuilder module = inner.getValue(); + List usesNodes = null; + boolean dataCollected = module.isAllUsesDataCollected(); + + while (!dataCollected) { + usesNodes = new ArrayList<>(module.getAllUsesNodes()); + for (UsesNodeBuilder usesNode : usesNodes) { + if (!usesNode.isDataCollected()) { + GroupingUtils.collectUsesData(usesNode); + } + } + dataCollected = module.isAllUsesDataCollected(); + } } } - for (UsesNodeBuilder usesNode : alluses) { - ParserUtils.processUsesNode(usesNode); + + // new cycle is must because in collecting data process new uses could + // be created + final List allModulesUses = new ArrayList<>(); + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + allModulesUses.addAll(inner.getValue().getAllUsesNodes()); + } } - for (UsesNodeBuilder usesNode : alluses) { - ParserUtils.performRefine(usesNode); - ParserUtils.updateUsesParent(usesNode, usesNode.getParent()); + + for (UsesNodeBuilder usesNode : allModulesUses) { + GroupingUtils.updateUsesParent(usesNode); + GroupingUtils.performRefine(usesNode); } - for (UsesNodeBuilder usesNode : alluses) { - ParserUtils.fixUsesNodesPath(usesNode); + for (UsesNodeBuilder usesNode : allModulesUses) { + GroupingUtils.fixUsesNodesPath(usesNode); } } /** - * Tries to search target grouping in given modules and resolve refine - * nodes. If grouping is not found in modules, method tries to find it in - * modules from context. * * @param modules * all loaded modules - * @param module - * current module * @param context * SchemaContext containing already resolved modules */ - private void resolveUsesNodesWithContext(final Map> modules, - final ModuleBuilder module, final SchemaContext context) { - final List allModuleUses = module.getAllUsesNodes(); - for (UsesNodeBuilder usesNode : allModuleUses) { - // process uses operation - final GroupingBuilder targetGrouping = GroupingUtils - .getTargetGroupingFromModules(usesNode, modules, module); - if (targetGrouping == null) { - // TODO implement - } else { - usesNode.setGroupingPath(targetGrouping.getPath()); - GroupingUtils.loadTargetGroupingData(usesNode, targetGrouping); - } - } - for (UsesNodeBuilder usesNode : allModuleUses) { - final GroupingBuilder targetGrouping = GroupingUtils - .getTargetGroupingFromModules(usesNode, modules, module); - if (targetGrouping == null) { - // TODO implement - } else { - GroupingUtils.loadTargetGroupingUses(usesNode, targetGrouping); + private void resolveUsesWithContext(final Map> modules, + final SchemaContext context) { + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + ModuleBuilder module = inner.getValue(); + List usesNodes = null; + boolean dataCollected = module.isAllUsesDataCollected(); + + while (!dataCollected) { + usesNodes = new ArrayList<>(module.getAllUsesNodes()); + for (UsesNodeBuilder usesNode : usesNodes) { + if (!usesNode.isDataCollected()) { + if (usesNode.getGroupingBuilder() == null) { + GroupingUtils.collectUsesDataFromContext(usesNode); + } else { + GroupingUtils.collectUsesData(usesNode); + } + } + } + dataCollected = module.isAllUsesDataCollected(); + } } } - } - // TODO use in implementation - private void processUsesNode(final UsesNodeBuilder usesNode, final GroupingDefinition targetGrouping) { - final String moduleName = usesNode.getModuleName(); - final int line = usesNode.getLine(); - DataNodeContainerBuilder parent = usesNode.getParent(); - URI namespace = null; - Date revision = null; - String prefix = null; - if (parent instanceof ModuleBuilder) { - ModuleBuilder m = (ModuleBuilder) parent; - namespace = m.getNamespace(); - revision = m.getRevision(); - prefix = m.getPrefix(); - } else { - QName parentQName = parent.getQName(); - namespace = parentQName.getNamespace(); - revision = parentQName.getRevision(); - prefix = parentQName.getPrefix(); - } - SchemaPath parentPath = parent.getPath(); - - final Set newChildren = new HashSet<>(); - for (DataSchemaNode child : targetGrouping.getChildNodes()) { - if (child != null) { - DataSchemaNodeBuilder newChild = null; - QName newQName = new QName(namespace, revision, prefix, child.getQName().getLocalName()); - if (child instanceof AnyXmlSchemaNode) { - newChild = createAnyXml((AnyXmlSchemaNode) child, newQName, moduleName, line); - } else if (child instanceof ChoiceNode) { - newChild = createChoice((ChoiceNode) child, newQName, moduleName, line); - } else if (child instanceof ContainerSchemaNode) { - newChild = createContainer((ContainerSchemaNode) child, newQName, moduleName, line); - } else if (child instanceof LeafListSchemaNode) { - newChild = createLeafList((LeafListSchemaNode) child, newQName, moduleName, line); - } else if (child instanceof LeafSchemaNode) { - newChild = createLeafBuilder((LeafSchemaNode) child, newQName, moduleName, line); - } else if (child instanceof ListSchemaNode) { - newChild = createList((ListSchemaNode) child, newQName, moduleName, line); - } - - if (newChild == null) { - throw new YangParseException(moduleName, line, - "Unknown member of target grouping while resolving uses node."); - } - if (newChild instanceof GroupingMember) { - ((GroupingMember) newChild).setAddedByUses(true); - } - - newChild.setPath(createSchemaPath(parentPath, newQName)); - newChildren.add(newChild); + // new cycle is must because in collecting data process new uses could + // be created + final List allModulesUses = new ArrayList<>(); + for (Map.Entry> entry : modules.entrySet()) { + for (Map.Entry inner : entry.getValue().entrySet()) { + allModulesUses.addAll(inner.getValue().getAllUsesNodes()); } } - usesNode.getFinalChildren().addAll(newChildren); - - final Set newGroupings = new HashSet<>(); - for (GroupingDefinition g : targetGrouping.getGroupings()) { - QName newQName = new QName(namespace, revision, prefix, g.getQName().getLocalName()); - GroupingBuilder newGrouping = createGrouping(g, newQName, moduleName, line); - newGrouping.setAddedByUses(true); - newGrouping.setPath(createSchemaPath(parentPath, newQName)); - newGroupings.add(newGrouping); - } - usesNode.getFinalGroupings().addAll(newGroupings); - - final Set newTypedefs = new HashSet<>(); - for (TypeDefinition td : targetGrouping.getTypeDefinitions()) { - QName newQName = new QName(namespace, revision, prefix, td.getQName().getLocalName()); - TypeDefinitionBuilder newType = createTypedef((ExtendedType) td, newQName, moduleName, line); - newType.setAddedByUses(true); - newType.setPath(createSchemaPath(parentPath, newQName)); - newTypedefs.add(newType); + + for (UsesNodeBuilder usesNode : allModulesUses) { + GroupingUtils.updateUsesParent(usesNode); + GroupingUtils.performRefine(usesNode); } - usesNode.getFinalTypedefs().addAll(newTypedefs); - - final List newUnknownNodes = new ArrayList<>(); - for (UnknownSchemaNode un : targetGrouping.getUnknownSchemaNodes()) { - QName newQName = new QName(namespace, revision, prefix, un.getQName().getLocalName()); - UnknownSchemaNodeBuilder newNode = createUnknownSchemaNode(un, newQName, moduleName, line); - newNode.setAddedByUses(true); - newNode.setPath(createSchemaPath(parentPath, newQName)); - newUnknownNodes.add(newNode); + for (UsesNodeBuilder usesNode : allModulesUses) { + GroupingUtils.fixUsesNodesPath(usesNode); } - usesNode.getFinalUnknownNodes().addAll(newUnknownNodes); } private void resolveUnknownNodes(final Map> modules, final ModuleBuilder module) { diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java index 32d245e98f..53ad43a32e 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java @@ -52,6 +52,7 @@ import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtC import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.util.BaseTypes; import org.opendaylight.yangtools.yang.model.util.YangTypesConverter; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder; @@ -87,18 +88,27 @@ public final class YangParserListenerImpl extends YangParserBaseListener { private Date revision = new Date(0L); public final static DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - private final Stack actualPath = new Stack(); + private final Stack> actualPath = new Stack<>(); + + private void addNodeToPath(QName name) { + actualPath.peek().push(name); + } + + private QName removeNodeFromPath() { + return actualPath.peek().pop(); + } @Override public void enterModule_stmt(YangParser.Module_stmtContext ctx) { moduleName = stringFromNode(ctx); - logger.debug("enter module " + moduleName); - actualPath.push(moduleName); + logger.debug("entering module " + moduleName); + enterLog("module", moduleName, 0); + actualPath.push(new Stack()); + moduleBuilder = new ModuleBuilder(moduleName); String description = null; String reference = null; - for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if (child instanceof Description_stmtContext) { @@ -117,7 +127,8 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitModule_stmt(YangParser.Module_stmtContext ctx) { - exitLog("module", actualPath.pop()); + exitLog("module", ""); + actualPath.pop(); } @Override @@ -256,6 +267,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { final int line = ctx.getStart().getLine(); final String augmentPath = stringFromNode(ctx); enterLog("augment", augmentPath, line); + actualPath.push(new Stack()); AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath); @@ -273,13 +285,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener { } moduleBuilder.enterNode(builder); - actualPath.push(augmentPath); } @Override public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("augment", actualPath.pop()); + exitLog("augment", ""); + actualPath.pop(); } @Override @@ -287,9 +299,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener { final int line = ctx.getStart().getLine(); final String extName = stringFromNode(ctx); enterLog("extension", extName, line); - QName qname = new QName(namespace, revision, yangModelPrefix, extName); + addNodeToPath(qname); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + ExtensionBuilder builder = moduleBuilder.addExtension(qname, line); + builder.setPath(path); parseSchemaNodeArgs(ctx, builder); String argument = null; @@ -306,13 +321,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener { builder.setYinElement(yin); moduleBuilder.enterNode(builder); - actualPath.push(extName); } @Override public void exitExtension_stmt(YangParser.Extension_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("extension", actualPath.pop()); + exitLog("extension", removeNodeFromPath()); } @Override @@ -320,22 +334,23 @@ public final class YangParserListenerImpl extends YangParserBaseListener { final int line = ctx.getStart().getLine(); final String typedefName = stringFromNode(ctx); enterLog("typedef", typedefName, line); - QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName); - TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName); - moduleBuilder.enterNode(builder); - actualPath.push(typedefName); + addNodeToPath(typedefQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName); + builder.setPath(path); parseSchemaNodeArgs(ctx, builder); builder.setUnits(parseUnits(ctx)); builder.setDefaultValue(parseDefault(ctx)); + + moduleBuilder.enterNode(builder); } @Override public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("typedef", actualPath.pop()); + exitLog("typedef", removeNodeFromPath()); } @Override @@ -361,32 +376,39 @@ public final class YangParserListenerImpl extends YangParserBaseListener { // check for types which must have body checkMissingBody(typeName, moduleName, line); // if there are no constraints, just grab default base yang type - type = YangTypesConverter.javaTypeForBaseYangType(actualPath, namespace, revision, typeName); + QName qname = BaseTypes.constructQName(typeName); + addNodeToPath(qname); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + type = YangTypesConverter.javaTypeForBaseYangType(path, typeName); moduleBuilder.setType(type); } else { if ("union".equals(typeName)) { - SchemaPath p = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, typeName); + QName qname = BaseTypes.constructQName("union"); + addNodeToPath(qname); + SchemaPath path = createActualSchemaPath(actualPath.peek()); UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision); moduleBuilder.enterNode(unionBuilder); - unionBuilder.setPath(p); + unionBuilder.setPath(path); } else if ("identityref".equals(typeName)) { - SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, typeName); + QName qname = BaseTypes.constructQName("identityref"); + addNodeToPath(qname); + SchemaPath path = createActualSchemaPath(actualPath.peek()); moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody)); } else { - type = parseTypeWithBody(typeName, typeBody, actualPath, namespace, revision, yangModelPrefix, - moduleBuilder.getActualNode()); + type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix, moduleBuilder.getActualNode()); moduleBuilder.setType(type); + addNodeToPath(type.getQName()); } } } else { - type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath, namespace, revision, yangModelPrefix, + type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix, moduleBuilder.getActualNode()); // add parent node of this type statement to dirty nodes moduleBuilder.markActualNodeDirty(); moduleBuilder.setType(type); + addNodeToPath(type.getQName()); } - actualPath.push(typeName); } private QName parseQName(String typeName) { @@ -412,7 +434,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { if ("union".equals(typeName)) { moduleBuilder.exitNode(); } - exitLog("type", actualPath.pop()); + exitLog("type", removeNodeFromPath()); } @Override @@ -420,20 +442,21 @@ public final class YangParserListenerImpl extends YangParserBaseListener { final int line = ctx.getStart().getLine(); final String groupName = stringFromNode(ctx); enterLog("grouping", groupName, line); - QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName); - GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName); - moduleBuilder.enterNode(builder); - actualPath.push(groupName); + addNodeToPath(groupQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName); + builder.setPath(path); parseSchemaNodeArgs(ctx, builder); + + moduleBuilder.enterNode(builder); } @Override public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("grouping", actualPath.pop()); + exitLog("grouping", removeNodeFromPath()); } @Override @@ -443,12 +466,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("container", containerName, line); QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName); - SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, containerName); + addNodeToPath(containerQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path); - moduleBuilder.enterNode(builder); - actualPath.push(containerName); - parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line)); @@ -460,12 +481,14 @@ public final class YangParserListenerImpl extends YangParserBaseListener { break; } } + + moduleBuilder.enterNode(builder); } @Override public void exitContainer_stmt(Container_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("container", actualPath.pop()); + exitLog("container", removeNodeFromPath()); } @Override @@ -475,12 +498,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("leaf", leafName, line); QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName); - SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, leafName); - - LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, schemaPath); - moduleBuilder.enterNode(builder); - actualPath.push(leafName); + addNodeToPath(leafQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line)); @@ -497,12 +518,14 @@ public final class YangParserListenerImpl extends YangParserBaseListener { } builder.setDefaultStr(defaultStr); builder.setUnits(unitsStr); + + moduleBuilder.enterNode(builder); } @Override public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("leaf", actualPath.pop()); + exitLog("leaf", removeNodeFromPath()); } @Override @@ -514,16 +537,17 @@ public final class YangParserListenerImpl extends YangParserBaseListener { UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr); moduleBuilder.enterNode(builder); - actualPath.push(groupingPathStr); } @Override public void exitUses_stmt(YangParser.Uses_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("uses", actualPath.pop()); + exitLog("uses", ""); } - @Override public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) { + @Override + public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) { + actualPath.push(new Stack()); final int line = ctx.getStart().getLine(); final String augmentPath = stringFromNode(ctx); enterLog("augment", augmentPath, line); @@ -544,12 +568,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener { } moduleBuilder.enterNode(builder); - actualPath.push(augmentPath); } - @Override public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) { + @Override + public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("augment", actualPath.pop()); + exitLog("augment", ""); + actualPath.pop(); } @Override @@ -560,13 +585,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener { RefineHolder refine = parseRefine(ctx, moduleName); moduleBuilder.addRefine(refine); moduleBuilder.enterNode(refine); - actualPath.push(refineString); } @Override public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("refine", actualPath.pop()); + exitLog("refine", ""); } @Override @@ -574,13 +598,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener { final int line = ctx.getStart().getLine(); final String leafListName = stringFromNode(ctx); enterLog("leaf-list", leafListName, line); - QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName); - SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, leafListName); + addNodeToPath(leafListQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, schemaPath); + LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path); moduleBuilder.enterNode(builder); - actualPath.push(leafListName); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); @@ -600,7 +623,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("leaf-list", actualPath.pop()); + exitLog("leaf-list", removeNodeFromPath()); } @Override @@ -610,11 +633,11 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("list", listName, line); QName listQName = new QName(namespace, revision, yangModelPrefix, listName); - SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, listName); + addNodeToPath(listQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, schemaPath); + ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path); moduleBuilder.enterNode(builder); - actualPath.push(listName); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); @@ -638,7 +661,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitList_stmt(List_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("list", actualPath.pop()); + exitLog("list", removeNodeFromPath()); } @Override @@ -648,11 +671,11 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("anyxml", anyXmlName, line); QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName); - SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, anyXmlName); + addNodeToPath(anyXmlQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, schemaPath); + AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path); moduleBuilder.enterNode(builder); - actualPath.push(anyXmlName); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); @@ -662,7 +685,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("anyxml", actualPath.pop()); + exitLog("anyxml", removeNodeFromPath()); } @Override @@ -672,12 +695,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("choice", choiceName, line); QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName); + addNodeToPath(choiceQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName); + builder.setPath(path); moduleBuilder.enterNode(builder); - actualPath.push(choiceName); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line)); @@ -696,7 +720,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("choice", actualPath.pop()); + exitLog("choice", removeNodeFromPath()); } @Override @@ -706,11 +730,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("case", caseName, line); QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName); + addNodeToPath(caseQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName); + builder.setPath(path); moduleBuilder.enterNode(builder); - actualPath.push(caseName); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); } @@ -718,7 +744,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitCase_stmt(YangParser.Case_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("case", actualPath.pop()); + exitLog("case", removeNodeFromPath()); } @Override @@ -728,18 +754,20 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("notification", notificationName, line); QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName); + addNodeToPath(notificationQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName); + builder.setPath(path); moduleBuilder.enterNode(builder); - actualPath.push(notificationName); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, builder); } @Override public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("notification", actualPath.pop()); + exitLog("notification", removeNodeFromPath()); } // Unknown nodes @@ -774,8 +802,11 @@ public final class YangParserListenerImpl extends YangParserBaseListener { UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname); builder.setNodeType(nodeType); builder.setNodeParameter(nodeParameter); - actualPath.push(nodeParameter); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter)); + + SchemaPath path = createActualSchemaPath(actualPath.peek()); + builder.setPath(path); + parseSchemaNodeArgs(ctx, builder); moduleBuilder.enterNode(builder); } @@ -783,7 +814,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("unknown-node", actualPath.pop()); + exitLog("unknown-node", removeNodeFromPath()); } @Override @@ -795,16 +826,18 @@ public final class YangParserListenerImpl extends YangParserBaseListener { QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName); RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName); moduleBuilder.enterNode(rpcBuilder); - actualPath.push(rpcName); + addNodeToPath(rpcQName); + + SchemaPath path = createActualSchemaPath(actualPath.peek()); + rpcBuilder.setPath(path); - rpcBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, rpcBuilder); } @Override public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("rpc", actualPath.pop()); + exitLog("rpc", removeNodeFromPath()); } @Override @@ -814,11 +847,11 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog(input, input, line); QName rpcQName = new QName(namespace, revision, yangModelPrefix, input); - SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, input); + addNodeToPath(rpcQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path); moduleBuilder.enterNode(builder); - actualPath.push(input); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); @@ -827,7 +860,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitInput_stmt(YangParser.Input_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("input", actualPath.pop()); + exitLog("input", removeNodeFromPath()); } @Override @@ -837,11 +870,11 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog(output, output, line); QName rpcQName = new QName(namespace, revision, yangModelPrefix, output); - SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, output); + addNodeToPath(rpcQName); + SchemaPath path = createActualSchemaPath(actualPath.peek()); ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line); moduleBuilder.enterNode(builder); - actualPath.push(output); parseSchemaNodeArgs(ctx, builder); parseConstraints(ctx, builder.getConstraints()); @@ -850,7 +883,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitOutput_stmt(YangParser.Output_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("output", actualPath.pop()); + exitLog("output", removeNodeFromPath()); } @Override @@ -862,16 +895,17 @@ public final class YangParserListenerImpl extends YangParserBaseListener { QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName); FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName); moduleBuilder.enterNode(featureBuilder); - actualPath.push(featureName); + addNodeToPath(featureQName); - featureBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); + SchemaPath path = createActualSchemaPath(actualPath.peek()); + featureBuilder.setPath(path); parseSchemaNodeArgs(ctx, featureBuilder); } @Override public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("feature", actualPath.pop()); + exitLog("feature", removeNodeFromPath()); } @Override @@ -884,7 +918,6 @@ public final class YangParserListenerImpl extends YangParserBaseListener { String deviate = null; DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath); moduleBuilder.enterNode(builder); - actualPath.push(targetPath); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -907,7 +940,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("deviation", actualPath.pop()); + exitLog("deviation", ""); } @Override @@ -917,11 +950,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener { enterLog("identity", identityName, line); final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName); - IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, actualPath, line); + IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line); moduleBuilder.enterNode(builder); - actualPath.push(identityName); + addNodeToPath(identityQName); + + SchemaPath path = createActualSchemaPath(actualPath.peek()); + builder.setPath(path); - builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix)); parseSchemaNodeArgs(ctx, builder); for (int i = 0; i < ctx.getChildCount(); i++) { @@ -936,7 +971,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { @Override public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) { moduleBuilder.exitNode(); - exitLog("identity", actualPath.pop()); + exitLog("identity", removeNodeFromPath()); } public ModuleBuilder getModuleBuilder() { @@ -951,6 +986,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener { logger.trace("exiting {} {}", p1, p2); } + private void exitLog(String p1, QName p2) { + logger.trace("exiting {} {}", p1, p2.getLocalName()); + } + private void setLog(String p1, String p2) { logger.trace("setting {} {}", p1, p2); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java index 13ad34793c..4b5d8dc566 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java @@ -9,12 +9,25 @@ package org.opendaylight.yangtools.yang.parser.util; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; +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.MustDefinition; +import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; @@ -76,21 +89,21 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - AnyXmlBuilder c = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); + AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - return c; + return copy; } private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) { @@ -98,27 +111,27 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - ChoiceBuilder c = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); + ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); for (ChoiceCaseBuilder childNode : old.getCases()) { - c.addCase(copy(childNode, c, updateQName)); + copy.addCase(copy(childNode, copy, updateQName)); } for (AugmentationSchemaBuilder augment : old.getAugmentations()) { - c.addAugmentation(copyAugment(augment, c)); + copy.addAugmentation(copyAugment(augment, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - return c; + return copy; } private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) { @@ -126,33 +139,33 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - ChoiceCaseBuilder c = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.getChildNodes().addAll(old.getChildNodes()); + ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.getChildNodes().addAll(old.getChildNodes()); for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { - c.addChildNode(copy(childNode, c, updateQName)); + copy.addChildNode(copy(childNode, copy, updateQName)); } - c.getGroupings().addAll(old.getGroupings()); + copy.getGroupings().addAll(old.getGroupings()); for (GroupingBuilder grouping : old.getGroupingBuilders()) { - c.addGrouping(copy(grouping, c, updateQName)); + copy.addGrouping(copy(grouping, copy, updateQName)); } for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) { - c.addTypedef(copy(tdb, c, updateQName)); + copy.addTypedef(copy(tdb, copy, updateQName)); } for (UsesNodeBuilder oldUses : old.getUsesNodes()) { - c.addUsesNode(copyUses(oldUses, c)); + copy.addUsesNode(copyUses(oldUses, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - return c; + return copy; } private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent, @@ -161,40 +174,40 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - ContainerSchemaNodeBuilder c = new ContainerSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), + ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setPresence(old.isPresence()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); - c.setChildNodes(old.getChildNodes()); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setPresence(old.isPresence()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); + copy.setChildNodes(old.getChildNodes()); for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { - c.addChildNode(copy(childNode, c, updateQName)); + copy.addChildNode(copy(childNode, copy, updateQName)); } - c.getGroupings().addAll(old.getGroupings()); + copy.getGroupings().addAll(old.getGroupings()); for (GroupingBuilder grouping : old.getGroupingBuilders()) { - c.addGrouping(copy(grouping, c, updateQName)); + copy.addGrouping(copy(grouping, copy, updateQName)); } for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) { - c.addTypedef(copy(tdb, c, updateQName)); + copy.addTypedef(copy(tdb, copy, updateQName)); } for (UsesNodeBuilder oldUses : old.getUsesNodes()) { - c.addUsesNode(copyUses(oldUses, c)); + copy.addUsesNode(copyUses(oldUses, copy)); } for (AugmentationSchemaBuilder augment : old.getAugmentations()) { - c.addAugmentation(copyAugment(augment, c)); + copy.addAugmentation(copyAugment(augment, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - return c; + return copy; } private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) { @@ -202,31 +215,31 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - LeafSchemaNodeBuilder c = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, + LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } if (old.getType() == null) { - c.setTypedef(copy(old.getTypedef(), c, updateQName)); + copy.setTypedef(copy(old.getTypedef(), copy, updateQName)); } else { - c.setType(old.getType()); + copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType())); } - c.setDefaultStr(old.getDefaultStr()); - c.setUnits(old.getUnits()); + copy.setDefaultStr(old.getDefaultStr()); + copy.setUnits(old.getUnits()); - return c; + return copy; } public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) { @@ -234,30 +247,30 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - LeafListSchemaNodeBuilder c = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), + LeafListSchemaNodeBuilder copy = new LeafListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } if (old.getType() == null) { - c.setTypedef(copy(old.getTypedef(), c, updateQName)); + copy.setTypedef(copy(old.getTypedef(), copy, updateQName)); } else { - c.setType(old.getType()); + copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType())); } - c.setUserOrdered(old.isUserOrdered()); + copy.setUserOrdered(old.isUserOrdered()); - return c; + return copy; } private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) { @@ -265,88 +278,133 @@ public class CopyUtils { QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - ListSchemaNodeBuilder c = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, + ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath); - copyConstraints(c.getConstraints(), old.getConstraints()); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAugmenting(old.isAugmenting()); - c.setAddedByUses(old.isAddedByUses()); - c.setConfiguration(old.isConfiguration()); - c.setChildNodes(old.getChildNodes()); + copyConstraints(copy.getConstraints(), old.getConstraints()); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setConfiguration(old.isConfiguration()); + copy.setChildNodes(old.getChildNodes()); for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { - c.addChildNode(copy(childNode, c, updateQName)); + copy.addChildNode(copy(childNode, copy, updateQName)); } - c.getGroupings().addAll(old.getGroupings()); + copy.getGroupings().addAll(old.getGroupings()); for (GroupingBuilder grouping : old.getGroupingBuilders()) { - c.addGrouping(copy(grouping, c, updateQName)); + copy.addGrouping(copy(grouping, copy, updateQName)); } for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) { - c.addTypedef(copy(tdb, c, updateQName)); + copy.addTypedef(copy(tdb, copy, updateQName)); } for (UsesNodeBuilder oldUses : old.getUsesNodes()) { - c.addUsesNode(copyUses(oldUses, c)); + copy.addUsesNode(copyUses(oldUses, copy)); } for (AugmentationSchemaBuilder augment : old.getAugmentations()) { - c.addAugmentation(copyAugment(augment, c)); + copy.addAugmentation(copyAugment(augment, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - c.setUserOrdered(old.isUserOrdered()); - c.setKeyDefinition(old.getKeyDefinition()); + copy.setUserOrdered(old.isUserOrdered()); + copy.setKeyDefinition(old.getKeyDefinition()); - return c; + return copy; } - static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) { + public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) { DataBean data = getdata(old, newParent, updateQName); QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; - GroupingBuilderImpl c = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName); - c.setParent(newParent); - c.setPath(newSchemaPath); - c.setDescription(old.getDescription()); - c.setReference(old.getReference()); - c.setStatus(old.getStatus()); - c.setAddedByUses(old.isAddedByUses()); - c.setChildNodes(old.getChildNodes()); + GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setChildNodes(old.getChildNodes()); for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { - c.addChildNode(copy(childNode, c, updateQName)); + copy.addChildNode(copy(childNode, copy, updateQName)); } - c.getGroupings().addAll(old.getGroupings()); + copy.getGroupings().addAll(old.getGroupings()); for (GroupingBuilder grouping : old.getGroupingBuilders()) { - c.addGrouping(copy(grouping, c, updateQName)); + copy.addGrouping(copy(grouping, copy, updateQName)); } for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) { - c.addTypedef(copy(tdb, c, updateQName)); + copy.addTypedef(copy(tdb, copy, updateQName)); } for (UsesNodeBuilder oldUses : old.getUsesNodes()) { - c.addUsesNode(copyUses(oldUses, c)); + copy.addUsesNode(copyUses(oldUses, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - c.addUnknownNodeBuilder((copy(un, c, updateQName))); + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); } - return c; + return copy; + } + + public static GroupingBuilder copyGroupingWithoutDeep(GroupingBuilder old, Builder newParent, boolean updateQName) { + DataBean data = getdata(old, newParent, updateQName); + QName newQName = data.qname; + SchemaPath newSchemaPath = data.schemaPath; + + GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName); + copy.setParent(newParent); + copy.setPath(newSchemaPath); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.setAddedByUses(old.isAddedByUses()); + copy.setChildNodes(old.getChildNodes()); + for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { + copy.addChildNode(copy(childNode, copy, updateQName)); + } + copy.getGroupings().addAll(old.getGroupings()); + for (GroupingBuilder grouping : old.getGroupingBuilders()) { + copy.addGrouping(copy(grouping, copy, updateQName)); + } + for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) { + copy.addTypedef(copy(tdb, copy, updateQName)); + } + for (UsesNodeBuilder oldUses : old.getUsesNodes()) { + copy.addUsesNode(copyUses(oldUses, copy)); + + } + for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { + copy.addUnknownNodeBuilder((copy(un, copy, updateQName))); + } + + return copy; } - static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) { + public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) { DataBean data = getdata(old, newParent, updateQName); QName newQName = data.qname; SchemaPath newSchemaPath = data.schemaPath; TypeDefinitionBuilder type = null; if (old instanceof UnionTypeBuilder) { + UnionTypeBuilder oldUnion = (UnionTypeBuilder)old; type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine()); + type.setParent(newParent); + type.setPath(newSchemaPath); + for(TypeDefinition td : oldUnion.getTypes()) { + type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), td)); + } + for(TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) { + type.setTypedef(copy(tdb, type, updateQName)); + } } else if (old instanceof IdentityrefTypeBuilder) { type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(), ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath); + type.setParent(newParent); + type.setPath(newSchemaPath); } else { type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName); type.setParent(newParent); @@ -355,7 +413,7 @@ public class CopyUtils { if (old.getType() == null) { type.setTypedef(copy(old.getTypedef(), type, updateQName)); } else { - type.setType(old.getType()); + type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), old.getType())); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { @@ -387,88 +445,51 @@ public class CopyUtils { } static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) { - UsesNodeBuilder u = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(), + UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(), old.getGroupingName()); - u.setParent(newParent); - u.setGroupingPath(old.getGroupingPath()); - u.setAugmenting(old.isAugmenting()); - u.setAddedByUses(old.isAddedByUses()); - u.getAugmentations().addAll(old.getAugmentations()); - u.getRefineNodes().addAll(old.getRefineNodes()); - u.getRefines().addAll(old.getRefines()); - u.getFinalChildren().addAll(old.getFinalChildren()); - u.getFinalGroupings().addAll(old.getFinalGroupings()); - u.getFinalTypedefs().addAll(old.getFinalTypedefs()); - u.getFinalUnknownNodes().addAll(old.getFinalUnknownNodes()); - - Set oldChildren = old.getTargetChildren(); - Set newChildren = new HashSet<>(); - if (oldChildren != null) { - for (DataSchemaNodeBuilder child : old.getTargetChildren()) { - newChildren.add(CopyUtils.copy(child, newParent, true)); - } - } - u.setTargetChildren(newChildren); - - Set oldTypedefs = old.getTargetTypedefs(); - Set newTypedefs = new HashSet<>(); - if (oldTypedefs != null) { - for (TypeDefinitionBuilder typedef : old.getTargetTypedefs()) { - newTypedefs.add(CopyUtils.copy(typedef, newParent, true)); - } - } - u.setTargetTypedefs(newTypedefs); - - Set oldGroupings = old.getTargetGroupings(); - Set newGroupings = new HashSet<>(); - if (oldGroupings != null) { - for (GroupingBuilder grouping : old.getTargetGroupings()) { - newGroupings.add(copy(grouping, newParent, true)); - } - } - u.setTargetGroupings(newGroupings); - - List oldUN = old.getTargetUnknownNodes(); - List newUN = new ArrayList<>(); - if (oldUN != null) { - for (UnknownSchemaNodeBuilder un : oldUN) { - newUN.add(copy(un, newParent, true)); - } - } - u.setTargetUnknownNodes(newUN); - - for (UsesNodeBuilder uses : old.getTargetGroupingUses()) { - u.getTargetGroupingUses().add(copyUses(uses, uses.getParent())); - } + copy.setParent(newParent); + copy.setGroupingPath(old.getGroupingPath()); + // TODO grouping vs grouping path? + copy.setGrouping(old.getGroupingBuilder()); + copy.setAugmenting(old.isAugmenting()); + copy.setAddedByUses(old.isAddedByUses()); + copy.getAugmentations().addAll(old.getAugmentations()); + copy.getRefineNodes().addAll(old.getRefineNodes()); + copy.getRefines().addAll(old.getRefines()); + + copy.setTargetChildren(old.getTargetChildren()); + copy.setTargetTypedefs(old.getTargetTypedefs()); + copy.setTargetGroupings(old.getTargetGroupings()); + copy.setTargetUnknownNodes(old.getTargetUnknownNodes()); // add new uses to collection of uses in module ModuleBuilder module = ParserUtils.getParentModule(newParent); - module.addUsesNode(u); + module.addUsesNode(copy); - return u; + return copy; } private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) { - AugmentationSchemaBuilderImpl a = new AugmentationSchemaBuilderImpl(newParent.getModuleName(), + AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(), newParent.getLine(), old.getTargetPathAsString()); - a.setParent(newParent); + copy.setParent(newParent); - a.setDescription(old.getDescription()); - a.setReference(old.getReference()); - a.setStatus(old.getStatus()); - a.addWhenCondition(old.getWhenCondition()); - a.setChildNodes(old.getChildNodes()); + copy.setDescription(old.getDescription()); + copy.setReference(old.getReference()); + copy.setStatus(old.getStatus()); + copy.addWhenCondition(old.getWhenCondition()); + copy.setChildNodes(old.getChildNodes()); for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) { - a.addChildNode(copy(childNode, a, false)); + copy.addChildNode(copy(childNode, copy, false)); } for (UsesNodeBuilder oldUses : old.getUsesNodes()) { - a.addUsesNode(copyUses(oldUses, a)); + copy.addUsesNode(copyUses(oldUses, copy)); } for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) { - a.addUnknownNodeBuilder((copy(un, a, false))); + copy.addUnknownNodeBuilder((copy(un, copy, false))); } - return a; + return copy; } static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) { @@ -525,6 +546,9 @@ public class CopyUtils { newPath = new ArrayList<>(parent.getPath().getPath()); newPath.add(newQName); } else { + if(old == null) { + System.out.println(); + } newQName = old.getQName(); newPath = new ArrayList<>(parent.getPath().getPath()); newPath.add(newQName); @@ -545,4 +569,270 @@ public class CopyUtils { } } + + /** + * Create AnyXmlBuilder from given AnyXmlSchemaNode. + * + * @param anyxml + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return anyxml builder based on given anyxml node + */ + public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) { + final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath()); + convertDataSchemaNode(anyxml, builder); + builder.setConfiguration(anyxml.isConfiguration()); + builder.setUnknownNodes(anyxml.getUnknownSchemaNodes()); + return builder; + } + + /** + * Create GroupingBuilder from given GroupingDefinition. + * + * @param grouping + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return grouping builder based on given grouping node + */ + public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) { + final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname); + builder.setPath(grouping.getPath()); + builder.setChildNodes(grouping.getChildNodes()); + builder.setGroupings(grouping.getGroupings()); + builder.setTypedefs(grouping.getTypeDefinitions()); + builder.setUsesnodes(grouping.getUses()); + builder.setUnknownNodes(grouping.getUnknownSchemaNodes()); + builder.setDescription(grouping.getDescription()); + builder.setReference(grouping.getReference()); + builder.setStatus(grouping.getStatus()); + return builder; + } + + /** + * Create TypeDefinitionBuilder from given ExtendedType. + * + * @param typedef + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return typedef builder based on given typedef node + */ + public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) { + final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname); + builder.setPath(typedef.getPath()); + builder.setDefaultValue(typedef.getDefaultValue()); + builder.setUnits(typedef.getUnits()); + builder.setDescription(typedef.getDescription()); + builder.setReference(typedef.getReference()); + builder.setStatus(typedef.getStatus()); + builder.setRanges(typedef.getRanges()); + builder.setLengths(typedef.getLengths()); + builder.setPatterns(typedef.getPatterns()); + builder.setFractionDigits(typedef.getFractionDigits()); + final TypeDefinition type = typedef.getBaseType(); + builder.setType(type); + builder.setUnits(typedef.getUnits()); + builder.setUnknownNodes(typedef.getUnknownSchemaNodes()); + return builder; + } + + /** + * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode. + * + * @param unknownNode + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return unknown node builder based on given unknown node + */ + public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname, + String moduleName, int line) { + final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname); + builder.setPath(unknownNode.getPath()); + builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes()); + builder.setDescription(unknownNode.getDescription()); + builder.setReference(unknownNode.getReference()); + builder.setStatus(unknownNode.getStatus()); + builder.setAddedByUses(unknownNode.isAddedByUses()); + builder.setNodeType(unknownNode.getNodeType()); + builder.setNodeParameter(unknownNode.getNodeParameter()); + return builder; + } + + + /** + * Create LeafSchemaNodeBuilder from given LeafSchemaNode. + * + * @param leaf + * leaf from which to create builder + * @param qname + * @param moduleName + * current module name + * @param line + * line in module + * @return leaf builder based on given leaf node + */ + public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) { + final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath()); + convertDataSchemaNode(leaf, builder); + builder.setConfiguration(leaf.isConfiguration()); + final TypeDefinition type = leaf.getType(); + builder.setType(type); + builder.setPath(leaf.getPath()); + builder.setUnknownNodes(leaf.getUnknownSchemaNodes()); + builder.setDefaultStr(leaf.getDefault()); + builder.setUnits(leaf.getUnits()); + return builder; + } + + /** + * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode. + * + * @param container + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return container builder based on given container node + */ + public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname, + String moduleName, int line) { + final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname, + container.getPath()); + convertDataSchemaNode(container, builder); + builder.setConfiguration(container.isConfiguration()); + builder.setUnknownNodes(container.getUnknownSchemaNodes()); + builder.setChildNodes(container.getChildNodes()); + builder.setGroupings(container.getGroupings()); + builder.setTypedefs(container.getTypeDefinitions()); + builder.setAugmentations(container.getAvailableAugmentations()); + builder.setUsesnodes(container.getUses()); + builder.setPresence(container.isPresenceContainer()); + return builder; + } + + /** + * Create ListSchemaNodeBuilder from given ListSchemaNode. + * + * @param list + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return list builder based on given list node + */ + public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) { + ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath()); + convertDataSchemaNode(list, builder); + builder.setConfiguration(list.isConfiguration()); + builder.setUnknownNodes(list.getUnknownSchemaNodes()); + builder.setTypedefs(list.getTypeDefinitions()); + builder.setChildNodes(list.getChildNodes()); + builder.setGroupings(list.getGroupings()); + builder.setAugmentations(list.getAvailableAugmentations()); + builder.setUsesnodes(list.getUses()); + builder.setUserOrdered(builder.isUserOrdered()); + return builder; + } + + /** + * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode. + * + * @param leafList + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return leaf-list builder based on given leaf-list node + */ + public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName, + int line) { + final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname, + leafList.getPath()); + convertDataSchemaNode(leafList, builder); + builder.setConfiguration(leafList.isConfiguration()); + builder.setType(leafList.getType()); + builder.setUnknownNodes(leafList.getUnknownSchemaNodes()); + builder.setUserOrdered(leafList.isUserOrdered()); + return builder; + } + + /** + * Create ChoiceBuilder from given ChoiceNode. + * + * @param choice + * @param qname + * @param moduleName + * current module name + * @param line + * current line in module + * @return choice builder based on given choice node + */ + public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) { + final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname); + convertDataSchemaNode(choice, builder); + builder.setConfiguration(choice.isConfiguration()); + builder.setCases(choice.getCases()); + builder.setUnknownNodes(choice.getUnknownSchemaNodes()); + builder.setDefaultCase(choice.getDefaultCase()); + return builder; + } + + + /** + * Set DataSchemaNode arguments to builder object + * + * @param node + * node from which arguments should be read + * @param builder + * builder to which arguments should be set + */ + private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) { + builder.setPath(node.getPath()); + builder.setDescription(node.getDescription()); + builder.setReference(node.getReference()); + builder.setStatus(node.getStatus()); + builder.setAugmenting(node.isAugmenting()); + copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints()); + } + + /** + * Copy constraints from constraints definition to constraints builder. + * + * @param nodeConstraints + * definition from which constraints will be copied + * @param constraints + * builder to which constraints will be added + */ + private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints, + final ConstraintsBuilder constraints) { + final RevisionAwareXPath when = nodeConstraints.getWhenCondition(); + final Set must = nodeConstraints.getMustConstraints(); + + if (when != null) { + constraints.addWhenCondition(when.toString()); + } + if (must != null) { + for (MustDefinition md : must) { + constraints.addMustDefinition(md); + } + } + constraints.setMandatory(nodeConstraints.isMandatory()); + constraints.setMinElements(nodeConstraints.getMinElements()); + constraints.setMaxElements(nodeConstraints.getMaxElements()); + } + } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java index a09b1a2cb1..9dda792c74 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java @@ -7,6 +7,8 @@ */ package org.opendaylight.yangtools.yang.parser.util; +import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.createSchemaPath; + import java.net.URI; import java.util.ArrayList; import java.util.Date; @@ -16,10 +18,20 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; +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.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; @@ -175,183 +187,269 @@ public class GroupingUtils { } /** - * Copy target grouping data to given uses node. - *

- * Copy all data-schema-nodes, groupings, typedefs and unknown nodes from - * target grouping to uses node. - *

+ * Add nodes defined in uses target grouping to uses parent. * * @param usesNode - * @param targetGrouping */ - public static void loadTargetGroupingData(final UsesNodeBuilder usesNode, final GroupingBuilder targetGrouping) { + public static void updateUsesParent(UsesNodeBuilder usesNode) { + DataNodeContainerBuilder parent = usesNode.getParent(); + // child nodes - Set targetChildren = new HashSet<>(); - for (DataSchemaNodeBuilder targetChild : targetGrouping.getChildNodeBuilders()) { - targetChildren.add(CopyUtils.copy(targetChild, usesNode.getParent(), true)); + for (DataSchemaNodeBuilder child : usesNode.getTargetChildren()) { + if (child instanceof GroupingMember) { + ((GroupingMember) child).setAddedByUses(true); + } + parent.addChildNode(child); } - usesNode.setTargetChildren(targetChildren); // groupings - Set targetGroupingGroupings = new HashSet<>(); - for (GroupingBuilder targetGroupingGrouping : targetGrouping.getGroupingBuilders()) { - targetGroupingGroupings.add(CopyUtils.copy(targetGroupingGrouping, usesNode.getParent(), true)); + for (GroupingBuilder gb : usesNode.getTargetGroupings()) { + gb.setAddedByUses(true); + parent.addGrouping(gb); } - usesNode.setTargetGroupings(targetGroupingGroupings); // typedefs - Set targetGroupingTypedefs = new HashSet<>(); - for(TypeDefinitionBuilder targetGroupingTypedef : targetGrouping.getTypeDefinitionBuilders()) { - targetGroupingTypedefs.add(CopyUtils.copy(targetGroupingTypedef, usesNode.getParent(), true)); + for (TypeDefinitionBuilder tdb : usesNode.getTargetTypedefs()) { + tdb.setAddedByUses(true); + parent.addTypedef(tdb); } - usesNode.setTargetTypedefs(targetGroupingTypedefs); // unknown nodes - List targetGroupingUNs = new ArrayList<>(); - for(UnknownSchemaNodeBuilder targetGroupingUN : targetGrouping.getUnknownNodeBuilders()) { - targetGroupingUNs.add(CopyUtils.copy(targetGroupingUN, usesNode.getParent(), true)); + for (UnknownSchemaNodeBuilder un : usesNode.getTargetUnknownNodes()) { + un.setAddedByUses(true); + parent.addUnknownNodeBuilder(un); } - usesNode.setTargetUnknownNodes(targetGroupingUNs); + } - usesNode.setLoadDone(true); + public static void collectUsesData(UsesNodeBuilder usesNode) { + usesNode.setTargetChildren(collectUsesChildNodes(usesNode)); + usesNode.setTargetTypedefs(collectUsesTypedefs(usesNode)); + usesNode.setTargetGroupings(collectUsesGroupings(usesNode)); + usesNode.setTargetUnknownNodes(collectUsesUnknownNodes(usesNode)); + usesNode.setDataCollected(true); } - /** - * Copy all data from target grouping which were added by uses. - *

- * Traverse uses statements in target grouping and copy all - * data-schema-nodes, groupings, typedefs and unknown nodes to current uses - * node. - *

- * - * @param usesNode - * @param targetGrouping - */ - public static void loadTargetGroupingUses(final UsesNodeBuilder usesNode, final GroupingBuilder targetGrouping) { - usesNode.getTargetGroupingUses().addAll(targetGrouping.getUsesNodes()); + private static Set collectUsesChildNodes(UsesNodeBuilder usesNode) { + final GroupingBuilder target = usesNode.getGroupingBuilder(); + Set childNodes = target.getChildNodeBuilders(); + Set copies = new HashSet<>(); + for (DataSchemaNodeBuilder childNode : childNodes) { + copies.add(CopyUtils.copy(childNode, usesNode.getParent(), true)); + } + for (UsesNodeBuilder targetUses : target.getUsesNodes()) { + copies.addAll(collectUsesChildNodes(targetUses)); + } + return copies; } - /** - * Create copy of collection of given nodes with new schema path. - * - * @param nodes - * nodes to copy - * @param parentPath - * schema path of parent node - * @param namespace - * new namespace of node qname - * @param revision - * new revision of node qname - * @param prefix - * new prefix of node qname - * @param moduleName - * current yang module name - * @param line - * current line in yang module - * @return collection of new nodes with corrected path - */ - public static Set copyUsesTargetNodesWithNewPath(UsesNodeBuilder usesNode, Builder parent) { - Set newNodes = new HashSet<>(); + private static Set collectUsesTypedefs(UsesNodeBuilder usesNode) { + final GroupingBuilder target = usesNode.getGroupingBuilder(); + Set typedefs = target.getTypeDefinitionBuilders(); + Set copies = new HashSet<>(); + for (TypeDefinitionBuilder typedef : typedefs) { + copies.add(CopyUtils.copy(typedef, usesNode.getParent(), true)); + } + for (UsesNodeBuilder targetUses : target.getUsesNodes()) { + copies.addAll(collectUsesTypedefs(targetUses)); + } + return copies; + } - for (DataSchemaNodeBuilder node : usesNode.getTargetChildren()) { - if (node != null) { - if (node instanceof GroupingMember) { - ((GroupingMember) node).setAddedByUses(true); - } - newNodes.add(node); - } + private static Set collectUsesGroupings(UsesNodeBuilder usesNode) { + final GroupingBuilder target = usesNode.getGroupingBuilder(); + Set groupings = target.getGroupingBuilders(); + Set copies = new HashSet<>(); + for (GroupingBuilder grouping : groupings) { + copies.add(CopyUtils.copy(grouping, usesNode.getParent(), true)); + } + for (UsesNodeBuilder targetUses : target.getUsesNodes()) { + copies.addAll(collectUsesGroupings(targetUses)); } + return copies; + } - return newNodes; + private static List collectUsesUnknownNodes(UsesNodeBuilder usesNode) { + final GroupingBuilder target = usesNode.getGroupingBuilder(); + List unknownNodes = target.getUnknownNodeBuilders(); + List copies = new ArrayList<>(); + for (UnknownSchemaNodeBuilder unknownNode : unknownNodes) { + copies.add(CopyUtils.copy(unknownNode, usesNode.getParent(), true)); + } + for (UsesNodeBuilder targetUses : target.getUsesNodes()) { + copies.addAll(collectUsesUnknownNodes(targetUses)); + } + return copies; } - /** - * Create copy of collection of given groupings with new schema path. - * - * @param groupings - * groupings to copy - * @param parentPath - * schema path of parent node - * @param namespace - * new namespace of node qname - * @param revision - * new revision of node qname - * @param prefix - * new prefix of node qname - * @return collection of new groupings with corrected path - */ - public static Set copyUsesTargetGroupingsWithNewPath(UsesNodeBuilder usesNode, - SchemaPath parentPath, URI namespace, Date revision, String prefix) { - Set newGroupings = new HashSet<>(); - for (GroupingBuilder node : usesNode.getTargetGroupings()) { - if (node != null) { - if (node instanceof GroupingMember) { - ((GroupingMember) node).setAddedByUses(true); + public static void collectUsesDataFromContext(UsesNodeBuilder usesNode) { + DataNodeContainerBuilder parent = usesNode.getParent(); + URI namespace = parent.getQName().getNamespace(); + Date revision = parent.getQName().getRevision(); + String prefix = parent.getQName().getPrefix(); + String moduleName = parent.getModuleName(); + int line = parent.getLine(); + + // child nodes + final Set newChildren = new HashSet<>(); + for (DataSchemaNode child : usesNode.getGroupingDefinition().getChildNodes()) { + if (child != null) { + DataSchemaNodeBuilder newChild = null; + QName newQName = new QName(namespace, revision, prefix, child.getQName().getLocalName()); + if (child instanceof AnyXmlSchemaNode) { + newChild = CopyUtils.createAnyXml((AnyXmlSchemaNode) child, newQName, moduleName, line); + } else if (child instanceof ChoiceNode) { + newChild = CopyUtils.createChoice((ChoiceNode) child, newQName, moduleName, line); + } else if (child instanceof ContainerSchemaNode) { + newChild = CopyUtils.createContainer((ContainerSchemaNode) child, newQName, moduleName, line); + } else if (child instanceof LeafListSchemaNode) { + newChild = CopyUtils.createLeafList((LeafListSchemaNode) child, newQName, moduleName, line); + } else if (child instanceof LeafSchemaNode) { + newChild = CopyUtils.createLeafBuilder((LeafSchemaNode) child, newQName, moduleName, line); + } else if (child instanceof ListSchemaNode) { + newChild = CopyUtils.createList((ListSchemaNode) child, newQName, moduleName, line); + } + + if (newChild == null) { + throw new YangParseException(moduleName, line, + "Unknown member of target grouping while resolving uses node."); + } + if (newChild instanceof GroupingMember) { + ((GroupingMember) newChild).setAddedByUses(true); } - newGroupings.add(node); + + newChild.setPath(createSchemaPath(parent.getPath(), newQName)); + newChildren.add(newChild); } } + usesNode.setTargetChildren(newChildren); + + // groupings + final Set newGroupings = new HashSet<>(); + for (GroupingDefinition g : usesNode.getGroupingDefinition().getGroupings()) { + QName newQName = new QName(namespace, revision, prefix, g.getQName().getLocalName()); + GroupingBuilder newGrouping = CopyUtils.createGrouping(g, newQName, moduleName, line); + newGrouping.setAddedByUses(true); + newGrouping.setPath(createSchemaPath(parent.getPath(), newQName)); + newGroupings.add(newGrouping); + } + usesNode.setTargetGroupings(newGroupings); + + // typedefs + final Set newTypedefs = new HashSet<>(); + for (TypeDefinition td : usesNode.getGroupingDefinition().getTypeDefinitions()) { + QName newQName = new QName(namespace, revision, prefix, td.getQName().getLocalName()); + TypeDefinitionBuilder newType = CopyUtils.createTypedef((ExtendedType) td, newQName, moduleName, line); + newType.setAddedByUses(true); + newType.setPath(createSchemaPath(parent.getPath(), newQName)); + newTypedefs.add(newType); + } + usesNode.setTargetTypedefs(newTypedefs); + + // unknown nodes + final List newUnknownNodes = new ArrayList<>(); + for (UnknownSchemaNode un : usesNode.getGroupingDefinition().getUnknownSchemaNodes()) { + QName newQName = new QName(namespace, revision, prefix, un.getQName().getLocalName()); + UnknownSchemaNodeBuilder newNode = CopyUtils.createUnknownSchemaNode(un, newQName, moduleName, line); + newNode.setAddedByUses(true); + newNode.setPath(createSchemaPath(parent.getPath(), newQName)); + newUnknownNodes.add(newNode); + } + usesNode.setTargetUnknownNodes(newUnknownNodes); - return newGroupings; + usesNode.setDataCollected(true); } - /** - * Create copy of collection of given typedefs with new schema path. - * - * @param typedefs - * typedefs to copy - * @param parentPath - * schema path of parent node - * @param namespace - * new namespace of node qname - * @param revision - * new revision of node qname - * @param prefix - * new prefix of node qname - * @return collection of new typedefs with corrected path - */ - public static Set copyUsesTargetTypedefsWithNewPath(UsesNodeBuilder usesNode, - SchemaPath parentPath, URI namespace, Date revision, String prefix) { - Set newTypedefs = new HashSet<>(); - - for (TypeDefinitionBuilder node : usesNode.getTargetTypedefs()) { - if (node != null) { - if (node instanceof GroupingMember) { - ((GroupingMember) node).setAddedByUses(true); + public static void fixUsesNodesPath(UsesNodeBuilder usesNode) { + DataNodeContainerBuilder parent = usesNode.getParent(); + + // child nodes + Set currentChildNodes = parent.getChildNodeBuilders(); + Set toRemove = new HashSet<>(); + Set toAdd = new HashSet<>(); + for (DataSchemaNodeBuilder child : currentChildNodes) { + if (child instanceof GroupingMember) { + GroupingMember gm = (GroupingMember) child; + if (gm.isAddedByUses()) { + toRemove.add(child); + DataSchemaNodeBuilder copy = CopyUtils.copy(child, parent, true); + ParserUtils.correctNodePath(copy, parent.getPath()); + toAdd.add(copy); } - newTypedefs.add(node); } } + currentChildNodes.removeAll(toRemove); + currentChildNodes.addAll(toAdd); - return newTypedefs; + // groupings + Set currentGroupings = parent.getGroupingBuilders(); + Set toRemoveG = new HashSet<>(); + Set toAddG = new HashSet<>(); + for (GroupingBuilder child : currentGroupings) { + if (child.isAddedByUses()) { + toRemoveG.add(child); + GroupingBuilder copy = CopyUtils.copy(child, parent, true); + ParserUtils.correctNodePath(copy, parent.getPath()); + toAddG.add(copy); + } + + } + currentGroupings.removeAll(toRemoveG); + currentGroupings.addAll(toAddG); + + // typedefs + Set currentTypedefs = parent.getTypeDefinitionBuilders(); + Set toRemoveTD = new HashSet<>(); + Set toAddTD = new HashSet<>(); + for (TypeDefinitionBuilder child : currentTypedefs) { + if (child.isAddedByUses()) { + toRemoveTD.add(child); + TypeDefinitionBuilder copy = CopyUtils.copy(child, parent, true); + ParserUtils.correctNodePath(copy, parent.getPath()); + toAddTD.add(copy); + } + + } + currentTypedefs.removeAll(toRemoveTD); + currentTypedefs.addAll(toAddTD); + + // unknown nodes + List currentUN = parent.getUnknownNodeBuilders(); + List toRemoveUN = new ArrayList<>(); + List toAddUN = new ArrayList<>(); + for (UnknownSchemaNodeBuilder un : currentUN) { + if (un.isAddedByUses()) { + toRemoveUN.add(un); + UnknownSchemaNodeBuilder copy = CopyUtils.copy(un, parent, true); + ParserUtils.correctNodePath(copy, parent.getPath()); + toAddUN.add(copy); + } + } + currentUN.removeAll(toRemoveUN); + currentUN.addAll(toAddUN); } /** - * Create copy of collection of given unknown nodes with new schema path. + * Perform refinement of uses target grouping nodes. Uses process has to be + * already performed. * * @param usesNode - * @param parentPath - * schema path of parent node - * @param namespace - * new namespace of node qname - * @param revision - * new revision of node qname - * @param prefix - * new prefix of node qname - * @return collection of new unknownNodes with corrected path */ - public static List copyUsesTargetUnknownNodesWithNewPath(UsesNodeBuilder usesNode, - SchemaPath parentPath, URI namespace, Date revision, String prefix) { - List newUnknownNodes = new ArrayList<>(); - - for (UnknownSchemaNodeBuilder node : usesNode.getTargetUnknownNodes()) { - if (node != null) { - node.setAddedByUses(true); - newUnknownNodes.add(node); + public static void performRefine(UsesNodeBuilder usesNode) { + for (RefineHolder refine : usesNode.getRefines()) { + DataSchemaNodeBuilder nodeToRefine = null; + for (DataSchemaNodeBuilder dataNode : usesNode.getParent().getChildNodeBuilders()) { + if (refine.getName().equals(dataNode.getQName().getLocalName())) { + nodeToRefine = dataNode; + break; + } + } + if (nodeToRefine == null) { + throw new YangParseException(refine.getModuleName(), refine.getLine(), "Refine target node '" + + refine.getName() + "' not found"); } + RefineUtils.performRefine(nodeToRefine, refine); + usesNode.addRefineNode(nodeToRefine); } - - return newUnknownNodes; } } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java index cd38293582..51d7e3120b 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java @@ -238,29 +238,14 @@ public final class ParserListenerUtils { } /** - * Create SchemaPath from actualPath and names. + * Create SchemaPath from actualPath and new node name. * * @param actualPath * current position in model - * @param namespace - * @param revision - * @param prefix - * @param names - * @return SchemaPath object. + * @return SchemaPath object */ - public static SchemaPath createActualSchemaPath(final List actualPath, final URI namespace, - final Date revision, final String prefix, final String... names) { - final List path = new ArrayList(); - QName qname; - // start from index 1 - module name omited - for (int i = 1; i < actualPath.size(); i++) { - qname = new QName(namespace, revision, prefix, actualPath.get(i)); - path.add(qname); - } - for (String name : names) { - qname = new QName(namespace, revision, prefix, name); - path.add(qname); - } + public static SchemaPath createActualSchemaPath(final Stack actualPath) { + final List path = new ArrayList(actualPath); return new SchemaPath(path, true); } @@ -300,14 +285,12 @@ public final class ParserListenerUtils { * type body context to parse * @param path * actual position in YANG model - * @param moduleName current module name - * @param namespace - * @param revision - * @param prefix + * @param moduleName + * current module name * @return List of EnumPair object parsed from given context */ private static List getEnumConstants(final Type_body_stmtsContext ctx, - final List path, final String moduleName, final URI namespace, final Date revision, final String prefix) { + final Stack path, final String moduleName) { List enumConstants = new ArrayList(); for (int i = 0; i < ctx.getChildCount(); i++) { @@ -317,8 +300,7 @@ public final class ParserListenerUtils { for (int j = 0; j < enumSpecChild.getChildCount(); j++) { ParseTree enumChild = enumSpecChild.getChild(j); if (enumChild instanceof Enum_stmtContext) { - EnumPair enumPair = createEnumPair((Enum_stmtContext) enumChild, highestValue, path, moduleName, namespace, - revision, prefix); + EnumPair enumPair = createEnumPair((Enum_stmtContext) enumChild, highestValue, path, moduleName); if (enumPair.getValue() > highestValue) { highestValue = enumPair.getValue(); } @@ -341,25 +323,18 @@ public final class ParserListenerUtils { * actual position in YANG model * @param moduleName * current module name - * @param namespace - * @param revision - * @param prefix * @return EnumPair object parsed from given context */ private static EnumTypeDefinition.EnumPair createEnumPair(final Enum_stmtContext ctx, final int highestValue, - final List path, final String moduleName, final URI namespace, final Date revision, - final String prefix) { + final Stack actualPath, final String moduleName) { final String name = stringFromNode(ctx); - final QName qname = new QName(namespace, revision, prefix, name); + SchemaPath path = createTypePath(actualPath, name); Integer value = null; String description = null; String reference = null; Status status = null; - List enumPairPath = new ArrayList(path); - enumPairPath.add(name); - for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); if (child instanceof Value_stmtContext) { @@ -383,8 +358,8 @@ public final class ParserListenerUtils { } EnumPairImpl result = new EnumPairImpl(); - result.qname = qname; - result.path = createActualSchemaPath(enumPairPath, namespace, revision, prefix); + result.qname = path.getPath().get(path.getPath().size() - 1); + result.path = path; result.description = description; result.reference = reference; result.status = status; @@ -519,6 +494,7 @@ public final class ParserListenerUtils { * * @param ctx * type body context to parse + * @param moduleName * @return List of RangeConstraint created from this context */ private static List getRangeConstraints(final Type_body_stmtsContext ctx, final String moduleName) { @@ -543,6 +519,7 @@ public final class ParserListenerUtils { * * @param ctx * range context to parse + * @param moduleName * @return List of RangeConstraints parsed from this context */ private static List parseRangeConstraints(final Range_stmtContext ctx, final String moduleName) { @@ -585,6 +562,7 @@ public final class ParserListenerUtils { * * @param ctx * type body context to parse + * @param moduleName * @return List of LengthConstraint created from this context */ private static List getLengthConstraints(final Type_body_stmtsContext ctx, final String moduleName) { @@ -609,6 +587,7 @@ public final class ParserListenerUtils { * * @param ctx * length context to parse + * @param moduleName * @return List of LengthConstraints parsed from this context */ private static List parseLengthConstraints(final Length_stmtContext ctx, final String moduleName) { @@ -649,6 +628,8 @@ public final class ParserListenerUtils { /** * @param value * value to parse + * @param moduleName name of current module + * @param line current line in module * @return wrapper object of primitive java type or UnknownBoundaryNumber if * type is one of special YANG values 'min' or 'max' */ @@ -790,14 +771,12 @@ public final class ParserListenerUtils { * type body context to parse * @param actualPath * current position in YANG model - * @param moduleName current module name - * @param namespace - * @param revision - * @param prefix + * @param moduleName + * current module name * @return List of Bit objects created from this context */ - private static List getBits(Type_body_stmtsContext ctx, List actualPath, - String moduleName, URI namespace, Date revision, String prefix) { + private static List getBits(Type_body_stmtsContext ctx, Stack actualPath, + String moduleName) { final List bits = new ArrayList(); for (int j = 0; j < ctx.getChildCount(); j++) { ParseTree bitsSpecChild = ctx.getChild(j); @@ -806,8 +785,7 @@ public final class ParserListenerUtils { for (int k = 0; k < bitsSpecChild.getChildCount(); k++) { ParseTree bitChild = bitsSpecChild.getChild(k); if (bitChild instanceof Bit_stmtContext) { - Bit bit = parseBit((Bit_stmtContext) bitChild, highestPosition, actualPath, moduleName, namespace, - revision, prefix); + Bit bit = parseBit((Bit_stmtContext) bitChild, highestPosition, actualPath, moduleName); if (bit.getPosition() > highestPosition) { highestPosition = bit.getPosition(); } @@ -828,27 +806,20 @@ public final class ParserListenerUtils { * current highest position in bits type * @param actualPath * current position in YANG model - * @param moduleName current module name - * @param namespace - * @param revision - * @param prefix + * @param moduleName + * current module name * @return Bit object parsed from this context */ private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx, long highestPosition, - List actualPath, final String moduleName, final URI namespace, final Date revision, final String prefix) { + Stack actualPath, final String moduleName) { String name = stringFromNode(ctx); - final QName qname = new QName(namespace, revision, prefix, name); Long position = null; String description = null; String reference = null; Status status = Status.CURRENT; - Stack bitPath = new Stack(); - bitPath.addAll(actualPath); - bitPath.add(name); - - SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace, revision, prefix); + SchemaPath schemaPath = createBaseTypePath(actualPath, name); for (int i = 0; i < ctx.getChildCount(); i++) { ParseTree child = ctx.getChild(i); @@ -873,7 +844,8 @@ public final class ParserListenerUtils { } final List unknownNodes = Collections.emptyList(); - return new BitImpl(position, qname, schemaPath, description, reference, status, unknownNodes); + return new BitImpl(position, schemaPath.getPath().get(schemaPath.getPath().size() - 1), schemaPath, + description, reference, status, unknownNodes); } /** @@ -975,7 +947,8 @@ public final class ParserListenerUtils { * * @param ctx * config context to parse - * @param moduleName current module name + * @param moduleName + * current module name * @return true if given context contains string 'true', false otherwise */ private static Boolean parseConfig(final Config_stmtContext ctx, final String moduleName) { @@ -1016,7 +989,7 @@ public final class ParserListenerUtils { * @return UnknownType object with constraints from parsed type body */ public static TypeDefinition parseUnknownTypeWithBody(final QName typedefQName, - final Type_body_stmtsContext ctx, final List actualPath, final URI namespace, final Date revision, + final Type_body_stmtsContext ctx, final Stack actualPath, final URI namespace, final Date revision, final String prefix, final Builder parent) { String moduleName = parent.getModuleName(); String typeName = typedefQName.getLocalName(); @@ -1040,8 +1013,7 @@ public final class ParserListenerUtils { TypeDefinition baseType = unknownType.build(); TypeDefinition result = null; QName qname = new QName(namespace, revision, prefix, typeName); - SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, - false); + SchemaPath schemaPath = createTypePath(actualPath, typeName); ExtendedType.Builder typeBuilder = new ExtendedType.Builder(qname, baseType, null, null, schemaPath); typeBuilder.ranges(rangeStatements); @@ -1076,9 +1048,10 @@ public final class ParserListenerUtils { * parent builder * @return TypeDefinition object based on parsed values. */ - public static TypeDefinition parseTypeWithBody(final String typeName, - final Type_body_stmtsContext typeBody, final List actualPath, final URI namespace, - final Date revision, final String prefix, final Builder parent) { + public static TypeDefinition parseTypeWithBody(final String typeName, final Type_body_stmtsContext typeBody, + final Stack actualPath, final URI namespace, final Date revision, final String prefix, + final Builder parent) { + final String moduleName = parent.getModuleName(); final int line = typeBody.getStart().getLine(); TypeDefinition baseType = null; @@ -1094,27 +1067,30 @@ public final class ParserListenerUtils { constraints.addPatterns(patternStatements); constraints.addRanges(rangeStatements); - SchemaPath baseTypePathFinal = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, - true); - SchemaPath baseTypePath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, true, false); + SchemaPath baseTypePath = createBaseTypePath(actualPath, typeName); + SchemaPath extBaseTypePath = createExtendedBaseTypePath(actualPath, namespace, revision, prefix, typeName); + + if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) { + extBaseTypePath = baseTypePath; + } if ("decimal64".equals(typeName)) { if (rangeStatements.isEmpty()) { - return new Decimal64(baseTypePathFinal, fractionDigits); + return new Decimal64(baseTypePath, fractionDigits); } - Decimal64 decimalType = new Decimal64(baseTypePath, fractionDigits); + Decimal64 decimalType = new Decimal64(extBaseTypePath, fractionDigits); constraints.addRanges(decimalType.getRangeStatements()); baseType = decimalType; } else if (typeName.startsWith("int")) { IntegerTypeDefinition intType = null; if ("int8".equals(typeName)) { - intType = new Int8(baseTypePath); + intType = new Int8(extBaseTypePath); } else if ("int16".equals(typeName)) { - intType = new Int16(baseTypePath); + intType = new Int16(extBaseTypePath); } else if ("int32".equals(typeName)) { - intType = new Int32(baseTypePath); + intType = new Int32(extBaseTypePath); } else if ("int64".equals(typeName)) { - intType = new Int64(baseTypePath); + intType = new Int64(extBaseTypePath); } if (intType == null) { throw new YangParseException(moduleName, line, "Unknown yang type " + typeName); @@ -1124,13 +1100,13 @@ public final class ParserListenerUtils { } else if (typeName.startsWith("uint")) { UnsignedIntegerTypeDefinition uintType = null; if ("uint8".equals(typeName)) { - uintType = new Uint8(baseTypePath); + uintType = new Uint8(extBaseTypePath); } else if ("uint16".equals(typeName)) { - uintType = new Uint16(baseTypePath); + uintType = new Uint16(extBaseTypePath); } else if ("uint32".equals(typeName)) { - uintType = new Uint32(baseTypePath); + uintType = new Uint32(extBaseTypePath); } else if ("uint64".equals(typeName)) { - uintType = new Uint64(baseTypePath); + uintType = new Uint64(extBaseTypePath); } if (uintType == null) { throw new YangParseException(moduleName, line, "Unknown yang type " + typeName); @@ -1138,27 +1114,26 @@ public final class ParserListenerUtils { constraints.addRanges(uintType.getRangeStatements()); baseType = uintType; } else if ("enumeration".equals(typeName)) { - List enumConstants = getEnumConstants(typeBody, actualPath, moduleName, namespace, - revision, prefix); - return new EnumerationType(baseTypePathFinal, enumConstants); + List enumConstants = getEnumConstants(typeBody, actualPath, moduleName); + return new EnumerationType(baseTypePath, enumConstants); } else if ("string".equals(typeName)) { - StringTypeDefinition stringType = new StringType(baseTypePath); + StringTypeDefinition stringType = new StringType(extBaseTypePath); constraints.addLengths(stringType.getLengthStatements()); baseType = stringType; } else if ("bits".equals(typeName)) { - return new BitsType(baseTypePathFinal, getBits(typeBody, actualPath, moduleName, namespace, revision, prefix)); + return new BitsType(baseTypePath, getBits(typeBody, actualPath, moduleName)); } else if ("leafref".equals(typeName)) { final String path = parseLeafrefPath(typeBody); final boolean absolute = path.startsWith("/"); RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path, absolute); - return new Leafref(baseTypePathFinal, xpath); + return new Leafref(baseTypePath, xpath); } else if ("binary".equals(typeName)) { - BinaryTypeDefinition binaryType = new BinaryType(baseTypePath); + BinaryTypeDefinition binaryType = new BinaryType(extBaseTypePath); constraints.addLengths(binaryType.getLengthConstraints()); baseType = binaryType; } else if ("instance-identifier".equals(typeName)) { boolean requireInstance = isRequireInstance(typeBody); - return new InstanceIdentifier(baseTypePath, null, requireInstance); + return new InstanceIdentifier(extBaseTypePath, null, requireInstance); } if (parent instanceof TypeDefinitionBuilder && !(parent instanceof UnionTypeBuilder)) { @@ -1171,10 +1146,13 @@ public final class ParserListenerUtils { } TypeDefinition result = null; - QName qname = new QName(namespace, revision, prefix, typeName); ExtendedType.Builder typeBuilder = null; - SchemaPath schemaPath = createTypeSchemaPath(actualPath, namespace, revision, prefix, typeName, false, false); + List path = new ArrayList(actualPath); + path.add(new QName(namespace, revision, prefix, typeName)); + SchemaPath schemaPath = new SchemaPath(path, true); + + QName qname = schemaPath.getPath().get(schemaPath.getPath().size() - 1); typeBuilder = new ExtendedType.Builder(qname, baseType, "", "", schemaPath); typeBuilder.ranges(constraints.getRange()); @@ -1186,47 +1164,30 @@ public final class ParserListenerUtils { return result; } - /** - * Create SchemaPath object from given path list with namespace, revision - * and prefix based on given values. - * - * @param actualPath - * current position in model - * @param namespace - * @param revision - * @param prefix - * @param typeName - * @param isBaseYangType - * if this is base yang type - * @param isBaseYangTypeFinal - * if this is base yang type without restrictions - * @return SchemaPath object. - */ - private static SchemaPath createTypeSchemaPath(final List actualPath, final URI namespace, - final Date revision, final String prefix, final String typeName, final boolean isBaseYangType, - final boolean isBaseYangTypeFinal) { - List typePath = new ArrayList(actualPath); - if (isBaseYangType && !isBaseYangTypeFinal) { - typePath.add(typeName); - } - - final List path = new ArrayList(); - QName qname; - // start from index 1 -> module name omited - for (int i = 1; i < typePath.size(); i++) { - qname = new QName(namespace, revision, prefix, typePath.get(i)); - path.add(qname); - } - QName typeQName; - if (isBaseYangType) { - typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName); - } else { - typeQName = new QName(namespace, revision, prefix, typeName); - } + private static SchemaPath createTypePath(Stack actual, String typeName) { + QName last = actual.peek(); + QName typeQName = new QName(last.getNamespace(), last.getRevision(), last.getPrefix(), typeName); + List path = new ArrayList(actual); path.add(typeQName); return new SchemaPath(path, true); } + private static SchemaPath createBaseTypePath(Stack actual, String typeName) { + List path = new ArrayList(actual); + path.add(BaseTypes.constructQName(typeName)); + return new SchemaPath(path, true); + } + + private static SchemaPath createExtendedBaseTypePath(Stack actual, URI namespace, Date revision, + String prefix, String typeName) { + QName extTypeName = new QName(namespace, revision, prefix, typeName); + QName baseTypeName = BaseTypes.constructQName(typeName); + List path = new ArrayList(actual); + path.add(extTypeName); + path.add(baseTypeName); + return new SchemaPath(path, true); + } + /** * Parse given context and find identityref base value. * @@ -1483,6 +1444,7 @@ public final class ParserListenerUtils { * * @param refineCtx * refine statement + * @param moduleName name of current module * @return RefineHolder object representing this refine statement */ public static RefineHolder parseRefine(Refine_stmtContext refineCtx, String moduleName) { diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.java index 0875db13b6..2cd239b438 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.java @@ -7,37 +7,24 @@ */ package org.opendaylight.yangtools.yang.parser.util; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.ChoiceNode; -import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition; -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.GroupingDefinition; -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.ModuleImport; -import org.opendaylight.yangtools.yang.model.api.MustDefinition; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; -import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; @@ -49,9 +36,6 @@ import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; @@ -65,6 +49,7 @@ import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.IdentityrefType; import org.opendaylight.yangtools.yang.model.util.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.util.Leafref; +import org.opendaylight.yangtools.yang.model.util.StringType; import org.opendaylight.yangtools.yang.model.util.UnionType; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder; @@ -72,30 +57,23 @@ import org.opendaylight.yangtools.yang.parser.builder.api.Builder; import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder; -import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember; import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder; import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder; -import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder.ChoiceNodeImpl; import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder.ChoiceCaseNodeImpl; -import org.opendaylight.yangtools.yang.parser.builder.impl.ConstraintsBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder.ContainerSchemaNodeImpl; -import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingBuilderImpl; import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder; -import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder; -import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder.ListSchemaNodeImpl; import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder; import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder.NotificationDefinitionImpl; -import org.opendaylight.yangtools.yang.parser.builder.impl.TypeDefinitionBuilderImpl; -import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder; public final class ParserUtils { @@ -263,14 +241,16 @@ public final class ParserUtils { correctNodePath(childCopy, target.getPath()); try { target.addChildNode(childCopy); - } catch(YangParseException e) { + } catch (YangParseException e) { // more descriptive message - throw new YangParseException(augment.getModuleName(), augment.getLine(), "Failed to perform augmentation: "+ e.getMessage()); + throw new YangParseException(augment.getModuleName(), augment.getLine(), + "Failed to perform augmentation: " + e.getMessage()); } } for (UsesNodeBuilder usesNode : augment.getUsesNodes()) { - target.addUsesNode(CopyUtils.copyUses(usesNode, target)); + UsesNodeBuilder copy = CopyUtils.copyUses(usesNode, target); + target.addUsesNode(copy); } } @@ -296,7 +276,6 @@ public final class ParserUtils { "Error in augment parsing: cannot augment uses to choice"); } } - } static void correctNodePath(final SchemaNodeBuilder node, final SchemaPath parentSchemaPath) { @@ -324,7 +303,7 @@ public final class ParserUtils { // if node can contains type, correct path for this type too if (node instanceof TypeAwareBuilder) { TypeAwareBuilder nodeBuilder = (TypeAwareBuilder) node; - correctTypeAwareNodePath(nodeBuilder, node.getPath()); + correctTypeAwareNodePath(nodeBuilder); } } @@ -336,89 +315,51 @@ public final class ParserUtils { * @param parentSchemaPath * schema path of parent node */ - private static void correctTypeAwareNodePath(final TypeAwareBuilder node, final SchemaPath parentSchemaPath) { - final QName nodeBuilderQName = node.getQName(); + public static void correctTypeAwareNodePath(final TypeAwareBuilder node) { + final SchemaPath parentSchemaPath = node.getPath(); final TypeDefinition nodeType = node.getType(); - Integer fd = null; - List lengths = null; - List patterns = null; - List ranges = null; - - if (nodeType != null) { - if (nodeType instanceof ExtendedType) { - ExtendedType et = (ExtendedType) nodeType; - if (nodeType.getQName().getLocalName().equals(nodeType.getBaseType().getQName().getLocalName())) { - fd = et.getFractionDigits(); - lengths = et.getLengths(); - patterns = et.getPatterns(); - ranges = et.getRanges(); - if (!hasConstraints(fd, lengths, patterns, ranges)) { - return; - } - } + // handle union type + if (node instanceof UnionTypeBuilder) { + for (TypeDefinitionBuilder tdb : ((UnionTypeBuilder) node).getTypedefs()) { + SchemaPath newSchemaPath = createSchemaPath(node.getPath(), tdb.getQName()); + tdb.setPath(newSchemaPath); + correctTypeAwareNodePath(tdb); } - TypeDefinition newType = createCorrectTypeDefinition(parentSchemaPath, nodeBuilderQName, nodeType); - node.setType(newType); - } else { - TypeDefinitionBuilder nodeBuilderTypedef = node.getTypedef(); - - fd = nodeBuilderTypedef.getFractionDigits(); - lengths = nodeBuilderTypedef.getLengths(); - patterns = nodeBuilderTypedef.getPatterns(); - ranges = nodeBuilderTypedef.getRanges(); - - String tdbTypeName = nodeBuilderTypedef.getQName().getLocalName(); - String baseTypeName = null; - if (nodeBuilderTypedef.getType() == null) { - baseTypeName = nodeBuilderTypedef.getTypedef().getQName().getLocalName(); - } else { - baseTypeName = nodeBuilderTypedef.getType().getQName().getLocalName(); - } - if (!(tdbTypeName.equals(baseTypeName))) { - return; - } - - if (!hasConstraints(fd, lengths, patterns, ranges)) { - return; + List> oldTypes = ((UnionTypeBuilder) node).getTypes(); + List> newTypes = new ArrayList<>(); + for (TypeDefinition td : oldTypes) { + TypeDefinition newType = createCorrectTypeDefinition(node.getPath(), td); + newTypes.add(newType); } + oldTypes.clear(); + oldTypes.addAll(newTypes); + return; + } - SchemaPath newSchemaPath = createSchemaPath(nodeBuilderTypedef.getPath(), nodeBuilderQName, - nodeBuilderTypedef.getQName()); - nodeBuilderTypedef.setPath(newSchemaPath); + // handle identityref type + if (node instanceof IdentityrefTypeBuilder) { + return; } - } - /** - * Check if there are some constraints. - * - * @param fd - * fraction digits - * @param lengths - * length constraints - * @param patterns - * pattern constraints - * @param ranges - * range constraints - * @return true, if any of constraints are present, false otherwise - */ - private static boolean hasConstraints(final Integer fd, final List lengths, - final List patterns, final List ranges) { - if (fd == null && (lengths == null || lengths.isEmpty()) && (patterns == null || patterns.isEmpty()) - && (ranges == null || ranges.isEmpty())) { - return false; + // default handling + if (nodeType == null) { + TypeDefinitionBuilder nodeTypedef = node.getTypedef(); + SchemaPath newSchemaPath = createSchemaPath(parentSchemaPath, nodeTypedef.getQName()); + nodeTypedef.setPath(newSchemaPath); + correctTypeAwareNodePath(nodeTypedef); } else { - return true; + TypeDefinition newType = createCorrectTypeDefinition(parentSchemaPath, nodeType); + node.setType(newType); } + } - private static TypeDefinition createCorrectTypeDefinition(SchemaPath parentSchemaPath, QName nodeQName, - TypeDefinition nodeType) { + public static TypeDefinition createCorrectTypeDefinition(SchemaPath parentSchemaPath, TypeDefinition nodeType) { TypeDefinition result = null; if (nodeType != null) { - QName nodeTypeQName = nodeType.getQName(); - SchemaPath newSchemaPath = createSchemaPath(parentSchemaPath, nodeQName, nodeTypeQName); + SchemaPath newSchemaPath = createSchemaPath(parentSchemaPath, nodeType.getQName()); if (nodeType instanceof BinaryTypeDefinition) { BinaryTypeDefinition binType = (BinaryTypeDefinition) nodeType; @@ -456,12 +397,11 @@ public final class ParserUtils { return new InstanceIdentifier(newSchemaPath, instIdType.getPathStatement(), instIdType.requireInstance()); } else if (nodeType instanceof StringTypeDefinition) { - result = TypeUtils.createNewStringType(parentSchemaPath, nodeQName, (StringTypeDefinition) nodeType); + result = new StringType(newSchemaPath); } else if (nodeType instanceof IntegerTypeDefinition) { - result = TypeUtils.createNewIntType(parentSchemaPath, nodeQName, (IntegerTypeDefinition) nodeType); + result = TypeUtils.createNewIntType(newSchemaPath, (IntegerTypeDefinition) nodeType); } else if (nodeType instanceof UnsignedIntegerTypeDefinition) { - result = TypeUtils.createNewUintType(parentSchemaPath, nodeQName, - (UnsignedIntegerTypeDefinition) nodeType); + result = TypeUtils.createNewUintType(newSchemaPath, (UnsignedIntegerTypeDefinition) nodeType); } else if (nodeType instanceof LeafrefTypeDefinition) { result = new Leafref(newSchemaPath, ((LeafrefTypeDefinition) nodeType).getPathStatement()); } else if (nodeType instanceof UnionTypeDefinition) { @@ -475,269 +415,6 @@ public final class ParserUtils { return result; } - /** - * Create LeafSchemaNodeBuilder from given LeafSchemaNode. - * - * @param leaf - * leaf from which to create builder - * @param qname - * @param moduleName - * current module name - * @param line - * line in module - * @return leaf builder based on given leaf node - */ - public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) { - final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath()); - convertDataSchemaNode(leaf, builder); - builder.setConfiguration(leaf.isConfiguration()); - final TypeDefinition type = leaf.getType(); - builder.setType(type); - builder.setPath(leaf.getPath()); - builder.setUnknownNodes(leaf.getUnknownSchemaNodes()); - builder.setDefaultStr(leaf.getDefault()); - builder.setUnits(leaf.getUnits()); - return builder; - } - - /** - * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode. - * - * @param container - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return container builder based on given container node - */ - public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname, - String moduleName, int line) { - final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname, - container.getPath()); - convertDataSchemaNode(container, builder); - builder.setConfiguration(container.isConfiguration()); - builder.setUnknownNodes(container.getUnknownSchemaNodes()); - builder.setChildNodes(container.getChildNodes()); - builder.setGroupings(container.getGroupings()); - builder.setTypedefs(container.getTypeDefinitions()); - builder.setAugmentations(container.getAvailableAugmentations()); - builder.setUsesnodes(container.getUses()); - builder.setPresence(container.isPresenceContainer()); - return builder; - } - - /** - * Create ListSchemaNodeBuilder from given ListSchemaNode. - * - * @param list - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return list builder based on given list node - */ - public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) { - ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath()); - convertDataSchemaNode(list, builder); - builder.setConfiguration(list.isConfiguration()); - builder.setUnknownNodes(list.getUnknownSchemaNodes()); - builder.setTypedefs(list.getTypeDefinitions()); - builder.setChildNodes(list.getChildNodes()); - builder.setGroupings(list.getGroupings()); - builder.setAugmentations(list.getAvailableAugmentations()); - builder.setUsesnodes(list.getUses()); - builder.setUserOrdered(builder.isUserOrdered()); - return builder; - } - - /** - * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode. - * - * @param leafList - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return leaf-list builder based on given leaf-list node - */ - public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName, - int line) { - final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname, - leafList.getPath()); - convertDataSchemaNode(leafList, builder); - builder.setConfiguration(leafList.isConfiguration()); - builder.setType(leafList.getType()); - builder.setUnknownNodes(leafList.getUnknownSchemaNodes()); - builder.setUserOrdered(leafList.isUserOrdered()); - return builder; - } - - /** - * Create ChoiceBuilder from given ChoiceNode. - * - * @param choice - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return choice builder based on given choice node - */ - public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) { - final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname); - convertDataSchemaNode(choice, builder); - builder.setConfiguration(choice.isConfiguration()); - builder.setCases(choice.getCases()); - builder.setUnknownNodes(choice.getUnknownSchemaNodes()); - builder.setDefaultCase(choice.getDefaultCase()); - return builder; - } - - /** - * Create AnyXmlBuilder from given AnyXmlSchemaNode. - * - * @param anyxml - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return anyxml builder based on given anyxml node - */ - public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) { - final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath()); - convertDataSchemaNode(anyxml, builder); - builder.setConfiguration(anyxml.isConfiguration()); - builder.setUnknownNodes(anyxml.getUnknownSchemaNodes()); - return builder; - } - - /** - * Create GroupingBuilder from given GroupingDefinition. - * - * @param grouping - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return grouping builder based on given grouping node - */ - public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) { - final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname); - builder.setPath(grouping.getPath()); - builder.setChildNodes(grouping.getChildNodes()); - builder.setGroupings(grouping.getGroupings()); - builder.setTypedefs(grouping.getTypeDefinitions()); - builder.setUsesnodes(grouping.getUses()); - builder.setUnknownNodes(grouping.getUnknownSchemaNodes()); - builder.setDescription(grouping.getDescription()); - builder.setReference(grouping.getReference()); - builder.setStatus(grouping.getStatus()); - return builder; - } - - /** - * Create TypeDefinitionBuilder from given ExtendedType. - * - * @param typedef - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return typedef builder based on given typedef node - */ - public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) { - final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname); - builder.setPath(typedef.getPath()); - builder.setDefaultValue(typedef.getDefaultValue()); - builder.setUnits(typedef.getUnits()); - builder.setDescription(typedef.getDescription()); - builder.setReference(typedef.getReference()); - builder.setStatus(typedef.getStatus()); - builder.setRanges(typedef.getRanges()); - builder.setLengths(typedef.getLengths()); - builder.setPatterns(typedef.getPatterns()); - builder.setFractionDigits(typedef.getFractionDigits()); - final TypeDefinition type = typedef.getBaseType(); - builder.setType(type); - builder.setUnits(typedef.getUnits()); - builder.setUnknownNodes(typedef.getUnknownSchemaNodes()); - return builder; - } - - /** - * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode. - * - * @param unknownNode - * @param qname - * @param moduleName - * current module name - * @param line - * current line in module - * @return unknown node builder based on given unknown node - */ - public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname, - String moduleName, int line) { - final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname); - builder.setPath(unknownNode.getPath()); - builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes()); - builder.setDescription(unknownNode.getDescription()); - builder.setReference(unknownNode.getReference()); - builder.setStatus(unknownNode.getStatus()); - builder.setAddedByUses(unknownNode.isAddedByUses()); - builder.setNodeType(unknownNode.getNodeType()); - builder.setNodeParameter(unknownNode.getNodeParameter()); - return builder; - } - - /** - * Set DataSchemaNode arguments to builder object - * - * @param node - * node from which arguments should be read - * @param builder - * builder to which arguments should be set - */ - private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) { - builder.setPath(node.getPath()); - builder.setDescription(node.getDescription()); - builder.setReference(node.getReference()); - builder.setStatus(node.getStatus()); - builder.setAugmenting(node.isAugmenting()); - copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints()); - } - - /** - * Copy constraints from constraints definition to constraints builder. - * - * @param nodeConstraints - * definition from which constraints will be copied - * @param constraints - * builder to which constraints will be added - */ - private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints, - final ConstraintsBuilder constraints) { - final RevisionAwareXPath when = nodeConstraints.getWhenCondition(); - final Set must = nodeConstraints.getMustConstraints(); - - if (when != null) { - constraints.addWhenCondition(when.toString()); - } - if (must != null) { - for (MustDefinition md : must) { - constraints.addMustDefinition(md); - } - } - constraints.setMandatory(nodeConstraints.isMandatory()); - constraints.setMinElements(nodeConstraints.getMinElements()); - constraints.setMaxElements(nodeConstraints.getMaxElements()); - } - /** * Find augment target node and perform augmentation. * @@ -746,12 +423,10 @@ public final class ParserUtils { * parent of first node in path * @param path * path to augment target - * @param isUsesAugment - * if this augment is defined under uses node * @return true if augment process succeed, false otherwise */ public static boolean processAugmentation(final AugmentationSchemaBuilder augment, final Builder firstNodeParent, - final List path, boolean isUsesAugment) { + final List path) { // traverse augment target path and try to reach target node String currentName = null; Builder currentParent = firstNodeParent; @@ -814,15 +489,13 @@ public final class ParserUtils { } private static DataSchemaNodeBuilder findNodeInUses(String localName, UsesNodeBuilder uses) { - Set usesTargetChildren = uses.getTargetChildren(); - if (usesTargetChildren != null) { - for (DataSchemaNodeBuilder child : uses.getTargetChildren()) { - if (child.getQName().getLocalName().equals(localName)) { - return child; - } + GroupingBuilder target = uses.getGroupingBuilder(); + for (DataSchemaNodeBuilder child : target.getChildNodeBuilders()) { + if (child.getQName().getLocalName().equals(localName)) { + return child; } } - for (UsesNodeBuilder usesNode : uses.getTargetGroupingUses()) { + for (UsesNodeBuilder usesNode : target.getUsesNodes()) { DataSchemaNodeBuilder result = findNodeInUses(localName, usesNode); if (result != null) { return result; @@ -961,182 +634,6 @@ public final class ParserUtils { return result; } - /** - * Load uses target nodes and all uses target uses target nodes. Set this - * collection as uses final children. - * - * @param module - * current module - * @param usesNode - */ - public static void processUsesNode(final UsesNodeBuilder usesNode) { - ModuleBuilder module = getParentModule(usesNode); - DataNodeContainerBuilder parent = usesNode.getParent(); - URI namespace = null; - Date revision = null; - String prefix = null; - if (parent instanceof ModuleBuilder || parent instanceof AugmentationSchemaBuilder) { - namespace = module.getNamespace(); - revision = module.getRevision(); - prefix = module.getPrefix(); - } else { - QName parentQName = parent.getQName(); - namespace = parentQName.getNamespace(); - revision = parentQName.getRevision(); - prefix = parentQName.getPrefix(); - } - SchemaPath parentPath = parent.getPath(); - - // child nodes - Set finalChildren = new HashSet<>(); - Set newChildren = GroupingUtils.copyUsesTargetNodesWithNewPath(usesNode, parent); - finalChildren.addAll(newChildren); - usesNode.getFinalChildren().addAll(finalChildren); - - // groupings - Set finalGroupings = new HashSet<>(); - Set newGroupings = GroupingUtils.copyUsesTargetGroupingsWithNewPath(usesNode, parentPath, - namespace, revision, prefix); - finalGroupings.addAll(newGroupings); - usesNode.getFinalGroupings().addAll(finalGroupings); - - // typedefs - Set finalTypedefs = new HashSet<>(); - Set newTypedefs = GroupingUtils.copyUsesTargetTypedefsWithNewPath(usesNode, parentPath, - namespace, revision, prefix); - finalTypedefs.addAll(newTypedefs); - usesNode.getFinalTypedefs().addAll(finalTypedefs); - - // unknown nodes - List finalUnknownNodes = new ArrayList<>(); - List newUnknownNodes = GroupingUtils.copyUsesTargetUnknownNodesWithNewPath(usesNode, - parentPath, namespace, revision, prefix); - finalUnknownNodes.addAll(newUnknownNodes); - usesNode.getFinalUnknownNodes().addAll(finalUnknownNodes); - } - - /** - * Add nodes defined in uses target grouping to uses parent. - * - * @param usesNode - */ - public static void updateUsesParent(UsesNodeBuilder usesNode, DataNodeContainerBuilder parent) { - // child nodes - for (DataSchemaNodeBuilder child : usesNode.getFinalChildren()) { - child.setParent(parent); - parent.addChildNode(child); - } - for (UsesNodeBuilder uses : usesNode.getTargetGroupingUses()) { - updateUsesParent(uses, parent); - } - - // groupings - for (GroupingBuilder gb : usesNode.getFinalGroupings()) { - parent.addGrouping(gb); - } - // typedefs - for (TypeDefinitionBuilder tdb : usesNode.getFinalTypedefs()) { - parent.addTypedef(tdb); - } - // unknown nodes - for (UnknownSchemaNodeBuilder un : usesNode.getFinalUnknownNodes()) { - parent.addUnknownNodeBuilder(un); - } - } - - public static void fixUsesNodesPath(UsesNodeBuilder usesNode) { - DataNodeContainerBuilder parent = usesNode.getParent(); - - // child nodes - Set currentChildNodes = parent.getChildNodeBuilders(); - Set toRemove = new HashSet<>(); - Set toAdd = new HashSet<>(); - for (DataSchemaNodeBuilder child : currentChildNodes) { - if (child instanceof GroupingMember) { - GroupingMember gm = (GroupingMember) child; - if (gm.isAddedByUses()) { - toRemove.add(child); - DataSchemaNodeBuilder copy = CopyUtils.copy(child, parent, true); - correctNodePath(copy, parent.getPath()); - toAdd.add(copy); - } - } - } - currentChildNodes.removeAll(toRemove); - currentChildNodes.addAll(toAdd); - - // groupings - Set currentGroupings = parent.getGroupingBuilders(); - Set toRemoveG = new HashSet<>(); - Set toAddG = new HashSet<>(); - for (GroupingBuilder child : currentGroupings) { - if (child.isAddedByUses()) { - toRemoveG.add(child); - GroupingBuilder copy = CopyUtils.copy(child, parent, true); - correctNodePath(copy, parent.getPath()); - toAddG.add(copy); - } - - } - currentGroupings.removeAll(toRemoveG); - currentGroupings.addAll(toAddG); - - // typedefs - Set currentTypedefs = parent.getTypeDefinitionBuilders(); - Set toRemoveTD = new HashSet<>(); - Set toAddTD = new HashSet<>(); - for (TypeDefinitionBuilder child : currentTypedefs) { - if (child.isAddedByUses()) { - toRemoveTD.add(child); - TypeDefinitionBuilder copy = CopyUtils.copy(child, parent, true); - correctNodePath(copy, parent.getPath()); - toAddTD.add(copy); - } - - } - currentTypedefs.removeAll(toRemoveTD); - currentTypedefs.addAll(toAddTD); - - // unknown nodes - List currentUN = parent.getUnknownNodeBuilders(); - List toRemoveUN = new ArrayList<>(); - List toAddUN = new ArrayList<>(); - for (UnknownSchemaNodeBuilder un : currentUN) { - if (un.isAddedByUses()) { - toRemoveUN.add(un); - UnknownSchemaNodeBuilder copy = CopyUtils.copy(un, parent, true); - correctNodePath(copy, parent.getPath()); - toAddUN.add(copy); - } - } - currentUN.removeAll(toRemoveUN); - currentUN.addAll(toAddUN); - } - - /** - * Perform refine process on uses children. It is expected that uses has - * already resolved all dependencies. - * - * @param usesNode - */ - public static void performRefine(UsesNodeBuilder usesNode) { - for (RefineHolder refine : usesNode.getRefines()) { - DataSchemaNodeBuilder nodeToRefine = null; - for (DataSchemaNodeBuilder dataNode : usesNode.getFinalChildren()) { - if (refine.getName().equals(dataNode.getQName().getLocalName())) { - nodeToRefine = dataNode; - break; - } - } - if (nodeToRefine == null) { - throw new YangParseException(refine.getModuleName(), refine.getLine(), "Refine target node '" - + refine.getName() + "' not found"); - } - RefineUtils.performRefine(nodeToRefine, refine); - usesNode.addRefineNode(nodeToRefine); - } - } - /** * Get module in which this node is defined. * diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java index 69020cbff7..6d69bff537 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java @@ -32,7 +32,6 @@ import org.opendaylight.yangtools.yang.model.util.Int16; import org.opendaylight.yangtools.yang.model.util.Int32; import org.opendaylight.yangtools.yang.model.util.Int64; import org.opendaylight.yangtools.yang.model.util.Int8; -import org.opendaylight.yangtools.yang.model.util.StringType; import org.opendaylight.yangtools.yang.model.util.Uint16; import org.opendaylight.yangtools.yang.model.util.Uint32; import org.opendaylight.yangtools.yang.model.util.Uint64; @@ -329,10 +328,10 @@ public class TypeUtils { */ static ExtendedType createNewExtendedType(final ExtendedType oldType, final SchemaPath newPath) { QName qname = oldType.getQName(); - TypeDefinition baseType = oldType.getBaseType(); + TypeDefinition newBaseType = ParserUtils.createCorrectTypeDefinition(newPath, oldType.getBaseType()); String desc = oldType.getDescription(); String ref = oldType.getReference(); - ExtendedType.Builder builder = new ExtendedType.Builder(qname, baseType, desc, ref, newPath); + ExtendedType.Builder builder = new ExtendedType.Builder(qname, newBaseType, desc, ref, newPath); builder.status(oldType.getStatus()); builder.lengths(oldType.getLengths()); builder.patterns(oldType.getPatterns()); @@ -342,22 +341,9 @@ public class TypeUtils { return builder.build(); } - static StringTypeDefinition createNewStringType(final SchemaPath schemaPath, final QName nodeQName, - final StringTypeDefinition nodeType) { - final List path = schemaPath.getPath(); - final List newPath = new ArrayList(path); - newPath.add(nodeQName); - newPath.add(nodeType.getQName()); - final SchemaPath newSchemaPath = new SchemaPath(newPath, schemaPath.isAbsolute()); - return new StringType(newSchemaPath); - } - - static IntegerTypeDefinition createNewIntType(final SchemaPath schemaPath, final QName nodeQName, + static IntegerTypeDefinition createNewIntType(final SchemaPath newSchemaPath, final IntegerTypeDefinition type) { - final QName typeQName = type.getQName(); - final SchemaPath newSchemaPath = createSchemaPath(schemaPath, nodeQName, typeQName); - final String localName = typeQName.getLocalName(); - + final String localName = type.getQName().getLocalName(); if ("int8".equals(localName)) { return new Int8(newSchemaPath); } else if ("int16".equals(localName)) { @@ -371,12 +357,9 @@ public class TypeUtils { } } - static UnsignedIntegerTypeDefinition createNewUintType(final SchemaPath schemaPath, final QName nodeQName, + static UnsignedIntegerTypeDefinition createNewUintType(final SchemaPath newSchemaPath, final UnsignedIntegerTypeDefinition type) { - final QName typeQName = type.getQName(); - final SchemaPath newSchemaPath = createSchemaPath(schemaPath, nodeQName, typeQName); - final String localName = typeQName.getLocalName(); - + final String localName = type.getQName().getLocalName(); if ("uint8".equals(localName)) { return new Uint8(newSchemaPath); } else if ("uint16".equals(localName)) { 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 5250846a42..91655a6f5d 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 @@ -14,6 +14,7 @@ import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -31,9 +32,7 @@ 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.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.Leafref; +import org.opendaylight.yangtools.yang.model.util.BaseTypes; import com.google.common.collect.Lists; @@ -107,6 +106,12 @@ public class AugmentTest { qnames[3] = new QName(types1NS, types1Rev, t1, "ds0ChannelNumber"); expectedPath = new SchemaPath(Lists.newArrayList(qnames), true); assertEquals(expectedPath, ds0ChannelNumber.getPath()); + // type of leaf ds0ChannelNumber + QName typeQName = BaseTypes.constructQName("string"); + List typePath = new ArrayList<>(Arrays.asList(qnames)); + typePath.add(typeQName); + expectedPath = new SchemaPath(typePath, true); + assertEquals(expectedPath, ds0ChannelNumber.getType().getPath()); // leaf interface-id qnames[3] = new QName(types1NS, types1Rev, t1, "interface-id"); @@ -328,33 +333,4 @@ public class AugmentTest { assertEquals(expectedPath, caseNode3Child.getPath()); } - @Test - public void testAugmentNodesTypeSchemaPath() throws Exception { - Module testModule = TestUtils.findModule(modules, "nodes"); - Set augments = testModule.getAugmentations(); - assertEquals(1, augments.size()); - AugmentationSchema augment = augments.iterator().next(); - - LeafSchemaNode ifcId = (LeafSchemaNode) augment.getDataChildByName("interface-id"); - Leafref ifcIdType = (Leafref) ifcId.getType(); - SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath(); - List ifcIdTypePath = ifcIdTypeSchemaPath.getPath(); - - Date expectedDate = simpleDateFormat.parse("2013-02-27"); - - QName q3 = new QName(types1NS, expectedDate, "data", "interface-id"); - assertEquals(q0, ifcIdTypePath.get(0)); - assertEquals(q1, ifcIdTypePath.get(1)); - assertEquals(q2, ifcIdTypePath.get(2)); - assertEquals(q3, ifcIdTypePath.get(3)); - - LeafSchemaNode myType = (LeafSchemaNode) augment.getDataChildByName("my-type"); - ExtendedType leafType = (ExtendedType) myType.getType(); - - testModule = TestUtils.findModule(modules, "types"); - TypeDefinition typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "int32-ext2"); - - assertEquals(typedef, leafType); - } - } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java index 1520dc514e..0cd9a7b6e1 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java @@ -117,9 +117,6 @@ public class TypesResolutionTest { + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?"; assertEquals(expectedPattern, ipv4.getPatterns().get(0).getRegularExpression()); - TypeDefinition ipv4Address = TestUtils.findTypedef(typedefs, "ipv4-address"); - assertEquals(ipv4Address, ipv4); - ExtendedType ipv6 = (ExtendedType) unionTypes.get(1); assertTrue(ipv6.getBaseType() instanceof StringTypeDefinition); List ipv6Patterns = ipv6.getPatterns(); @@ -128,9 +125,6 @@ public class TypesResolutionTest { + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?"; assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression()); - TypeDefinition ipv6Address = TestUtils.findTypedef(typedefs, "ipv6-address"); - assertEquals(ipv6Address, ipv6); - expectedPattern = "(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + "(%.+)?"; assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression()); @@ -312,8 +306,6 @@ public class TypesResolutionTest { LeafSchemaNode type = (LeafSchemaNode)tested.getDataChildByName("type"); assertNotNull(type); - TypeDefinition leafType = type.getType(); - assertEquals(testedType, leafType); } } 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 921b7070b7..1cc18cde67 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 @@ -28,6 +28,11 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.util.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.UnionType; + +import com.google.common.collect.Lists; public class UsesAugmentTest { private final URI ns = URI.create("urn:opendaylight:params:xml:ns:yang:uses-grouping"); @@ -41,12 +46,12 @@ public class UsesAugmentTest { rev = TestUtils.simpleDateFormat.parse("2013-07-30"); } - /** * Structure of testing model: * * notification pcreq * |-- leaf version (U) + * |-- leaf type (U) * |-- list requests * |-- |-- container rp * |-- |-- |-- leaf priority (U) @@ -59,7 +64,8 @@ public class UsesAugmentTest { * |-- |-- path-key-expansion * |-- |-- |-- container path-key * |-- |-- |-- |-- list path-keys (U) - * |-- |-- |-- |-- |-- leaf version (U, A) + * |-- |-- |-- |-- |-- leaf version (U) + * |-- |-- |-- |-- |-- leaf type (U) * |-- |-- |-- |-- |-- leaf processing-rule (U) * |-- |-- |-- |-- |-- leaf ignore (U) * |-- |-- container segment-computation @@ -92,8 +98,7 @@ public class UsesAugmentTest { * |-- |-- leaf processing-rule (U) * |-- |-- leaf ignore (U) * - * U = added by uses - * A = added by augment + * U = added by uses A = added by augment */ @Test public void testAugmentInUses() throws Exception { @@ -107,230 +112,363 @@ public class UsesAugmentTest { assertNotNull(pcreq); assertEquals(createPath("pcreq"), pcreq.getPath()); Set childNodes = pcreq.getChildNodes(); - assertEquals(3, childNodes.size()); + assertEquals(4, childNodes.size()); // * |-- leaf version (U) - LeafSchemaNode version = (LeafSchemaNode)pcreq.getDataChildByName("version"); + LeafSchemaNode version = (LeafSchemaNode) pcreq.getDataChildByName("version"); assertNotNull(version); assertEquals(createPath("pcreq", "version"), version.getPath()); + assertEquals(createPath("pcreq", "version", "protocol-version"), version.getType().getPath()); + assertEquals(createPathForYangType("pcreq", "version", "protocol-version", "uint8"), version.getType() + .getBaseType().getPath()); assertTrue(version.isAddedByUses()); + // * |-- leaf type (U) + LeafSchemaNode type = (LeafSchemaNode) pcreq.getDataChildByName("type"); + assertNotNull(type); + assertTrue(type.isAddedByUses()); + assertEquals(createPath("pcreq", "type"), type.getPath()); + assertEquals(createPath("pcreq", "type", "int-ext"), type.getType().getPath()); + UnionType union = (UnionType)type.getType().getBaseType(); + assertEquals(createPathForYangType("pcreq", "type", "int-ext", "union"), union.getPath()); + assertEquals(2, union.getTypes().size()); // * |-- list requests - ListSchemaNode requests = (ListSchemaNode)pcreq.getDataChildByName("requests"); + ListSchemaNode requests = (ListSchemaNode) pcreq.getDataChildByName("requests"); assertNotNull(requests); assertEquals(createPath("pcreq", "requests"), requests.getPath()); assertFalse(requests.isAddedByUses()); childNodes = requests.getChildNodes(); assertEquals(3, childNodes.size()); // * |-- |-- container rp - ContainerSchemaNode rp = (ContainerSchemaNode)requests.getDataChildByName("rp"); + ContainerSchemaNode rp = (ContainerSchemaNode) requests.getDataChildByName("rp"); assertNotNull(rp); assertEquals(createPath("pcreq", "requests", "rp"), rp.getPath()); assertFalse(rp.isAddedByUses()); childNodes = rp.getChildNodes(); assertEquals(4, childNodes.size()); // * |-- |-- |-- leaf priority (U) - LeafSchemaNode priority = (LeafSchemaNode)rp.getDataChildByName("priority"); + LeafSchemaNode priority = (LeafSchemaNode) rp.getDataChildByName("priority"); assertNotNull(priority); assertEquals(createPath("pcreq", "requests", "rp", "priority"), priority.getPath()); + assertEquals(createPath("pcreq", "requests", "rp", "priority", "uint8"), priority.getType().getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "rp", "priority", "uint8", "uint8"), priority.getType() + .getBaseType().getPath()); assertTrue(priority.isAddedByUses()); // * |-- |-- |-- container box (U) - ContainerSchemaNode box = (ContainerSchemaNode)rp.getDataChildByName("box"); + ContainerSchemaNode box = (ContainerSchemaNode) rp.getDataChildByName("box"); assertNotNull(box); assertEquals(createPath("pcreq", "requests", "rp", "box"), box.getPath()); assertTrue(box.isAddedByUses()); // * |-- |-- |-- |-- container order (A) - ContainerSchemaNode order = (ContainerSchemaNode)box.getDataChildByName("order"); + ContainerSchemaNode order = (ContainerSchemaNode) box.getDataChildByName("order"); assertNotNull(order); assertEquals(createPath("pcreq", "requests", "rp", "box", "order"), order.getPath()); assertFalse(order.isAddedByUses()); assertTrue(order.isAugmenting()); assertEquals(2, order.getChildNodes().size()); - // * |-- |-- |-- |-- |-- leaf processing-rule (U) - LeafSchemaNode delete = (LeafSchemaNode)order.getDataChildByName("delete"); + // * |-- |-- |-- |-- |-- leaf delete (U) + LeafSchemaNode delete = (LeafSchemaNode) order.getDataChildByName("delete"); assertNotNull(delete); assertEquals(createPath("pcreq", "requests", "rp", "box", "order", "delete"), delete.getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "rp", "box", "order", "delete", "uint32"), delete + .getType().getPath()); assertTrue(delete.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore (U) - LeafSchemaNode setup = (LeafSchemaNode)order.getDataChildByName("setup"); + LeafSchemaNode setup = (LeafSchemaNode) order.getDataChildByName("setup"); assertNotNull(setup); assertEquals(createPath("pcreq", "requests", "rp", "box", "order", "setup"), setup.getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "rp", "box", "order", "setup", "uint32"), setup + .getType().getPath()); assertTrue(setup.isAddedByUses()); // * |-- |-- |-- leaf processing-rule (U) - LeafSchemaNode processingRule = (LeafSchemaNode)rp.getDataChildByName("processing-rule"); + LeafSchemaNode processingRule = (LeafSchemaNode) rp.getDataChildByName("processing-rule"); assertNotNull(processingRule); assertEquals(createPath("pcreq", "requests", "rp", "processing-rule"), processingRule.getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "rp", "processing-rule", "boolean"), processingRule + .getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- leaf ignore (U) - LeafSchemaNode ignore = (LeafSchemaNode)rp.getDataChildByName("ignore"); + LeafSchemaNode ignore = (LeafSchemaNode) rp.getDataChildByName("ignore"); assertNotNull(ignore); assertEquals(createPath("pcreq", "requests", "rp", "ignore"), ignore.getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "rp", "ignore", "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- |-- path-key-expansion - ContainerSchemaNode pke = (ContainerSchemaNode)requests.getDataChildByName("path-key-expansion"); + ContainerSchemaNode pke = (ContainerSchemaNode) requests.getDataChildByName("path-key-expansion"); assertNotNull(pke); assertEquals(createPath("pcreq", "requests", "path-key-expansion"), pke.getPath()); assertFalse(pke.isAddedByUses()); // * |-- |-- |-- path-key - ContainerSchemaNode pathKey = (ContainerSchemaNode)pke.getDataChildByName("path-key"); + ContainerSchemaNode pathKey = (ContainerSchemaNode) pke.getDataChildByName("path-key"); assertNotNull(pathKey); assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key"), pathKey.getPath()); assertFalse(pathKey.isAddedByUses()); assertEquals(3, pathKey.getChildNodes().size()); // * |-- |-- |-- |-- list path-keys (U) - ListSchemaNode pathKeys = (ListSchemaNode)pathKey.getDataChildByName("path-keys"); + ListSchemaNode pathKeys = (ListSchemaNode) pathKey.getDataChildByName("path-keys"); assertNotNull(pathKeys); assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys"), pathKeys.getPath()); assertTrue(pathKeys.isAddedByUses()); childNodes = pathKeys.getChildNodes(); - assertEquals(1, childNodes.size()); + assertEquals(2, childNodes.size()); // * |-- |-- |-- |-- |-- leaf version (U) - version = (LeafSchemaNode)pathKeys.getDataChildByName("version"); + version = (LeafSchemaNode) pathKeys.getDataChildByName("version"); assertNotNull(version); - assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version"), version.getPath()); + assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version"), + version.getPath()); + assertEquals( + createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version", + "protocol-version"), version.getType().getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "version", + "protocol-version", "uint8"), version.getType().getBaseType().getPath()); assertTrue(version.isAddedByUses()); assertFalse(version.isAugmenting()); + // * |-- |-- |-- |-- |-- leaf type (U) + type = (LeafSchemaNode) pathKeys.getDataChildByName("type"); + assertNotNull(type); + assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "type"), + type.getPath()); + assertEquals( + createPath("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "type", + "int-ext"), type.getType().getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "path-key-expansion", "path-key", "path-keys", "type", + "int-ext", "union"), type.getType().getBaseType().getPath()); + assertTrue(type.isAddedByUses()); + assertFalse(type.isAugmenting()); // * |-- |-- |-- |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)pathKey.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) pathKey.getDataChildByName("processing-rule"); assertNotNull(processingRule); - assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "processing-rule"), processingRule.getPath()); + assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "processing-rule"), + processingRule.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "path-key-expansion", "path-key", "processing-rule", + "boolean"), processingRule.getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)pathKey.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) pathKey.getDataChildByName("ignore"); assertNotNull(ignore); assertEquals(createPath("pcreq", "requests", "path-key-expansion", "path-key", "ignore"), ignore.getPath()); + assertEquals(createPathForYangType("pcreq", "requests", "path-key-expansion", "path-key", "ignore", "boolean"), + ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- |-- container segment-computation - ContainerSchemaNode sc = (ContainerSchemaNode)requests.getDataChildByName("segment-computation"); + ContainerSchemaNode sc = (ContainerSchemaNode) requests.getDataChildByName("segment-computation"); assertNotNull(sc); assertEquals(createPath("pcreq", "requests", "segment-computation"), sc.getPath()); assertFalse(sc.isAddedByUses()); // * |-- |-- |-- container p2p - ContainerSchemaNode p2p = (ContainerSchemaNode)sc.getDataChildByName("p2p"); + ContainerSchemaNode p2p = (ContainerSchemaNode) sc.getDataChildByName("p2p"); assertNotNull(p2p); assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p"), p2p.getPath()); assertFalse(p2p.isAddedByUses()); // * |-- |-- |-- |-- container endpoints - ContainerSchemaNode endpoints = (ContainerSchemaNode)p2p.getDataChildByName("endpoints"); + ContainerSchemaNode endpoints = (ContainerSchemaNode) p2p.getDataChildByName("endpoints"); assertNotNull(endpoints); assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints"), endpoints.getPath()); assertFalse(endpoints.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)endpoints.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) endpoints.getDataChildByName("processing-rule"); assertNotNull(processingRule); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "processing-rule"), processingRule.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "processing-rule"), + processingRule.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "endpoints", + "processing-rule", "boolean"), processingRule.getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)endpoints.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) endpoints.getDataChildByName("ignore"); assertNotNull(ignore); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "ignore"), ignore.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "ignore"), + ignore.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "endpoints", "ignore", + "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- |-- container box - box = (ContainerSchemaNode)endpoints.getDataChildByName("box"); + box = (ContainerSchemaNode) endpoints.getDataChildByName("box"); assertNotNull(box); assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "box"), box.getPath()); assertTrue(box.isAddedByUses()); // * |-- |-- |-- |-- |-- choice address-family (U) - ChoiceNode af = (ChoiceNode)endpoints.getDataChildByName("address-family"); + ChoiceNode af = (ChoiceNode) endpoints.getDataChildByName("address-family"); assertNotNull(af); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "address-family"), af.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "endpoints", "address-family"), + af.getPath()); assertTrue(af.isAddedByUses()); // * |-- |-- |-- |-- container reported-route - ContainerSchemaNode reportedRoute = (ContainerSchemaNode)p2p.getDataChildByName("reported-route"); + ContainerSchemaNode reportedRoute = (ContainerSchemaNode) p2p.getDataChildByName("reported-route"); assertNotNull(reportedRoute); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route"), reportedRoute.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route"), + reportedRoute.getPath()); assertFalse(reportedRoute.isAddedByUses()); // * |-- |-- |-- |-- |-- container bandwidth - ContainerSchemaNode bandwidth = (ContainerSchemaNode)reportedRoute.getDataChildByName("bandwidth"); + ContainerSchemaNode bandwidth = (ContainerSchemaNode) reportedRoute.getDataChildByName("bandwidth"); assertNotNull(bandwidth); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "bandwidth"), bandwidth.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "bandwidth"), + bandwidth.getPath()); assertFalse(bandwidth.isAddedByUses()); // * |-- |-- |-- |-- |-- list subobjects - ListSchemaNode subobjects = (ListSchemaNode)reportedRoute.getDataChildByName("subobjects"); + ListSchemaNode subobjects = (ListSchemaNode) reportedRoute.getDataChildByName("subobjects"); assertNotNull(subobjects); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "subobjects"), subobjects.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "subobjects"), + subobjects.getPath()); assertTrue(subobjects.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)reportedRoute.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) reportedRoute.getDataChildByName("processing-rule"); assertNotNull(processingRule); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "processing-rule"), processingRule.getPath()); + assertEquals( + createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "processing-rule"), + processingRule.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "reported-route", + "processing-rule", "boolean"), processingRule.getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)reportedRoute.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) reportedRoute.getDataChildByName("ignore"); assertNotNull(ignore); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "ignore"), ignore.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "reported-route", "ignore"), + ignore.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "reported-route", "ignore", + "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- |-- |-- |-- container bandwidth (U) - bandwidth = (ContainerSchemaNode)p2p.getDataChildByName("bandwidth"); + bandwidth = (ContainerSchemaNode) p2p.getDataChildByName("bandwidth"); assertNotNull(bandwidth); assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth"), bandwidth.getPath()); assertTrue(bandwidth.isAddedByUses()); // * |-- |-- |-- |-- |-- container bandwidth (U) - ContainerSchemaNode bandwidthInner = (ContainerSchemaNode)bandwidth.getDataChildByName("bandwidth"); + ContainerSchemaNode bandwidthInner = (ContainerSchemaNode) bandwidth.getDataChildByName("bandwidth"); assertNotNull(bandwidthInner); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "bandwidth"), bandwidthInner.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "bandwidth"), + bandwidthInner.getPath()); assertTrue(bandwidthInner.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)bandwidth.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) bandwidth.getDataChildByName("processing-rule"); assertNotNull(processingRule); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "processing-rule"), processingRule.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "processing-rule"), + processingRule.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "bandwidth", + "processing-rule", "boolean"), processingRule.getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)bandwidth.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) bandwidth.getDataChildByName("ignore"); assertNotNull(ignore); - assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "ignore"), ignore.getPath()); + assertEquals(createPath("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "ignore"), + ignore.getPath()); + assertEquals( + createPathForYangType("pcreq", "requests", "segment-computation", "p2p", "bandwidth", "ignore", + "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- list svec - ListSchemaNode svec = (ListSchemaNode)pcreq.getDataChildByName("svec"); + ListSchemaNode svec = (ListSchemaNode) pcreq.getDataChildByName("svec"); assertNotNull(svec); assertEquals(createPath("pcreq", "svec"), svec.getPath()); assertFalse(svec.isAddedByUses()); // * |-- |-- list metric - ListSchemaNode metric = (ListSchemaNode)svec.getDataChildByName("metric"); + ListSchemaNode metric = (ListSchemaNode) svec.getDataChildByName("metric"); assertNotNull(metric); assertEquals(createPath("pcreq", "svec", "metric"), metric.getPath()); assertFalse(metric.isAddedByUses()); // * |-- |-- |-- leaf metric-type (U) - LeafSchemaNode metricType = (LeafSchemaNode)metric.getDataChildByName("metric-type"); + LeafSchemaNode metricType = (LeafSchemaNode) metric.getDataChildByName("metric-type"); assertNotNull(metricType); assertEquals(createPath("pcreq", "svec", "metric", "metric-type"), metricType.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "metric", "metric-type", "uint8"), metricType.getType() + .getPath()); assertTrue(metricType.isAddedByUses()); // * |-- |-- |-- box (U) - box = (ContainerSchemaNode)metric.getDataChildByName("box"); + box = (ContainerSchemaNode) metric.getDataChildByName("box"); assertNotNull(box); assertEquals(createPath("pcreq", "svec", "metric", "box"), box.getPath()); assertTrue(box.isAddedByUses()); // * |-- |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)metric.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) metric.getDataChildByName("processing-rule"); assertNotNull(processingRule); assertEquals(createPath("pcreq", "svec", "metric", "processing-rule"), processingRule.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "metric", "processing-rule", "boolean"), processingRule + .getType().getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)metric.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) metric.getDataChildByName("ignore"); assertNotNull(ignore); assertEquals(createPath("pcreq", "svec", "metric", "ignore"), ignore.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "metric", "ignore", "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); // * |-- |-- leaf link-diverse (U) - LeafSchemaNode linkDiverse = (LeafSchemaNode)svec.getDataChildByName("link-diverse"); + LeafSchemaNode linkDiverse = (LeafSchemaNode) svec.getDataChildByName("link-diverse"); assertNotNull(linkDiverse); assertEquals(createPath("pcreq", "svec", "link-diverse"), linkDiverse.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "link-diverse", "boolean"), linkDiverse.getType().getPath()); assertTrue(linkDiverse.isAddedByUses()); // * |-- |-- leaf processing-rule (U) - processingRule = (LeafSchemaNode)svec.getDataChildByName("processing-rule"); + processingRule = (LeafSchemaNode) svec.getDataChildByName("processing-rule"); assertNotNull(processingRule); assertEquals(createPath("pcreq", "svec", "processing-rule"), processingRule.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "processing-rule", "boolean"), processingRule.getType() + .getPath()); assertTrue(processingRule.isAddedByUses()); // * |-- |-- leaf ignore (U) - ignore = (LeafSchemaNode)svec.getDataChildByName("ignore"); + ignore = (LeafSchemaNode) svec.getDataChildByName("ignore"); assertNotNull(ignore); assertEquals(createPath("pcreq", "svec", "ignore"), ignore.getPath()); + assertEquals(createPathForYangType("pcreq", "svec", "ignore", "boolean"), ignore.getType().getPath()); assertTrue(ignore.isAddedByUses()); } private SchemaPath createPath(String... names) { List path = new ArrayList<>(); - for(String name : names) { + for (String name : names) { path.add(new QName(ns, rev, prefix, name)); } return new SchemaPath(path, true); } + private SchemaPath createPathForYangType(String... names) { + List path = new ArrayList<>(); + for (int i = 0; i < names.length - 1; i++) { + path.add(new QName(ns, rev, prefix, names[i])); + } + path.add(new QName(URI.create("urn:ietf:params:xml:ns:yang:1"), names[names.length - 1])); + return new SchemaPath(path, true); + } + + @Test + public void testTypedefs() throws FileNotFoundException { + modules = TestUtils.loadModules(getClass().getResource("/grouping-test").getPath()); + Module testModule = TestUtils.findModule(modules, "uses-grouping"); + Set> types = testModule.getTypeDefinitions(); + + TypeDefinition intExt = null; + for(TypeDefinition td : types) { + if("int-ext".equals(td.getQName().getLocalName())) { + intExt = td; + } + } + assertNotNull(intExt); + assertEquals(createPath("int-ext"), intExt.getPath()); + + UnionType union = (UnionType)intExt.getBaseType(); + + TypeDefinition uint8 = null; + TypeDefinition pv = null; + for(TypeDefinition td : union.getTypes()) { + if("uint8".equals(td.getQName().getLocalName())) { + uint8 = td; + } else if("protocol-version".equals(td.getQName().getLocalName())) { + pv = td; + } + } + assertNotNull(uint8); + assertNotNull(pv); + + SchemaPath expectedPath = null; + QName q0 = new QName(ns, rev, prefix, "int-ext"); + QName q1 = BaseTypes.constructQName("union"); + + expectedPath = new SchemaPath(Lists.newArrayList(q0, q1), true); + assertEquals(expectedPath, union.getPath()); + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserSimpleTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserSimpleTest.java index 119fcd7e74..55d24f78bd 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserSimpleTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserSimpleTest.java @@ -14,7 +14,9 @@ import java.net.URI; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.Set; import org.junit.Before; @@ -176,6 +178,9 @@ public class YangParserSimpleTest { assertNotNull(nodesId); LeafListSchemaNode added = (LeafListSchemaNode)nodes.getDataChildByName("added"); assertNotNull(added); + assertEquals(createPath("nodes", "added"), added.getPath()); + assertEquals(createPath("nodes", "added", "mytype"), added.getType().getPath()); + ListSchemaNode links = (ListSchemaNode) nodes.getDataChildByName("links"); assertNotNull(links); assertFalse(links.isUserOrdered()); @@ -198,4 +203,23 @@ public class YangParserSimpleTest { assertEquals(nodeGroupPath, use.getGroupingPath()); } + + private final URI ns = URI.create("urn:opendaylight:simple-nodes"); + private Date rev; + private final String prefix = "sn"; + + private SchemaPath createPath(String... names) { + try { + rev = TestUtils.simpleDateFormat.parse("2013-07-30"); + } catch (ParseException e) { + e.printStackTrace(); + } + + List path = new ArrayList<>(); + for (String name : names) { + path.add(new QName(ns, rev, prefix, name)); + } + return new SchemaPath(path, true); + } + } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java index 859ba9c9c5..5091b1b38b 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java @@ -761,7 +761,7 @@ public class YangParserTest { Module test = TestUtils.findModule(modules, "types"); Set> types = test.getTypeDefinitions(); - // my-base-int32-type + // int32-ext1 ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types, "int32-ext1"); QName int32TypedefQName = int32Typedef.getQName(); @@ -775,7 +775,7 @@ public class YangParserTest { assertEquals(1, typePath.size()); assertEquals(int32TypedefQName, typePath.get(0)); - // my-base-int32-type/int32 + // int32-ext1/int32 Int32 int32 = (Int32) int32Typedef.getBaseType(); QName int32QName = int32.getQName(); assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"), int32QName.getNamespace()); @@ -785,9 +785,9 @@ public class YangParserTest { SchemaPath int32SchemaPath = int32.getPath(); List int32Path = int32SchemaPath.getPath(); - assertEquals(3, int32Path.size()); + assertEquals(2, int32Path.size()); assertEquals(int32TypedefQName, int32Path.get(0)); - assertEquals(int32QName, int32Path.get(2)); + assertEquals(int32QName, int32Path.get(1)); } @Test @@ -795,7 +795,7 @@ public class YangParserTest { Module test = TestUtils.findModule(modules, "types"); Set> types = test.getTypeDefinitions(); - // my-base-int32-type + // my-decimal-type ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type"); QName myDecTypeQName = myDecType.getQName(); diff --git a/yang/yang-parser-impl/src/test/resources/grouping-test/uses-grouping.yang b/yang/yang-parser-impl/src/test/resources/grouping-test/uses-grouping.yang index 6f6de3b2a1..6e807422a4 100644 --- a/yang/yang-parser-impl/src/test/resources/grouping-test/uses-grouping.yang +++ b/yang/yang-parser-impl/src/test/resources/grouping-test/uses-grouping.yang @@ -10,6 +10,13 @@ module uses-grouping { revision "2013-07-30" { } + + typedef int-ext { + type union { + type uint8; + type protocol-version; + } + } typedef protocol-version { type uint8 { @@ -76,6 +83,9 @@ module uses-grouping { type protocol-version; default 1; } + leaf type { + type int-ext; + } } grouping metric-object { diff --git a/yang/yang-parser-impl/src/test/resources/simple-test/simple-nodes.yang b/yang/yang-parser-impl/src/test/resources/simple-test/simple-nodes.yang index 9c3fd1a39a..2dc1f89245 100644 --- a/yang/yang-parser-impl/src/test/resources/simple-test/simple-nodes.yang +++ b/yang/yang-parser-impl/src/test/resources/simple-test/simple-nodes.yang @@ -13,6 +13,10 @@ module simple-nodes { "Initial revision."; reference "will be defined"; } + + typedef mytype { + type string; + } // NOTE: simple comment @@ -56,7 +60,7 @@ module simple-nodes { type int32; } leaf-list added { - type int64; + type mytype; } list links { }