[ONOS-3892,3895,3882,3883,3896]Implementation of yang container, list, leaf, leaf-list parser
Change-Id: Id51839bc434044be8273382f80f15b12f0ec8709
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ListListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ListListener.java
index 7aceab6..82d69cb 100644
--- a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ListListener.java
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ListListener.java
@@ -16,8 +16,20 @@
package org.onosproject.yangutils.parser.impl.listeners;
+import org.onosproject.yangutils.datamodel.YangList;
+import org.onosproject.yangutils.datamodel.YangNode;
+import org.onosproject.yangutils.datamodel.YangNodeType;
+import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
+import org.onosproject.yangutils.parser.Parsable;
+import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
+import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
+import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
+import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
+import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
+import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
+import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
/*
* Reference: RFC6020 and YANG ANTLR Grammar
@@ -56,6 +68,8 @@
*/
public final class ListListener {
+ private static ParsableDataType yangConstruct;
+
/**
* Creates a new list listener.
*/
@@ -72,7 +86,44 @@
*/
public static void processListEntry(TreeWalkListener listener,
GeneratedYangParser.ListStatementContext ctx) {
- // TODO
+
+ YangNode curNode;
+
+ ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
+ ParsableDataType.LIST_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
+ ListenerErrorLocation.ENTRY);
+
+ boolean result = validateSubStatementsCardinality(ctx);
+ if (!result) {
+ throw new ParserException(ListenerErrorMessageConstruction
+ .constructListenerErrorMessage(ListenerErrorType.INVALID_CARDINALITY,
+ yangConstruct, "", ListenerErrorLocation.ENTRY));
+ }
+
+ YangList yangList = new YangList(YangNodeType.LIST_NODE);
+ yangList.setName(ctx.IDENTIFIER().getText());
+
+ Parsable curData = listener.getParsedDataStack().peek();
+ if (curData instanceof YangNode) {
+ curNode = (YangNode) curData;
+ try {
+ curNode.addChild(yangList);
+ } catch (DataModelException e) {
+ throw new ParserException(ListenerErrorMessageConstruction
+ .constructExtendedListenerErrorMessage(ListenerErrorType.UNHANDLED_PARSED_DATA,
+ ParsableDataType.LIST_DATA,
+ String.valueOf(ctx.IDENTIFIER().getText()),
+ ListenerErrorLocation.ENTRY,
+ e.getMessage()));
+ }
+ listener.getParsedDataStack().push(yangList);
+ } else {
+ throw new ParserException(ListenerErrorMessageConstruction
+ .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
+ ParsableDataType.LIST_DATA,
+ String.valueOf(ctx.IDENTIFIER().getText()),
+ ListenerErrorLocation.ENTRY));
+ }
}
/**
@@ -84,6 +135,77 @@
*/
public static void processListExit(TreeWalkListener listener,
GeneratedYangParser.ListStatementContext ctx) {
- // TODO
+
+ ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
+ ParsableDataType.LIST_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
+ ListenerErrorLocation.EXIT);
+
+ if (listener.getParsedDataStack().peek() instanceof YangList) {
+ listener.getParsedDataStack().pop();
+ } else {
+ throw new ParserException(ListenerErrorMessageConstruction
+ .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
+ ParsableDataType.LIST_DATA,
+ String.valueOf(ctx.IDENTIFIER().getText()),
+ ListenerErrorLocation.EXIT));
+ }
+ }
+
+ /**
+ * Validates the cardinality of list sub-statements as per grammar.
+ *
+ * @param ctx context object of the grammar rule.
+ * @return true/false validation success or failure.
+ */
+ public static boolean validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
+
+ if ((!ctx.keyStatement().isEmpty())
+ && (ctx.keyStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.KEY_DATA;
+ return false;
+ }
+
+ if ((!ctx.configStatement().isEmpty())
+ && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.CONFIG_DATA;
+ return false;
+ }
+
+ if ((!ctx.maxElementsStatement().isEmpty())
+ && (ctx.maxElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.MAX_ELEMENT_DATA;
+ return false;
+ }
+
+ if ((!ctx.minElementsStatement().isEmpty())
+ && (ctx.minElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.MIN_ELEMENT_DATA;
+ return false;
+ }
+
+ if ((!ctx.descriptionStatement().isEmpty())
+ && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.DESCRIPTION_DATA;
+ return false;
+ }
+
+ if ((!ctx.referenceStatement().isEmpty())
+ && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.REFERENCE_DATA;
+ return false;
+ }
+
+ if ((!ctx.statusStatement().isEmpty())
+ && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
+ yangConstruct = ParsableDataType.STATUS_DATA;
+ return false;
+ }
+
+ if (ctx.dataDefStatement().isEmpty()) {
+ yangConstruct = ParsableDataType.LIST_DATA;
+ return false;
+ }
+
+ return true;
}
}
\ No newline at end of file