blob: 643499f03481eabc825626b05b7c49af6faac5b5 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +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;
Vidyashree Rama59071f32016-02-20 19:27:56 +053018
Bharat saraswald9822e92016-04-05 15:13:44 +053019import org.onosproject.yangutils.datamodel.YangAugment;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053020import org.onosproject.yangutils.datamodel.YangCase;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053021import org.onosproject.yangutils.datamodel.YangContainer;
Vinod Kumar S427d2932016-04-20 13:02:58 +053022import org.onosproject.yangutils.datamodel.YangGrouping;
Bharat saraswald9822e92016-04-05 15:13:44 +053023import org.onosproject.yangutils.datamodel.YangInput;
Vidyashree Rama59071f32016-02-20 19:27:56 +053024import org.onosproject.yangutils.datamodel.YangList;
25import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053027import org.onosproject.yangutils.datamodel.YangNotification;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053028import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
30import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053031import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053033import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053034import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
35
Vinod Kumar S38046502016-03-23 15:30:27 +053036import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
37import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangContainerNode;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vinod Kumar S427d2932016-04-20 13:02:58 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
42 .constructExtendedListenerErrorMessage;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
44 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
48import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053050import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053051import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053052import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
53import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
54import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
55import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
56import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
57import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053058
59/*
60 * Reference: RFC6020 and YANG ANTLR Grammar
61 *
62 * ABNF grammar as per RFC6020
63 * container-stmt = container-keyword sep identifier-arg-str optsep
64 * (";" /
65 * "{" stmtsep
66 * ;; these stmts can appear in any order
67 * [when-stmt stmtsep]
68 * *(if-feature-stmt stmtsep)
69 * *(must-stmt stmtsep)
70 * [presence-stmt stmtsep]
71 * [config-stmt stmtsep]
72 * [status-stmt stmtsep]
73 * [description-stmt stmtsep]
74 * [reference-stmt stmtsep]
75 * *((typedef-stmt /
76 * grouping-stmt) stmtsep)
77 * *(data-def-stmt stmtsep)
78 * "}")
79 *
80 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053081 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Rama92fc5562016-02-12 18:44:12 +053082 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
83 * presenceStatement | configStatement | statusStatement | descriptionStatement |
84 * referenceStatement | typedefStatement | groupingStatement
85 * | dataDefStatement)* RIGHT_CURLY_BRACE);
86 */
87
88/**
Bharat saraswald9822e92016-04-05 15:13:44 +053089 * Represents listener based call back function corresponding to the "container"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053090 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
91 */
92public final class ContainerListener {
93
94 /**
95 * Creates a new container listener.
96 */
97 private ContainerListener() {
98 }
99
100 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 * It is called when parser receives an input matching the grammar rule
102 * (container), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530103 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530104 * @param listener listener's object
105 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530106 */
107 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530108 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530109
110 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530111 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
112
113 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530114
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530115 // Validate sub statement cardinality.
116 validateSubStatementsCardinality(ctx);
117
118 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530119 int line = ctx.getStart().getLine();
120 int charPositionInLine = ctx.getStart().getCharPositionInLine();
121 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530122
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 YangContainer container = getYangContainerNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530124 container.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530125
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530126 /*
127 * If "config" is not specified, the default is the same as the parent
128 * schema node's "config" value.
129 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530130 if (ctx.configStatement().isEmpty()) {
131 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
132 container.setConfig(parentConfig);
133 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530134
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530135 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 if (curData instanceof YangModule || curData instanceof YangContainer
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530137 || curData instanceof YangList || curData instanceof YangCase
138 || curData instanceof YangNotification
139 || curData instanceof YangInput || curData instanceof YangOutput
Vinod Kumar S427d2932016-04-20 13:02:58 +0530140 || curData instanceof YangAugment
141 || curData instanceof YangGrouping) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530142 YangNode curNode = (YangNode) curData;
143 try {
144 curNode.addChild(container);
145 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530146 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530148 }
149 listener.getParsedDataStack().push(container);
150 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530151 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530152 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530153 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530154 }
155
156 /**
157 * It is called when parser exits from grammar rule (container), it perform
158 * validations and updates the data model tree.
159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @param listener listener's object
161 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530162 */
163 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530164 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530165
166 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530167 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530168
169 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530170 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
171 try {
172 yangContainer.validateDataOnExit();
173 } catch (DataModelException e) {
174 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530175 CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530176 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530177 listener.getParsedDataStack().pop();
178 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530179 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530181 }
182 }
183
184 /**
185 * Validates the cardinality of container sub-statements as per grammar.
186 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530187 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530188 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530190
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530191 validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
192 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
193 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
194 ctx.identifier().getText());
195 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
196 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Vinod Kumar S427d2932016-04-20 13:02:58 +0530197 // TODO validate 'when' cardinality
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530198 }
199}