blob: edf0ed44a81abe38a73241b32ea408038b414be1 [file] [log] [blame]
Vidyashree Ramafe971cb2016-02-12 18:44:12 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Ramafe971cb2016-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
17/**
18 * Implements listener based call back function corresponding to the "leaf"
19 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
20 */
21package org.onosproject.yangutils.parser.impl.listeners;
22
Vidyashree Ramab24ca902016-02-13 21:47:58 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053024import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Mahesh Poojary S6986df32016-07-19 10:58:07 +053025import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053026import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053027import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053028import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053029import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama052ada62016-08-17 14:03:29 +053030import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Ramad3221322016-03-04 19:08:35 +053031
Vinod Kumar Sf677daf2016-04-15 18:08:57 +053032import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053033import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CONFIG_DATA;
34import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
35import static org.onosproject.yangutils.datamodel.utils.YangConstructType.LEAF_DATA;
36import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MANDATORY_DATA;
37import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
38import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
39import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
40import static org.onosproject.yangutils.datamodel.utils.YangConstructType.UNITS_DATA;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramae300f702016-02-20 19:27:56 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Gaurav Agrawal9564b552016-08-12 12:00:23 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Mahesh Poojary S6986df32016-07-19 10:58:07 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
Gaurav Agrawal9564b552016-08-12 12:00:23 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramae300f702016-02-20 19:27:56 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramae300f702016-02-20 19:27:56 +053050import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053051import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +053052import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053053import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangLeaf;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053054
55/*
56 * Reference: RFC6020 and YANG ANTLR Grammar
57 *
58 * ABNF grammar as per RFC6020
59 * leaf-stmt = leaf-keyword sep identifier-arg-str optsep
60 * "{" stmtsep
61 * ;; these stmts can appear in any order
62 * [when-stmt stmtsep]
63 * *(if-feature-stmt stmtsep)
64 * type-stmt stmtsep
65 * [units-stmt stmtsep]
66 * *(must-stmt stmtsep)
67 * [default-stmt stmtsep]
68 * [config-stmt stmtsep]
69 * [mandatory-stmt stmtsep]
70 * [status-stmt stmtsep]
71 * [description-stmt stmtsep]
72 * [reference-stmt stmtsep]
73 * "}"
74 *
75 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053076 * leafStatement : LEAF_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053077 * unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
78 * descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
79 */
80
81/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053082 * Represents listener based call back function corresponding to the "leaf" rule
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053083 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053084 */
85public final class LeafListener {
86
87 /**
88 * Creates a new leaf listener.
89 */
90 private LeafListener() {
91 }
92
93 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053094 * It is called when parser receives an input matching the grammar rule
95 * (leaf), performs validation and updates the data model tree.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053096 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053097 * @param listener listener's object
Gaurav Agrawal9564b552016-08-12 12:00:23 +053098 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053099 */
100 public static void processLeafEntry(TreeWalkListener listener,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530101 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530102
103 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530104 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), ENTRY);
105
106 String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_DATA, ctx);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530107
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530108 // Validate sub statement cardinality.
109 validateSubStatementsCardinality(ctx);
110
111 // Check for identifier collision
Vidyashree Ramad3221322016-03-04 19:08:35 +0530112 int line = ctx.getStart().getLine();
113 int charPositionInLine = ctx.getStart().getCharPositionInLine();
114 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_DATA);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530115
Vinod Kumar Sf677daf2016-04-15 18:08:57 +0530116 YangLeaf leaf = getYangLeaf(JAVA_GENERATION);
Gaurav Agrawale5057f02016-08-22 17:14:33 +0530117 leaf.setName(identifier);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530118
Vidyashree Rama052ada62016-08-17 14:03:29 +0530119 /*
120 * If "config" is not specified, the default is the same as the parent
121 * schema node's "config" value.
122 */
123 if (ctx.configStatement().isEmpty()) {
124 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
125 leaf.setConfig(parentConfig);
126 }
127
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530128 Parsable tmpData = listener.getParsedDataStack().peek();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530129 YangLeavesHolder leavesHolder;
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530130
131 if (tmpData instanceof YangLeavesHolder) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530132 leavesHolder = (YangLeavesHolder) tmpData;
133 leavesHolder.addLeaf(leaf);
134 leaf.setContainedIn(leavesHolder);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530135 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530136 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530137 ctx.identifier().getText(), ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530138 }
139
140 listener.getParsedDataStack().push(leaf);
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530141 }
142
143 /**
144 * It is called when parser exits from grammar rule (leaf), performs
145 * validation and updates the data model tree.
146 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530147 * @param listener listener's object
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530148 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530149 */
150 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530151 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530152
153 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530154 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530155
156 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530157 YangLeaf leafNode = (YangLeaf) listener.getParsedDataStack().peek();
158 try {
159 leafNode.validateDataOnExit();
160 } catch (DataModelException e) {
161 throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, LEAF_DATA,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530162 ctx.identifier().getText(), EXIT));
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530163 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530164 listener.getParsedDataStack().pop();
165 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530166 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Vinod Kumar Sf677daf2016-04-15 18:08:57 +0530167 ctx.identifier().getText(), EXIT));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530168 }
169 }
170
171 /**
172 * Validates the cardinality of leaf sub-statements as per grammar.
173 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530174 * @param ctx context object of the grammar rule
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530175 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530176 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530177
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +0530178 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
179 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
180 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
181 validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
182 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
183 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
184 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530185 //TODO when.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530186 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530187}