BUG-1578: fixed NPE caused by invalid import statement.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
index 417747a07ef0cfb8e14d0e0108dc8dd4ecb2cd05..ece9226253cb04bb012a19c0fa67b7fae1745619 100644 (file)
@@ -223,6 +223,14 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
         moduleBuilder.setQNameModule(moduleQName.getModule());
         moduleBuilder.setBelongsTo(belongsTo);
+        for (int i = 0; i < ctx.getChildCount(); ++i) {
+            final ParseTree treeNode = ctx.getChild(i);
+            if (treeNode instanceof Prefix_stmtContext) {
+                yangModelPrefix = stringFromNode(treeNode);
+                moduleBuilder.setPrefix(yangModelPrefix);
+                setLog("prefix", yangModelPrefix);
+            }
+        }
     }
 
     @Override
@@ -532,6 +540,20 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         }
     }
 
+    /**
+     * Method transforms string representation of yang element (i.e. leaf name, container name etc.) into QName.
+     * The namespace of QName is assigned from parent module same as revision date of module. If String qname parameter
+     * contains ":" the string is evaluated as prefix:name of element. In this case method will look into import map
+     * and extract correct ModuleImport. If such import is not present in import map the method will throw {@link YangParseException}
+     * <br>
+     * If ModuleImport is present but the value of namespace in ModuleImport is <code>null</code> the method will throw {@link YangParseException}
+     *
+     * @param qnameString QName value as String
+     * @param line line in Yang model document where QName occur.
+     * @return transformed string qname parameter as QName structure.
+     *
+     * @throws YangParseException
+     */
     private QName parseQName(final String qnameString, final int line) {
         final QName qname;
         if (qnameString.indexOf(':') == -1) {
@@ -545,17 +567,18 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             } else {
                 ModuleImport imp = moduleBuilder.getImport(prefix);
                 if (imp == null) {
-                    LOG.warn("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
-                    return QName.create(name);
+                    LOG.debug("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
+                    throw new YangParseException(moduleName, line, "Error in module " + moduleName
+                        + " No import found with prefix " + prefix + " not found.");
                 }
                 Date revision = imp.getRevision();
                 TreeMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
+                if (namespaces == null) {
+                    throw new YangParseException(moduleName, line, String.format("Imported module %s not found",
+                            imp.getModuleName()));
+                }
                 URI namespace;
                 if (revision == null) {
-                    if (namespaces == null) {
-                        throw new YangParseException(moduleName, line, "imported module " + imp.getModuleName()
-                                + " with prefix " + imp.getPrefix() + " not found.");
-                    }
                     revision = namespaces.lastEntry().getKey();
                     namespace = namespaces.lastEntry().getValue();
                 } else {
@@ -1121,6 +1144,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
 
         QName qname = null;
         try {
+            //FIXME: rewrite whole method to handle unknown nodes properly.
+            // This should be bugfix for bug https://bugs.opendaylight.org/show_bug.cgi?id=1539
+            // After this fix bug https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed since
+            // they are dependent!!!
             if (Strings.isNullOrEmpty(nodeParameter)) {
                 qname = nodeType;
             } else {
@@ -1132,7 +1159,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                     qname = QName.create(moduleQName, it.next());
                 }
             }
-        } catch (IllegalArgumentException e) {
+        } catch (IllegalArgumentException | YangParseException ex) {
             qname = nodeType;
         }