blob: cb54cca1a1202d1cab1f6699d45f18d5138b9fb2 [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;
29import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053030/*-
31 * Reference RFC 6020.
32 *
33 * The "container" statement is used to define an interior data node in the
34 * schema tree. It takes one argument, which is an identifier, followed by a
35 * block of sub-statements that holds detailed container information.
36 *
37 * A container node does not have a value, but it has a list of child nodes in
38 * the data tree. The child nodes are defined in the container's sub-statements.
39 *
40 * Containers with Presence
41 *
42 * YANG supports two styles of containers, those that exist only for organizing
43 * the hierarchy of data nodes, and those whose presence in the configuration
44 * has an explicit meaning.
45 *
46 * In the first style, the container has no meaning of its own, existing only to
47 * contain child nodes. This is the default style.
48 *
49 * For example, the set of scrambling options for Synchronous Optical Network
50 * (SONET) interfaces may be placed inside a "scrambling" container to enhance
51 * the organization of the configuration hierarchy, and to keep these nodes
52 * together. The "scrambling" node itself has no meaning, so removing the node
53 * when it becomes empty relieves the user from performing this task.
54 *
55 * In the second style, the presence of the container itself is configuration
56 * data, representing a single bit of configuration data. The container acts as
57 * both a configuration knob and a means of organizing related configuration.
58 * These containers are explicitly created and deleted.
59 *
60 * YANG calls this style a "presence container" and it is indicated using the
61 * "presence" statement, which takes as its argument a text string indicating
62 * what the presence of the node means.
63 *
64 * The container's Substatements
65 *
66 * +--------------+---------+-------------+------------------+
67 * | substatement | section | cardinality |data model mapping|
68 * +--------------+---------+-------------+------------------+
69 * | anyxml | 7.10 | 0..n | -not supported |
70 * | choice | 7.9 | 0..n | -child nodes |
71 * | config | 7.19.1 | 0..1 | -boolean |
72 * | container | 7.5 | 0..n | -child nodes |
73 * | description | 7.19.3 | 0..1 | -string |
74 * | grouping | 7.11 | 0..n | -child nodes |
75 * | if-feature | 7.18.2 | 0..n | -TODO |
76 * | leaf | 7.6 | 0..n | -YangLeaf |
77 * | leaf-list | 7.7 | 0..n | -YangLeafList |
78 * | list | 7.8 | 0..n | -child nodes |
79 * | must | 7.5.3 | 0..n | -TODO |
80 * | presence | 7.5.5 | 0..1 | -boolean |
81 * | reference | 7.19.4 | 0..1 | -string |
82 * | status | 7.19.2 | 0..1 | -YangStatus |
83 * | typedef | 7.3 | 0..n | -child nodes |
84 * | uses | 7.12 | 0..n | -child nodes |
85 * | when | 7.19.5 | 0..1 | -TODO |
86 * +--------------+---------+-------------+------------------+
87 */
88
89/**
90 * Data model node to maintain information defined in YANG container.
91 */
92public class YangContainer extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable {
93
94 /**
95 * Name of the container.
96 */
97 private String name;
98
99 /**
100 * If container maintains config data.
101 */
102 private boolean isConfig;
103
104 /**
105 * Description of container.
106 */
107 private String description;
108
109 /**
110 * List of leaves contained.
111 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 private List<YangLeaf<?>> listOfLeaf;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530113
114 /**
115 * List of leaf-lists contained.
116 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530117 private List<YangLeafList<?>> listOfLeafList;
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530118
119 /**
120 * If it is a presence container, then the textual documentation of presence
121 * usage.
122 */
123 private String presence;
124
125 /**
126 * Reference of the module.
127 */
128 private String reference;
129
130 /**
131 * Status of the node.
132 */
133 private YangStatusType status;
134
135 /**
136 * package of the generated java code.
137 */
138 private String pkg;
139
140 /**
141 * Cached Java File Handle.
142 */
143 private CachedFileHandle fileHandle;
144
145 /**
146 * Create a container node.
147 */
148 public YangContainer() {
149 super(YangNodeType.CONTAINER_NODE);
150 }
151
152 /* (non-Javadoc)
153 * @see org.onosproject.yangutils.datamodel.YangNode#getName()
154 */
155 @Override
156 public String getName() {
157 return name;
158 }
159
160 /* (non-Javadoc)
161 * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
162 */
163 @Override
164 public void setName(String name) {
165 this.name = name;
166 }
167
168 /**
169 * Get the config flag.
170 *
171 * @return the isConfig
172 */
173 public boolean isConfig() {
174 return isConfig;
175 }
176
177 /**
178 * Set the config flag.
179 *
180 * @param isCfg the config flag.
181 */
182 public void setConfig(boolean isCfg) {
183 isConfig = isCfg;
184 }
185
186 /**
187 * Get the description.
188 *
189 * @return the description.
190 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530191 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530192 public String getDescription() {
193 return description;
194 }
195
196 /**
197 * Set the description.
198 *
199 * @param description set the description.
200 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530202 public void setDescription(String description) {
203 this.description = description;
204 }
205
206 /**
207 * Get the list of leaves.
208 *
209 * @return the list of leaves.
210 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530211 @Override
212 public List<YangLeaf<?>> getListOfLeaf() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530213 return listOfLeaf;
214 }
215
216 /**
217 * Set the list of leaves.
218 *
219 * @param leafsList the list of leaf to set.
220 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530221 private void setListOfLeaf(List<YangLeaf<?>> leafsList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530222 listOfLeaf = leafsList;
223 }
224
225 /**
226 * Add a leaf.
227 *
228 * @param leaf the leaf to be added.
229 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530230 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530231 public void addLeaf(YangLeaf<?> leaf) {
232 if (getListOfLeaf() == null) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530233 setListOfLeaf(new LinkedList<YangLeaf<?>>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530234 }
235
236 getListOfLeaf().add(leaf);
237 }
238
239 /**
240 * Get the list of leaf-list.
241 *
242 * @return the list of leaf-list.
243 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530244 @Override
245 public List<YangLeafList<?>> getListOfLeafList() {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530246 return listOfLeafList;
247 }
248
249 /**
250 * Set the list of leaf-list.
251 *
252 * @param listOfLeafList the list of leaf-list to set.
253 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530254 private void setListOfLeafList(List<YangLeafList<?>> listOfLeafList) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530255 this.listOfLeafList = listOfLeafList;
256 }
257
258 /**
259 * Add a leaf-list.
260 *
261 * @param leafList the leaf-list to be added.
262 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530263 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530264 public void addLeafList(YangLeafList<?> leafList) {
265 if (getListOfLeafList() == null) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530266 setListOfLeafList(new LinkedList<YangLeafList<?>>());
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530267 }
268
269 getListOfLeafList().add(leafList);
270 }
271
272 /**
273 * Get the presence string if present.
274 *
275 * @return the isPressence.
276 */
277 public String getPresence() {
278 return presence;
279 }
280
281 /**
282 * Set the presence string.
283 *
284 * @param presence the presence flag
285 */
286 public void setPresence(String presence) {
287 this.presence = presence;
288 }
289
290 /**
291 * Get the textual reference.
292 *
293 * @return the reference.
294 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530295 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530296 public String getReference() {
297 return reference;
298 }
299
300 /**
301 * Set the textual reference.
302 *
303 * @param reference the reference to set.
304 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530305 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530306 public void setReference(String reference) {
307 this.reference = reference;
308 }
309
310 /**
311 * Get the status.
312 *
313 * @return the status.
314 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530315 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530316 public YangStatusType getStatus() {
317 return status;
318 }
319
320 /**
321 * Set the status.
322 *
323 * @param status the status to set.
324 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530325 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530326 public void setStatus(YangStatusType status) {
327 this.status = status;
328 }
329
330 /**
331 * Get the cached file handle.
332 *
333 * @return the fileHandle
334 */
335 public CachedFileHandle getFileHandle() {
336 return fileHandle;
337 }
338
339 /**
340 * Set the cached file handle.
341 *
342 * @param handle the fileHandle to set
343 */
344 public void setFileHandle(CachedFileHandle handle) {
345 fileHandle = handle;
346 }
347
348 /**
349 * Returns the type of the data.
350 *
351 * @return returns CONTAINER_DATA.
352 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530353 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530354 public ParsableDataType getParsableDataType() {
355 return ParsableDataType.CONTAINER_DATA;
356 }
357
358 /**
359 * Validate the data on entering the corresponding parse tree node.
360 *
361 * @throws DataModelException a violation of data model rules.
362 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530363 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530364 public void validateDataOnEntry() throws DataModelException {
365 // TODO auto-generated method stub, to be implemented by parser
366 }
367
368 /**
369 * Validate the data on exiting the corresponding parse tree node.
370 *
371 * @throws DataModelException a violation of data model rules.
372 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530373 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530374 public void validateDataOnExit() throws DataModelException {
375 // TODO auto-generated method stub, to be implemented by parser
376 }
377
378 /**
379 * Get the mapped java package.
380 *
381 * @return the java package
382 */
383 @Override
384 public String getPackage() {
385 return pkg;
386 }
387
388 /**
389 * Set the mapped java package.
390 *
391 * @param pcg the package to set
392 */
393 @Override
394 public void setPackage(String pcg) {
395 pkg = pcg;
396 }
397
398 /**
399 * Generate the java code corresponding to YANG container.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530400 *
401 * @throws IOException when fails to generate the source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530402 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530403 @Override
404 public void generateJavaCodeEntry() throws IOException {
405 YangNode parent = getParent();
406 String modPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), getName());
407 setPackage(modPkg);
408
409 CachedFileHandle handle = null;
410 try {
411 FileSystemUtil.createPackage(getPackage(), getName());
412 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
413 } catch (IOException e) {
414 throw new IOException("Failed to create the source files.");
415 }
416 setFileHandle(handle);
417 addLavesAttributes();
418 addLeafListAttributes();
419 }
420
421 /**
422 * Adds leaf attributes in generated files.
423 */
424 private void addLavesAttributes() {
425
426 List<YangLeaf<?>> leaves = getListOfLeaf();
427 if (leaves != null) {
428 for (YangLeaf<?> leaf : leaves) {
429 getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
430 }
431 }
432 }
433
434 /**
435 * Adds leaf list's attributes in generated files.
436 */
437 private void addLeafListAttributes() {
438 List<YangLeafList<?>> leavesList = getListOfLeafList();
439 if (leavesList != null) {
440 for (YangLeafList<?> leafList : leavesList) {
441 getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
442 }
443 }
444 return;
445
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530446 }
447
448 /**
449 * Free resources used to generate code.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530450 *
451 * @throws IOException when fails to generate source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530452 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530453 @Override
454 public void generateJavaCodeExit() throws IOException {
455 getFileHandle().close();
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530456 return;
457 }
458}