blob: e3303507cb79949139744fa0b678b139d62feee0 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
2 * Copyright 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;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053018import org.onosproject.yangutils.datamodel.YangContainer;
19import org.onosproject.yangutils.datamodel.YangNode;
20import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
21import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053022
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
31import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * container-stmt = container-keyword sep identifier-arg-str optsep
38 * (";" /
39 * "{" stmtsep
40 * ;; these stmts can appear in any order
41 * [when-stmt stmtsep]
42 * *(if-feature-stmt stmtsep)
43 * *(must-stmt stmtsep)
44 * [presence-stmt stmtsep]
45 * [config-stmt stmtsep]
46 * [status-stmt stmtsep]
47 * [description-stmt stmtsep]
48 * [reference-stmt stmtsep]
49 * *((typedef-stmt /
50 * grouping-stmt) stmtsep)
51 * *(data-def-stmt stmtsep)
52 * "}")
53 *
54 * ANTLR grammar rule
55 * containerStatement : CONTAINER_KEYWORD IDENTIFIER
56 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
57 * presenceStatement | configStatement | statusStatement | descriptionStatement |
58 * referenceStatement | typedefStatement | groupingStatement
59 * | dataDefStatement)* RIGHT_CURLY_BRACE);
60 */
61
62/**
63 * Implements listener based call back function corresponding to the "container"
64 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 */
66public final class ContainerListener {
67
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053068 private static ParsableDataType yangConstruct;
69
Vidyashree Rama92fc5562016-02-12 18:44:12 +053070 /**
71 * Creates a new container listener.
72 */
73 private ContainerListener() {
74 }
75
76 /**
77 * It is called when parser receives an input matching the grammar
78 * rule (container), performs validation and updates the data model
79 * tree.
80 *
81 * @param listener listener's object.
82 * @param ctx context object of the grammar rule.
83 */
84 public static void processContainerEntry(TreeWalkListener listener,
85 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053086
87 // Check for stack to be non empty.
88 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
89 ParsableDataType.CONTAINER_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
90 ListenerErrorLocation.ENTRY);
91
92 boolean result = validateSubStatementsCardinality(ctx);
93 if (!result) {
94 throw new ParserException(ListenerErrorMessageConstruction
95 .constructListenerErrorMessage(ListenerErrorType.INVALID_CARDINALITY,
96 yangConstruct, "", ListenerErrorLocation.ENTRY));
97 }
98
99 YangContainer container = new YangContainer();
100 container.setName(ctx.IDENTIFIER().getText());
101
102 Parsable curData = listener.getParsedDataStack().peek();
103
104 if (curData instanceof YangNode) {
105 YangNode curNode = (YangNode) curData;
106 try {
107 curNode.addChild(container);
108 } catch (DataModelException e) {
109 throw new ParserException(ListenerErrorMessageConstruction
110 .constructExtendedListenerErrorMessage(ListenerErrorType.UNHANDLED_PARSED_DATA,
111 ParsableDataType.CONTAINER_DATA,
112 String.valueOf(ctx.IDENTIFIER().getText()),
113 ListenerErrorLocation.ENTRY,
114 e.getMessage()));
115 }
116 listener.getParsedDataStack().push(container);
117 } else {
118 throw new ParserException(ListenerErrorMessageConstruction
119 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
120 ParsableDataType.CONTAINER_DATA,
121 String.valueOf(ctx.IDENTIFIER().getText()),
122 ListenerErrorLocation.ENTRY));
123 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530124 }
125
126 /**
127 * It is called when parser exits from grammar rule (container), it perform
128 * validations and updates the data model tree.
129 *
130 * @param listener listener's object.
131 * @param ctx context object of the grammar rule.
132 */
133 public static void processContainerExit(TreeWalkListener listener,
134 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530135
136 // Check for stack to be non empty.
137 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
138 ParsableDataType.CONTAINER_DATA, String.valueOf(ctx.IDENTIFIER().getText()),
139 ListenerErrorLocation.EXIT);
140
141 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
142 listener.getParsedDataStack().pop();
143 } else {
144 throw new ParserException(ListenerErrorMessageConstruction
145 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
146 ParsableDataType.CONTAINER_DATA,
147 String.valueOf(ctx.IDENTIFIER().getText()),
148 ListenerErrorLocation.EXIT));
149 }
150 }
151
152 /**
153 * Validates the cardinality of container sub-statements as per grammar.
154 *
155 * @param ctx context object of the grammar rule.
156 * @return true/false validation success or failure.
157 */
158 public static boolean validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
159
160 if ((!ctx.presenceStatement().isEmpty())
161 && (ctx.presenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
162 yangConstruct = ParsableDataType.PRESENCE_DATA;
163 return false;
164 }
165
166 if ((!ctx.configStatement().isEmpty())
167 && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
168 yangConstruct = ParsableDataType.CONFIG_DATA;
169 return false;
170 }
171
172 if ((!ctx.descriptionStatement().isEmpty())
173 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
174 yangConstruct = ParsableDataType.DESCRIPTION_DATA;
175 return false;
176 }
177
178 if ((!ctx.referenceStatement().isEmpty())
179 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
180 yangConstruct = ParsableDataType.REFERENCE_DATA;
181 return false;
182 }
183
184 if ((!ctx.statusStatement().isEmpty())
185 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
186 yangConstruct = ParsableDataType.STATUS_DATA;
187 return false;
188 }
189
190 return true;
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530191 }
192}