Fixed bug in uses statement resolving. 01/901/1
authorMartin Vitez <mvitez@cisco.com>
Mon, 19 Aug 2013 11:01:10 +0000 (13:01 +0200)
committerMartin Vitez <mvitez@cisco.com>
Mon, 19 Aug 2013 11:01:10 +0000 (13:01 +0200)
Signed-off-by: Martin Vitez <mvitez@cisco.com>
21 files changed:
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/IdentityrefType.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/YangTypesConverter.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/api/UsesNodeBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/ModuleBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/TypeDefinitionBuilderImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UnionTypeBuilder.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/UsesNodeBuilderImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/CopyUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/GroupingUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserListenerUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/ParserUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TypeUtils.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/AugmentTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/UsesAugmentTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserSimpleTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserTest.java
yang/yang-parser-impl/src/test/resources/grouping-test/uses-grouping.yang
yang/yang-parser-impl/src/test/resources/simple-test/simple-nodes.yang

index a1b399e295ea7b7b569b2351c6ef171c30f24ef7..f498608c0f91f007d629e94bd01aefa925804216 100644 (file)
@@ -1,10 +1,10 @@
 /*\r
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
 *\r
 * This program and the accompanying materials are made available under the\r
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
 * and is available at http://www.eclipse.org/legal/epl-v10.html\r
 */\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
 package org.opendaylight.yangtools.yang.model.util;\r
 \r
 import java.util.Collections;\r
@@ -17,7 +17,8 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;\r
 \r
 /**\r
- * The <code>default</code> implementation of Identityref Type Definition interface.\r
+ * The <code>default</code> implementation of Identityref Type Definition\r
+ * interface.\r
  *\r
  * @see IdentityrefTypeDefinition\r
  */\r
@@ -86,4 +87,9 @@ public final class IdentityrefType implements IdentityrefTypeDefinition {
         return baseType;\r
     }\r
 \r
+    @Override\r
+    public String toString() {\r
+        return "identityref " + identity.getLocalName();\r
+    }\r
+\r
 }\r
index cf486fd91b447e852fb8058425d814de96fc2618..f7ea048a0780003c444c1516a8f3a8dae802f0ed 100644 (file)
@@ -7,14 +7,9 @@
  */\r
 package org.opendaylight.yangtools.yang.model.util;\r
 \r
-import java.net.URI;\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
 import java.util.HashSet;\r
-import java.util.List;\r
 import java.util.Set;\r
 \r
-import org.opendaylight.yangtools.yang.common.QName;\r
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;\r
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;\r
 \r
@@ -47,12 +42,9 @@ public final class YangTypesConverter {
         return baseYangTypes.contains(type);\r
     }\r
 \r
-    public static TypeDefinition<?> javaTypeForBaseYangType(\r
-            List<String> actualPath, URI namespace, Date revision,\r
-            String typeName) {\r
+    public static TypeDefinition<?> javaTypeForBaseYangType(SchemaPath path, String typeName) {\r
         TypeDefinition<?> type = null;\r
 \r
-        SchemaPath path = createSchemaPath(actualPath, namespace, revision, typeName);\r
         if (typeName.startsWith("int")) {\r
             if ("int8".equals(typeName)) {\r
                 type = new Int8(path);\r
@@ -88,19 +80,4 @@ public final class YangTypesConverter {
         return type;\r
     }\r
 \r
-    private static SchemaPath createSchemaPath(List<String> actualPath, URI namespace, Date revision, String typeName) {\r
-        List<String> correctPath = new ArrayList<String>(actualPath);\r
-        // remove module name\r
-        correctPath.remove(0);\r
-\r
-        List<QName> path = new ArrayList<QName>();\r
-        for(String element : correctPath) {\r
-            path.add(new QName(namespace, revision, element));\r
-        }\r
-        // add type qname\r
-        QName typeQName = new QName(BaseTypes.BaseTypesNamespace, typeName);\r
-        path.add(typeQName);\r
-        return new SchemaPath(path, true);\r
-    }\r
-\r
 }\r
index 1808197de574791ba060e002fa03ddd589e84f19..0965f6421321b8fd957614b69cc8e7202a720bc7 100644 (file)
@@ -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<AugmentationSchemaBuilder> getAugmentations();
 
     void addAugment(AugmentationSchemaBuilder builder);
@@ -46,34 +55,28 @@ public interface UsesNodeBuilder extends GroupingMember, Builder {
 
     UsesNode build();
 
-    Set<DataSchemaNodeBuilder> getFinalChildren();
-
     Set<DataSchemaNodeBuilder> getTargetChildren();
 
     void setTargetChildren(Set<DataSchemaNodeBuilder> targetChildren);
 
-    Set<GroupingBuilder> getFinalGroupings();
-
     Set<GroupingBuilder> getTargetGroupings();
 
     void setTargetGroupings(Set<GroupingBuilder> targetGroupings);
 
-    Set<TypeDefinitionBuilder> getFinalTypedefs();
-
     Set<TypeDefinitionBuilder> getTargetTypedefs();
 
     void setTargetTypedefs(Set<TypeDefinitionBuilder> targetTypedefs);
 
-    List<UnknownSchemaNodeBuilder> getFinalUnknownNodes();
-
     List<UnknownSchemaNodeBuilder> getTargetUnknownNodes();
 
     void setTargetUnknownNodes(List<UnknownSchemaNodeBuilder> targetUnknownNodes);
 
-    List<UsesNodeBuilder> getTargetGroupingUses();
+    boolean isDataCollected();
+
+    void setDataCollected(boolean dataCollected);
 
-    boolean isLoadDone();
+    boolean isParentUpdated();
 
-    void setLoadDone(boolean loadDone);
+    void setParentUpdated(boolean parentUpdated);
 
 }
index df3bb5910a21fbf28b14886bf34ad1087678688a..67471068db920398c87a3358b10eaa1820998588 100644 (file)
@@ -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<String> 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");
index 3fae6d9f0c97c785dfd8e2760e3f96c90e29bbaa..75afe1062a55b48d08b58bfc17785789472c4d36 100644 (file)
@@ -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();
     }
 
 }
index 49f39d1dfc10f80d2ab0392c0031e08de10368eb..29598ce8931b71a235a6a55184883fb121c3c619 100644 (file)
@@ -56,7 +56,7 @@ public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
     }
 
     public List<TypeDefinitionBuilder> 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();
index 8eb3a9e81e6bd48c8d93741614743989bb2f520e..7da9c5edffb5a45545e646a2f015ca12d3b3798b 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Map;
 import java.util.Set;\r
 \r
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;\r
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;\r
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;\r
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;\r
@@ -38,34 +39,40 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
     private DataNodeContainerBuilder parent;\r
     private final String groupingName;\r
     private SchemaPath groupingPath;\r
+    private GroupingDefinition groupingDefinition;\r
+    private GroupingBuilder groupingBuilder;\r
     private boolean augmenting;\r
     private boolean addedByUses;\r
     private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();\r
     private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<SchemaNodeBuilder>();\r
     private final List<RefineHolder> refines = new ArrayList<RefineHolder>();\r
 \r
-    private final Set<DataSchemaNodeBuilder> finalChildren = new HashSet<>();\r
-    private Set<DataSchemaNodeBuilder> targetChildren;\r
+    private Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();\r
+    private Set<GroupingBuilder> targetGroupings = new HashSet<>();\r
+    private Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();\r
+    private List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();\r
 \r
-    private final Set<GroupingBuilder> finalGroupings = new HashSet<>();\r
-    private Set<GroupingBuilder> targetGroupings;\r
+    private boolean dataCollected;\r
+    private boolean parentUpdated;\r
 \r
-    private final Set<TypeDefinitionBuilder> finalTypedefs = new HashSet<>();\r
-    private Set<TypeDefinitionBuilder> targetTypedefs;\r
-\r
-    private final List<UnknownSchemaNodeBuilder> finalUnknownNodes = new ArrayList<>();\r
-    private List<UnknownSchemaNodeBuilder> targetUnknownNodes;\r
-\r
-    private final List<UsesNodeBuilder> targetGroupingUses = new ArrayList<>();\r
+    @Override\r
+    public boolean isDataCollected() {\r
+        return dataCollected;\r
+    }\r
 \r
-    boolean loadDone;\r
+    @Override\r
+    public void setDataCollected(boolean dataCollected) {\r
+        this.dataCollected = dataCollected;\r
+    }\r
 \r
-    public boolean isLoadDone() {\r
-        return loadDone;\r
+    @Override\r
+    public boolean isParentUpdated() {\r
+        return parentUpdated;\r
     }\r
 \r
-    public void setLoadDone(boolean loadDone) {\r
-        this.loadDone = loadDone;\r
+    @Override\r
+    public void setParentUpdated(boolean parentUpdated) {\r
+        this.parentUpdated = parentUpdated;\r
     }\r
 \r
     public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {\r
@@ -131,6 +138,26 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         this.groupingPath = groupingPath;\r
     }\r
 \r
+    @Override\r
+    public GroupingDefinition getGroupingDefinition() {\r
+        return groupingDefinition;\r
+    }\r
+\r
+    @Override\r
+    public void setGroupingDefinition(GroupingDefinition groupingDefinition) {\r
+        this.groupingDefinition = groupingDefinition;\r
+    }\r
+\r
+    @Override\r
+    public GroupingBuilder getGroupingBuilder() {\r
+        return groupingBuilder;\r
+    }\r
+\r
+    @Override\r
+    public void setGrouping(GroupingBuilder grouping) {\r
+        this.groupingBuilder = grouping;\r
+    }\r
+\r
     @Override\r
     public String getGroupingName() {\r
         return groupingName;\r
@@ -186,11 +213,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         refines.add(refine);\r
     }\r
 \r
-    @Override\r
-    public Set<DataSchemaNodeBuilder> getFinalChildren() {\r
-        return finalChildren;\r
-    }\r
-\r
     @Override\r
     public Set<DataSchemaNodeBuilder> getTargetChildren() {\r
         return targetChildren;\r
@@ -201,11 +223,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         this.targetChildren = targetChildren;\r
     }\r
 \r
-    @Override\r
-    public Set<GroupingBuilder> getFinalGroupings() {\r
-        return finalGroupings;\r
-    }\r
-\r
     @Override\r
     public Set<GroupingBuilder> getTargetGroupings() {\r
         return targetGroupings;\r
@@ -216,11 +233,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         this.targetGroupings = targetGroupings;\r
     }\r
 \r
-    @Override\r
-    public Set<TypeDefinitionBuilder> getFinalTypedefs() {\r
-        return finalTypedefs;\r
-    }\r
-\r
     @Override\r
     public Set<TypeDefinitionBuilder> getTargetTypedefs() {\r
         return targetTypedefs;\r
@@ -231,11 +243,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         this.targetTypedefs = targetTypedefs;\r
     }\r
 \r
-    @Override\r
-    public List<UnknownSchemaNodeBuilder> getFinalUnknownNodes() {\r
-        return finalUnknownNodes;\r
-    }\r
-\r
     @Override\r
     public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {\r
         return targetUnknownNodes;\r
@@ -246,11 +253,6 @@ public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNo
         this.targetUnknownNodes = targetUnknownNodes;\r
     }\r
 \r
-    @Override\r
-    public List<UsesNodeBuilder> getTargetGroupingUses() {\r
-        return targetGroupingUses;\r
-    }\r
-\r
     @Override\r
     public int hashCode() {\r
         final int prime = 31;\r
index 7a97c5335640b166b22efaf5b5a7bc0fa55318ee..2eff93d47200ab1f57e236a12267b1796d940790 100644 (file)
@@ -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<ModuleBuilder, Module> build(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        findUsesTargets(modules, null);
+
         // fix unresolved nodes
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> 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<ModuleBuilder, Module> buildWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
             SchemaContext context) {
+        findUsesTargets(modules, context);
+
         // fix unresolved nodes
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> 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<String, TreeMap<Date, ModuleBuilder>> 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<String, TreeMap<Date, ModuleBuilder>> modules) {
+        final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                dirtyNodes.addAll(inner.getValue().getDirtyNodes());
+            }
+        }
+
+        if (!dirtyNodes.isEmpty()) {
+            for (TypeAwareBuilder nodeToResolve : dirtyNodes) {
+                if (nodeToResolve instanceof UnionTypeBuilder) {
+                    List<TypeDefinitionBuilder> newTypes = new ArrayList<>();
+                    List<TypeDefinitionBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules,
             final ModuleBuilder module, SchemaContext context) {
         final Set<TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
@@ -470,7 +498,6 @@ public final class YangParserImpl implements YangModelParser {
         ModuleBuilder module = getParentModule(augmentBuilder);
         List<QName> 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<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        final List<UsesNodeBuilder> allModuleUses = module.getAllUsesNodes();
-        List<UsesNodeBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context) {
+        final List<UsesNodeBuilder> allUses = new ArrayList<>();
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules) {
-        final List<UsesNodeBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules) {
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
-                alluses.addAll(inner.getValue().getAllUsesNodes());
+                ModuleBuilder module = inner.getValue();
+                List<UsesNodeBuilder> 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<UsesNodeBuilder> allModulesUses = new ArrayList<>();
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, final SchemaContext context) {
-        final List<UsesNodeBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules,
+            final SchemaContext context) {
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                ModuleBuilder module = inner.getValue();
+                List<UsesNodeBuilder> 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<DataSchemaNodeBuilder> 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<UsesNodeBuilder> allModulesUses = new ArrayList<>();
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                allModulesUses.addAll(inner.getValue().getAllUsesNodes());
             }
         }
-        usesNode.getFinalChildren().addAll(newChildren);
-
-        final Set<GroupingBuilder> 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<TypeDefinitionBuilder> 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<UnknownSchemaNodeBuilder> 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<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
index 32d245e98fedee9473f89591d9ac77e59b6ab397..53ad43a32ef83f097952c90a00047abdcfcb81f0 100644 (file)
@@ -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<String> actualPath = new Stack<String>();
+    private final Stack<Stack<QName>> 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<QName>());
+
         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<QName>());
 
         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<QName>());
         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);
     }
index 13ad34793c2fa2b0b5a5acbadf1cda43811ccc3b..4b5d8dc5662cd15dedf5ee043eaa31ea5b586bef 100644 (file)
@@ -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<DataSchemaNodeBuilder> oldChildren = old.getTargetChildren();
-        Set<DataSchemaNodeBuilder> newChildren = new HashSet<>();
-        if (oldChildren != null) {
-            for (DataSchemaNodeBuilder child : old.getTargetChildren()) {
-                newChildren.add(CopyUtils.copy(child, newParent, true));
-            }
-        }
-        u.setTargetChildren(newChildren);
-
-        Set<TypeDefinitionBuilder> oldTypedefs = old.getTargetTypedefs();
-        Set<TypeDefinitionBuilder> newTypedefs = new HashSet<>();
-        if (oldTypedefs != null) {
-            for (TypeDefinitionBuilder typedef : old.getTargetTypedefs()) {
-                newTypedefs.add(CopyUtils.copy(typedef, newParent, true));
-            }
-        }
-        u.setTargetTypedefs(newTypedefs);
-
-        Set<GroupingBuilder> oldGroupings = old.getTargetGroupings();
-        Set<GroupingBuilder> newGroupings = new HashSet<>();
-        if (oldGroupings != null) {
-            for (GroupingBuilder grouping : old.getTargetGroupings()) {
-                newGroupings.add(copy(grouping, newParent, true));
-            }
-        }
-        u.setTargetGroupings(newGroupings);
-
-        List<UnknownSchemaNodeBuilder> oldUN = old.getTargetUnknownNodes();
-        List<UnknownSchemaNodeBuilder> 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<MustDefinition> 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());
+    }
+
 }
index a09b1a2cb12158411a1550d46374a4437fa22584..9dda792c74916f162483c2221cc9bff09a83361f 100644 (file)
@@ -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.
-     * <p>
-     * Copy all data-schema-nodes, groupings, typedefs and unknown nodes from
-     * target grouping to uses node.
-     * </p>
+     * 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<DataSchemaNodeBuilder> 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<GroupingBuilder> 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<TypeDefinitionBuilder> 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<UnknownSchemaNodeBuilder> 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.
-     * <p>
-     * Traverse uses statements in target grouping and copy all
-     * data-schema-nodes, groupings, typedefs and unknown nodes to current uses
-     * node.
-     * </p>
-     *
-     * @param usesNode
-     * @param targetGrouping
-     */
-    public static void loadTargetGroupingUses(final UsesNodeBuilder usesNode, final GroupingBuilder targetGrouping) {
-        usesNode.getTargetGroupingUses().addAll(targetGrouping.getUsesNodes());
+    private static Set<DataSchemaNodeBuilder> collectUsesChildNodes(UsesNodeBuilder usesNode) {
+        final GroupingBuilder target = usesNode.getGroupingBuilder();
+        Set<DataSchemaNodeBuilder> childNodes = target.getChildNodeBuilders();
+        Set<DataSchemaNodeBuilder> 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<DataSchemaNodeBuilder> copyUsesTargetNodesWithNewPath(UsesNodeBuilder usesNode, Builder parent) {
-        Set<DataSchemaNodeBuilder> newNodes = new HashSet<>();
+    private static Set<TypeDefinitionBuilder> collectUsesTypedefs(UsesNodeBuilder usesNode) {
+        final GroupingBuilder target = usesNode.getGroupingBuilder();
+        Set<TypeDefinitionBuilder> typedefs = target.getTypeDefinitionBuilders();
+        Set<TypeDefinitionBuilder> 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<GroupingBuilder> collectUsesGroupings(UsesNodeBuilder usesNode) {
+        final GroupingBuilder target = usesNode.getGroupingBuilder();
+        Set<GroupingBuilder> groupings = target.getGroupingBuilders();
+        Set<GroupingBuilder> 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<UnknownSchemaNodeBuilder> collectUsesUnknownNodes(UsesNodeBuilder usesNode) {
+        final GroupingBuilder target = usesNode.getGroupingBuilder();
+        List<UnknownSchemaNodeBuilder> unknownNodes = target.getUnknownNodeBuilders();
+        List<UnknownSchemaNodeBuilder> 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<GroupingBuilder> copyUsesTargetGroupingsWithNewPath(UsesNodeBuilder usesNode,
-            SchemaPath parentPath, URI namespace, Date revision, String prefix) {
-        Set<GroupingBuilder> 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<DataSchemaNodeBuilder> 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<GroupingBuilder> 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<TypeDefinitionBuilder> 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<UnknownSchemaNodeBuilder> 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<TypeDefinitionBuilder> copyUsesTargetTypedefsWithNewPath(UsesNodeBuilder usesNode,
-            SchemaPath parentPath, URI namespace, Date revision, String prefix) {
-        Set<TypeDefinitionBuilder> 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<DataSchemaNodeBuilder> currentChildNodes = parent.getChildNodeBuilders();
+        Set<DataSchemaNodeBuilder> toRemove = new HashSet<>();
+        Set<DataSchemaNodeBuilder> 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<GroupingBuilder> currentGroupings = parent.getGroupingBuilders();
+        Set<GroupingBuilder> toRemoveG = new HashSet<>();
+        Set<GroupingBuilder> 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<TypeDefinitionBuilder> currentTypedefs = parent.getTypeDefinitionBuilders();
+        Set<TypeDefinitionBuilder> toRemoveTD = new HashSet<>();
+        Set<TypeDefinitionBuilder> 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<UnknownSchemaNodeBuilder> currentUN = parent.getUnknownNodeBuilders();
+        List<UnknownSchemaNodeBuilder> toRemoveUN = new ArrayList<>();
+        List<UnknownSchemaNodeBuilder> 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<UnknownSchemaNodeBuilder> copyUsesTargetUnknownNodesWithNewPath(UsesNodeBuilder usesNode,
-            SchemaPath parentPath, URI namespace, Date revision, String prefix) {
-        List<UnknownSchemaNodeBuilder> 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;
     }
 
 }
index cd382935825ea53864318fe52c333ce7772e9801..51d7e3120b23bed689f06163b5db9f37f41d9d86 100644 (file)
@@ -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<String> actualPath, final URI namespace,
-            final Date revision, final String prefix, final String... names) {
-        final List<QName> path = new ArrayList<QName>();
-        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<QName> actualPath) {
+        final List<QName> path = new ArrayList<QName>(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<EnumTypeDefinition.EnumPair> getEnumConstants(final Type_body_stmtsContext ctx,
-            final List<String> path, final String moduleName, final URI namespace, final Date revision, final String prefix) {
+            final Stack<QName> path, final String moduleName) {
         List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<EnumTypeDefinition.EnumPair>();
 
         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<String> path, final String moduleName, final URI namespace, final Date revision,
-            final String prefix) {
+            final Stack<QName> 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<String> enumPairPath = new ArrayList<String>(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<RangeConstraint> 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<RangeConstraint> 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<LengthConstraint> 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<LengthConstraint> 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<BitsTypeDefinition.Bit> getBits(Type_body_stmtsContext ctx, List<String> actualPath,
-            String moduleName, URI namespace, Date revision, String prefix) {
+    private static List<BitsTypeDefinition.Bit> getBits(Type_body_stmtsContext ctx, Stack<QName> actualPath,
+            String moduleName) {
         final List<BitsTypeDefinition.Bit> bits = new ArrayList<BitsTypeDefinition.Bit>();
         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<String> actualPath, final String moduleName, final URI namespace, final Date revision, final String prefix) {
+            Stack<QName> 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<String> bitPath = new Stack<String>();
-        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<UnknownSchemaNode> 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<String> actualPath, final URI namespace, final Date revision,
+            final Type_body_stmtsContext ctx, final Stack<QName> 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<String> 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<QName> 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<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(typeBody, actualPath, moduleName, namespace,
-                    revision, prefix);
-            return new EnumerationType(baseTypePathFinal, enumConstants);
+            List<EnumTypeDefinition.EnumPair> 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<QName> path = new ArrayList<QName>(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<String> actualPath, final URI namespace,
-            final Date revision, final String prefix, final String typeName, final boolean isBaseYangType,
-            final boolean isBaseYangTypeFinal) {
-        List<String> typePath = new ArrayList<String>(actualPath);
-        if (isBaseYangType && !isBaseYangTypeFinal) {
-            typePath.add(typeName);
-        }
-
-        final List<QName> path = new ArrayList<QName>();
-        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<QName> actual, String typeName) {
+        QName last = actual.peek();
+        QName typeQName = new QName(last.getNamespace(), last.getRevision(), last.getPrefix(), typeName);
+        List<QName> path = new ArrayList<QName>(actual);
         path.add(typeQName);
         return new SchemaPath(path, true);
     }
 
+    private static SchemaPath createBaseTypePath(Stack<QName> actual, String typeName) {
+        List<QName> path = new ArrayList<QName>(actual);
+        path.add(BaseTypes.constructQName(typeName));
+        return new SchemaPath(path, true);
+    }
+
+    private static SchemaPath createExtendedBaseTypePath(Stack<QName> actual, URI namespace, Date revision,
+            String prefix, String typeName) {
+        QName extTypeName = new QName(namespace, revision, prefix, typeName);
+        QName baseTypeName = BaseTypes.constructQName(typeName);
+        List<QName> path = new ArrayList<QName>(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) {
index 0875db13b626a636e700d05dae5241995376f135..2cd239b4381a3065f819fa9048018c5aafa79249 100644 (file)
@@ -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<LengthConstraint> lengths = null;
-        List<PatternConstraint> patterns = null;
-        List<RangeConstraint> 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<TypeDefinition<?>> oldTypes = ((UnionTypeBuilder) node).getTypes();
+            List<TypeDefinition<?>> 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<LengthConstraint> lengths,
-            final List<PatternConstraint> patterns, final List<RangeConstraint> 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<MustDefinition> 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<QName> path, boolean isUsesAugment) {
+            final List<QName> 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<DataSchemaNodeBuilder> 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<DataSchemaNodeBuilder> finalChildren = new HashSet<>();
-        Set<DataSchemaNodeBuilder> newChildren = GroupingUtils.copyUsesTargetNodesWithNewPath(usesNode, parent);
-        finalChildren.addAll(newChildren);
-        usesNode.getFinalChildren().addAll(finalChildren);
-
-        // groupings
-        Set<GroupingBuilder> finalGroupings = new HashSet<>();
-        Set<GroupingBuilder> newGroupings = GroupingUtils.copyUsesTargetGroupingsWithNewPath(usesNode, parentPath,
-                namespace, revision, prefix);
-        finalGroupings.addAll(newGroupings);
-        usesNode.getFinalGroupings().addAll(finalGroupings);
-
-        // typedefs
-        Set<TypeDefinitionBuilder> finalTypedefs = new HashSet<>();
-        Set<TypeDefinitionBuilder> newTypedefs = GroupingUtils.copyUsesTargetTypedefsWithNewPath(usesNode, parentPath,
-                namespace, revision, prefix);
-        finalTypedefs.addAll(newTypedefs);
-        usesNode.getFinalTypedefs().addAll(finalTypedefs);
-
-        // unknown nodes
-        List<UnknownSchemaNodeBuilder> finalUnknownNodes = new ArrayList<>();
-        List<UnknownSchemaNodeBuilder> 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<DataSchemaNodeBuilder> currentChildNodes = parent.getChildNodeBuilders();
-        Set<DataSchemaNodeBuilder> toRemove = new HashSet<>();
-        Set<DataSchemaNodeBuilder> 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<GroupingBuilder> currentGroupings = parent.getGroupingBuilders();
-        Set<GroupingBuilder> toRemoveG = new HashSet<>();
-        Set<GroupingBuilder> 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<TypeDefinitionBuilder> currentTypedefs = parent.getTypeDefinitionBuilders();
-        Set<TypeDefinitionBuilder> toRemoveTD = new HashSet<>();
-        Set<TypeDefinitionBuilder> 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<UnknownSchemaNodeBuilder> currentUN = parent.getUnknownNodeBuilders();
-        List<UnknownSchemaNodeBuilder> toRemoveUN = new ArrayList<>();
-        List<UnknownSchemaNodeBuilder> 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.
      *
index 69020cbff704d1f61763412cfc9d4b6167bc1f86..6d69bff53712210bf5249af1a31fc1d27e3453c6 100644 (file)
@@ -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<QName> path = schemaPath.getPath();
-        final List<QName> newPath = new ArrayList<QName>(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)) {
index 5250846a429105c298dfa0cb18762b7cb3f7772a..91655a6f5d095c4f02b5042c885127a390c2a155 100644 (file)
@@ -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<QName> 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<AugmentationSchema> 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<QName> 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);
-    }
-
 }
index 1520dc514e3fb3fd3cc2091ac9728b468fdc8386..0cd9a7b6e1d21f8414252196f5944bc9de4d59e2 100644 (file)
@@ -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<PatternConstraint> 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);
     }
 
 }
index 921b7070b79fcffbfb5ff0ed30dc81e559b38417..1cc18cde67f08828d5b84594fda1b6f881be9a43 100644 (file)
@@ -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<DataSchemaNode> 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<QName> 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<QName> 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<TypeDefinition<?>> 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());
+    }
+
 }
index 119fcd7e7443fd22341de7d0398066167035c3bb..55d24f78bd9245859742e66ef4db299b9cbed0c9 100644 (file)
@@ -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<QName> path = new ArrayList<>();
+        for (String name : names) {
+            path.add(new QName(ns, rev, prefix, name));
+        }
+        return new SchemaPath(path, true);
+    }
+
 }
index 859ba9c9c56f66b12306ef995078b642acbc2c95..5091b1b38bf8461eefbcb4232adb770ba6210cc3 100644 (file)
@@ -761,7 +761,7 @@ public class YangParserTest {
         Module test = TestUtils.findModule(modules, "types");
         Set<TypeDefinition<?>> 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<QName> 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<TypeDefinition<?>> types = test.getTypeDefinitions();
 
-        // my-base-int32-type
+        // my-decimal-type
         ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types, "my-decimal-type");
         QName myDecTypeQName = myDecType.getQName();
 
index 6f6de3b2a10de7a61b2cd2dab05b22455a737f59..6e807422a4ba70cf4b962d8072299b431fcefef3 100644 (file)
@@ -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 {
index 9c3fd1a39a20bbb5598ccec78e32ba2621bed231..2dc1f89245e51732e7efbcf1cda021973cef3e72 100644 (file)
@@ -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 {
         }