blob: 92a0d4209556362d79e2386589037d2b351ad88a [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;
31import org.onosproject.yangutils.parser.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
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053041import static org.onosproject.yangutils.utils.YangConstructType.VALUE_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053042
43/**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Represents listener based call back function corresponding to the "value"
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053045 * 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 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053059 * @param listener Listener's object
60 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053061 */
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();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053069 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053070 case ENUM_DATA: {
71 YangEnum enumNode = (YangEnum) tmpNode;
72 if (!isEnumValueValid(listener, ctx)) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053073 ParserException parserException = new ParserException("Duplicate Value Entry");
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053074 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 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053090 * @param listener Listener's object
91 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053092 * @return validation result
93 */
94 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
95 Parsable enumNode = listener.getParsedDataStack().pop();
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053096
97 // Check for stack to be non empty.
98 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
99
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530100 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530102 case ENUMERATION_DATA: {
103 YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
104 for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
105 if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) {
106 listener.getParsedDataStack().push(enumNode);
107 return false;
108 }
109 }
110 listener.getParsedDataStack().push(enumNode);
111 return true;
112 }
113 default:
114 listener.getParsedDataStack().push(enumNode);
115 throw new ParserException(
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530116 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530117 }
118 }
119}