blob: 08edab158c8530d0351be7b6307b20c191e24924 [file] [log] [blame]
Gaurav Agrawal9c512e02016-02-25 04:37:05 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal9c512e02016-02-25 04:37:05 +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 * enum-stmt = enum-keyword sep string optsep
24 * (";" /
25 * "{" stmtsep
26 * ;; these stmts can appear in any order
27 * [value-stmt stmtsep]
28 * [status-stmt stmtsep]
29 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep]
31 * "}")
32 *
33 * ANTLR grammar rule
34 * enumStatement : ENUM_KEYWORD string (STMTEND | LEFT_CURLY_BRACE enumStatementBody RIGHT_CURLY_BRACE);
35 *
36 * enumStatementBody : valueStatement? statusStatement? descriptionStatement? referenceStatement?
37 * | valueStatement? statusStatement? referenceStatement? descriptionStatement?
38 * | valueStatement? descriptionStatement? statusStatement? referenceStatement?
39 * | valueStatement? descriptionStatement? referenceStatement? statusStatement?
40 * | valueStatement? referenceStatement? statusStatement? descriptionStatement?
41 * | valueStatement? referenceStatement? descriptionStatement? statusStatement?
42 * | statusStatement? valueStatement? descriptionStatement? referenceStatement?
43 * | statusStatement? valueStatement? referenceStatement? descriptionStatement?
44 * | statusStatement? descriptionStatement? descriptionStatement? valueStatement?
45 * | statusStatement? descriptionStatement? valueStatement? descriptionStatement?
46 * | statusStatement? referenceStatement? valueStatement? descriptionStatement?
47 * | statusStatement? referenceStatement? descriptionStatement? valueStatement?
48 * | descriptionStatement? valueStatement? statusStatement? referenceStatement?
49 * | descriptionStatement? valueStatement? referenceStatement? statusStatement?
50 * | descriptionStatement? statusStatement? valueStatement? referenceStatement?
51 * | descriptionStatement? statusStatement? referenceStatement? valueStatement?
52 * | descriptionStatement? referenceStatement? valueStatement? statusStatement?
53 * | descriptionStatement? referenceStatement? statusStatement? valueStatement?
54 * | referenceStatement? valueStatement? descriptionStatement? statusStatement?
55 * | referenceStatement? valueStatement? statusStatement? descriptionStatement?
56 * | referenceStatement? statusStatement? descriptionStatement? valueStatement?
57 * | referenceStatement? statusStatement? valueStatement? descriptionStatement?
58 * | referenceStatement? descriptionStatement? valueStatement? statusStatement?
59 * | referenceStatement? descriptionStatement? statusStatement? valueStatement?
60 * ;
61 */
62
63import org.onosproject.yangutils.datamodel.YangEnum;
64import org.onosproject.yangutils.datamodel.YangEnumeration;
65import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
66import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053067import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
68import org.onosproject.yangutils.parser.exceptions.ParserException;
69import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Bharat saraswald9822e92016-04-05 15:13:44 +053070
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053071import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
72import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
73import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
74import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
75import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.DUPLICATE_ENTRY;
76import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Bharat saraswald9822e92016-04-05 15:13:44 +053080import static org.onosproject.yangutils.utils.YangConstructType.ENUM_DATA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053081import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
82import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053083
84/**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Represents listener based call back function corresponding to the "enum" rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053086 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
87 */
88public final class EnumListener {
89
90 /**
91 * Creates a new enum listener.
92 */
93 private EnumListener() {
94 }
95
96 /**
97 * It is called when parser enters grammar rule (enum), 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 Agrawal9c512e02016-02-25 04:37:05 +0530102 */
103 public static void processEnumEntry(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) {
104
105 // Check for stack to be non empty.
106 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), ENTRY);
107
108 YangEnum enumNode = new YangEnum();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530109 enumNode.setNamedValue(getValidNamedValue(ctx.string().getText()));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530110 listener.getParsedDataStack().push(enumNode);
111 }
112
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530113 /*Removes quotes from the enum name if present.*/
Bharat saraswalc0e04842016-05-12 13:16:57 +0530114 private static String getValidNamedValue(String name) {
115 if (name.contains(QUOTES)) {
116 name = name.replace(QUOTES, EMPTY_STRING);
117 }
118 return name;
119 }
120
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530121 /**
122 * It is called when parser exits from grammar rule (enum), it perform
123 * validations and update the data model tree.
124 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530125 * @param listener Listener's object
126 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530127 */
128 public static void processEnumExit(TreeWalkListener listener, GeneratedYangParser.EnumStatementContext ctx) {
129
130 // Check for stack to be non empty.
131 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT);
132
133 Parsable tmpEnumNode = listener.getParsedDataStack().peek();
134 if (tmpEnumNode instanceof YangEnum) {
135 listener.getParsedDataStack().pop();
136
137 // Check for stack to be non empty.
138 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT);
139
140 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530141 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530142 case ENUMERATION_DATA: {
143 YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530144 if (ctx.enumStatementBody() == null || ctx.enumStatementBody().valueStatement() == null) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530145 int maxValue = 0;
146 boolean isValuePresent = false;
147
148 for (YangEnum curEnum : yangEnumeration.getEnumSet()) {
149 if (maxValue <= curEnum.getValue()) {
150 maxValue = curEnum.getValue();
151 isValuePresent = true;
152 }
153 }
154 if (isValuePresent) {
155 maxValue++;
156 }
157 ((YangEnum) tmpEnumNode).setValue(maxValue);
158 }
159 try {
160 yangEnumeration.addEnumInfo((YangEnum) tmpEnumNode);
161 } catch (DataModelException e) {
162 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
163 DUPLICATE_ENTRY, ENUM_DATA, ctx.string().getText(), EXIT, e.getMessage()));
164 parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
165 parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
166 throw parserException;
167 }
168 break;
169 }
170 default:
171 throw new ParserException(
172 constructListenerErrorMessage(INVALID_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
173 }
174 } else {
175 throw new ParserException(
Bharat saraswalc0e04842016-05-12 13:16:57 +0530176 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(),
177 EXIT));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530178 }
179 }
180}