blob: d6f06ff4d4e14ed72528ef93835b452907c03740 [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);
Bharat saraswale3175d32016-08-31 17:50:11 +0530118 leaf.setLineNumber(line);
119 leaf.setCharPosition(charPositionInLine);
120 leaf.setFileName(listener.getFileName());
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530121
Vidyashree Rama052ada62016-08-17 14:03:29 +0530122 /*
123 * If "config" is not specified, the default is the same as the parent
124 * schema node's "config" value.
125 */
126 if (ctx.configStatement().isEmpty()) {
127 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
128 leaf.setConfig(parentConfig);
129 }
130
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530131 Parsable tmpData = listener.getParsedDataStack().peek();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530132 YangLeavesHolder leavesHolder;
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530133
134 if (tmpData instanceof YangLeavesHolder) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530135 leavesHolder = (YangLeavesHolder) tmpData;
136 leavesHolder.addLeaf(leaf);
137 leaf.setContainedIn(leavesHolder);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530138 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530139 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530140 ctx.identifier().getText(), ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530141 }
142
143 listener.getParsedDataStack().push(leaf);
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530144 }
145
146 /**
147 * It is called when parser exits from grammar rule (leaf), performs
148 * validation and updates the data model tree.
149 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530150 * @param listener listener's object
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530151 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530152 */
153 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530154 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530155
156 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530157 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530158
159 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530160 YangLeaf leafNode = (YangLeaf) listener.getParsedDataStack().peek();
161 try {
162 leafNode.validateDataOnExit();
163 } catch (DataModelException e) {
164 throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, LEAF_DATA,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530165 ctx.identifier().getText(), EXIT));
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530166 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530167 listener.getParsedDataStack().pop();
168 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530169 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Vinod Kumar Sf677daf2016-04-15 18:08:57 +0530170 ctx.identifier().getText(), EXIT));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530171 }
172 }
173
174 /**
175 * Validates the cardinality of leaf sub-statements as per grammar.
176 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530177 * @param ctx context object of the grammar rule
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530178 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530179 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530180
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +0530181 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
182 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
183 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
184 validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
185 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
186 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
187 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530188 //TODO when.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530189 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530190}