blob: 9cfc5aa6d2f48f58b1abcc2db45370a3806ea217 [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;
Bharat saraswal96dfef02016-06-16 00:29:12 +053066import org.onosproject.yangutils.datamodel.utils.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
Bharat saraswal96dfef02016-06-16 00:29:12 +053071import static org.onosproject.yangutils.datamodel.utils.YangConstructType.ENUM_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053072import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
73import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
74import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
75import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
76import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.DUPLICATE_ENTRY;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
80import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
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()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530149 if (curEnum.getValue() == Integer.MAX_VALUE) {
150 ParserException parserException = new ParserException("YANG file error : "
151 + "An enum value MUST be specified for enum substatements following the one"
152 + "with the current highest value");
153 parserException.setLine(ctx.getStart().getLine());
154 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
155 throw parserException;
156 } else if (maxValue <= curEnum.getValue()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530157 maxValue = curEnum.getValue();
158 isValuePresent = true;
159 }
160 }
161 if (isValuePresent) {
162 maxValue++;
163 }
164 ((YangEnum) tmpEnumNode).setValue(maxValue);
165 }
166 try {
167 yangEnumeration.addEnumInfo((YangEnum) tmpEnumNode);
168 } catch (DataModelException e) {
169 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
170 DUPLICATE_ENTRY, ENUM_DATA, ctx.string().getText(), EXIT, e.getMessage()));
Vidyashree Rama1db15562016-05-17 16:16:15 +0530171 parserException.setLine(ctx.getStart().getLine());
172 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530173 throw parserException;
174 }
175 break;
176 }
177 default:
178 throw new ParserException(
179 constructListenerErrorMessage(INVALID_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
180 }
181 } else {
182 throw new ParserException(
Vidyashree Rama1db15562016-05-17 16:16:15 +0530183 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530184 }
185 }
186}