Merge "Bug 1817 - Add in ietf-restonf to the yangtools-models feature."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
index 417747a07ef0cfb8e14d0e0108dc8dd4ecb2cd05..fdaaf33d8865c085dec2ee0c34526962d7f67f14 100644 (file)
@@ -70,6 +70,7 @@ import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParserBaseListener;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
@@ -127,14 +128,14 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
      * Create a new instance.
      *
      * FIXME: the resulting type needs to be extracted, such that we can reuse
-     *        the "BaseListener" aspect, which need not be exposed to the user.
-     *        Maybe factor out a base class into repo.spi?
+     * the "BaseListener" aspect, which need not be exposed to the user. Maybe
+     * factor out a base class into repo.spi?
      *
      * @param namespaceContext
      * @param sourcePath
      * @param walker
      * @param tree
-     * @return
+     * @return new instance of YangParserListenerImpl
      */
     public static YangParserListenerImpl create(final Map<String, TreeMap<Date, URI>> namespaceContext,
             final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
@@ -223,6 +224,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
@@ -338,8 +347,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             final ParseTree treeNode = ctx.getChild(i);
             if (treeNode instanceof Prefix_stmtContext) {
                 importPrefix = stringFromNode(treeNode);
-            }
-            if (treeNode instanceof Revision_date_stmtContext) {
+            } else if (treeNode instanceof Revision_date_stmtContext) {
                 String importRevisionStr = stringFromNode(treeNode);
                 try {
                     importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
@@ -532,36 +540,56 @@ 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) {
-            qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), qnameString);
+            qname = QName.create(moduleQName, qnameString);
         } else {
             final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
             final String prefix = split.next();
             final String name = split.next();
             if (prefix.equals(moduleBuilder.getPrefix())) {
-                qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), name);
+                qname = QName.create(moduleQName, name);
             } 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 {
+                    // FIXME: this lookup does not look right, as we will end up with
+                    //        a qname which does not have a namespace. At any rate we
+                    //        should arrive at a QNameModule!
                     namespace = namespaces.get(revision);
                 }
-                qname = QName.create(namespace, revision, name);
+
+                final QNameModule mod = QNameModule.cachedReference(QNameModule.create(namespace, revision));
+                qname = QName.create(mod, name);
             }
         }
         return qname;
@@ -1121,6 +1149,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 +1164,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                     qname = QName.create(moduleQName, it.next());
                 }
             }
-        } catch (IllegalArgumentException e) {
+        } catch (IllegalArgumentException | YangParseException ex) {
             qname = nodeType;
         }