Added support for parsing submodules & added dependency utility parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
index 53ad43a32ef83f097952c90a00047abdcfcb81f0..12c230d5bc3894d649990fb7d458fd978a8e17a0 100644 (file)
@@ -10,12 +10,8 @@ package org.opendaylight.yangtools.yang.parser.impl;
 import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.*;
 
 import java.net.URI;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.Stack;
+import java.text.*;
+import java.util.*;
 
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.opendaylight.yangtools.antlrv4.code.gen.*;
@@ -54,32 +50,15 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
 import org.opendaylight.yangtools.yang.model.util.YangTypesConverter;
-import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ExtensionBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.*;
+import org.opendaylight.yangtools.yang.parser.builder.impl.*;
 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class YangParserListenerImpl extends YangParserBaseListener {
-    private static final Logger logger = LoggerFactory.getLogger(YangParserListenerImpl.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(YangParserListenerImpl.class);
+    private static final String AUGMENT_STR = "augment";
 
     private ModuleBuilder moduleBuilder;
     private String moduleName;
@@ -87,7 +66,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     private String yangModelPrefix;
     private Date revision = new Date(0L);
 
-    public final static DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+    private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     private final Stack<Stack<QName>> actualPath = new Stack<>();
 
     private void addNodeToPath(QName name) {
@@ -101,7 +80,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     @Override
     public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
         moduleName = stringFromNode(ctx);
-        logger.debug("entering module " + moduleName);
+        LOGGER.debug("entering module " + moduleName);
         enterLog("module", moduleName, 0);
         actualPath.push(new Stack<QName>());
 
@@ -131,6 +110,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);
+
+        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());
@@ -212,9 +226,9 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
         final String revisionDateStr = stringFromNode(treeNode);
         try {
-            final Date revision = simpleDateFormat.parse(revisionDateStr);
-            if ((revision != null) && (this.revision.compareTo(revision) < 0)) {
-                this.revision = revision;
+            final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
+            if ((revisionDate != null) && (this.revision.compareTo(revisionDate) < 0)) {
+                this.revision = revisionDate;
                 moduleBuilder.setRevision(this.revision);
                 setLog("revision", this.revision.toString());
                 for (int i = 0; i < treeNode.getChildCount(); ++i) {
@@ -226,7 +240,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             }
         } catch (ParseException e) {
             final String message = "Failed to parse revision string: " + revisionDateStr;
-            logger.warn(message);
+            LOGGER.warn(message);
         }
     }
 
@@ -247,9 +261,9 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             if (treeNode instanceof Revision_date_stmtContext) {
                 String importRevisionStr = stringFromNode(treeNode);
                 try {
-                    importRevision = simpleDateFormat.parse(importRevisionStr);
+                    importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
                 } catch (ParseException e) {
-                    logger.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
+                    LOGGER.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
                 }
             }
         }
@@ -266,7 +280,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String augmentPath = stringFromNode(ctx);
-        enterLog("augment", augmentPath, line);
+        enterLog(AUGMENT_STR, augmentPath, line);
         actualPath.push(new Stack<QName>());
 
         AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
@@ -290,7 +304,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     @Override
     public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("augment", "");
+        exitLog(AUGMENT_STR, "");
         actualPath.pop();
     }
 
@@ -303,8 +317,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(qname);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        ExtensionBuilder builder = moduleBuilder.addExtension(qname, line);
-        builder.setPath(path);
+        ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
         parseSchemaNodeArgs(ctx, builder);
 
         String argument = null;
@@ -338,8 +351,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(typedefQName);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName);
-        builder.setPath(path);
+        TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
         parseSchemaNodeArgs(ctx, builder);
         builder.setUnits(parseUnits(ctx));
         builder.setDefaultValue(parseDefault(ctx));
@@ -376,33 +388,36 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 // check for types which must have body
                 checkMissingBody(typeName, moduleName, line);
                 // if there are no constraints, just grab default base yang type
-                QName qname = BaseTypes.constructQName(typeName);
-                addNodeToPath(qname);
-                SchemaPath path = createActualSchemaPath(actualPath.peek());
-                type = YangTypesConverter.javaTypeForBaseYangType(path, typeName);
+                type = YangTypesConverter.javaTypeForBaseYangType(typeName);
+                addNodeToPath(type.getQName());
                 moduleBuilder.setType(type);
             } else {
-                if ("union".equals(typeName)) {
-                    QName qname = BaseTypes.constructQName("union");
-                    addNodeToPath(qname);
-                    SchemaPath path = createActualSchemaPath(actualPath.peek());
-                    UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
-                    moduleBuilder.enterNode(unionBuilder);
-                    unionBuilder.setPath(path);
-                } else if ("identityref".equals(typeName)) {
-                    QName qname = BaseTypes.constructQName("identityref");
-                    addNodeToPath(qname);
-                    SchemaPath path = createActualSchemaPath(actualPath.peek());
-                    moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
-                } else {
-                    type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix, moduleBuilder.getActualNode());
-                    moduleBuilder.setType(type);
-                    addNodeToPath(type.getQName());
+                QName qname;
+                switch (typeName) {
+                    case "union":
+                        qname = BaseTypes.constructQName("union");
+                        addNodeToPath(qname);
+                        UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
+                        Builder parent = moduleBuilder.getActualNode();
+                        unionBuilder.setParent(parent);
+                        moduleBuilder.enterNode(unionBuilder);
+                        break;
+                    case "identityref":
+                        qname = BaseTypes.constructQName("identityref");
+                        addNodeToPath(qname);
+                        SchemaPath path = createActualSchemaPath(actualPath.peek());
+                        moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
+                        break;
+                    default:
+                        type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision,
+                                yangModelPrefix, moduleBuilder.getActualNode());
+                        moduleBuilder.setType(type);
+                        addNodeToPath(type.getQName());
                 }
             }
         } else {
-            type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix,
-                    moduleBuilder.getActualNode());
+            type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision,
+                    yangModelPrefix, moduleBuilder.getActualNode());
             // add parent node of this type statement to dirty nodes
             moduleBuilder.markActualNodeDirty();
             moduleBuilder.setType(type);
@@ -446,8 +461,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(groupQName);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName);
-        builder.setPath(path);
+        GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
         parseSchemaNodeArgs(ctx, builder);
 
         moduleBuilder.enterNode(builder);
@@ -550,7 +564,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         actualPath.push(new Stack<QName>());
         final int line = ctx.getStart().getLine();
         final String augmentPath = stringFromNode(ctx);
-        enterLog("augment", augmentPath, line);
+        enterLog(AUGMENT_STR, augmentPath, line);
 
         AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
 
@@ -573,7 +587,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     @Override
     public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("augment", "");
+        exitLog(AUGMENT_STR, "");
         actualPath.pop();
     }
 
@@ -698,8 +712,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(choiceQName);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName);
-        builder.setPath(path);
+        ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
         moduleBuilder.enterNode(builder);
 
         parseSchemaNodeArgs(ctx, builder);
@@ -733,8 +746,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(caseQName);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName);
-        builder.setPath(path);
+        ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
         moduleBuilder.enterNode(builder);
 
         parseSchemaNodeArgs(ctx, builder);
@@ -757,8 +769,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         addNodeToPath(notificationQName);
         SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName);
-        builder.setPath(path);
+        NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
         moduleBuilder.enterNode(builder);
 
         parseSchemaNodeArgs(ctx, builder);
@@ -777,14 +788,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         final String nodeParameter = stringFromNode(ctx);
         enterLog("unknown-node", nodeParameter, line);
 
-        QName nodeType = null;
-
+        QName nodeType;
         final String nodeTypeStr = ctx.getChild(0).getText();
         final String[] splittedElement = nodeTypeStr.split(":");
         if (splittedElement.length == 1) {
-            nodeType = new QName(null, null, yangModelPrefix, splittedElement[0]);
+            nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
         } else {
-            nodeType = new QName(null, null, splittedElement[0], splittedElement[1]);
+            nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
         }
 
         QName qname;
@@ -799,13 +809,13 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
         }
 
-        UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname);
+        addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
+        SchemaPath path = createActualSchemaPath(actualPath.peek());
+
+        UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
         builder.setNodeType(nodeType);
         builder.setNodeParameter(nodeParameter);
-        addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
 
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
-        builder.setPath(path);
 
         parseSchemaNodeArgs(ctx, builder);
         moduleBuilder.enterNode(builder);
@@ -824,12 +834,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         enterLog("rpc", rpcName, line);
 
         QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
-        RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName);
-        moduleBuilder.enterNode(rpcBuilder);
         addNodeToPath(rpcQName);
-
         SchemaPath path = createActualSchemaPath(actualPath.peek());
-        rpcBuilder.setPath(path);
+
+        RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
+        moduleBuilder.enterNode(rpcBuilder);
+
 
         parseSchemaNodeArgs(ctx, rpcBuilder);
     }
@@ -893,12 +903,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         enterLog("feature", featureName, line);
 
         QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
-        FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName);
-        moduleBuilder.enterNode(featureBuilder);
         addNodeToPath(featureQName);
-
         SchemaPath path = createActualSchemaPath(actualPath.peek());
-        featureBuilder.setPath(path);
+
+        FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
+        moduleBuilder.enterNode(featureBuilder);
+
         parseSchemaNodeArgs(ctx, featureBuilder);
     }
 
@@ -950,12 +960,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         enterLog("identity", identityName, line);
 
         final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
-        IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line);
-        moduleBuilder.enterNode(builder);
         addNodeToPath(identityQName);
-
         SchemaPath path = createActualSchemaPath(actualPath.peek());
-        builder.setPath(path);
+
+        IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
+        moduleBuilder.enterNode(builder);
+
 
         parseSchemaNodeArgs(ctx, builder);
 
@@ -979,19 +989,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     private void enterLog(String p1, String p2, int line) {
-        logger.trace("entering {} {} ({})", p1, p2, line);
+        LOGGER.trace("entering {} {} ({})", p1, p2, line);
     }
 
     private void exitLog(String p1, String p2) {
-        logger.trace("exiting {} {}", p1, p2);
+        LOGGER.trace("exiting {} {}", p1, p2);
     }
 
     private void exitLog(String p1, QName p2) {
-        logger.trace("exiting {} {}", p1, p2.getLocalName());
+        LOGGER.trace("exiting {} {}", p1, p2.getLocalName());
     }
 
     private void setLog(String p1, String p2) {
-        logger.trace("setting {} {}", p1, p2);
+        LOGGER.trace("setting {} {}", p1, p2);
     }
 
 }