Bug 3799: NPE when including a submodule of a more recent revision than the module
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / BuilderUtils.java
index 1c202f61004c5d7ac84a57532571897da679cf80..3c999263c27af2d2dfd372eeafc0a54d9964c993 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;
@@ -832,6 +833,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());
     }