blob: 4a2481a903ac500d3c6edb797c78abcb8de35eb4 [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
17package org.onosproject.yangutils.parser.impl.listeners;
18
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangLeafList;
20import org.onosproject.yangutils.datamodel.YangLeavesHolder;
21import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053025
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053026import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
27import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangLeafList;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
32 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053033import 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;
Bharat saraswald9822e92016-04-05 15:13:44 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053040import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
41import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
42import static org.onosproject.yangutils.utils.YangConstructType.LEAF_LIST_DATA;
43import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA;
44import static org.onosproject.yangutils.utils.YangConstructType.MIN_ELEMENT_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053049
50/*
51 * Reference: RFC6020 and YANG ANTLR Grammar
52 *
53 * ABNF grammar as per RFC6020
54 * leaf-list-stmt = leaf-list-keyword sep identifier-arg-str optsep
55 * "{" stmtsep
56 * ;; these stmts can appear in any order
57 * [when-stmt stmtsep]
58 * *(if-feature-stmt stmtsep)
59 * type-stmt stmtsep
60 * [units-stmt stmtsep]
61 * *(must-stmt stmtsep)
62 * [config-stmt stmtsep]
63 * [min-elements-stmt stmtsep]
64 * [max-elements-stmt stmtsep]
65 * [ordered-by-stmt stmtsep]
66 * [status-stmt stmtsep]
67 * [description-stmt stmtsep]
68 * [reference-stmt stmtsep]
69 * "}"
70 *
71 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053072 * leafListStatement : LEAF_LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053073 * typeStatement | unitsStatement | mustStatement | configStatement | minElementsStatement | maxElementsStatement |
74 * orderedByStatement | statusStatement | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
75 */
76
77/**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Represents listener based call back function corresponding to the "leaf-list"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053079 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
80 */
81public final class LeafListListener {
82
83 /**
84 * Creates a new leaf list listener.
85 */
86 private LeafListListener() {
87 }
88
89 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053090 * It is called when parser receives an input matching the grammar rule
91 * (leaf-list), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053092 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053093 * @param listener listener's object
94 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053095 */
96 public static void processLeafListEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053097 GeneratedYangParser.LeafListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053098
99 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530100 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), ENTRY);
101
102 String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_LIST_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530103
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530104 // Validate sub statement cardinality.
105 validateSubStatementsCardinality(ctx);
106
107 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530108 int line = ctx.getStart().getLine();
109 int charPositionInLine = ctx.getStart().getCharPositionInLine();
110 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_LIST_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530111
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530112 YangLeafList leafList = getYangLeafList(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530113 leafList.setLeafName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530114
115 Parsable tmpData = listener.getParsedDataStack().peek();
116 YangLeavesHolder leaves;
117
118 if (tmpData instanceof YangLeavesHolder) {
119 leaves = (YangLeavesHolder) tmpData;
120 leaves.addLeafList(leafList);
121 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530122 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_LIST_DATA,
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530124 }
125 listener.getParsedDataStack().push(leafList);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530126 }
127
128 /**
129 * It is called when parser exits from grammar rule (leaf-list), it performs
130 * validation and updates the data model tree.
131 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530132 * @param listener listener's object
133 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530134 */
135 public static void processLeafListExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530136 GeneratedYangParser.LeafListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530137
138 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530139 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530140
141 if (listener.getParsedDataStack().peek() instanceof YangLeafList) {
142 listener.getParsedDataStack().pop();
143 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530144 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_LIST_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530145 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530146 }
147 }
148
149 /**
150 * Validates the cardinality of leaf-list sub-statements as per grammar.
151 *
Vidyashree Rama468f8282016-03-04 19:08:35 +0530152 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530153 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530155
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530156 validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.identifier().getText(), ctx);
157 validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
158 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
159 validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA,
160 ctx.identifier().getText());
161 validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA,
162 ctx.identifier().getText());
163 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA,
164 ctx.identifier().getText());
165 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
166 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 //TODO ordered by
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530168 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169}