blob: 16f45c4d821524858e6d311e9cff97ef3f813e26 [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;
19import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053020import org.onosproject.yangutils.utils.YangConstructType;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053021
janani b4e53f9b2016-04-26 18:49:20 +053022import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
Vinod Kumar S427d2932016-04-20 13:02:58 +053023import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
24
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053025/*-
26 * Reference RFC 6020.
27 *
28 * The "uses" statement is used to reference a "grouping" definition. It takes
29 * one argument, which is the name of the grouping.
30 *
31 * The effect of a "uses" reference to a grouping is that the nodes defined by
32 * the grouping are copied into the current schema tree, and then updated
33 * according to the "refine" and "augment" statements.
34 *
35 * The identifiers defined in the grouping are not bound to a namespace until
36 * the contents of the grouping are added to the schema tree via a "uses"
37 * statement that does not appear inside a "grouping" statement, at which point
38 * they are bound to the namespace of the current module.
39 *
40 * The uses's sub-statements
41 *
42 * +--------------+---------+-------------+------------------+
43 * | substatement | section | cardinality |data model mapping|
44 * +--------------+---------+-------------+------------------+
45 * | augment | 7.15 | 0..1 | -child nodes |
46 * | description | 7.19.3 | 0..1 | -string |
47 * | if-feature | 7.18.2 | 0..n | -TODO |
48 * | refine | 7.12.2 | 0..1 | -TODO |
49 * | reference | 7.19.4 | 0..1 | -string |
50 * | status | 7.19.2 | 0..1 | -YangStatus |
51 * | when | 7.19.5 | 0..1 | -TODO |
52 * +--------------+---------+-------------+------------------+
53 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053054
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053055/**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Represents data model node to maintain information defined in YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053057 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053058public class YangUses
59 extends YangNode
janani b4e53f9b2016-04-26 18:49:20 +053060 implements YangCommonInfo, Parsable, Resolvable, CollisionDetector {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053061
62 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053063 * YANG node identifier.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053064 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053065 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053066
67 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053068 * Referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053069 */
70 private YangGrouping refGroup;
71
72 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053073 * Description of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053074 */
75 private String description;
76
77 /**
78 * YANG reference.
79 */
80 private String reference;
81
82 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053083 * Status of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053084 */
85 private YangStatusType status;
86
87 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053088 * Status of resolution. If completely resolved enum value is "RESOLVED",
89 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
90 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053091 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053092 */
93 private ResolvableStatus resolvableStatus;
94
95 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * Creates an YANG uses node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053097 */
98 public YangUses() {
99 super(YangNodeType.USES_NODE);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530100 nodeIdentifier = new YangNodeIdentifier();
101 resolvableStatus = ResolvableStatus.UNRESOLVED;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530102 }
103
104 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530105 * Returns the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530106 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530107 * @return the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530108 */
109 public YangGrouping getRefGroup() {
110 return refGroup;
111 }
112
113 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * Sets the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530115 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530116 * @param refGroup the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530117 */
118 public void setRefGroup(YangGrouping refGroup) {
119 this.refGroup = refGroup;
120 }
121
122 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530123 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530124 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530125 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530126 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530127 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530128 public String getDescription() {
129 return description;
130 }
131
132 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530133 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530134 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530135 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530136 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530137 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530138 public void setDescription(String description) {
139 this.description = description;
140 }
141
142 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530143 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530144 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530145 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530146 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530147 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530148 public String getReference() {
149 return reference;
150 }
151
152 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530153 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530154 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530155 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530156 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530157 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530158 public void setReference(String reference) {
159 this.reference = reference;
160 }
161
162 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530163 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530164 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530165 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530166 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530167 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530168 public YangStatusType getStatus() {
169 return status;
170 }
171
172 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530173 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530174 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530175 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530176 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530177 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530178 public void setStatus(YangStatusType status) {
179 this.status = status;
180 }
181
182 /**
183 * Returns the type of the data.
184 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530185 * @return returns USES_DATA
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530186 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530187 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530188 public YangConstructType getYangConstructType() {
189 return YangConstructType.USES_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530190 }
191
192 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530193 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530194 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530195 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530196 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530197 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530198 public void validateDataOnEntry()
199 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530200 // TODO auto-generated method stub, to be implemented by parser
201 }
202
203 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530204 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530205 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530206 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530207 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530208 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530209 public void validateDataOnExit()
210 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530211 // TODO auto-generated method stub, to be implemented by parser
212 }
213
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530214 @Override
215 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530216 return nodeIdentifier.getName();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530217 }
218
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530219 @Override
220 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530221 nodeIdentifier.setName(name);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530222 }
223
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530224 /**
225 * Returns node identifier.
226 *
227 * @return node identifier
228 */
229 public YangNodeIdentifier getNodeIdentifier() {
230 return nodeIdentifier;
231 }
232
233 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530234 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530235 *
236 * @param nodeIdentifier the node identifier
237 */
238 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
239 this.nodeIdentifier = nodeIdentifier;
240 }
241
242 /**
243 * Returns prefix associated with uses.
244 *
245 * @return prefix associated with uses
246 */
247 public String getPrefix() {
248 return nodeIdentifier.getPrefix();
249 }
250
251 /**
252 * Get prefix associated with uses.
253 *
254 * @param prefix prefix associated with uses
255 */
256 public void setPrefix(String prefix) {
257 nodeIdentifier.setPrefix(prefix);
258 }
259
260 @Override
Vinod Kumar S427d2932016-04-20 13:02:58 +0530261 public void resolve()
262 throws DataModelException {
263
264 YangGrouping referredGrouping = getRefGroup();
265
266 if (referredGrouping == null) {
267 throw new DataModelException("YANG uses linker error, cannot resolve uses");
268 }
269
270 YangNode usesParentNode = getParentNodeInGenCode(this);
janani b4e53f9b2016-04-26 18:49:20 +0530271 if ((!(usesParentNode instanceof YangLeavesHolder))
272 || (!(usesParentNode instanceof CollisionDetector))) {
Vinod Kumar S427d2932016-04-20 13:02:58 +0530273 throw new DataModelException("YANG uses holder construct is wrong");
274 }
275
276 YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
277 if (referredGrouping.getListOfLeaf() != null) {
278 for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
janani b4e53f9b2016-04-26 18:49:20 +0530279 ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getLeafName(),
280 YangConstructType.LEAF_DATA);
Vinod Kumar S427d2932016-04-20 13:02:58 +0530281 usesParentLeavesHolder.addLeaf(leaf);
282 }
283 }
284 if (referredGrouping.getListOfLeafList() != null) {
285 for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
janani b4e53f9b2016-04-26 18:49:20 +0530286 ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getLeafName(),
287 YangConstructType.LEAF_LIST_DATA);
Vinod Kumar S427d2932016-04-20 13:02:58 +0530288 usesParentLeavesHolder.addLeafList(leafList);
289 }
290 }
291
292 YangNode.cloneSubTree(getRefGroup(), usesParentNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530293 }
294
295 @Override
296 public ResolvableStatus getResolvableStatus() {
297 return resolvableStatus;
298 }
299
300 @Override
301 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
302 this.resolvableStatus = resolvableStatus;
303 }
janani b4e53f9b2016-04-26 18:49:20 +0530304
305 @Override
306 public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
307 detectCollidingChildUtil(identifierName, dataType, this);
308 }
309
310 @Override
311 public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
312
313 if (getName().equals(identifierName)) {
314 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as uses \""
315 + getName() + "\"");
316 }
317 }
318
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530319}