blob: 39efa6b3726191dd9a856bf2bdf15631e50ce683 [file] [log] [blame]
Gaurav Agrawal9c512e02016-02-25 04:37:05 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal9c512e02016-02-25 04:37:05 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19/*
20 * Reference: RFC6020 and YANG ANTLR Grammar
21 *
22 * ABNF grammar as per RFC6020
23 * value-stmt = value-keyword sep integer-value stmtend
24 *
25 * ANTLR grammar rule
26 * valueStatement : VALUE_KEYWORD ((MINUS INTEGER) | INTEGER) STMTEND;
27 */
28
29import org.onosproject.yangutils.datamodel.YangEnum;
30import org.onosproject.yangutils.datamodel.YangEnumeration;
Bharat saraswal96dfef02016-06-16 00:29:12 +053031import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053032import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
33import org.onosproject.yangutils.parser.exceptions.ParserException;
34import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Bharat saraswald9822e92016-04-05 15:13:44 +053035
Bharat saraswal96dfef02016-06-16 00:29:12 +053036import static org.onosproject.yangutils.datamodel.utils.YangConstructType.VALUE_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama1db15562016-05-17 16:16:15 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIntegerValue;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053043
44/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents listener based call back function corresponding to the "value"
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053046 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
47 */
48public final class ValueListener {
49
50 /**
51 * Creates a new value listener.
52 */
53 private ValueListener() {
54 }
55
56 /**
57 * It is called when parser receives an input matching the grammar rule
58 * (value), perform validations and update the data model tree.
59 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053060 * @param listener Listener's object
61 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053062 */
63 public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
64
65 // Check for stack to be non empty.
Vidyashree Rama1db15562016-05-17 16:16:15 +053066 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY);
67
68 // Validate value
69 int value = getValidIntegerValue(ctx.value().getText(), VALUE_DATA, ctx);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053070
71 // Obtain the node of the stack.
72 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053074 case ENUM_DATA: {
75 YangEnum enumNode = (YangEnum) tmpNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +053076 if (!isEnumValueValid(listener, ctx, value)) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053077 ParserException parserException = new ParserException("Duplicate Value Entry");
Vidyashree Rama1db15562016-05-17 16:16:15 +053078 parserException.setLine(ctx.getStart().getLine());
79 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053080 throw parserException;
81 }
Vidyashree Rama1db15562016-05-17 16:16:15 +053082 enumNode.setValue(value);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053083 break;
84 }
85 default:
86 throw new ParserException(
Vidyashree Rama1db15562016-05-17 16:16:15 +053087 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053088 }
89 }
90
91 /**
92 * Validates ENUM value uniqueness.
93 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 * @param listener Listener's object
95 * @param ctx context object of the grammar rule
Vidyashree Rama1db15562016-05-17 16:16:15 +053096 * @param value enum value
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053097 * @return validation result
98 */
Vidyashree Rama1db15562016-05-17 16:16:15 +053099 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx,
100 int value) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530101 Parsable enumNode = listener.getParsedDataStack().pop();
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530102
103 // Check for stack to be non empty.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530104 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530105
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530106 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530107 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530108 case ENUMERATION_DATA: {
109 YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
110 for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530111 if (value == curEnum.getValue()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530112 listener.getParsedDataStack().push(enumNode);
113 return false;
114 }
115 }
116 listener.getParsedDataStack().push(enumNode);
117 return true;
118 }
119 default:
120 listener.getParsedDataStack().push(enumNode);
121 throw new ParserException(
Vidyashree Rama1db15562016-05-17 16:16:15 +0530122 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.value().getText(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530123 }
124 }
125}