BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / BuilderUtils.java
index e98553e8009013e5b4f5c2bc2c60123fdd147630..792db8d9c84b8a1a677df8511343fa097565845b 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.NavigableMap;
 import java.util.Set;
 import java.util.TreeMap;
@@ -77,6 +78,10 @@ import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
 public final class BuilderUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(BuilderUtils.class);
@@ -121,22 +126,6 @@ public final class BuilderUtils {
         });
     }
 
-    /**
-     * Create new SchemaPath from given path and qname.
-     *
-     * @param schemaPath
-     *            base path
-     * @param qname
-     *            one or more qnames added to base path
-     * @return new SchemaPath from given path and qname
-     *
-     * @deprecated Use {@link SchemaPath#createChild(QName...)} instead.
-     */
-    @Deprecated
-    public static SchemaPath createSchemaPath(final SchemaPath schemaPath, final QName... qname) {
-        return schemaPath.createChild(qname);
-    }
-
     /**
      * Find dependent module based on given prefix
      *
@@ -180,7 +169,7 @@ public final class BuilderUtils {
         return dependentModule;
     }
 
-    public static ModuleBuilder findModuleFromBuilders(ModuleImport imp, Iterable<ModuleBuilder> modules) {
+    public static ModuleBuilder findModuleFromBuilders(final ModuleImport imp, final Iterable<ModuleBuilder> modules) {
         String name = imp.getModuleName();
         Date revision = imp.getRevision();
         NavigableMap<Date, ModuleBuilder> map = new TreeMap<>();
@@ -343,7 +332,7 @@ public final class BuilderUtils {
     /**
      * Set addedByUses flag to true for node and all its child nodes.
      *
-     * @param node
+     * @param node grouping member node
      */
     public static void setNodeAddedByUses(final GroupingMember node) {
         node.setAddedByUses(true);
@@ -360,6 +349,41 @@ public final class BuilderUtils {
         }
     }
 
+    /**
+     * Find builder of schema node under parent builder (including under
+     * AugmentationSchemaBuilder).
+     *
+     * @param path
+     *            - path of target schema node builder
+     * @param parent
+     *            - base data node container builder under which the target
+     *            schema node builder should be found
+     * @return builder of schema node
+     */
+    public static SchemaNodeBuilder findTargetNode(final Iterable<QName> path,
+            final DataNodeContainerBuilder parent) {
+
+        Preconditions.checkNotNull(parent);
+        Preconditions.checkNotNull(path);
+
+        SchemaNodeBuilder foundNode = null;
+
+        final Iterator<QName> pathIterator = path.iterator();
+        if (pathIterator.hasNext()) {
+            String name = pathIterator.next().getLocalName();
+            foundNode = parent.getDataChildByName(name);
+            if (foundNode == null) {
+                foundNode = findUnknownNode(name, parent);
+            }
+        }
+
+        if (pathIterator.hasNext() && foundNode != null) {
+            return findSchemaNode(Iterables.skip(path, 1), foundNode);
+        } else {
+            return foundNode;
+        }
+    }
+
     public static SchemaNodeBuilder findSchemaNode(final Iterable<QName> path, final SchemaNodeBuilder parentNode) {
         SchemaNodeBuilder node = null;
         SchemaNodeBuilder parent = parentNode;
@@ -693,7 +717,7 @@ public final class BuilderUtils {
     /**
      * Get module in which this node is defined.
      *
-     * @param node
+     * @param node node
      * @return builder of module where this node is defined
      */
     public static ModuleBuilder getParentModule(final Builder node) {
@@ -813,6 +837,26 @@ public final class BuilderUtils {
         if (qname.getRevision() == null) {
             return map.lastEntry().getValue();
         }
+
+        final Entry<Date, ModuleBuilder> lastEntry = map.lastEntry();
+        if (qname.getRevision().compareTo(lastEntry.getKey()) > 0) {
+            /*
+             * We are trying to find more recent revision of module than is in
+             * the map. Most probably the yang models are not referenced
+             * correctly and the revision of a base module or submodule has not
+             * been updated along with revision of a referenced module or
+             * submodule. However, we should return the most recent entry in the
+             * map, otherwise the null pointer exception occurs (see Bug3799).
+             */
+            LOG.warn(String
+                    .format("Attempt to find more recent revision of module than is available. "
+                            + "The requested revision is [%s], but the most recent available revision of module is [%s]."
+                            + " Most probably some of Yang models do not have updated revision or they are not "
+                            + "referenced correctly.",
+                            qname.getRevision(), lastEntry.getKey()));
+            return lastEntry.getValue();
+        }
+
         return map.get(qname.getRevision());
     }