blob: 07dc946e5b3a64c7250402cee49656fe0433d18a [file] [log] [blame]
Vidyashree Ramafe971cb2016-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 Ramae300f702016-02-20 19:27:56 +053018
Vidyashree Ramab24ca902016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangContainer;
Vidyashree Ramae300f702016-02-20 19:27:56 +053020import org.onosproject.yangutils.datamodel.YangList;
21import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053022import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.parser.Parsable;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053027import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramad3221322016-03-04 19:08:35 +053028import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
29
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053030import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
31import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangContainerNode;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramae300f702016-02-20 19:27:56 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramae300f702016-02-20 19:27:56 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Ramae300f702016-02-20 19:27:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Ramae300f702016-02-20 19:27:56 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramae300f702016-02-20 19:27:56 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053044import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
49import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053050
51/*
52 * Reference: RFC6020 and YANG ANTLR Grammar
53 *
54 * ABNF grammar as per RFC6020
55 * container-stmt = container-keyword sep identifier-arg-str optsep
56 * (";" /
57 * "{" stmtsep
58 * ;; these stmts can appear in any order
59 * [when-stmt stmtsep]
60 * *(if-feature-stmt stmtsep)
61 * *(must-stmt stmtsep)
62 * [presence-stmt stmtsep]
63 * [config-stmt stmtsep]
64 * [status-stmt stmtsep]
65 * [description-stmt stmtsep]
66 * [reference-stmt stmtsep]
67 * *((typedef-stmt /
68 * grouping-stmt) stmtsep)
69 * *(data-def-stmt stmtsep)
70 * "}")
71 *
72 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053073 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053074 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
75 * presenceStatement | configStatement | statusStatement | descriptionStatement |
76 * referenceStatement | typedefStatement | groupingStatement
77 * | dataDefStatement)* RIGHT_CURLY_BRACE);
78 */
79
80/**
81 * Implements listener based call back function corresponding to the "container"
82 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
83 */
84public final class ContainerListener {
85
86 /**
87 * Creates a new container listener.
88 */
89 private ContainerListener() {
90 }
91
92 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053093 * It is called when parser receives an input matching the grammar rule
94 * (container), performs validation and updates the data model tree.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053095 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053096 * @param listener listener's object
97 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053098 */
99 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530100 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530101
102 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530103 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
104
105 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530106
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530107 // Validate sub statement cardinality.
108 validateSubStatementsCardinality(ctx);
109
110 // Check for identifier collision
Vidyashree Ramad3221322016-03-04 19:08:35 +0530111 int line = ctx.getStart().getLine();
112 int charPositionInLine = ctx.getStart().getCharPositionInLine();
113 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530114
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530115 YangContainer container = getYangContainerNode(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +0530116 container.setName(identifier);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530117
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530118 /*
119 * If "config" is not specified, the default is the same as the parent
120 * schema node's "config" value.
121 */
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530122 if (ctx.configStatement().isEmpty()) {
123 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
124 container.setConfig(parentConfig);
125 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530126
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530127 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 if (curData instanceof YangModule || curData instanceof YangContainer
129 || curData instanceof YangList) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530130 YangNode curNode = (YangNode) curData;
131 try {
132 curNode.addChild(container);
133 } catch (DataModelException e) {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530134 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530135 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530136 }
137 listener.getParsedDataStack().push(container);
138 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530139 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530140 ctx.identifier().getText(), ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530141 }
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530142 }
143
144 /**
145 * It is called when parser exits from grammar rule (container), it perform
146 * validations and updates the data model tree.
147 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530148 * @param listener listener's object
149 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530150 */
151 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530152 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530153
154 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530155 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530156
157 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530158 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
159 try {
160 yangContainer.validateDataOnExit();
161 } catch (DataModelException e) {
162 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530163 CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530164 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530165 listener.getParsedDataStack().pop();
166 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530167 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530168 ctx.identifier().getText(), EXIT));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530169 }
170 }
171
172 /**
173 * Validates the cardinality of container sub-statements as per grammar.
174 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530175 * @param ctx context object of the grammar rule
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530176 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530177 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530178
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +0530179 validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
180 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
181 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
182 ctx.identifier().getText());
183 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
184 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530185 // TODO when, grouping, typedef.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530186 }
187}