blob: 7f15f4e0d733a253183390d7d463045727516e47 [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 * 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
64import org.onosproject.yangutils.datamodel.YangBit;
65import org.onosproject.yangutils.datamodel.YangBits;
66import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
67import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053068import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
69import org.onosproject.yangutils.parser.exceptions.ParserException;
70import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053071
72import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
73import static org.onosproject.yangutils.utils.YangConstructType.BIT_DATA;
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +053074import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
75import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
76import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
80import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
81import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
82import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
83
84/**
85 * Implements listener based call back function corresponding to the "bit"
86 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
87 */
88public final class BitListener {
89
90 /**
91 * Creates a new bit listener.
92 */
93 private BitListener() {
94 }
95
96 /**
97 * It is called when parser enters grammar rule (bit), it perform
98 * validations and updates the data model tree.
99 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530100 * @param listener listener's object
101 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530102 */
103 public static void processBitEntry(TreeWalkListener listener,
104 GeneratedYangParser.BitStatementContext ctx) {
105
106 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530107 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), ENTRY);
108
109 String identifier = getValidIdentifier(ctx.identifier().getText(), BIT_DATA, ctx);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530110
111 YangBit bitNode = new YangBit();
Vidyashree Rama468f8282016-03-04 19:08:35 +0530112 bitNode.setBitName(identifier);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530113 listener.getParsedDataStack().push(bitNode);
114 }
115
116 /**
117 * It is called when parser exits from grammar rule (bit), it perform
118 * validations and update the data model tree.
119 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 * @param listener Listener's object
121 * @param ctx context object of the grammar rule
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530122 */
123 public static void processBitExit(TreeWalkListener listener,
124 GeneratedYangParser.BitStatementContext ctx) {
125
126 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530127 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530128
129 Parsable tmpBitNode = listener.getParsedDataStack().peek();
130 if (tmpBitNode instanceof YangBit) {
131 listener.getParsedDataStack().pop();
132
133 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530134 checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT);
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530135
136 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530137 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530138 case BITS_DATA: {
139 YangBits yangBits = (YangBits) tmpNode;
140 if ((ctx.bitBodyStatement() == null) || (ctx.bitBodyStatement().positionStatement() == null)) {
141 int maxPosition = 0;
142 boolean isPositionPresent = false;
143
144 for (YangBit curBit : yangBits.getBitSet()) {
145 if (maxPosition <= curBit.getPosition()) {
146 maxPosition = curBit.getPosition();
147 isPositionPresent = true;
148 }
149 }
150 if (isPositionPresent) {
151 maxPosition++;
152 }
153 ((YangBit) tmpBitNode).setPosition(maxPosition);
154 }
155 try {
156 yangBits.addBitInfo((YangBit) tmpBitNode);
157 } catch (DataModelException e) {
158 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530159 INVALID_CONTENT, BIT_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
160 parserException.setLine(ctx.getStart().getLine());
161 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530162 throw parserException;
163 }
164 break;
165 }
166 default:
167 throw new ParserException(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530168 constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530169 }
170 } else {
171 throw new ParserException(
Vidyashree Rama468f8282016-03-04 19:08:35 +0530172 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.identifier().getText(), EXIT));
Gaurav Agrawal0cfdeed2016-02-26 02:47:39 +0530173 }
174 }
175}