blob: 4dce199e67a9c01df3e3834f12d2826c1e17426b [file] [log] [blame]
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +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 * bit-stmt = bit-keyword sep identifier-arg-str optsep
24 * (";" /
25 * "{" stmtsep
26 * ;; these stmts can appear in any order
27 * [position-stmt stmtsep]
28 * [status-stmt stmtsep]
29 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep]
31 * "}"
32 * "}")
33 *
34 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053035 * bitStatement : BIT_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053036 *
37 * bitBodyStatement : positionStatement? statusStatement? descriptionStatement? referenceStatement?
38 * | positionStatement? statusStatement? referenceStatement? descriptionStatement?
39 * | positionStatement? descriptionStatement? statusStatement? referenceStatement?
40 * | positionStatement? descriptionStatement? referenceStatement? statusStatement?
41 * | positionStatement? referenceStatement? statusStatement? descriptionStatement?
42 * | positionStatement? referenceStatement? descriptionStatement? statusStatement?
43 * | statusStatement? positionStatement? descriptionStatement? referenceStatement?
44 * | statusStatement? positionStatement? referenceStatement? descriptionStatement?
45 * | statusStatement? descriptionStatement? descriptionStatement? positionStatement?
46 * | statusStatement? descriptionStatement? positionStatement? descriptionStatement?
47 * | statusStatement? referenceStatement? positionStatement? descriptionStatement?
48 * | statusStatement? referenceStatement? descriptionStatement? positionStatement?
49 * | descriptionStatement? positionStatement? statusStatement? referenceStatement?
50 * | descriptionStatement? positionStatement? referenceStatement? statusStatement?
51 * | descriptionStatement? statusStatement? positionStatement? referenceStatement?
52 * | descriptionStatement? statusStatement? referenceStatement? positionStatement?
53 * | descriptionStatement? referenceStatement? positionStatement? statusStatement?
54 * | descriptionStatement? referenceStatement? statusStatement? positionStatement?
55 * | referenceStatement? positionStatement? descriptionStatement? statusStatement?
56 * | referenceStatement? positionStatement? statusStatement? descriptionStatement?
57 * | referenceStatement? statusStatement? descriptionStatement? positionStatement?
58 * | referenceStatement? statusStatement? positionStatement? descriptionStatement?
59 * | referenceStatement? descriptionStatement? positionStatement? statusStatement?
60 * | referenceStatement? descriptionStatement? statusStatement? positionStatement?
61 * ;
62 */
63
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +053064import java.util.Map;
65
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053066import org.onosproject.yangutils.datamodel.YangBit;
67import org.onosproject.yangutils.datamodel.YangBits;
68import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053069import org.onosproject.yangutils.datamodel.utils.Parsable;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053070import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
71import org.onosproject.yangutils.parser.exceptions.ParserException;
72import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053073
74import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Bharat saraswal96dfef02016-06-16 00:29:12 +053075import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BIT_DATA;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053076import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
80import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
81import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
82import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
83import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
84import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
85
86/**
Bharat saraswald9822e92016-04-05 15:13:44 +053087 * Represents listener based call back function corresponding to the "bit"
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053088 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
89 */
90public final class BitListener {
91
92 /**
93 * Creates a new bit listener.
94 */
95 private BitListener() {
96 }
97
98 /**
99 * It is called when parser enters grammar rule (bit), it perform
100 * validations and updates the data model tree.
101 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530102 * @param listener listener's object
103 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530104 */
105 public static void processBitEntry(TreeWalkListener listener,
106 GeneratedYangParser.BitStatementContext ctx) {
107
108 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530109 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY);
110
111 String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530112
113 YangBit bitNode = new YangBit();
Vidyashree Rama468f8282016-03-04 19:08:35 +0530114 bitNode.setBitName(identifier);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530115 listener.getParsedDataStack().push(bitNode);
116 }
117
118 /**
119 * It is called when parser exits from grammar rule (bit), it perform
120 * validations and update the data model tree.
121 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530122 * @param listener Listener's object
123 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530124 */
125 public static void processBitExit(TreeWalkListener listener,
126 GeneratedYangParser.BitStatementContext ctx) {
127
128 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530129 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530130
131 Parsable tmpBitNode = listener.getParsedDataStack().peek();
132 if (tmpBitNode instanceof YangBit) {
133 listener.getParsedDataStack().pop();
134
135 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530136 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530137
138 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530139 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530140 case BITS_DATA: {
141 YangBits yangBits = (YangBits) tmpNode;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530142 if (ctx.bitBodyStatement() == null || ctx.bitBodyStatement().positionStatement() == null) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530143 int maxPosition = 0;
144 boolean isPositionPresent = false;
145
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530146 for (Map.Entry<Integer, YangBit> element : yangBits.getBitPositionMap().entrySet()) {
147 if (maxPosition <= element.getKey()) {
148 maxPosition = element.getKey();
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530149 isPositionPresent = true;
150 }
151 }
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +0530152
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530153 if (isPositionPresent) {
154 maxPosition++;
155 }
156 ((YangBit) tmpBitNode).setPosition(maxPosition);
157 }
158 try {
159 yangBits.addBitInfo((YangBit) tmpBitNode);
160 } catch (DataModelException e) {
161 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530162 INVALID_CONTENT, BIT_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
163 parserException.setLine(ctx.getStart().getLine());
164 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530165 throw parserException;
166 }
167 break;
168 }
169 default:
170 throw new ParserException(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530171 constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530172 }
173 } else {
174 throw new ParserException(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530175 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530176 }
177 }
178}