blob: 0a648af207ddfa3fc0b4261dbfb0d34e96e6a30d [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;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053028import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053029
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053030import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
31import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangLeaf;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-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;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
36 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Bharat saraswald9822e92016-04-05 15:13:44 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053044import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.LEAF_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.MANDATORY_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
49import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
50import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
51import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053052
53/*
54 * Reference: RFC6020 and YANG ANTLR Grammar
55 *
56 * ABNF grammar as per RFC6020
57 * leaf-stmt = leaf-keyword sep identifier-arg-str optsep
58 * "{" stmtsep
59 * ;; these stmts can appear in any order
60 * [when-stmt stmtsep]
61 * *(if-feature-stmt stmtsep)
62 * type-stmt stmtsep
63 * [units-stmt stmtsep]
64 * *(must-stmt stmtsep)
65 * [default-stmt stmtsep]
66 * [config-stmt stmtsep]
67 * [mandatory-stmt stmtsep]
68 * [status-stmt stmtsep]
69 * [description-stmt stmtsep]
70 * [reference-stmt stmtsep]
71 * "}"
72 *
73 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053074 * leafStatement : LEAF_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053075 * unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
76 * descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
77 */
78
79/**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Represents listener based call back function corresponding to the "leaf" rule
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053081 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053082 */
83public final class LeafListener {
84
85 /**
86 * Creates a new leaf listener.
87 */
88 private LeafListener() {
89 }
90
91 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 * It is called when parser receives an input matching the grammar rule
93 * (leaf), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053094 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 * @param listener listener's object
96 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053097 */
98 public static void processLeafEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053099 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530100
101 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530102 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), ENTRY);
103
104 String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530105
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 // Validate sub statement cardinality.
107 validateSubStatementsCardinality(ctx);
108
109 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530110 int line = ctx.getStart().getLine();
111 int charPositionInLine = ctx.getStart().getCharPositionInLine();
112 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530113
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530114 YangLeaf leaf = getYangLeaf(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530115 leaf.setLeafName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530116
117 Parsable tmpData = listener.getParsedDataStack().peek();
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530118 YangLeavesHolder leavesHolder;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530119
120 if (tmpData instanceof YangLeavesHolder) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 leavesHolder = (YangLeavesHolder) tmpData;
122 leavesHolder.addLeaf(leaf);
123 leaf.setContainedIn(leavesHolder);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530124 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530125 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530126 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530127 }
128
129 listener.getParsedDataStack().push(leaf);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530130 }
131
132 /**
133 * It is called when parser exits from grammar rule (leaf), performs
134 * validation and updates the data model tree.
135 *
Vidyashree Rama468f8282016-03-04 19:08:35 +0530136 * @param listener listener's object
137 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530138 */
139 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530140 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530141
142 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530143 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530144
145 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
146 listener.getParsedDataStack().pop();
147 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530148 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530149 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530150 }
151 }
152
153 /**
154 * Validates the cardinality of leaf sub-statements as per grammar.
155 *
Vidyashree Rama468f8282016-03-04 19:08:35 +0530156 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530157 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530158 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530159
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530160 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
161 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
162 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
163 validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
164 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
165 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
166 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 //TODO when.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530168 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169}