blob: ef4237ba1def6a25b271178ec936fafc6bef6fd0 [file] [log] [blame]
janani be18b5342016-07-13 21:06:41 +05301/*
2 * Copyright 2016-present 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.YangLeaf;
20import org.onosproject.yangutils.datamodel.YangLeafList;
21import org.onosproject.yangutils.datamodel.YangLeafRef;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
25import org.onosproject.yangutils.datamodel.utils.Parsable;
26import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
27import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28import org.onosproject.yangutils.parser.exceptions.ParserException;
29import org.onosproject.yangutils.parser.impl.TreeWalkListener;
30
31import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
32import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED;
33import static org.onosproject.yangutils.datamodel.utils.YangConstructType.LEAFREF_DATA;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
43
44/*
45 * Reference: RFC6020 and YANG ANTLR Grammar
46 *
47 * ABNF grammar as per RFC6020
48 * type-body-stmts = numerical-restrictions /
49 * decimal64-specification /
50 * string-restrictions /
51 * enum-specification /
52 * leafref-specification /
53 * identityref-specification /
54 * instance-identifier-specification /
55 * bits-specification /
56 * union-specification
57 *
58 * leafref-specification =
59 * ;; these stmts can appear in any order
60 * path-stmt stmtsep
61 * [require-instance-stmt stmtsep]
62 *
63 * ANTLR grammar rule
64 *
65 * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
66 * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
67 * | bitsSpecification | unionSpecification;
68 *
69 * leafrefSpecification : (pathStatement (requireInstanceStatement)?) | ((requireInstanceStatement)? pathStatement);
70 */
71
72/**
73 * Represents listener based call back function corresponding to the
74 * "leafref" rule defined in ANTLR grammar file for corresponding ABNF rule
75 * in RFC 6020.
76 */
77public final class LeafrefListener {
78
79 /**
80 * Creates a new leafref listener.
81 */
82 private LeafrefListener() {
83 }
84
85 /**
86 * It is called when parser receives an input matching the grammar rule
87 * (leafref), perform validations and updates the data model tree.
88 *
89 * @param listener listener's object
90 * @param ctx context object of the grammar rule
91 */
92 public static void processLeafrefEntry(TreeWalkListener listener,
93 GeneratedYangParser.LeafrefSpecificationContext ctx) {
94
95 // Check for stack to be non empty.
96 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAFREF_DATA, "", ENTRY);
97
98 int errorLine = ctx.getStart().getLine();
99 int errorPosition = ctx.getStart().getCharPositionInLine();
100
101 YangLeafRef<?> leafRef = new YangLeafRef<>();
102
janani b23ccc312016-07-14 19:35:22 +0530103 leafRef.setLineNumber(errorLine);
104 leafRef.setCharPosition(errorPosition);
janani be18b5342016-07-13 21:06:41 +0530105 Parsable typeData = listener.getParsedDataStack().pop();
106
107 if (!(typeData instanceof YangType)) {
108 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
109 "", ENTRY));
110 }
111
112 YangType type = (YangType) typeData;
113 type.setDataTypeExtendedInfo(leafRef);
114
115 // Setting by default the value of require-instance as true.
116 leafRef.setRequireInstance(true);
117 Parsable tmpData = listener.getParsedDataStack().peek();
118
119 switch (tmpData.getYangConstructType()) {
120
121 case LEAF_DATA:
122
123 // Parent YANG node of leaf to be added in resolution information.
124 YangLeaf leaf = (YangLeaf) listener.getParsedDataStack().pop();
125 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
126 listener.getParsedDataStack().push(leaf);
127
128 // Verify parent node of leaf.
129 if (!(parentNodeOfLeaf instanceof YangNode)) {
130 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
131 "", ENTRY));
132 }
133
134 leafRef.setResolvableStatus(UNRESOLVED);
janani b23ccc312016-07-14 19:35:22 +0530135 leafRef.setParentNodeOfLeafref((YangNode) parentNodeOfLeaf);
136 if (listener.getGroupingDepth() == 0) {
137 // Add resolution information to the list.
138 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangLeafRef>(leafRef,
139 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
140 addToResolutionList(resolutionInfo);
141 }
janani be18b5342016-07-13 21:06:41 +0530142 break;
143
144 case LEAF_LIST_DATA:
145
146 // Parent YANG node of leaf-list to be added in resolution information.
147 YangLeafList leafList = (YangLeafList) listener.getParsedDataStack().pop();
148 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
149 listener.getParsedDataStack().push(leafList);
150
151 // Verify parent node of leaf-list.
152 if (!(parentNodeOfLeafList instanceof YangNode)) {
153 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
154 "", ENTRY));
155 }
156
157 leafRef.setResolvableStatus(UNRESOLVED);
janani b23ccc312016-07-14 19:35:22 +0530158 leafRef.setParentNodeOfLeafref((YangNode) parentNodeOfLeafList);
janani be18b5342016-07-13 21:06:41 +0530159
janani b23ccc312016-07-14 19:35:22 +0530160 if (listener.getGroupingDepth() == 0) {
161 // Add resolution information to the list.
162 YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<YangLeafRef>(leafRef,
163 (YangNode) parentNodeOfLeafList, errorLine, errorPosition);
164 addToResolutionList(resolutionInfoImpl);
165 }
janani be18b5342016-07-13 21:06:41 +0530166 break;
167
168 case TYPEDEF_DATA:
janani b23ccc312016-07-14 19:35:22 +0530169 Parsable parentNodeOfLeafref = listener.getParsedDataStack().peek();
170 leafRef.setParentNodeOfLeafref((YangNode) parentNodeOfLeafref);
janani be18b5342016-07-13 21:06:41 +0530171 /*
172 * Do not add the leaf ref to resolution list. It needs to be
173 * added to resolution list, when leaf/leaf list references to
174 * this typedef. At this time that leaf/leaf-list becomes the
175 * parent for the leafref.
176 */
177 break;
178
179 default:
180 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
181 "", ENTRY));
182 }
183 listener.getParsedDataStack().push(typeData);
184 listener.getParsedDataStack().push(leafRef);
185 }
186
187 /**
188 * It is called when parser exits from grammar rule (leafref), it performs
189 * validation and updates the data model tree.
190 *
191 * @param listener listener's object
192 * @param ctx context object of the grammar rule
193 */
194 public static void processLeafrefExit(TreeWalkListener listener,
195 GeneratedYangParser.LeafrefSpecificationContext ctx) {
196
197 // Check for stack to be non empty.
198 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, LEAFREF_DATA, "", EXIT);
199
200 Parsable parsableType = listener.getParsedDataStack().pop();
201 if (!(parsableType instanceof YangLeafRef)) {
202 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
203 "", EXIT));
204 }
205 }
206
207 /**
208 * Adds to resolution list.
209 *
210 * @param resolutionInfo resolution information
211 */
212 private static void addToResolutionList(YangResolutionInfoImpl resolutionInfo) {
213
214 try {
215 addResolutionInfo(resolutionInfo);
216 } catch (DataModelException e) {
217 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
218 LEAFREF_DATA, "", ENTRY, e.getMessage()));
219 }
220 }
221}