blob: ad59e128d2a837dd19ef92ffa6d82a4fac3a9699 [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
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 Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053024import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053025import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053026import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053027import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053028import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053029import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053030
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053031import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswal96dfef02016-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 Agrawal8e8770a2016-02-27 03:57:50 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-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;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
44 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswald9822e92016-04-05 15:13:44 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053050import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053051import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053052import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswal96dfef02016-06-16 00:29:12 +053053import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangLeaf;
Vidyashree Rama92fc5562016-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 Rama468f8282016-03-04 19:08:35 +053076 * leafStatement : LEAF_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053077 * unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
78 * descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
79 */
80
81/**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Represents listener based call back function corresponding to the "leaf" rule
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053083 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-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 Agrawal8e8770a2016-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 Rama92fc5562016-02-12 18:44:12 +053096 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053097 * @param listener listener's object
98 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053099 */
100 public static void processLeafEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530102
103 // Check for stack to be non empty.
Vidyashree Rama468f8282016-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 Rama4f1f08b2016-02-13 21:47:58 +0530107
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530108 // Validate sub statement cardinality.
109 validateSubStatementsCardinality(ctx);
110
111 // Check for identifier collision
Vidyashree Rama468f8282016-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 Rama4f1f08b2016-02-13 21:47:58 +0530115
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530116 YangLeaf leaf = getYangLeaf(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530117 leaf.setLeafName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530118
119 Parsable tmpData = listener.getParsedDataStack().peek();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530120 YangLeavesHolder leavesHolder;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530121
122 if (tmpData instanceof YangLeavesHolder) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530123 leavesHolder = (YangLeavesHolder) tmpData;
124 leavesHolder.addLeaf(leaf);
125 leaf.setContainedIn(leavesHolder);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530126 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530127 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530128 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530129 }
130
131 listener.getParsedDataStack().push(leaf);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530132 }
133
134 /**
135 * It is called when parser exits from grammar rule (leaf), performs
136 * validation and updates the data model tree.
137 *
Vidyashree Rama468f8282016-03-04 19:08:35 +0530138 * @param listener listener's object
139 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530140 */
141 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530142 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530143
144 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530145 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530146
147 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
Mahesh Poojary Sbbd48492016-07-19 10:58:07 +0530148 YangLeaf leafNode = (YangLeaf) listener.getParsedDataStack().peek();
149 try {
150 leafNode.validateDataOnExit();
151 } catch (DataModelException e) {
152 throw new ParserException(constructListenerErrorMessage(INVALID_CONTENT, LEAF_DATA,
153 ctx.identifier().getText(), EXIT));
154 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530155 listener.getParsedDataStack().pop();
156 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530157 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530158 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530159 }
160 }
161
162 /**
163 * Validates the cardinality of leaf sub-statements as per grammar.
164 *
Vidyashree Rama468f8282016-03-04 19:08:35 +0530165 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530166 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530168
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530169 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
170 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
171 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
172 validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
173 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
174 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
175 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530176 //TODO when.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530177 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530178}