blob: 67dacbbe3de52cc54c04cc09d5cd22dd2ecd4bc9 [file] [log] [blame]
Gaurav Agrawalbd804472016-03-25 11:25:36 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawalbd804472016-03-25 11:25:36 +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
19import org.onosproject.yangutils.datamodel.YangContainer;
20import org.onosproject.yangutils.datamodel.YangGrouping;
21import org.onosproject.yangutils.datamodel.YangInput;
22import org.onosproject.yangutils.datamodel.YangList;
23import org.onosproject.yangutils.datamodel.YangModule;
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangNotification;
26import org.onosproject.yangutils.datamodel.YangOutput;
27import org.onosproject.yangutils.datamodel.YangRpc;
28import org.onosproject.yangutils.datamodel.YangSubModule;
29import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
30import org.onosproject.yangutils.parser.Parsable;
31import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
33import org.onosproject.yangutils.parser.impl.TreeWalkListener;
34
35import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
36import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangGroupingNode;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
44import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
47import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
48import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053049import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
50import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA;
51import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
52import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053053
Gaurav Agrawalbd804472016-03-25 11:25:36 +053054/*
55 * Reference: RFC6020 and YANG ANTLR Grammar
56 *
57 * ABNF grammar as per RFC6020
58 * grouping-stmt = grouping-keyword sep identifier-arg-str optsep
59 * (";" /
60 * "{" stmtsep
61 * ;; these stmts can appear in any order
62 * [status-stmt stmtsep]
63 * [description-stmt stmtsep]
64 * [reference-stmt stmtsep]
65 * *((typedef-stmt /
66 * grouping-stmt) stmtsep)
67 * *(data-def-stmt stmtsep)
68 * "}")
69 *
70 * ANTLR grammar rule
71 * groupingStatement : GROUPING_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE
72 * (statusStatement | descriptionStatement | referenceStatement | typedefStatement | groupingStatement
73 * | dataDefStatement)* RIGHT_CURLY_BRACE);
74 */
75
76/**
Bharat saraswald9822e92016-04-05 15:13:44 +053077 * Represents listener based call back function corresponding to the "grouping"
Gaurav Agrawalbd804472016-03-25 11:25:36 +053078 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
79 */
80public final class GroupingListener {
81
82 /**
83 * Creates a new grouping listener.
84 */
85 private GroupingListener() {
86 }
87
88 /**
89 * It is called when parser enters grammar rule (grouping), it perform
90 * validations and updates the data model tree.
91 *
92 * @param listener listener's object
93 * @param ctx context object of the grammar rule
94 */
95 public static void processGroupingEntry(TreeWalkListener listener,
96 GeneratedYangParser.GroupingStatementContext ctx) {
97
98 // Check for stack to be non empty.
99 checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), ENTRY);
100
101 // Check validity of identifier and remove double quotes.
102 String identifier = getValidIdentifier(ctx.identifier().getText(), GROUPING_DATA, ctx);
103
104 // Validate sub statement cardinality.
105 validateSubStatementsCardinality(ctx);
106
107 Parsable curData = listener.getParsedDataStack().peek();
108
109 // Check for identifier collision
110 int line = ctx.getStart().getLine();
111 int charPositionInLine = ctx.getStart().getCharPositionInLine();
112 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, GROUPING_DATA);
113
114 if (curData instanceof YangModule || curData instanceof YangSubModule
115 || curData instanceof YangContainer || curData instanceof YangNotification
116 || curData instanceof YangList || curData instanceof YangGrouping
117 || curData instanceof YangRpc || curData instanceof YangInput
118 || curData instanceof YangOutput) {
119
120 YangGrouping groupingNode = getYangGroupingNode(JAVA_GENERATION);
121 groupingNode.setName(identifier);
122
123 YangNode curNode = (YangNode) curData;
124 try {
125 curNode.addChild(groupingNode);
126 } catch (DataModelException e) {
127 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
128 GROUPING_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
129 }
130 listener.getParsedDataStack().push(groupingNode);
131 } else {
132 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER,
133 GROUPING_DATA, ctx.identifier().getText(), ENTRY));
134 }
135 }
136
137 /**
138 * It is called when parser exits from grammar rule (grouping), it perform
139 * validations and update the data model tree.
140 *
141 * @param listener Listener's object
142 * @param ctx context object of the grammar rule
143 */
144 public static void processGroupingExit(TreeWalkListener listener,
145 GeneratedYangParser.GroupingStatementContext ctx) {
146
147 // Check for stack to be non empty.
148 checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), EXIT);
149
150 if (listener.getParsedDataStack().peek() instanceof YangGrouping) {
151 listener.getParsedDataStack().pop();
152 } else {
153 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, GROUPING_DATA,
154 ctx.identifier().getText(), EXIT));
155 }
156 }
157
158 /**
159 * Validates the cardinality of case sub-statements as per grammar.
160 *
161 * @param ctx context object of the grammar rule
162 */
163 private static void validateSubStatementsCardinality(GeneratedYangParser.GroupingStatementContext ctx) {
164
165 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, GROUPING_DATA, ctx.identifier().getText());
166 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, GROUPING_DATA,
167 ctx.identifier().getText());
168 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, GROUPING_DATA, ctx.identifier().getText());
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530169 }
170}