blob: 4eae3a20fb44997b4c062da06f9aafbe443fec84 [file] [log] [blame]
Vinod Kumar S2ff139c2016-02-16 01:37:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S2ff139c2016-02-16 01:37:16 +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 */
16package org.onosproject.yangutils.datamodel;
17
18import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053019import org.onosproject.yangutils.linker.exceptions.LinkerException;
20import org.onosproject.yangutils.linker.impl.Resolvable;
21import org.onosproject.yangutils.linker.impl.ResolvableStatus;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053022import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053023import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053024
janani b4e53f9b2016-04-26 18:49:20 +053025import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
Vinod Kumar S427d2932016-04-20 13:02:58 +053026import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
27
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053028/*-
29 * Reference RFC 6020.
30 *
31 * The "uses" statement is used to reference a "grouping" definition. It takes
32 * one argument, which is the name of the grouping.
33 *
34 * The effect of a "uses" reference to a grouping is that the nodes defined by
35 * the grouping are copied into the current schema tree, and then updated
36 * according to the "refine" and "augment" statements.
37 *
38 * The identifiers defined in the grouping are not bound to a namespace until
39 * the contents of the grouping are added to the schema tree via a "uses"
40 * statement that does not appear inside a "grouping" statement, at which point
41 * they are bound to the namespace of the current module.
42 *
43 * The uses's sub-statements
44 *
45 * +--------------+---------+-------------+------------------+
46 * | substatement | section | cardinality |data model mapping|
47 * +--------------+---------+-------------+------------------+
48 * | augment | 7.15 | 0..1 | -child nodes |
49 * | description | 7.19.3 | 0..1 | -string |
50 * | if-feature | 7.18.2 | 0..n | -TODO |
51 * | refine | 7.12.2 | 0..1 | -TODO |
52 * | reference | 7.19.4 | 0..1 | -string |
53 * | status | 7.19.2 | 0..1 | -YangStatus |
54 * | when | 7.19.5 | 0..1 | -TODO |
55 * +--------------+---------+-------------+------------------+
56 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053057
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053058/**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Represents data model node to maintain information defined in YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053060 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053061public class YangUses
62 extends YangNode
janani b4e53f9b2016-04-26 18:49:20 +053063 implements YangCommonInfo, Parsable, Resolvable, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064
65 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053066 * YANG node identifier.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053067 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053068 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053069
70 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053071 * Referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053072 */
73 private YangGrouping refGroup;
74
75 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053076 * Description of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053077 */
78 private String description;
79
80 /**
81 * YANG reference.
82 */
83 private String reference;
84
85 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053086 * Status of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053087 */
88 private YangStatusType status;
89
90 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053091 * Status of resolution. If completely resolved enum value is "RESOLVED",
92 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
93 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053094 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053095 */
96 private ResolvableStatus resolvableStatus;
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Creates an YANG uses node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530100 */
101 public YangUses() {
102 super(YangNodeType.USES_NODE);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530103 nodeIdentifier = new YangNodeIdentifier();
104 resolvableStatus = ResolvableStatus.UNRESOLVED;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530105 }
106
107 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530108 * Returns the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530109 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530110 * @return the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530111 */
112 public YangGrouping getRefGroup() {
113 return refGroup;
114 }
115
116 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530117 * Sets the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530118 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530119 * @param refGroup the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530120 */
121 public void setRefGroup(YangGrouping refGroup) {
122 this.refGroup = refGroup;
123 }
124
125 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530126 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530127 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530128 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530129 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530130 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530131 public String getDescription() {
132 return description;
133 }
134
135 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530136 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530137 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530138 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530139 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530140 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530141 public void setDescription(String description) {
142 this.description = description;
143 }
144
145 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530146 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530147 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530148 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530149 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530150 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530151 public String getReference() {
152 return reference;
153 }
154
155 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530156 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530157 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530158 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530159 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530160 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530161 public void setReference(String reference) {
162 this.reference = reference;
163 }
164
165 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530166 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530168 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530169 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530170 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530171 public YangStatusType getStatus() {
172 return status;
173 }
174
175 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530176 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530177 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530178 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530179 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530180 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530181 public void setStatus(YangStatusType status) {
182 this.status = status;
183 }
184
185 /**
186 * Returns the type of the data.
187 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530188 * @return returns USES_DATA
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530189 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530190 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 public YangConstructType getYangConstructType() {
192 return YangConstructType.USES_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530193 }
194
195 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530197 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530198 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530199 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530200 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530201 public void validateDataOnEntry()
202 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530203 // TODO auto-generated method stub, to be implemented by parser
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530208 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530209 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530211 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530212 public void validateDataOnExit()
213 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530214 // TODO auto-generated method stub, to be implemented by parser
215 }
216
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530217 @Override
218 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530219 return nodeIdentifier.getName();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530220 }
221
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530222 @Override
223 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530224 nodeIdentifier.setName(name);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530225 }
226
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530227 /**
228 * Returns node identifier.
229 *
230 * @return node identifier
231 */
232 public YangNodeIdentifier getNodeIdentifier() {
233 return nodeIdentifier;
234 }
235
236 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530237 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530238 *
239 * @param nodeIdentifier the node identifier
240 */
241 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
242 this.nodeIdentifier = nodeIdentifier;
243 }
244
245 /**
246 * Returns prefix associated with uses.
247 *
248 * @return prefix associated with uses
249 */
250 public String getPrefix() {
251 return nodeIdentifier.getPrefix();
252 }
253
254 /**
255 * Get prefix associated with uses.
256 *
257 * @param prefix prefix associated with uses
258 */
259 public void setPrefix(String prefix) {
260 nodeIdentifier.setPrefix(prefix);
261 }
262
263 @Override
Vinod Kumar S427d2932016-04-20 13:02:58 +0530264 public void resolve()
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 throws LinkerException {
Vinod Kumar S427d2932016-04-20 13:02:58 +0530266
267 YangGrouping referredGrouping = getRefGroup();
268
269 if (referredGrouping == null) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530270 throw new LinkerException("Linker Exception: YANG uses linker error, cannot resolve uses");
Vinod Kumar S427d2932016-04-20 13:02:58 +0530271 }
272
273 YangNode usesParentNode = getParentNodeInGenCode(this);
Bharat saraswalcad0e652016-05-26 23:48:38 +0530274 if (!(usesParentNode instanceof YangLeavesHolder)
275 || !(usesParentNode instanceof CollisionDetector)) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276 throw new LinkerException("Linker Exception: YANG uses holder construct is wrong");
Vinod Kumar S427d2932016-04-20 13:02:58 +0530277 }
278
279 YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
280 if (referredGrouping.getListOfLeaf() != null) {
281 for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530282 try {
283 ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getName(),
284 YangConstructType.LEAF_DATA);
285 } catch (DataModelException e) {
286 throw new LinkerException(e.getMessage());
287 }
Vinod Kumar S427d2932016-04-20 13:02:58 +0530288 usesParentLeavesHolder.addLeaf(leaf);
289 }
290 }
291 if (referredGrouping.getListOfLeafList() != null) {
292 for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530293 try {
294 ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
295 YangConstructType.LEAF_LIST_DATA);
296 } catch (DataModelException e) {
297 throw new LinkerException(e.getMessage());
298 }
Vinod Kumar S427d2932016-04-20 13:02:58 +0530299 usesParentLeavesHolder.addLeafList(leafList);
300 }
301 }
302
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530303 try {
304 YangNode.cloneSubTree(getRefGroup(), usesParentNode);
305 } catch (DataModelException e) {
306 throw new LinkerException(e.getMessage());
307 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530308 }
309
310 @Override
311 public ResolvableStatus getResolvableStatus() {
312 return resolvableStatus;
313 }
314
315 @Override
316 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
317 this.resolvableStatus = resolvableStatus;
318 }
janani b4e53f9b2016-04-26 18:49:20 +0530319
320 @Override
321 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
322 detectCollidingChildUtil(identifierName, dataType, this);
323 }
324
325 @Override
326 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
327
328 if (getName().equals(identifierName)) {
329 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as uses \""
330 + getName() + "\"");
331 }
332 }
333
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530334}