Vidyashree Rama
Committed by Gerrit Code Review

[ONOS-3892,3895,3882,3883,3896]Implementation of yang container, list, leaf, leaf-list parser

Change-Id: Id51839bc434044be8273382f80f15b12f0ec8709
Showing 17 changed files with 830 additions and 141 deletions
......@@ -217,5 +217,21 @@ public enum YangDataTypes {
/**
* Derived Data type.
*/
DERIVED
DERIVED;
/**
* Returns YANG data type for corresponding type name.
*
* @param name type name from YANG file.
* @return YANG data type for corresponding type name.
*/
public static YangDataTypes getType(String name) {
name = name.replace("\"", "");
for (YangDataTypes yangDataType : values()) {
if (yangDataType.name().equalsIgnoreCase(name)) {
return yangDataType;
}
}
return YangDataTypes.DERIVED;
}
}
......
......@@ -36,6 +36,8 @@ import java.io.IOException;
*/
public class YangUtilsParserManager implements YangUtilsParser {
public static final int SUB_STATEMENT_CARDINALITY = 1;
@Override
public YangNode getDataModel(String yangFile) throws IOException, ParserException {
......
......@@ -16,8 +16,19 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangList;
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.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
......@@ -55,18 +66,41 @@ public final class ConfigListener {
*/
public static void processConfigEntry(TreeWalkListener listener,
GeneratedYangParser.ConfigStatementContext ctx) {
// TODO method implementation
}
boolean isConfig = false;
/**
* It is called when parser exits from grammar rule (config), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processConfigExit(TreeWalkListener listener,
GeneratedYangParser.ConfigStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.CONFIG_DATA, "", ListenerErrorLocation.ENTRY);
if (ctx.TRUE_KEYWORD() != null) {
isConfig = true;
}
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpData;
leaf.setConfig(isConfig);
break;
case CONTAINER_DATA:
YangContainer container = (YangContainer) tmpData;
container.setConfig(isConfig);
break;
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setConfig(isConfig);
break;
case LIST_DATA:
YangList yangList = (YangList) tmpData;
yangList.setConfig(isConfig);
break;
case CHOICE_DATA: // TODO
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.CONFIG_DATA,
"", ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -15,9 +15,20 @@
*/
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangNode;
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
......@@ -54,6 +65,8 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
*/
public final class ContainerListener {
private static ParsableDataType yangConstruct;
/**
* Creates a new container listener.
*/
......@@ -70,7 +83,44 @@ public final class ContainerListener {
*/
public static void processContainerEntry(TreeWalkListener listener,
GeneratedYangParser.ContainerStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.CONTAINER_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));
}
YangContainer container = new YangContainer();
container.setName(ctx.IDENTIFIER().getText());
Parsable curData = listener.getParsedDataStack().peek();
if (curData instanceof YangNode) {
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(container);
} catch (DataModelException e) {
throw new ParserException(ListenerErrorMessageConstruction
.constructExtendedListenerErrorMessage(ListenerErrorType.UNHANDLED_PARSED_DATA,
ParsableDataType.CONTAINER_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.ENTRY,
e.getMessage()));
}
listener.getParsedDataStack().push(container);
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.CONTAINER_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.ENTRY));
}
}
/**
......@@ -82,6 +132,61 @@ public final class ContainerListener {
*/
public static void processContainerExit(TreeWalkListener listener,
GeneratedYangParser.ContainerStatementContext ctx) {
//TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.CONTAINER_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT);
if (listener.getParsedDataStack().peek() instanceof YangContainer) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.CONTAINER_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT));
}
}
/**
* Validates the cardinality of container 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.ContainerStatementContext ctx) {
if ((!ctx.presenceStatement().isEmpty())
&& (ctx.presenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.PRESENCE_DATA;
return false;
}
if ((!ctx.configStatement().isEmpty())
&& (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.CONFIG_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;
}
return true;
}
}
......
......@@ -16,8 +16,16 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangDesc;
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.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
......@@ -51,18 +59,22 @@ public final class DescriptionListener {
*/
public static void processDescriptionEntry(TreeWalkListener listener,
GeneratedYangParser.DescriptionStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (description), it perform
* validations and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processDescriptionExit(TreeWalkListener listener,
GeneratedYangParser.DescriptionStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.DESCRIPTION_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData instanceof YangDesc) {
YangDesc description = (YangDesc) tmpData;
description.setDescription(ctx.string().getText());
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.DESCRIPTION_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,16 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangList;
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.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
......@@ -51,18 +59,30 @@ public final class KeyListener {
*/
public static void processKeyEntry(TreeWalkListener listener,
GeneratedYangParser.KeyStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (key), it perform
* validations and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processKeyExit(TreeWalkListener listener,
GeneratedYangParser.KeyStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.KEY_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (listener.getParsedDataStack().peek() instanceof YangList) {
YangList yangList = (YangList) tmpData;
String tmpKeyValue = ctx.string().getText().replace("\"", "");
if (tmpKeyValue.contains(" ")) {
String[] keyValues = tmpKeyValue.split(" ");
for (String keyValue : keyValues) {
yangList.addKey(keyValue);
}
} else {
yangList.addKey(tmpKeyValue);
}
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.KEY_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,18 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
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
......@@ -52,6 +62,8 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
*/
public final class LeafListListener {
private static ParsableDataType yangConstruct;
/**
* Creates a new leaf list listener.
*/
......@@ -68,7 +80,36 @@ public final class LeafListListener {
*/
public static void processLeafListEntry(TreeWalkListener listener,
GeneratedYangParser.LeafListStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.LEAF_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));
}
YangLeafList leafList = new YangLeafList();
leafList.setLeafName(ctx.IDENTIFIER().getText());
Parsable tmpData = listener.getParsedDataStack().peek();
YangLeavesHolder leaves;
if (tmpData instanceof YangLeavesHolder) {
leaves = (YangLeavesHolder) tmpData;
leaves.addLeafList(leafList);
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.LEAF_LIST_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.ENTRY));
}
listener.getParsedDataStack().push(leafList);
}
/**
......@@ -80,6 +121,80 @@ public final class LeafListListener {
*/
public static void processLeafListExit(TreeWalkListener listener,
GeneratedYangParser.LeafListStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.LEAF_LIST_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT);
if (listener.getParsedDataStack().peek() instanceof YangLeafList) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.LEAF_LIST_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT));
}
}
/**
* Validates the cardinality of leaf-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
.LeafListStatementContext ctx) {
if (ctx.typeStatement().isEmpty()
|| (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.TYPE_DATA;
return false;
}
if ((!ctx.unitsStatement().isEmpty())
&& (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.UNITS_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;
}
return true;
}
}
\ No newline at end of file
......
......@@ -20,8 +20,18 @@
*/
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeaf;
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.datamodel.YangLeavesHolder;
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
......@@ -55,6 +65,8 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
*/
public final class LeafListener {
private static ParsableDataType yangConstruct;
/**
* Creates a new leaf listener.
*/
......@@ -71,7 +83,37 @@ public final class LeafListener {
*/
public static void processLeafEntry(TreeWalkListener listener,
GeneratedYangParser.LeafStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.LEAF_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));
}
YangLeaf leaf = new YangLeaf();
leaf.setLeafName(ctx.IDENTIFIER().getText());
Parsable tmpData = listener.getParsedDataStack().peek();
YangLeavesHolder leaves;
if (tmpData instanceof YangLeavesHolder) {
leaves = (YangLeavesHolder) tmpData;
leaves.addLeaf(leaf);
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.LEAF_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.ENTRY));
}
listener.getParsedDataStack().push(leaf);
}
/**
......@@ -83,6 +125,74 @@ public final class LeafListener {
*/
public static void processLeafExit(TreeWalkListener listener,
GeneratedYangParser.LeafStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.LEAF_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT);
if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
listener.getParsedDataStack().pop();
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.LEAF_DATA,
String.valueOf(ctx.IDENTIFIER().getText()),
ListenerErrorLocation.EXIT));
}
}
/**
* Validates the cardinality of leaf 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
.LeafStatementContext ctx) {
if (ctx.typeStatement().isEmpty()
|| (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.TYPE_DATA;
return false;
}
if ((!ctx.unitsStatement().isEmpty())
&& (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.UNITS_DATA;
return false;
}
if ((!ctx.configStatement().isEmpty())
&& (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.CONFIG_DATA;
return false;
}
if ((!ctx.mandatoryStatement().isEmpty())
&& (ctx.mandatoryStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
yangConstruct = ParsableDataType.MANDATORY_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;
}
return true;
}
}
\ No newline at end of file
......
......@@ -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 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
*/
public final class ListListener {
private static ParsableDataType yangConstruct;
/**
* Creates a new list listener.
*/
......@@ -72,7 +86,44 @@ public final class ListListener {
*/
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 final class ListListener {
*/
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
......
......@@ -16,8 +16,16 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeaf;
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.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
......@@ -57,18 +65,27 @@ public final class MandatoryListener {
*/
public static void processMandatoryEntry(TreeWalkListener listener,
GeneratedYangParser.MandatoryStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (mandatory), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processMandatoryExit(TreeWalkListener listener,
GeneratedYangParser.MandatoryStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.MANDATORY_DATA, "", ListenerErrorLocation.ENTRY);
Parsable tmpNode = listener.getParsedDataStack().peek();
switch (tmpNode.getParsableDataType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpNode;
if (ctx.TRUE_KEYWORD() != null) {
leaf.setMandatory(true);
} else {
leaf.setMandatory(false);
}
break;
case CHOICE_DATA: // TODO
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.MANDATORY_DATA, "", ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,17 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangList;
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.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
......@@ -54,18 +63,33 @@ public final class MaxElementsListener {
*/
public static void processMaxElementsEntry(TreeWalkListener listener,
GeneratedYangParser.MaxElementsStatementContext ctx) {
// TODO method implementation
}
int maxElementsValue;
/**
* It is called when parser exits from grammar rule (max-elements), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processMaxElementsExit(TreeWalkListener listener,
GeneratedYangParser.MaxElementsStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.MAX_ELEMENT_DATA, "", ListenerErrorLocation.ENTRY);
if (ctx.maxValueArgument().UNBOUNDED_KEYWORD() != null) {
maxElementsValue = Integer.MAX_VALUE;
} else {
maxElementsValue = Integer.parseInt(ctx.maxValueArgument().INTEGER().getText());
}
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setMaxElelements(maxElementsValue);
break;
case LIST_DATA:
YangList yangList = (YangList) tmpData;
yangList.setMaxElelements(maxElementsValue);
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.MAX_ELEMENT_DATA,
"", ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,17 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangList;
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.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
......@@ -55,18 +64,28 @@ public final class MinElementsListener {
*/
public static void processMinElementsEntry(TreeWalkListener listener,
GeneratedYangParser.MinElementsStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (min-elements), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processMinElementsExit(TreeWalkListener listener,
GeneratedYangParser.MinElementsStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.MIN_ELEMENT_DATA, String.valueOf(ctx.INTEGER().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setMinElements(Integer.parseInt(ctx.INTEGER().getText()));
break;
case LIST_DATA:
YangList yangList = (YangList) tmpData;
yangList.setMinElements(Integer.parseInt(ctx.INTEGER().getText()));
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.MIN_ELEMENT_DATA,
String.valueOf(ctx.INTEGER().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,16 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangContainer;
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.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
......@@ -51,18 +59,22 @@ public final class PresenceListener {
*/
public static void processPresenceEntry(TreeWalkListener listener,
GeneratedYangParser.PresenceStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (presence), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processPresenceExit(TreeWalkListener listener,
GeneratedYangParser.PresenceStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.PRESENCE_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData.getParsableDataType() == ParsableDataType.CONTAINER_DATA) {
YangContainer container = (YangContainer) tmpData;
container.setPresence(ctx.string().getText());
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.PRESENCE_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,16 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangReference;
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.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
......@@ -51,18 +59,22 @@ public final class ReferenceListener {
*/
public static void processReferenceEntry(TreeWalkListener listener,
GeneratedYangParser.ReferenceStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (reference), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processReferenceExit(TreeWalkListener listener,
GeneratedYangParser.ReferenceStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.REFERENCE_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData instanceof YangReference) {
YangReference reference = (YangReference) tmpData;
reference.setReference(ctx.string().getText());
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.REFERENCE_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,17 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangStatus;
import org.onosproject.yangutils.datamodel.YangStatusType;
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.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,18 +65,28 @@ public final class StatusListener {
*/
public static void processStatusEntry(TreeWalkListener listener,
GeneratedYangParser.StatusStatementContext ctx) {
// TODO method implementation
}
YangStatusType status;
/**
* It is called when parser exits from grammar rule (status), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processStatusExit(TreeWalkListener listener,
GeneratedYangParser.StatusStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.STATUS_DATA, "", ListenerErrorLocation.ENTRY);
if (ctx.CURRENT_KEYWORD() != null) {
status = YangStatusType.CURRENT.CURRENT;
} else if (ctx.DEPRECATED_KEYWORD() != null) {
status = YangStatusType.DEPRECATED;
} else {
status = YangStatusType.OBSOLETE.OBSOLETE;
}
Parsable tmpData = listener.getParsedDataStack().peek();
if (tmpData instanceof YangStatus) {
YangStatus yangStatus = (YangStatus) tmpData;
yangStatus.setStatus(status);
} else {
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.STATUS_DATA, "", ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,19 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangType;
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.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
......@@ -55,18 +66,35 @@ public final class TypeListener {
*/
public static void processTypeEntry(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (type), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processTypeExit(TreeWalkListener listener,
GeneratedYangParser.TypeStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.TYPE_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
YangType type = new YangType();
YangDataTypes yangDataTypes = YangDataTypes.getType(ctx.string().getText());
type.setDataTypeName(ctx.string().getText());
type.setDataType(yangDataTypes);
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpData;
leaf.setDataType(type);
break;
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setDataType(type);
break;
case TYPEDEF_DATA: //TODO
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.TYPE_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......
......@@ -16,8 +16,17 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
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.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
......@@ -51,18 +60,31 @@ public final class UnitsListener {
*/
public static void processUnitsEntry(TreeWalkListener listener,
GeneratedYangParser.UnitsStatementContext ctx) {
// TODO method implementation
}
/**
* It is called when parser exits from grammar rule (units), it performs
* validation and updates the data model tree.
*
* @param listener listener's object.
* @param ctx context object of the grammar rule.
*/
public static void processUnitsExit(TreeWalkListener listener,
GeneratedYangParser.UnitsStatementContext ctx) {
// TODO method implementation
// Check for stack to be non empty.
ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
ParsableDataType.UNITS_DATA, String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY);
Parsable tmpData = listener.getParsedDataStack().peek();
switch (tmpData.getParsableDataType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpData;
leaf.setUnits(ctx.string().getText());
break;
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setUnits(ctx.string().getText());
break;
case TYPEDEF_DATA:
// TODO
break;
default:
throw new ParserException(ListenerErrorMessageConstruction
.constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
ParsableDataType.UNITS_DATA,
String.valueOf(ctx.string().getText()),
ListenerErrorLocation.ENTRY));
}
}
}
\ No newline at end of file
......