blob: 9bca04d379cebbc16967a563ecc468f42cbbd23c [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;
47import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053048import org.onosproject.yangutils.datamodel.YangUnion;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053049import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053050import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
51import org.onosproject.yangutils.parser.exceptions.ParserException;
52import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053053
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053054import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
55import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
56import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
57import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
58import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
59import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
60import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061import static org.onosproject.yangutils.utils.YangConstructType.ENUMERATION_DATA;
62import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053063
64/**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Represents listener based call back function corresponding to the
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053066 * "enumeration" rule defined in ANTLR grammar file for corresponding ABNF rule
67 * in RFC 6020.
68 */
69public final class EnumerationListener {
70
71 /**
72 * Creates a new enumeration listener.
73 */
74 private EnumerationListener() {
75 }
76
77 /**
78 * It is called when parser enters grammar rule (enumeration), it perform
79 * validations and updates the data model tree.
80 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053081 * @param listener listener's object
82 * @param ctx context object of the grammar rule
Gaurav Agrawal9c512e02016-02-25 04:37:05 +053083 */
84 public static void processEnumerationEntry(TreeWalkListener listener,
85 GeneratedYangParser.EnumSpecificationContext ctx) {
86
87 // Check for stack to be non empty.
88 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
89
90 if (listener.getParsedDataStack().peek() instanceof YangType) {
91 YangEnumeration enumerationNode = new YangEnumeration();
92 Parsable typeData = listener.getParsedDataStack().pop();
93
94 // Check for stack to be non empty.
95 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
96
97 Parsable tmpData = listener.getParsedDataStack().peek();
98
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053099 switch (tmpData.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530100 case LEAF_DATA:
101 enumerationNode.setEnumerationName(((YangLeaf) tmpData).getLeafName());
102 break;
103 case LEAF_LIST_DATA:
104 enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName());
105 break;
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530106 case UNION_DATA:
107 enumerationNode.setEnumerationName(((YangUnion) tmpData).getUnionName());
108 break;
109 // TODO typedef, deviate.
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530110 default:
111 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530112 ((YangType<?>) typeData).getDataTypeName(), ENTRY));
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530113 }
114 listener.getParsedDataStack().push(typeData);
115 listener.getParsedDataStack().push(enumerationNode);
116 } else {
117 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", ENTRY));
118 }
119 }
120
121 /**
122 * It is called when parser exits from grammar rule (enumeration), it
123 * perform 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 processEnumerationExit(TreeWalkListener listener,
129 GeneratedYangParser.EnumSpecificationContext ctx) {
130
131 // Check for stack to be non empty.
132 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
133
134 Parsable tmpEnumerationNode = listener.getParsedDataStack().peek();
135 if (tmpEnumerationNode instanceof YangEnumeration) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530136 YangEnumeration enumerationNode = (YangEnumeration) tmpEnumerationNode;
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530137 listener.getParsedDataStack().pop();
138
139 // Check for stack to be non empty.
140 checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", EXIT);
141
142 Parsable tmpNode = listener.getParsedDataStack().peek();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530143 switch (tmpNode.getYangConstructType()) {
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530144 case TYPE_DATA: {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530145 YangType<YangEnumeration> typeNode = (YangType<YangEnumeration>) tmpNode;
146 typeNode.setDataTypeExtendedInfo(enumerationNode);
Gaurav Agrawal9c512e02016-02-25 04:37:05 +0530147 break;
148 }
149 default:
150 throw new ParserException(
151 constructListenerErrorMessage(INVALID_HOLDER, ENUMERATION_DATA, "", EXIT));
152 }
153 } else {
154 throw new ParserException(
155 constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUMERATION_DATA, "", EXIT));
156 }
157 }
158}