Merge "Do not return null when no RPC is registered"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
index f59f6881ae99b455cc7da825b317a2f8f5d6dad9..222e718650bfbbecb75906302b77b225069bf227 100644 (file)
@@ -56,10 +56,13 @@ import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.base.Strings;
+
 public final class YangParserListenerImpl extends YangParserBaseListener {
     private static final Logger LOGGER = LoggerFactory.getLogger(YangParserListenerImpl.class);
     private static final String AUGMENT_STR = "augment";
 
+    private final String sourcePath;
     private ModuleBuilder moduleBuilder;
     private String moduleName;
     private URI namespace;
@@ -77,6 +80,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         return actualPath.peek().pop();
     }
 
+    public YangParserListenerImpl(String sourcePath) {
+        this.sourcePath = sourcePath;
+    }
+
     @Override
     public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
         moduleName = stringFromNode(ctx);
@@ -84,7 +91,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         enterLog("module", moduleName, 0);
         actualPath.push(new Stack<QName>());
 
-        moduleBuilder = new ModuleBuilder(moduleName);
+        moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
 
         String description = null;
         String reference = null;
@@ -110,6 +117,41 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         actualPath.pop();
     }
 
+    @Override public void enterSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
+        moduleName = stringFromNode(ctx);
+        LOGGER.debug("entering submodule " + moduleName);
+        enterLog("submodule", moduleName, 0);
+        actualPath.push(new Stack<QName>());
+
+        moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
+
+        String description = null;
+        String reference = null;
+        for (int i = 0; i < ctx.getChildCount(); i++) {
+            ParseTree child = ctx.getChild(i);
+            if (child instanceof Description_stmtContext) {
+                description = stringFromNode(child);
+            } else if (child instanceof Reference_stmtContext) {
+                reference = stringFromNode(child);
+            } else {
+                if (description != null && reference != null) {
+                    break;
+                }
+            }
+        }
+        moduleBuilder.setDescription(description);
+        moduleBuilder.setReference(reference);
+    }
+
+    @Override public void exitSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
+        exitLog("submodule", "");
+        actualPath.pop();
+    }
+
+    @Override public void enterBelongs_to_stmt(YangParser.Belongs_to_stmtContext ctx) {
+        moduleBuilder.setBelongsTo(stringFromNode(ctx));
+    }
+
     @Override
     public void enterModule_header_stmts(Module_header_stmtsContext ctx) {
         enterLog("module_header", "", ctx.getStart().getLine());
@@ -622,7 +664,6 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         parseConstraints(ctx, builder.getConstraints());
         builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
 
-        String keyDefinition = "";
         for (int i = 0; i < ctx.getChildCount(); ++i) {
             ParseTree childNode = ctx.getChild(i);
             if (childNode instanceof Ordered_by_stmtContext) {
@@ -630,9 +671,8 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 final boolean userOrdered = parseUserOrdered(orderedBy);
                 builder.setUserOrdered(userOrdered);
             } else if (childNode instanceof Key_stmtContext) {
-                keyDefinition = stringFromNode(childNode);
-                List<QName> key = createListKey(keyDefinition, namespace, revision, yangModelPrefix);
-                builder.setKeyDefinition(key);
+                List<String> key = createListKey((Key_stmtContext) childNode);
+                builder.setKeys(key);
             }
         }
     }
@@ -762,19 +802,23 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
         }
 
-        QName qname;
-        if (nodeParameter != null) {
-            String[] splittedName = nodeParameter.split(":");
-            if (splittedName.length == 2) {
-                qname = new QName(null, null, splittedName[0], splittedName[1]);
+        QName qname = null;
+        try {
+            if (!Strings.isNullOrEmpty(nodeParameter)) {
+                String[] splittedName = nodeParameter.split(":");
+                if (splittedName.length == 2) {
+                    qname = new QName(null, null, splittedName[0], splittedName[1]);
+                } else {
+                    qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
+                }
             } else {
-                qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
+                qname = nodeType;
             }
-        } else {
-            qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
+        } catch (IllegalArgumentException e) {
+            qname = nodeType;
+            
         }
-
-        addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
+        addNodeToPath(qname);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
         UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);