blob: 80cf8a76a9b563ed35a58e08808ddbfacbcebb00 [file] [log] [blame]
Gaurav Agrawal9c512e02016-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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.utils.YangConstructType.VALUE_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053033import 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;
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;
41
42/**
43 * Implements listener based call back function corresponding to the "value"
44 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
45 */
46public final class ValueListener {
47
48 /**
49 * Creates a new value listener.
50 */
51 private ValueListener() {
52 }
53
54 /**
55 * It is called when parser receives an input matching the grammar rule
56 * (value), perform validations and update the data model tree.
57 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053058 * @param listener Listener's object
59 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053060 */
61 public static void processValueEntry(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
62
63 // Check for stack to be non empty.
64 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
65
66 // Obtain the node of the stack.
67 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053068 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053069 case ENUM_DATA: {
70 YangEnum enumNode = (YangEnum) tmpNode;
71 if (!isEnumValueValid(listener, ctx)) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053072 ParserException parserException = new ParserException("Duplicate Value Entry");
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053073 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
74 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
75 throw parserException;
76 }
77 enumNode.setValue(Integer.valueOf(ctx.INTEGER().getText()));
78 break;
79 }
80 default:
81 throw new ParserException(
82 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
83 }
84 }
85
86 /**
87 * Validates ENUM value uniqueness.
88 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053089 * @param listener Listener's object
90 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053091 * @return validation result
92 */
93 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
94 Parsable enumNode = listener.getParsedDataStack().pop();
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053095
96 // Check for stack to be non empty.
97 checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
98
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053099 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530100 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530101 case ENUMERATION_DATA: {
102 YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
103 for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
104 if (Integer.valueOf(ctx.INTEGER().getText()) == curEnum.getValue()) {
105 listener.getParsedDataStack().push(enumNode);
106 return false;
107 }
108 }
109 listener.getParsedDataStack().push(enumNode);
110 return true;
111 }
112 default:
113 listener.getParsedDataStack().push(enumNode);
114 throw new ParserException(
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530115 constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530116 }
117 }
118}