blob: 91adfbfe25f4f0b2868891319c251c37fcf0d592 [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 * type-body-stmts = numerical-restrictions /
24 * decimal64-specification /
25 * string-restrictions /
26 * enum-specification /
27 * leafref-specification /
28 * identityref-specification /
29 * instance-identifier-specification /
30 * bits-specification /
31 * union-specification
32 *
33 * enum-specification = 1*(enum-stmt stmtsep)
34 *
35 * ANTLR grammar rule
36 *
37 * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
38 * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
39 * | bitsSpecification | unionSpecification;
40 *
41 * enumSpecification : enumStatement+;
42 */
43
44import org.onosproject.yangutils.datamodel.YangEnumeration;
45import org.onosproject.yangutils.datamodel.YangLeaf;
46import org.onosproject.yangutils.datamodel.YangLeafList;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053047import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053048import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053049import org.onosproject.yangutils.datamodel.YangTypeDef;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053050import org.onosproject.yangutils.datamodel.YangUnion;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053051import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053052import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053053import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
54import org.onosproject.yangutils.parser.exceptions.ParserException;
55import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053056import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057
Bharat saraswald72411a2016-04-19 01:00:16 +053058import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
59import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangEnumerationNode;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053060import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
61import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053063import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
64import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
65import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
66import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053067import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053068import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053069import static org.onosproject.yangutils.utils.YangConstructType.ENUMERATION_DATA;
70import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053071
72/**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Represents listener based call back function corresponding to the
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053074 * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule
75 * in RFC 6020.
76 */
77public final class EnumerationListener {
78
79 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053080 * Suffix to be used while creating enumeration class.
81 */
82 private static final String ENUMERATION_CLASS_SUFFIX = "_enum";
83
84 /**
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053085 * Creates a new enumeration listener.
86 */
87 private EnumerationListener() {
88 }
89
90 /**
91 * It is called when parser enters grammar rule (enumeration), it perform
92 * validations and updates the data model tree.
93 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 * @param listener listener's object
95 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053096 */
97 public static void processEnumerationEntry(TreeWalkListener listener,
98 GeneratedYangParser.EnumSpecificationContext ctx) {
99
100 // Check for stack to be non empty.
101 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
102
103 if (listener.getParsedDataStack().peek() instanceof YangType) {
Bharat saraswald72411a2016-04-19 01:00:16 +0530104 YangEnumeration enumerationNode = getYangEnumerationNode(JAVA_GENERATION);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530105 Parsable typeData = listener.getParsedDataStack().pop();
106
107 // Check for stack to be non empty.
108 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
109
110 Parsable tmpData = listener.getParsedDataStack().peek();
111
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 switch (tmpData.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530113 case LEAF_DATA:
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530114 // Set the name of enumeration same as leaf.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 enumerationNode.setName(((YangLeaf) tmpData).getName() + ENUMERATION_CLASS_SUFFIX);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530116 // Pop the stack entry to obtain the parent YANG node.
117 Parsable leaf = listener.getParsedDataStack().pop();
118 // Add the enumeration node to the parent holder of leaf.
119 addChildToParentNode(listener, enumerationNode);
120 // Push the popped entry back to the stack.
121 listener.getParsedDataStack().push(leaf);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530122 break;
123 case LEAF_LIST_DATA:
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 // Set the name of enumeration same as leaf list.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530125 enumerationNode.setName(((YangLeafList) tmpData).getName() + ENUMERATION_CLASS_SUFFIX);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530126 // Pop the stack entry to obtain the parent YANG node.
127 Parsable leafList = listener.getParsedDataStack().pop();
128 // Add the enumeration node to the parent holder of leaf.
129 addChildToParentNode(listener, enumerationNode);
130 // Push the popped entry back to the stack.
131 listener.getParsedDataStack().push(leafList);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530132 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530133 case UNION_DATA:
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530134 YangUnion yangUnion = (YangUnion) tmpData;
135 /*
136 * In case parent of enumeration is a union, name of the
137 * enumeration is parent union name suffixed with running
138 * integer number, this is done because under union there
139 * could be multiple child union types.
140 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530141 enumerationNode.setName(yangUnion.getName() + ENUMERATION_CLASS_SUFFIX
142 + yangUnion.getChildUnionNumber());
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530143 // Increment the running number.
144 yangUnion.setChildUnionNumber(yangUnion.getChildUnionNumber() + 1);
145 // Add union as a child to parent union.
146 addChildToParentNode(listener, enumerationNode);
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530147 break;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530148 case TYPEDEF_DATA:
149 YangTypeDef typeDef = (YangTypeDef) tmpData;
150 // Set the name of enumeration same as typedef name.
151 enumerationNode.setName(typeDef.getName() + ENUMERATION_CLASS_SUFFIX);
152 // Add enumeration as a child to parent type def.
153 addChildToParentNode(listener, enumerationNode);
154 break;
155 // TODO deviate.
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530156 default:
157 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530158 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530159 }
160 listener.getParsedDataStack().push(typeData);
161 listener.getParsedDataStack().push(enumerationNode);
162 } else {
163 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY));
164 }
165 }
166
167 /**
168 * It is called when parser exits from grammar rule (enumeration), it
169 * perform validations and update the data model tree.
170 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530171 * @param listener Listener's object
172 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530173 */
174 public static void processEnumerationExit(TreeWalkListener listener,
175 GeneratedYangParser.EnumSpecificationContext ctx) {
176
177 // Check for stack to be non empty.
178 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
179
180 Parsable tmpEnumerationNode = listener.getParsedDataStack().peek();
181 if (tmpEnumerationNode instanceof YangEnumeration) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530182 YangEnumeration enumerationNode = (YangEnumeration) tmpEnumerationNode;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530183 listener.getParsedDataStack().pop();
184
185 // Check for stack to be non empty.
186 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
187
188 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530190 case TYPE_DATA: {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530191 YangType<YangEnumeration> typeNode = (YangType<YangEnumeration>) tmpNode;
192 typeNode.setDataTypeExtendedInfo(enumerationNode);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530193 break;
194 }
195 default:
196 throw new ParserException(
197 constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT));
198 }
199 } else {
200 throw new ParserException(
201 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT));
202 }
203 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530204
205 /**
206 * Adds the enumeration node to the parent holder.
207 *
208 * @param listener listener's object
209 * @param enumerationNode enumeration node which needs to be added to parent
210 */
211 private static void addChildToParentNode(TreeWalkListener listener, YangEnumeration enumerationNode) {
212 if (!(listener.getParsedDataStack().peek() instanceof YangNode)) {
213 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA,
214 "", ENTRY));
215 } else {
216 YangNode curNode = (YangNode) listener.getParsedDataStack().peek();
217 try {
218 curNode.addChild(enumerationNode);
219 } catch (DataModelException e) {
220 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
221 YangConstructType.ENUMERATION_DATA, "", ENTRY, e.getMessage()));
222 }
223 }
224 }
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530225}