blob: 41835d613fab186be3bf591cde8b0cc718156b20 [file] [log] [blame]
Gaurav Agrawal480a47d2016-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 Agrawaldb828bd2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.utils.YangConstructType.POSITION_DATA;
Gaurav Agrawal480a47d2016-02-26 02:47:39 +053040import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
41import org.onosproject.yangutils.parser.exceptions.ParserException;
42import org.onosproject.yangutils.parser.impl.TreeWalkListener;
43import 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;
48
49/**
50 * Implements listener based call back function corresponding to the "position"
51 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
52 */
53public final class PositionListener {
54
55 // Exact message in case position is invalid.
56 private static String errMsg;
57
58 /**
59 * Creates a new position listener.
60 */
61 private PositionListener() {
62 }
63
64 /**
65 * It is called when parser receives an input matching the grammar rule
66 * (position), perform validations and update the data model tree.
67 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053068 * @param listener Listener's object
69 * @param ctx context object of the grammar rule
Gaurav Agrawal480a47d2016-02-26 02:47:39 +053070 */
71 public static void processPositionEntry(TreeWalkListener listener,
72 GeneratedYangParser.PositionStatementContext ctx) {
73
74 // Check for stack to be non empty.
75 checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
76
77 // Obtain the node of the stack.
78 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053079 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal480a47d2016-02-26 02:47:39 +053080 case BIT_DATA: {
81 YangBit bitNode = (YangBit) tmpNode;
82 if (!isBitPositionValid(listener, ctx)) {
83 ParserException parserException = new ParserException(errMsg);
84 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
85 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
86 throw parserException;
87 }
88 bitNode.setPosition(Integer.valueOf(ctx.INTEGER().getText()));
89 break;
90 }
91 default:
92 throw new ParserException(
93 constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
94 }
95 }
96
97 /**
98 * Validates BITS position value correctness and uniqueness.
99 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530100 * @param listener Listener's object
101 * @param ctx context object of the grammar rule
Gaurav Agrawal480a47d2016-02-26 02:47:39 +0530102 * @return validation result
103 */
104 private static boolean isBitPositionValid(TreeWalkListener listener,
105 GeneratedYangParser.PositionStatementContext ctx) {
106 Parsable bitNode = listener.getParsedDataStack().pop();
107
108 // Check for stack to be non empty.
109 checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
110
111 if (Integer.valueOf(ctx.INTEGER().getText()) < 0) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530112 errMsg = "YANG file error: Negative value of position is invalid.";
Gaurav Agrawal480a47d2016-02-26 02:47:39 +0530113 listener.getParsedDataStack().push(bitNode);
114 return false;
115 }
116
117 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530118 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal480a47d2016-02-26 02:47:39 +0530119 case BITS_DATA: {
120 YangBits yangBits = (YangBits) tmpNode;
121 for (YangBit curBit : yangBits.getBitSet()) {
122 if (Integer.valueOf(ctx.INTEGER().getText()) == curBit.getPosition()) {
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530123 errMsg = "YANG file error: Duplicate value of position is invalid.";
Gaurav Agrawal480a47d2016-02-26 02:47:39 +0530124 listener.getParsedDataStack().push(bitNode);
125 return false;
126 }
127 }
128 listener.getParsedDataStack().push(bitNode);
129 return true;
130 }
131 default:
132 listener.getParsedDataStack().push(bitNode);
133 throw new ParserException(
134 constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
135 }
136 }
137}