blob: 07ada45f8a43437d0b1b7d6912dd8c9715676687 [file] [log] [blame]
Gaurav Agrawal4ce3acf2016-02-25 04:37:05 +05301/*
2 * Copyright 2014-2016 Open Networking Laboratory
3 *
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;
31import org.onosproject.yangutils.parser.Parsable;
32import static org.onosproject.yangutils.parser.ParsableDataType.VALUE_DATA;
33import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
34import org.onosproject.yangutils.parser.exceptions.ParserException;
35import org.onosproject.yangutils.parser.impl.TreeWalkListener;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
38import 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;
42
43/**
44 * Implements listener based call back function corresponding to the "value"
45 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
46 */
47public final class ValueListener {
48
49 /**
50 * Creates a new value listener.
51 */
52 private ValueListener() {
53 }
54
55 /**
56 * It is called when parser receives an input matching the grammar rule
57 * (value), perform validations and update the data model tree.
58 *
59 * @param listener Listener's object.
60 * @param ctx context object of the grammar rule.
61 */
62 public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
63
64 // Check for stack to be non empty.
65 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
66
67 // Obtain the node of the stack.
68 Parsable tmpNode = listener.getParsedDataStack().peek();
69 switch (tmpNode.getParsableDataType()) {
70 case ENUM_DATA: {
71 YangEnum enumNode = (YangEnum) tmpNode;
72 if (!isEnumValueValid(listener, ctx)) {
73 ParserException parserException = new ParserException("Input version not supported");
74 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
75 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
76 throw parserException;
77 }
78 enumNode.setValue(Integer.valueOf(ctx.INTEGER().getText()));
79 break;
80 }
81 default:
82 throw new ParserException(
83 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
84 }
85 }
86
87 /**
88 * Validates ENUM value uniqueness.
89 *
90 * @param listener Listener's object.
91 * @param ctx context object of the grammar rule.
92 * @return validation result
93 */
94 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
95 Parsable enumNode = listener.getParsedDataStack().pop();
96 Parsable tmpNode = listener.getParsedDataStack().peek();
97 switch (tmpNode.getParsableDataType()) {
98 case ENUMERATION_DATA: {
99 YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
100 for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
101 if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) {
102 listener.getParsedDataStack().push(enumNode);
103 return false;
104 }
105 }
106 listener.getParsedDataStack().push(enumNode);
107 return true;
108 }
109 default:
110 listener.getParsedDataStack().push(enumNode);
111 throw new ParserException(
112 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), EXIT));
113 }
114 }
115}