BUG-1342: fixed bug in resolving augments with target added by uses.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / SchemaContextUtil.java
index 2af7159ffed81dd8d70cabaf205648acbefc2b0d..fbf4c4389b05f140dbc3452c367a18627f707109 100644 (file)
@@ -10,9 +10,7 @@ package org.opendaylight.yangtools.yang.model.util;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
-
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -22,7 +20,6 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
@@ -82,7 +79,7 @@ public final class SchemaContextUtil {
         Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
         Preconditions.checkArgument(schemaPath != null, "Schema Path reference cannot be NULL");
 
-        final List<QName> prefixedPath = (schemaPath.getPath());
+        final Iterable<QName> prefixedPath = schemaPath.getPathFromRoot();
         if (prefixedPath == null) {
             LOG.debug("Schema path {} has null path", schemaPath);
             return null;
@@ -243,8 +240,9 @@ public final class SchemaContextUtil {
         return findNodeInModule(module, path);
     }
 
-    public static GroupingDefinition findGrouping(final SchemaContext context, final Module module, final List<QName> path) {
-        QName first = path.get(0);
+    public static GroupingDefinition findGrouping(final SchemaContext context, final Module module, final Iterable<QName> path) {
+        Iterator<QName> iterator = path.iterator();
+        QName first = iterator.next();
         Module m = context.findModuleByNamespace(first.getNamespace()).iterator().next();
         DataNodeContainer currentParent = m;
         for (QName qname : path) {
@@ -519,7 +517,7 @@ public final class SchemaContextUtil {
     }
 
     private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
-        if (node.getPath().getPath().size() != 1) {
+        if (Iterables.size(node.getPath().getPathTowardsRoot()) != 1) {
             QName currentName = node.getQName();
             // tmpPath is used to track level of nesting
             List<QName> tmpPath = new ArrayList<>();
@@ -627,8 +625,12 @@ public final class SchemaContextUtil {
     }
 
     private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
-        SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
-
+        SchemaNode targetGrouping = SchemaContextUtil.findNodeInSchemaContext(ctx, u.getGroupingPath()
+                .getPathFromRoot());
+        if (!(targetGrouping instanceof GroupingDefinition)) {
+            targetGrouping = findGrouping(ctx, getParentModule(targetGrouping, ctx), u.getGroupingPath()
+                    .getPathFromRoot());
+        }
         Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
                 "Failed to generate code for augment in %s", u);
         GroupingDefinition gr = (GroupingDefinition) targetGrouping;
@@ -869,14 +871,13 @@ public final class SchemaContextUtil {
             ++colCount;
         }
 
-        final ImmutableList<QName> relative = ImmutableList.copyOf(
+        final Iterable<QName> parent = leafrefParentNode.getPath().getPathFromRoot();
+        return Iterables.concat(Iterables.limit(parent, Iterables.size(parent) - colCount),
                 Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
                     @Override
                     public QName apply(final String input) {
                         return stringPathPartToQName(context, module, input);
                     }
                 }));
-        final Iterable<QName> parent = leafrefParentNode.getPath().getPathFromRoot();
-        return Iterables.concat(Iterables.limit(parent, Iterables.size(parent) - colCount), relative);
     }
 }