blob: 43e600b0b59ed987eec302676d307134c704d165 [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +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 * position-stmt = position-keyword sep
24 * position-value-arg-str stmtend
25 * position-value-arg-str = < a string that matches the rule
26 * position-value-arg >
27 * position-value-arg = non-negative-integer-value
28 * non-negative-integer-value = "0" / positive-integer-value
29 * positive-integer-value = (non-zero-digit *DIGIT)
30 * zero-integer-value = 1*DIGIT
31 *
32 * ANTLR grammar rule
33 * positionStatement : POSITION_KEYWORD INTEGER STMTEND;
34 */
35
36import org.onosproject.yangutils.datamodel.YangBit;
37import org.onosproject.yangutils.datamodel.YangBits;
38import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053039import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
40import org.onosproject.yangutils.parser.exceptions.ParserException;
41import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053042
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
44import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
47import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053048import static org.onosproject.yangutils.utils.YangConstructType.POSITION_DATA;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053049
50/**
51 * Implements listener based call back function corresponding to the "position"
52 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
53 */
54public final class PositionListener {
55
56 // Exact message in case position is invalid.
57 private static String errMsg;
58
59 /**
60 * Creates a new position listener.
61 */
62 private PositionListener() {
63 }
64
65 /**
66 * It is called when parser receives an input matching the grammar rule
67 * (position), perform validations and update the data model tree.
68 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053069 * @param listener Listener's object
70 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053071 */
72 public static void processPositionEntry(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +053073 GeneratedYangParser.PositionStatementContext ctx) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053074
75 // Check for stack to be non empty.
76 checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
77
78 // Obtain the node of the stack.
79 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053080 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053081 case BIT_DATA: {
82 YangBit bitNode = (YangBit) tmpNode;
83 if (!isBitPositionValid(listener, ctx)) {
84 ParserException parserException = new ParserException(errMsg);
85 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
86 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
87 throw parserException;
88 }
89 bitNode.setPosition(Integer.valueOf(ctx.INTEGER().getText()));
90 break;
91 }
92 default:
93 throw new ParserException(
94 constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
95 }
96 }
97
98 /**
99 * Validates BITS position value correctness and uniqueness.
100 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 * @param listener Listener's object
102 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530103 * @return validation result
104 */
105 private static boolean isBitPositionValid(TreeWalkListener listener,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530106 GeneratedYangParser.PositionStatementContext ctx) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530107 Parsable bitNode = listener.getParsedDataStack().pop();
108
109 // Check for stack to be non empty.
110 checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
111
112 if (Integer.valueOf(ctx.INTEGER().getText()) < 0) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 errMsg = "YANG file error: Negative value of position is invalid.";
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530114 listener.getParsedDataStack().push(bitNode);
115 return false;
116 }
117
118 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530119 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530120 case BITS_DATA: {
121 YangBits yangBits = (YangBits) tmpNode;
122 for (YangBit curBit : yangBits.getBitSet()) {
123 if (Integer.valueOf(ctx.INTEGER().getText()) == curBit.getPosition()) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530124 errMsg = "YANG file error: Duplicate value of position is invalid.";
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530125 listener.getParsedDataStack().push(bitNode);
126 return false;
127 }
128 }
129 listener.getParsedDataStack().push(bitNode);
130 return true;
131 }
132 default:
133 listener.getParsedDataStack().push(bitNode);
134 throw new ParserException(
135 constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
136 }
137 }
138}