blob: 7f9b4a3af2d37932774c854bce955e36f90837f3 [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
Vinod Kumar S427d2932016-04-20 13:02:58 +053022import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
23
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053024/*-
25 * Reference RFC 6020.
26 *
27 * The "uses" statement is used to reference a "grouping" definition. It takes
28 * one argument, which is the name of the grouping.
29 *
30 * The effect of a "uses" reference to a grouping is that the nodes defined by
31 * the grouping are copied into the current schema tree, and then updated
32 * according to the "refine" and "augment" statements.
33 *
34 * The identifiers defined in the grouping are not bound to a namespace until
35 * the contents of the grouping are added to the schema tree via a "uses"
36 * statement that does not appear inside a "grouping" statement, at which point
37 * they are bound to the namespace of the current module.
38 *
39 * The uses's sub-statements
40 *
41 * +--------------+---------+-------------+------------------+
42 * | substatement | section | cardinality |data model mapping|
43 * +--------------+---------+-------------+------------------+
44 * | augment | 7.15 | 0..1 | -child nodes |
45 * | description | 7.19.3 | 0..1 | -string |
46 * | if-feature | 7.18.2 | 0..n | -TODO |
47 * | refine | 7.12.2 | 0..1 | -TODO |
48 * | reference | 7.19.4 | 0..1 | -string |
49 * | status | 7.19.2 | 0..1 | -YangStatus |
50 * | when | 7.19.5 | 0..1 | -TODO |
51 * +--------------+---------+-------------+------------------+
52 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053053
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053054/**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Represents data model node to maintain information defined in YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053056 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053057public class YangUses
58 extends YangNode
59 implements YangCommonInfo, Parsable, Resolvable {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053060
61 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053062 * YANG node identifier.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053063 */
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053064 private YangNodeIdentifier nodeIdentifier;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053065
66 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053067 * Referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053068 */
69 private YangGrouping refGroup;
70
71 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053072 * Description of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053073 */
74 private String description;
75
76 /**
77 * YANG reference.
78 */
79 private String reference;
80
81 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +053082 * Status of YANG uses.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053083 */
84 private YangStatusType status;
85
86 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053087 * Status of resolution. If completely resolved enum value is "RESOLVED",
88 * if not enum value is "UNRESOLVED", in case reference of grouping/typedef
89 * is added to uses/type but it's not resolved value of enum should be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053090 * "INTRA_FILE_RESOLVED".
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053091 */
92 private ResolvableStatus resolvableStatus;
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Creates an YANG uses node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +053096 */
97 public YangUses() {
98 super(YangNodeType.USES_NODE);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053099 nodeIdentifier = new YangNodeIdentifier();
100 resolvableStatus = ResolvableStatus.UNRESOLVED;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530101 }
102
103 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530104 * Returns the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530105 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 * @return the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530107 */
108 public YangGrouping getRefGroup() {
109 return refGroup;
110 }
111
112 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530113 * Sets the referred group.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530114 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530115 * @param refGroup the referred group
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530116 */
117 public void setRefGroup(YangGrouping refGroup) {
118 this.refGroup = refGroup;
119 }
120
121 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530122 * Returns the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530123 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530124 * @return the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530125 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530126 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530127 public String getDescription() {
128 return description;
129 }
130
131 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530132 * Sets the description.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530133 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530134 * @param description set the description
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530135 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530136 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530137 public void setDescription(String description) {
138 this.description = description;
139 }
140
141 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530142 * Returns the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530143 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530144 * @return the reference
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530145 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530146 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530147 public String getReference() {
148 return reference;
149 }
150
151 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530152 * Sets the textual reference.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530153 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530154 * @param reference the reference to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530155 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530156 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530157 public void setReference(String reference) {
158 this.reference = reference;
159 }
160
161 /**
Gaurav Agrawalbd804472016-03-25 11:25:36 +0530162 * Returns the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530163 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530164 * @return the status
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530165 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530166 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530167 public YangStatusType getStatus() {
168 return status;
169 }
170
171 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530172 * Sets the status.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530173 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 * @param status the status to set
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530175 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530176 @Override
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530177 public void setStatus(YangStatusType status) {
178 this.status = status;
179 }
180
181 /**
182 * Returns the type of the data.
183 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530184 * @return returns USES_DATA
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530185 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530186 @Override
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530187 public YangConstructType getYangConstructType() {
188 return YangConstructType.USES_DATA;
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530189 }
190
191 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530192 * Validates the data on entering the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530193 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530194 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530195 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530196 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530197 public void validateDataOnEntry()
198 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530199 // TODO auto-generated method stub, to be implemented by parser
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Validates the data on exiting the corresponding parse tree node.
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530204 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530205 * @throws DataModelException a violation of data model rules
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530206 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530207 @Override
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530208 public void validateDataOnExit()
209 throws DataModelException {
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530210 // TODO auto-generated method stub, to be implemented by parser
211 }
212
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530213 @Override
214 public String getName() {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530215 return nodeIdentifier.getName();
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530216 }
217
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530218 @Override
219 public void setName(String name) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530220 nodeIdentifier.setName(name);
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530221 }
222
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530223 /**
224 * Returns node identifier.
225 *
226 * @return node identifier
227 */
228 public YangNodeIdentifier getNodeIdentifier() {
229 return nodeIdentifier;
230 }
231
232 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530233 * Sets node identifier.
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530234 *
235 * @param nodeIdentifier the node identifier
236 */
237 public void setNodeIdentifier(YangNodeIdentifier nodeIdentifier) {
238 this.nodeIdentifier = nodeIdentifier;
239 }
240
241 /**
242 * Returns prefix associated with uses.
243 *
244 * @return prefix associated with uses
245 */
246 public String getPrefix() {
247 return nodeIdentifier.getPrefix();
248 }
249
250 /**
251 * Get prefix associated with uses.
252 *
253 * @param prefix prefix associated with uses
254 */
255 public void setPrefix(String prefix) {
256 nodeIdentifier.setPrefix(prefix);
257 }
258
259 @Override
Vinod Kumar S427d2932016-04-20 13:02:58 +0530260 public void resolve()
261 throws DataModelException {
262
263 YangGrouping referredGrouping = getRefGroup();
264
265 if (referredGrouping == null) {
266 throw new DataModelException("YANG uses linker error, cannot resolve uses");
267 }
268
269 YangNode usesParentNode = getParentNodeInGenCode(this);
270 if (!(usesParentNode instanceof YangLeavesHolder)) {
271 throw new DataModelException("YANG uses holder construct is wrong");
272 }
273
274 YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
275 if (referredGrouping.getListOfLeaf() != null) {
276 for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
277 usesParentLeavesHolder.addLeaf(leaf);
278 }
279 }
280 if (referredGrouping.getListOfLeafList() != null) {
281 for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
282 usesParentLeavesHolder.addLeafList(leafList);
283 }
284 }
285
286 YangNode.cloneSubTree(getRefGroup(), usesParentNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530287 }
288
289 @Override
290 public ResolvableStatus getResolvableStatus() {
291 return resolvableStatus;
292 }
293
294 @Override
295 public void setResolvableStatus(ResolvableStatus resolvableStatus) {
296 this.resolvableStatus = resolvableStatus;
297 }
Vinod Kumar S2ff139c2016-02-16 01:37:16 +0530298}