blob: 99baf4a7e5b8e03563553ac675167dd389175fc7 [file] [log] [blame]
Vinod Kumar S67e7be62016-02-11 20:13:28 +05301/*
2 * Copyright 2016 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.datamodel;
18
Bharat saraswal870c56f2016-02-20 21:57:16 +053019import java.io.IOException;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053020import java.util.LinkedList;
21import java.util.List;
22
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.parser.Parsable;
25import org.onosproject.yangutils.parser.ParsableDataType;
Bharat saraswal870c56f2016-02-20 21:57:16 +053026import org.onosproject.yangutils.translator.CachedFileHandle;
27import org.onosproject.yangutils.translator.GeneratedFileType;
28import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053029import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswal870c56f2016-02-20 21:57:16 +053030import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053031/*-
32 * Reference RFC 6020.
33 *
34 * The "container" statement is used to define an interior data node in the
35 * schema tree. It takes one argument, which is an identifier, followed by a
36 * block of sub-statements that holds detailed container information.
37 *
38 * A container node does not have a value, but it has a list of child nodes in
39 * the data tree. The child nodes are defined in the container's sub-statements.
40 *
41 * Containers with Presence
42 *
43 * YANG supports two styles of containers, those that exist only for organizing
44 * the hierarchy of data nodes, and those whose presence in the configuration
45 * has an explicit meaning.
46 *
47 * In the first style, the container has no meaning of its own, existing only to
48 * contain child nodes. This is the default style.
49 *
50 * For example, the set of scrambling options for Synchronous Optical Network
51 * (SONET) interfaces may be placed inside a "scrambling" container to enhance
52 * the organization of the configuration hierarchy, and to keep these nodes
53 * together. The "scrambling" node itself has no meaning, so removing the node
54 * when it becomes empty relieves the user from performing this task.
55 *
56 * In the second style, the presence of the container itself is configuration
57 * data, representing a single bit of configuration data. The container acts as
58 * both a configuration knob and a means of organizing related configuration.
59 * These containers are explicitly created and deleted.
60 *
61 * YANG calls this style a "presence container" and it is indicated using the
62 * "presence" statement, which takes as its argument a text string indicating
63 * what the presence of the node means.
64 *
65 * The container's Substatements
66 *
67 * +--------------+---------+-------------+------------------+
68 * | substatement | section | cardinality |data model mapping|
69 * +--------------+---------+-------------+------------------+
70 * | anyxml | 7.10 | 0..n | -not supported |
71 * | choice | 7.9 | 0..n | -child nodes |
72 * | config | 7.19.1 | 0..1 | -boolean |
73 * | container | 7.5 | 0..n | -child nodes |
74 * | description | 7.19.3 | 0..1 | -string |
75 * | grouping | 7.11 | 0..n | -child nodes |
76 * | if-feature | 7.18.2 | 0..n | -TODO |
77 * | leaf | 7.6 | 0..n | -YangLeaf |
78 * | leaf-list | 7.7 | 0..n | -YangLeafList |
79 * | list | 7.8 | 0..n | -child nodes |
80 * | must | 7.5.3 | 0..n | -TODO |
81 * | presence | 7.5.5 | 0..1 | -boolean |
82 * | reference | 7.19.4 | 0..1 | -string |
83 * | status | 7.19.2 | 0..1 | -YangStatus |
84 * | typedef | 7.3 | 0..n | -child nodes |
85 * | uses | 7.12 | 0..n | -child nodes |
86 * | when | 7.19.5 | 0..1 | -TODO |
87 * +--------------+---------+-------------+------------------+
88 */
89
90/**
91 * Data model node to maintain information defined in YANG container.
92 */
93public class YangContainer extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable {
94
95 /**
96 * Name of the container.
97 */
98 private String name;
99
100 /**
101 * If container maintains config data.
102 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530103 private Boolean isConfig;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530104
105 /**
106 * Description of container.
107 */
108 private String description;
109
110 /**
111 * List of leaves contained.
112 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530113 private List<YangLeaf> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530114
115 /**
116 * List of leaf-lists contained.
117 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530118 private List<YangLeafList> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530119
120 /**
121 * If it is a presence container, then the textual documentation of presence
122 * usage.
123 */
124 private String presence;
125
126 /**
127 * Reference of the module.
128 */
129 private String reference;
130
131 /**
132 * Status of the node.
133 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530134 private YangStatusType status = YangStatusType.CURRENT;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530135
136 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530137 * Package of the generated java code.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530138 */
139 private String pkg;
140
141 /**
142 * Cached Java File Handle.
143 */
144 private CachedFileHandle fileHandle;
145
146 /**
147 * Create a container node.
148 */
149 public YangContainer() {
150 super(YangNodeType.CONTAINER_NODE);
151 }
152
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530153 /**
154 * Get the YANG name of container.
155 *
156 * @return the name of container as defined in YANG file.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530157 */
158 @Override
159 public String getName() {
160 return name;
161 }
162
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530163 /**
164 * Set the YANG name of container.
165 *
166 * @param name the name of container as defined in YANG file.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530167 */
168 @Override
169 public void setName(String name) {
170 this.name = name;
171 }
172
173 /**
174 * Get the config flag.
175 *
176 * @return the isConfig
177 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530178 public Boolean isConfig() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530179 return isConfig;
180 }
181
182 /**
183 * Set the config flag.
184 *
185 * @param isCfg the config flag.
186 */
187 public void setConfig(boolean isCfg) {
188 isConfig = isCfg;
189 }
190
191 /**
192 * Get the description.
193 *
194 * @return the description.
195 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530196 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530197 public String getDescription() {
198 return description;
199 }
200
201 /**
202 * Set the description.
203 *
204 * @param description set the description.
205 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530206 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530207 public void setDescription(String description) {
208 this.description = description;
209 }
210
211 /**
212 * Get the list of leaves.
213 *
214 * @return the list of leaves.
215 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530216 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530217 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530218 return listOfLeaf;
219 }
220
221 /**
222 * Set the list of leaves.
223 *
224 * @param leafsList the list of leaf to set.
225 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530226 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530227 listOfLeaf = leafsList;
228 }
229
230 /**
231 * Add a leaf.
232 *
233 * @param leaf the leaf to be added.
234 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530235 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530236 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530237 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530238 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530239 }
240
241 getListOfLeaf().add(leaf);
242 }
243
244 /**
245 * Get the list of leaf-list.
246 *
247 * @return the list of leaf-list.
248 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530249 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530250 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530251 return listOfLeafList;
252 }
253
254 /**
255 * Set the list of leaf-list.
256 *
257 * @param listOfLeafList the list of leaf-list to set.
258 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530259 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530260 this.listOfLeafList = listOfLeafList;
261 }
262
263 /**
264 * Add a leaf-list.
265 *
266 * @param leafList the leaf-list to be added.
267 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530268 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530269 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530270 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530271 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530272 }
273
274 getListOfLeafList().add(leafList);
275 }
276
277 /**
278 * Get the presence string if present.
279 *
280 * @return the isPressence.
281 */
282 public String getPresence() {
283 return presence;
284 }
285
286 /**
287 * Set the presence string.
288 *
289 * @param presence the presence flag
290 */
291 public void setPresence(String presence) {
292 this.presence = presence;
293 }
294
295 /**
296 * Get the textual reference.
297 *
298 * @return the reference.
299 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530300 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530301 public String getReference() {
302 return reference;
303 }
304
305 /**
306 * Set the textual reference.
307 *
308 * @param reference the reference to set.
309 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530310 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530311 public void setReference(String reference) {
312 this.reference = reference;
313 }
314
315 /**
316 * Get the status.
317 *
318 * @return the status.
319 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530320 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530321 public YangStatusType getStatus() {
322 return status;
323 }
324
325 /**
326 * Set the status.
327 *
328 * @param status the status to set.
329 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530330 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530331 public void setStatus(YangStatusType status) {
332 this.status = status;
333 }
334
335 /**
336 * Get the cached file handle.
337 *
338 * @return the fileHandle
339 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530340 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530341 public CachedFileHandle getFileHandle() {
342 return fileHandle;
343 }
344
345 /**
346 * Set the cached file handle.
347 *
348 * @param handle the fileHandle to set
349 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530350 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530351 public void setFileHandle(CachedFileHandle handle) {
352 fileHandle = handle;
353 }
354
355 /**
356 * Returns the type of the data.
357 *
358 * @return returns CONTAINER_DATA.
359 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530360 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530361 public ParsableDataType getParsableDataType() {
362 return ParsableDataType.CONTAINER_DATA;
363 }
364
365 /**
366 * Validate the data on entering the corresponding parse tree node.
367 *
368 * @throws DataModelException a violation of data model rules.
369 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530370 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530371 public void validateDataOnEntry() throws DataModelException {
372 // TODO auto-generated method stub, to be implemented by parser
373 }
374
375 /**
376 * Validate the data on exiting the corresponding parse tree node.
377 *
378 * @throws DataModelException a violation of data model rules.
379 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530380 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530381 public void validateDataOnExit() throws DataModelException {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530382 List<YangLeaf> leaves = getListOfLeaf();
383 List<YangLeafList> leafLists = getListOfLeafList();
384
385 setDefaultConfigValueToChild(leaves, leafLists);
386 validateConfig(leaves, leafLists);
387 }
388
389 /**
390 * Sets the config's value to all leaf if leaf's config statement is not specified.
391 *
392 * @param leaves list of leaf attributes of container.
393 * @param leafLists list of leaf-list attributes of container.
394 */
395 private void setDefaultConfigValueToChild(List<YangLeaf> leaves, List<YangLeafList> leafLists) {
396
397 /* If "config" is not specified, the default is the same as the parent
398 schema node's "config" value.*/
399 if (leaves != null) {
400 for (YangLeaf leaf : leaves) {
401 if (leaf.isConfig() == null) {
402 leaf.setConfig(isConfig);
403 }
404 }
405 }
406
407 /* If "config" is not specified, the default is the same as the parent
408 schema node's "config" value.*/
409 if (leafLists != null) {
410 for (YangLeafList leafList : leafLists) {
411 if (leafList.isConfig() == null) {
412 leafList.setConfig(isConfig);
413 }
414 }
415 }
416 }
417
418 /**
419 * Validates config statement of container.
420 *
421 * @param leaves list of leaf attributes of container.
422 * @param leafLists list of leaf-list attributes of container.
423 * @throws DataModelException a violation of data model rules.
424 */
425 private void validateConfig(List<YangLeaf> leaves, List<YangLeafList> leafLists) throws DataModelException {
426
427 /* If a node has "config" set to "false", no node underneath it can have
428 "config" set to "true".*/
429 if ((!isConfig) && (leaves != null)) {
430 for (YangLeaf leaf : leaves) {
431 if (leaf.isConfig()) {
432 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
433 "it can have \"config\" set to \"true\".");
434 }
435 }
436 }
437
438 if ((!isConfig) && (leafLists != null)) {
439 for (YangLeafList leafList : leafLists) {
440 if (leafList.isConfig()) {
441 throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
442 "it can have \"config\" set to \"true\".");
443 }
444 }
445 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530446 }
447
448 /**
449 * Get the mapped java package.
450 *
451 * @return the java package
452 */
453 @Override
454 public String getPackage() {
455 return pkg;
456 }
457
458 /**
459 * Set the mapped java package.
460 *
461 * @param pcg the package to set
462 */
463 @Override
464 public void setPackage(String pcg) {
465 pkg = pcg;
466 }
467
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530468 /**
469 * Generate the java code corresponding to YANG container.
470 *
471 * @throws IOException when fails to generate the source files.
472 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530473 @Override
474 public void generateJavaCodeEntry() throws IOException {
475 YangNode parent = getParent();
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530476 String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
477 setPackage(contPkg);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530478
479 CachedFileHandle handle = null;
480 try {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530481 FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530482 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530483 handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530484 } catch (IOException e) {
485 throw new IOException("Failed to create the source files.");
486 }
487 setFileHandle(handle);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530488
489 addLeavesAttributes();
490 addLeafListAttributes();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530491 addAttributeInParent();
492 }
493
494 /**
495 * Adds current node attribute to parent file.
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530496 */
497 private void addAttributeInParent() {
498 if (getParent() != null) {
499 getParent().getFileHandle().setChildsPackage(getPackage());
500 getParent().getFileHandle().addAttributeInfo(null, getName(), false);
501 }
502 }
503
504 @Override
505 public void generateJavaCodeExit() throws IOException {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530506 getFileHandle().close();
507 return;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530508 }
509
510 /**
511 * Adds leaf attributes in generated files.
512 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530513 private void addLeavesAttributes() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530514
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530515 List<YangLeaf> leaves = getListOfLeaf();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530516 if (leaves != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530517 for (YangLeaf leaf : leaves) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530518 getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
519 }
520 }
521 }
522
523 /**
524 * Adds leaf list's attributes in generated files.
525 */
526 private void addLeafListAttributes() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530527 List<YangLeafList> leavesList = getListOfLeafList();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530528 if (leavesList != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530529 for (YangLeafList leafList : leavesList) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530530 getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
531 }
532 }
533 return;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530534 }
535}