blob: f7dbf8c97c6574e74e28ffeaa66ba7bbb7d1a1ea [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;
Bharat saraswald9822e92016-04-05 15:13:44 +053022import org.onosproject.yangutils.datamodel.YangInput;
Vidyashree Rama59071f32016-02-20 19:27:56 +053023import org.onosproject.yangutils.datamodel.YangList;
24import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053026import org.onosproject.yangutils.datamodel.YangNotification;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053027import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053028import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
29import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053031import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053033import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
34
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
36import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangContainerNode;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053049import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
50import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
51import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
52import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
53import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
54import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053055
56/*
57 * Reference: RFC6020 and YANG ANTLR Grammar
58 *
59 * ABNF grammar as per RFC6020
60 * container-stmt = container-keyword sep identifier-arg-str optsep
61 * (";" /
62 * "{" stmtsep
63 * ;; these stmts can appear in any order
64 * [when-stmt stmtsep]
65 * *(if-feature-stmt stmtsep)
66 * *(must-stmt stmtsep)
67 * [presence-stmt stmtsep]
68 * [config-stmt stmtsep]
69 * [status-stmt stmtsep]
70 * [description-stmt stmtsep]
71 * [reference-stmt stmtsep]
72 * *((typedef-stmt /
73 * grouping-stmt) stmtsep)
74 * *(data-def-stmt stmtsep)
75 * "}")
76 *
77 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053078 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Rama92fc5562016-02-12 18:44:12 +053079 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
80 * presenceStatement | configStatement | statusStatement | descriptionStatement |
81 * referenceStatement | typedefStatement | groupingStatement
82 * | dataDefStatement)* RIGHT_CURLY_BRACE);
83 */
84
85/**
Bharat saraswald9822e92016-04-05 15:13:44 +053086 * Represents listener based call back function corresponding to the "container"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053087 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
88 */
89public final class ContainerListener {
90
91 /**
92 * Creates a new container listener.
93 */
94 private ContainerListener() {
95 }
96
97 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053098 * It is called when parser receives an input matching the grammar rule
99 * (container), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530100 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 * @param listener listener's object
102 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530103 */
104 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530106
107 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530108 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
109
110 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530111
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 // Validate sub statement cardinality.
113 validateSubStatementsCardinality(ctx);
114
115 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530116 int line = ctx.getStart().getLine();
117 int charPositionInLine = ctx.getStart().getCharPositionInLine();
118 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530119
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 YangContainer container = getYangContainerNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530121 container.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530122
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530123 /*
124 * If "config" is not specified, the default is the same as the parent
125 * schema node's "config" value.
126 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530127 if (ctx.configStatement().isEmpty()) {
128 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
129 container.setConfig(parentConfig);
130 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530131
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530132 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 if (curData instanceof YangModule || curData instanceof YangContainer
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530134 || curData instanceof YangList || curData instanceof YangCase
135 || curData instanceof YangNotification
136 || curData instanceof YangInput || curData instanceof YangOutput
137 || curData instanceof YangAugment) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530138 YangNode curNode = (YangNode) curData;
139 try {
140 curNode.addChild(container);
141 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530142 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530144 }
145 listener.getParsedDataStack().push(container);
146 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530147 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530148 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530149 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530150 }
151
152 /**
153 * It is called when parser exits from grammar rule (container), it perform
154 * validations and updates the data model tree.
155 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530156 * @param listener listener's object
157 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530158 */
159 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530161
162 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530163 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530164
165 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530166 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
167 try {
168 yangContainer.validateDataOnExit();
169 } catch (DataModelException e) {
170 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530171 CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530172 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530173 listener.getParsedDataStack().pop();
174 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530175 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530176 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530177 }
178 }
179
180 /**
181 * Validates the cardinality of container sub-statements as per grammar.
182 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530183 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530184 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530185 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530186
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530187 validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
188 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
189 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
190 ctx.identifier().getText());
191 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
192 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 // TODO when, grouping, typedef.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530194 }
195}