blob: ed100a06d5f14dda70233012e56c8caea72d2197 [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 Ramad3221322016-03-04 19:08:35 +053030
Vinod Kumar Sf677daf2016-04-15 18:08:57 +053031import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053032import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CONFIG_DATA;
33import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
34import static org.onosproject.yangutils.datamodel.utils.YangConstructType.LEAF_DATA;
35import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MANDATORY_DATA;
36import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
37import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
38import static org.onosproject.yangutils.datamodel.utils.YangConstructType.TYPE_DATA;
39import static org.onosproject.yangutils.datamodel.utils.YangConstructType.UNITS_DATA;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramae300f702016-02-20 19:27:56 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Gaurav Agrawal9564b552016-08-12 12:00:23 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Mahesh Poojary S6986df32016-07-19 10:58:07 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
Gaurav Agrawal9564b552016-08-12 12:00:23 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramae300f702016-02-20 19:27:56 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramae300f702016-02-20 19:27:56 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053050import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +053051import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswalc2d3be12016-06-16 00:29:12 +053052import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangLeaf;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053053
54/*
55 * Reference: RFC6020 and YANG ANTLR Grammar
56 *
57 * ABNF grammar as per RFC6020
58 * leaf-stmt = leaf-keyword sep identifier-arg-str optsep
59 * "{" stmtsep
60 * ;; these stmts can appear in any order
61 * [when-stmt stmtsep]
62 * *(if-feature-stmt stmtsep)
63 * type-stmt stmtsep
64 * [units-stmt stmtsep]
65 * *(must-stmt stmtsep)
66 * [default-stmt stmtsep]
67 * [config-stmt stmtsep]
68 * [mandatory-stmt stmtsep]
69 * [status-stmt stmtsep]
70 * [description-stmt stmtsep]
71 * [reference-stmt stmtsep]
72 * "}"
73 *
74 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053075 * leafStatement : LEAF_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053076 * unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
77 * descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
78 */
79
80/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053081 * Represents listener based call back function corresponding to the "leaf" rule
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053082 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053083 */
84public final class LeafListener {
85
86 /**
87 * Creates a new leaf listener.
88 */
89 private LeafListener() {
90 }
91
92 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053093 * It is called when parser receives an input matching the grammar rule
94 * (leaf), 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
Gaurav Agrawal9564b552016-08-12 12:00:23 +053097 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053098 */
99 public static void processLeafEntry(TreeWalkListener listener,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530100 GeneratedYangParser.LeafStatementContext 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, LEAF_DATA, ctx.identifier().getText(), ENTRY);
104
105 String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_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, LEAF_DATA);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530114
Vinod Kumar Sf677daf2016-04-15 18:08:57 +0530115 YangLeaf leaf = getYangLeaf(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +0530116 leaf.setLeafName(identifier);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530117
118 Parsable tmpData = listener.getParsedDataStack().peek();
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530119 YangLeavesHolder leavesHolder;
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530120
121 if (tmpData instanceof YangLeavesHolder) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530122 leavesHolder = (YangLeavesHolder) tmpData;
123 leavesHolder.addLeaf(leaf);
124 leaf.setContainedIn(leavesHolder);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530125 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530126 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530127 ctx.identifier().getText(), ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530128 }
129
130 listener.getParsedDataStack().push(leaf);
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530131 }
132
133 /**
134 * It is called when parser exits from grammar rule (leaf), performs
135 * validation and updates the data model tree.
136 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530137 * @param listener listener's object
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530138 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530139 */
140 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530141 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530142
143 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530144 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530145
146 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530147 YangLeaf leafNode = (YangLeaf) listener.getParsedDataStack().peek();
148 try {
149 leafNode.validateDataOnExit();
150 } catch (DataModelException e) {
151 throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, LEAF_DATA,
Gaurav Agrawal9564b552016-08-12 12:00:23 +0530152 ctx.identifier().getText(), EXIT));
Mahesh Poojary S6986df32016-07-19 10:58:07 +0530153 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530154 listener.getParsedDataStack().pop();
155 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530156 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Vinod Kumar Sf677daf2016-04-15 18:08:57 +0530157 ctx.identifier().getText(), EXIT));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530158 }
159 }
160
161 /**
162 * Validates the cardinality of leaf sub-statements as per grammar.
163 *
Vidyashree Ramad3221322016-03-04 19:08:35 +0530164 * @param ctx context object of the grammar rule
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530165 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530166 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530167
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +0530168 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
169 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
170 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
171 validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
172 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
173 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
174 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530175 //TODO when.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530176 }
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530177}