X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fimpl%2FYangParserListenerImpl.java;h=222e718650bfbbecb75906302b77b225069bf227;hb=f1110ba0f38666e3b0f66abfbf098bca1c73f104;hp=53ad43a32ef83f097952c90a00047abdcfcb81f0;hpb=c147a8fc1d4e445d08a5aa1994863e87c9472a08;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java index 53ad43a32e..222e718650 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java @@ -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,40 +50,26 @@ 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; +import com.google.common.base.Strings; + 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 final String sourcePath; private ModuleBuilder moduleBuilder; private String moduleName; private URI namespace; 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> actualPath = new Stack<>(); private void addNodeToPath(QName name) { @@ -98,14 +80,18 @@ 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); - logger.debug("entering module " + moduleName); + LOGGER.debug("entering module " + moduleName); enterLog("module", moduleName, 0); actualPath.push(new Stack()); - moduleBuilder = new ModuleBuilder(moduleName); + moduleBuilder = new ModuleBuilder(moduleName, sourcePath); String description = null; String reference = null; @@ -131,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()); + + 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()); @@ -212,9 +233,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 +247,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 +268,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 +287,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()); AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath); @@ -290,7 +311,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 +324,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 +358,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 +395,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 +468,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 +571,7 @@ public final class YangParserListenerImpl extends YangParserBaseListener { actualPath.push(new Stack()); 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 +594,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(); } @@ -643,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) { @@ -651,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 key = createListKey(keyDefinition, namespace, revision, yangModelPrefix); - builder.setKeyDefinition(key); + List key = createListKey((Key_stmtContext) childNode); + builder.setKeys(key); } } } @@ -698,8 +717,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 +751,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 +774,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,35 +793,38 @@ 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; - 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(qname); + SchemaPath path = createActualSchemaPath(actualPath.peek()); - UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname); + 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 +843,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 +912,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 +969,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 +998,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); } }