blob: 226f62171109cb4301e76e885a4908135d712c7d [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053030import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
39import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
40import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
41import static org.onosproject.yangutils.utils.YangConstructType.LEAF_DATA;
42import static org.onosproject.yangutils.utils.YangConstructType.MANDATORY_DATA;
43import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
44import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053047
48/*
49 * Reference: RFC6020 and YANG ANTLR Grammar
50 *
51 * ABNF grammar as per RFC6020
52 * leaf-stmt = leaf-keyword sep identifier-arg-str optsep
53 * "{" stmtsep
54 * ;; these stmts can appear in any order
55 * [when-stmt stmtsep]
56 * *(if-feature-stmt stmtsep)
57 * type-stmt stmtsep
58 * [units-stmt stmtsep]
59 * *(must-stmt stmtsep)
60 * [default-stmt stmtsep]
61 * [config-stmt stmtsep]
62 * [mandatory-stmt stmtsep]
63 * [status-stmt stmtsep]
64 * [description-stmt stmtsep]
65 * [reference-stmt stmtsep]
66 * "}"
67 *
68 * ANTLR grammar rule
69 * leafStatement : LEAF_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | typeStatement |
70 * unitsStatement | mustStatement | defaultStatement | configStatement | mandatoryStatement | statusStatement |
71 * descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
72 */
73
74/**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053075 * Implements listener based call back function corresponding to the "leaf" rule
76 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053077 */
78public final class LeafListener {
79
80 /**
81 * Creates a new leaf listener.
82 */
83 private LeafListener() {
84 }
85
86 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053087 * It is called when parser receives an input matching the grammar rule
88 * (leaf), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053089 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053090 * @param listener listener's object
91 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053092 */
93 public static void processLeafEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053095
96 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053097 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053098
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053099 // Validate sub statement cardinality.
100 validateSubStatementsCardinality(ctx);
101
102 // Check for identifier collision
103 int line = ctx.IDENTIFIER().getSymbol().getLine();
104 int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
105 String identifierName = ctx.IDENTIFIER().getText();
106 detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, LEAF_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530107
108 YangLeaf leaf = new YangLeaf();
109 leaf.setLeafName(ctx.IDENTIFIER().getText());
110
111 Parsable tmpData = listener.getParsedDataStack().peek();
112 YangLeavesHolder leaves;
113
114 if (tmpData instanceof YangLeavesHolder) {
115 leaves = (YangLeavesHolder) tmpData;
116 leaves.addLeaf(leaf);
117 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530118 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_DATA,
119 ctx.IDENTIFIER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530120 }
121
122 listener.getParsedDataStack().push(leaf);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530123 }
124
125 /**
126 * It is called when parser exits from grammar rule (leaf), performs
127 * validation and updates the data model tree.
128 *
129 * @param listener listener's object.
130 * @param ctx context object of the grammar rule.
131 */
132 public static void processLeafExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530133 GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530134
135 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530136 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_DATA, ctx.IDENTIFIER().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530137
138 if (listener.getParsedDataStack().peek() instanceof YangLeaf) {
139 listener.getParsedDataStack().pop();
140 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530141 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_DATA,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530142 ctx.IDENTIFIER().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530143 }
144 }
145
146 /**
147 * Validates the cardinality of leaf sub-statements as per grammar.
148 *
149 * @param ctx context object of the grammar rule.
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530150 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530151 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530152
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530153 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
154 validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
155 validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
156 validateCardinality(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
157 validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
158 validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
159 validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.IDENTIFIER().getText());
160 //TODO when.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530161 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162}