blob: 6ada8c5c9b7acc88620759076240e38b81e8ada8 [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
103 Parsable typeData = listener.getParsedDataStack().pop();
104
105 if (!(typeData instanceof YangType)) {
106 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
107 "", ENTRY));
108 }
109
110 YangType type = (YangType) typeData;
111 type.setDataTypeExtendedInfo(leafRef);
112
113 // Setting by default the value of require-instance as true.
114 leafRef.setRequireInstance(true);
115 Parsable tmpData = listener.getParsedDataStack().peek();
116
117 switch (tmpData.getYangConstructType()) {
118
119 case LEAF_DATA:
120
121 // Parent YANG node of leaf to be added in resolution information.
122 YangLeaf leaf = (YangLeaf) listener.getParsedDataStack().pop();
123 Parsable parentNodeOfLeaf = listener.getParsedDataStack().peek();
124 listener.getParsedDataStack().push(leaf);
125
126 // Verify parent node of leaf.
127 if (!(parentNodeOfLeaf instanceof YangNode)) {
128 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
129 "", ENTRY));
130 }
131
132 leafRef.setResolvableStatus(UNRESOLVED);
133
134 // Add resolution information to the list.
135 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangLeafRef>(leafRef,
136 (YangNode) parentNodeOfLeaf, errorLine, errorPosition);
137 addToResolutionList(resolutionInfo);
138 break;
139
140 case LEAF_LIST_DATA:
141
142 // Parent YANG node of leaf-list to be added in resolution information.
143 YangLeafList leafList = (YangLeafList) listener.getParsedDataStack().pop();
144 Parsable parentNodeOfLeafList = listener.getParsedDataStack().peek();
145 listener.getParsedDataStack().push(leafList);
146
147 // Verify parent node of leaf-list.
148 if (!(parentNodeOfLeafList instanceof YangNode)) {
149 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
150 "", ENTRY));
151 }
152
153 leafRef.setResolvableStatus(UNRESOLVED);
154
155 // Add resolution information to the list.
156 YangResolutionInfoImpl resolutionInfoImpl = new YangResolutionInfoImpl<YangLeafRef>(leafRef,
157 (YangNode) parentNodeOfLeafList, errorLine, errorPosition);
158 addToResolutionList(resolutionInfoImpl);
159 break;
160
161 case TYPEDEF_DATA:
162 /*
163 * Do not add the leaf ref to resolution list. It needs to be
164 * added to resolution list, when leaf/leaf list references to
165 * this typedef. At this time that leaf/leaf-list becomes the
166 * parent for the leafref.
167 */
168 break;
169
170 default:
171 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
172 "", ENTRY));
173 }
174 listener.getParsedDataStack().push(typeData);
175 listener.getParsedDataStack().push(leafRef);
176 }
177
178 /**
179 * It is called when parser exits from grammar rule (leafref), it performs
180 * validation and updates the data model tree.
181 *
182 * @param listener listener's object
183 * @param ctx context object of the grammar rule
184 */
185 public static void processLeafrefExit(TreeWalkListener listener,
186 GeneratedYangParser.LeafrefSpecificationContext ctx) {
187
188 // Check for stack to be non empty.
189 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, LEAFREF_DATA, "", EXIT);
190
191 Parsable parsableType = listener.getParsedDataStack().pop();
192 if (!(parsableType instanceof YangLeafRef)) {
193 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAFREF_DATA,
194 "", EXIT));
195 }
196 }
197
198 /**
199 * Adds to resolution list.
200 *
201 * @param resolutionInfo resolution information
202 */
203 private static void addToResolutionList(YangResolutionInfoImpl resolutionInfo) {
204
205 try {
206 addResolutionInfo(resolutionInfo);
207 } catch (DataModelException e) {
208 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
209 LEAFREF_DATA, "", ENTRY, e.getMessage()));
210 }
211 }
212}