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