blob: 9d412531daad67cd36358a7abe9d7c415fb7f475 [file] [log] [blame]
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +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 * body-stmts = *((extension-stmt /
23 * feature-stmt /
24 * identity-stmt /
25 * typedef-stmt /
26 * grouping-stmt /
27 * data-def-stmt /
28 * augment-stmt /
29 * rpc-stmt /
30 * notification-stmt /
31 * deviation-stmt) stmtsep)
32 *
33 * typedef-stmt = typedef-keyword sep identifier-arg-str optsep
34 * "{" stmtsep
35 * ;; these stmts can appear in any order
36 * type-stmt stmtsep
37 * [units-stmt stmtsep]
38 * [default-stmt stmtsep]
39 * [status-stmt stmtsep]
40 * [description-stmt stmtsep]
41 * [reference-stmt stmtsep]
42 * "}"
43 *
44 * ANTLR grammar rule
45 * typedefStatement : TYPEDEF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE
46 * (typeStatement | unitsStatement | defaultStatement | statusStatement
47 * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
48 */
49
50import org.onosproject.yangutils.datamodel.YangContainer;
51import org.onosproject.yangutils.datamodel.YangList;
52import org.onosproject.yangutils.datamodel.YangModule;
53import org.onosproject.yangutils.datamodel.YangNode;
54import org.onosproject.yangutils.datamodel.YangSubModule;
55import org.onosproject.yangutils.datamodel.YangTypeDef;
56import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
57import org.onosproject.yangutils.parser.Parsable;
58import org.onosproject.yangutils.parser.ParsableDataType;
59import static org.onosproject.yangutils.parser.ParsableDataType.TYPEDEF_DATA;
60import static org.onosproject.yangutils.parser.ParsableDataType.UNITS_DATA;
61import static org.onosproject.yangutils.parser.ParsableDataType.DEFAULT_DATA;
62import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
63import static org.onosproject.yangutils.parser.ParsableDataType.DESCRIPTION_DATA;
64import static org.onosproject.yangutils.parser.ParsableDataType.REFERENCE_DATA;
65import static org.onosproject.yangutils.parser.ParsableDataType.STATUS_DATA;
66import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
67import org.onosproject.yangutils.parser.exceptions.ParserException;
68import org.onosproject.yangutils.parser.impl.TreeWalkListener;
69import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
70import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
71import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
72import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
73import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
74import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
75import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
76import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
77import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
78import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
79import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
80
81/**
82 * Implements listener based call back function corresponding to the "typedef"
83 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
84 */
85public final class TypeDefListener {
86
87 private static ParsableDataType yangConstruct;
88
89 /**
90 * Creates a new typedef listener.
91 */
92 private TypeDefListener() {
93 }
94
95 /**
96 * It is called when parser enters grammar rule (typedef), it perform
97 * validations and updates the data model tree.
98 *
99 * @param listener listener's object.
100 * @param ctx context object of the grammar rule.
101 */
102 public static void processTypeDefEntry(TreeWalkListener listener,
103 GeneratedYangParser.TypedefStatementContext ctx) {
104
105 // Check for stack to be non empty.
106 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
107
108 boolean result = validateSubStatementsCardinality(ctx);
109 if (!result) {
110 throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
111 }
112
113 YangTypeDef typeDefNode = new YangTypeDef();
114 typeDefNode.setDerivedName(ctx.IDENTIFIER().getText());
115
116 Parsable curData = listener.getParsedDataStack().peek();
117
118 if (curData instanceof YangModule | curData instanceof YangSubModule | curData instanceof YangContainer
119 | curData instanceof YangList) {
120 /*
121 * TODO YangGrouping, YangRpc, YangInput, YangOutput, Notification.
122 */
123 YangNode curNode = (YangNode) curData;
124 try {
125 curNode.addChild(typeDefNode);
126 } catch (DataModelException e) {
127 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
128 TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
129 }
130 listener.getParsedDataStack().push(typeDefNode);
131 } else {
132 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
133 TYPEDEF_DATA, ctx.IDENTIFIER().getText(), ENTRY));
134 }
135 }
136
137 /**
138 * It is called when parser exits from grammar rule (typedef), it perform
139 * validations and updates the data model tree.
140 *
141 * @param listener listener's object.
142 * @param ctx context object of the grammar rule.
143 */
144 public static void processTypeDefExit(TreeWalkListener listener,
145 GeneratedYangParser.TypedefStatementContext ctx) {
146
147 // Check for stack to be non empty.
148 checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPEDEF_DATA, ctx.IDENTIFIER().getText(), EXIT);
149
150 if (listener.getParsedDataStack().peek() instanceof YangTypeDef) {
151 listener.getParsedDataStack().pop();
152 } else {
153 listener.getErrorInformation().setErrorFlag(true);
154 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, TYPEDEF_DATA,
155 ctx.IDENTIFIER().getText(), EXIT));
156 }
157 }
158
159 /**
160 * Validates the cardinality of typedef sub-statements as per grammar.
161 *
162 * @param ctx context object of the grammar rule.
163 * @return true/false validation success or failure.
164 */
165 private static boolean validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) {
166
167 if ((!ctx.unitsStatement().isEmpty())
168 && (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
169 yangConstruct = UNITS_DATA;
170 return false;
171 }
172
173 if ((!ctx.defaultStatement().isEmpty())
174 && (ctx.defaultStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
175 yangConstruct = DEFAULT_DATA;
176 return false;
177 }
178
179 if (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY) {
180 yangConstruct = TYPE_DATA;
181 return false;
182 }
183
184 if ((!ctx.descriptionStatement().isEmpty())
185 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
186 yangConstruct = DESCRIPTION_DATA;
187 return false;
188 }
189
190 if ((!ctx.referenceStatement().isEmpty())
191 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
192 yangConstruct = REFERENCE_DATA;
193 return false;
194 }
195
196 if ((!ctx.statusStatement().isEmpty())
197 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
198 yangConstruct = STATUS_DATA;
199 return false;
200 }
201 return true;
202 }
203}