blob: c35fafa690e9ec6e2740e0a21497a38fad325eac [file] [log] [blame]
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +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 */
16package org.onosproject.yangutils.datamodel;
17
Bharat saraswal870c56f2016-02-20 21:57:16 +053018import java.io.IOException;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053019import java.util.LinkedList;
20import java.util.List;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053021
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.parser.Parsable;
24import org.onosproject.yangutils.parser.ParsableDataType;
Bharat saraswal870c56f2016-02-20 21:57:16 +053025import org.onosproject.yangutils.translator.CachedFileHandle;
Vinod Kumar S67e7be62016-02-11 20:13:28 +053026import org.onosproject.yangutils.translator.CodeGenerator;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import 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/*-
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053032 * Reference:RFC 6020.
33 * The "module" statement defines the module's name,
34 * and groups all statements that belong to the module together. The "module"
35 * statement's argument is the name of the module, followed by a block of
36 * sub statements that hold detailed module information.
37 * The module's sub statements
38 *
39 * +--------------+---------+-------------+-----------------------+
40 * |sub statement | section | cardinality | data model mapping |
41 * +--------------+---------+-------------+-----------------------+
42 * | anyxml | 7.10 | 0..n | not supported |
43 * | augment | 7.15 | 0..n | child nodes |
44 * | choice | 7.9 | 0..n | child nodes |
45 * | contact | 7.1.8 | 0..1 | string |
46 * | container | 7.5 | 0..n | child nodes |
47 * | description | 7.19.3 | 0..1 | string |
48 * | deviation | 7.18.3 | 0..n | TODO |
49 * | extension | 7.17 | 0..n | TODO |
50 * | feature | 7.18.1 | 0..n | TODO |
51 * | grouping | 7.11 | 0..n | child nodes |
52 * | identity | 7.16 | 0..n | TODO |
53 * | import | 7.1.5 | 0..n | list of import info |
54 * | include | 7.1.6 | 0..n | list of include info |
55 * | leaf | 7.6 | 0..n | list of leaf info |
56 * | leaf-list | 7.7 | 0..n | list of leaf-list info|
57 * | list | 7.8 | 0..n | child nodes |
58 * | namespace | 7.1.3 | 1 | string/uri |
59 * | notification | 7.14 | 0..n | TODO |
60 * | organization | 7.1.7 | 0..1 | string |
61 * | prefix | 7.1.4 | 1 | string |
62 * | reference | 7.19.4 | 0..1 | string |
63 * | revision | 7.1.9 | 0..n | revision |
64 * | rpc | 7.13 | 0..n | TODO |
65 * | typedef | 7.3 | 0..n | child nodes |
66 * | uses | 7.12 | 0..n | child nodes |
67 * | YANG-version | 7.1.2 | 0..1 | int |
68 * +--------------+---------+-------------+-----------------------+
69 */
70
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053071/**
72 * Data model node to maintain information defined in YANG module.
73 */
Bharat saraswal870c56f2016-02-20 21:57:16 +053074public class YangModule extends YangNode implements YangLeavesHolder, YangDesc, YangReference, Parsable, CodeGenerator {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053075
76 /**
77 * Name of the module.
78 */
79 private String name;
80
81 /**
82 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053083 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053084 * The "contact" statement provides contact information for the module. The
85 * argument is a string that is used to specify contact information for the
86 * person or persons to whom technical queries concerning this module should
87 * be sent, such as their name, postal address, telephone number, and
88 * electronic mail address.
89 */
90 private String contact;
91
92 /**
93 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053094 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053095 * The "description" statement takes as an argument a string that contains a
96 * human-readable textual description of this definition. The text is
97 * provided in a language (or languages) chosen by the module developer; for
98 * the sake of interoperability.
99 */
100 private String description;
101
102 /**
103 * List of YANG modules imported.
104 */
105 private List<YangImport> importList;
106
107 /**
108 * List of YANG sub-modules included.
109 */
110 private List<YangInclude> includeList;
111
112 /**
113 * List of leaves at root level in the module.
114 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530115 private List<YangLeaf<?>> listOfLeaf;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530116
117 /**
118 * List of leaf-lists at root level in the module.
119 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530120 private List<YangLeafList<?>> listOfLeafList;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530121
122 /**
123 * Name space of the module.
124 */
125 private YangNameSpace nameSpace;
126
127 /**
128 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530129 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530130 * The "organization" statement defines the party responsible for this
131 * module. The argument is a string that is used to specify a textual
132 * description of the organization(s) under whose auspices this module was
133 * developed.
134 */
135 private String organization;
136
137 /**
138 * Prefix to refer to the objects in module.
139 */
140 private String prefix;
141
142 /**
143 * Reference of the module.
144 */
145 private String reference;
146
147 /**
148 * Revision info of the module.
149 */
150 private YangRevision revision;
151
152 /**
153 * YANG version.
154 */
155 private byte version;
156
157 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530158 * package of the generated java code.
159 */
160 private String pkg;
161
162 /**
163 * Cached Java File Handle.
164 */
165 private CachedFileHandle fileHandle;
166
167 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530168 * Create a YANG node of module type.
169 */
170 public YangModule() {
171 super(YangNodeType.MODULE_NODE);
172 }
173
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530174 /* (non-Javadoc)
175 * @see org.onosproject.yangutils.datamodel.YangNode#getName()
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530176 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530177 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530178 public String getName() {
179 return name;
180 }
181
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530182 /* (non-Javadoc)
183 * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530184 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530185 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530186 public void setName(String moduleName) {
187 name = moduleName;
188 }
189
190 /**
191 * Get the contact details of the module owner.
192 *
193 * @return the contact details of YANG owner.
194 */
195 public String getContact() {
196 return contact;
197 }
198
199 /**
200 * Set the contact details of the module owner.
201 *
202 * @param contact the contact details of YANG owner.
203 */
204 public void setContact(String contact) {
205 this.contact = contact;
206 }
207
208 /**
209 * Get the description of module.
210 *
211 * @return the description of YANG module.
212 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530213 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530214 public String getDescription() {
215 return description;
216 }
217
218 /**
219 * Set the description of module.
220 *
221 * @param description set the description of YANG module.
222 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530223 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530224 public void setDescription(String description) {
225 this.description = description;
226 }
227
228 /**
229 * Get the list of imported modules.
230 *
231 * @return the list of imported modules.
232 */
233 public List<YangImport> getImportList() {
234 return importList;
235 }
236
237 /**
238 * prevent setting the import list from outside.
239 *
240 * @param importList the import list to set.
241 */
242 private void setImportList(List<YangImport> importList) {
243 this.importList = importList;
244 }
245
246 /**
247 * Add the imported module information to the import list.
248 *
249 * @param importedModule module being imported.
250 */
251 public void addImportedInfo(YangImport importedModule) {
252
253 if (getImportList() == null) {
254 setImportList(new LinkedList<YangImport>());
255 }
256
257 getImportList().add(importedModule);
258
259 return;
260 }
261
262 /**
263 * Get the list of included sub modules.
264 *
265 * @return the included list of sub modules.
266 */
267 public List<YangInclude> getIncludeList() {
268 return includeList;
269 }
270
271 /**
272 * Set the list of included sub modules.
273 *
274 * @param includeList the included list to set.
275 */
276 private void setIncludeList(List<YangInclude> includeList) {
277 this.includeList = includeList;
278 }
279
280 /**
281 * Add the included sub module information to the include list.
282 *
283 * @param includeModule submodule being included.
284 */
285 public void addIncludedInfo(YangInclude includeModule) {
286
287 if (getIncludeList() == null) {
288 setIncludeList(new LinkedList<YangInclude>());
289 }
290
291 getIncludeList().add(includeModule);
292 return;
293 }
294
295 /**
296 * Get the list of leaves in module.
297 *
298 * @return the list of leaves.
299 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530300 @Override
301 public List<YangLeaf<?>> getListOfLeaf() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530302 return listOfLeaf;
303 }
304
305 /**
306 * Set the list of leaf in module.
307 *
308 * @param leafsList the list of leaf to set.
309 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530310 private void setListOfLeaf(List<YangLeaf<?>> leafsList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530311 listOfLeaf = leafsList;
312 }
313
314 /**
315 * Add a leaf in module.
316 *
317 * @param leaf the leaf to be added.
318 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530320 public void addLeaf(YangLeaf<?> leaf) {
321 if (getListOfLeaf() == null) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530322 setListOfLeaf(new LinkedList<YangLeaf<?>>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530323 }
324
325 getListOfLeaf().add(leaf);
326 }
327
328 /**
329 * Get the list of leaf-list from module.
330 *
331 * @return the list of leaf-list.
332 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 @Override
334 public List<YangLeafList<?>> getListOfLeafList() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530335 return listOfLeafList;
336 }
337
338 /**
339 * Set the list of leaf-list in module.
340 *
341 * @param listOfLeafList the list of leaf-list to set.
342 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530343 private void setListOfLeafList(List<YangLeafList<?>> listOfLeafList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530344 this.listOfLeafList = listOfLeafList;
345 }
346
347 /**
348 * Add a leaf-list in module.
349 *
350 * @param leafList the leaf-list to be added.
351 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530352 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530353 public void addLeafList(YangLeafList<?> leafList) {
354 if (getListOfLeafList() == null) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530355 setListOfLeafList(new LinkedList<YangLeafList<?>>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530356 }
357
358 getListOfLeafList().add(leafList);
359 }
360
361 /**
362 * Get the name space of module elements.
363 *
364 * @return the nameSpace.
365 */
366 public YangNameSpace getNameSpace() {
367 return nameSpace;
368 }
369
370 /**
371 * Set the name space of module elements.
372 *
373 * @param nameSpace the nameSpace to set.
374 */
375 public void setNameSpace(YangNameSpace nameSpace) {
376 this.nameSpace = nameSpace;
377 }
378
379 /**
380 * Get the modules organization.
381 *
382 * @return the organization.
383 */
384 public String getOrganization() {
385 return organization;
386 }
387
388 /**
389 * Set the modules organization.
390 *
391 * @param org the organization to set.
392 */
393 public void setOrganization(String org) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530394 organization = org;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530395 }
396
397 /**
398 * Get the prefix.
399 *
400 * @return the prefix
401 */
402 public String getPrefix() {
403 return prefix;
404 }
405
406 /**
407 * Set the prefix.
408 *
409 * @param prefix the prefix to set.
410 */
411 public void setPrefix(String prefix) {
412 this.prefix = prefix;
413 }
414
415 /**
416 * Get the textual reference.
417 *
418 * @return the reference.
419 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530420 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530421 public String getReference() {
422 return reference;
423 }
424
425 /**
426 * Set the textual reference.
427 *
428 * @param reference the reference to set.
429 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530430 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530431 public void setReference(String reference) {
432 this.reference = reference;
433 }
434
435 /**
436 * Get the revision.
437 *
438 * @return the revision.
439 */
440 public YangRevision getRevision() {
441 return revision;
442 }
443
444 /**
445 * Set the revision.
446 *
447 * @param revision the revision to set.
448 */
449 public void setRevision(YangRevision revision) {
450 this.revision = revision;
451 }
452
453 /**
454 * Get the version.
455 *
456 * @return the version.
457 */
458 public byte getVersion() {
459 return version;
460 }
461
462 /**
463 * Set the version.
464 *
465 * @param version the version to set.
466 */
467 public void setVersion(byte version) {
468 this.version = version;
469 }
470
471 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530472 * Get the mapped java package.
473 *
474 * @return the java package
475 */
476 @Override
477 public String getPackage() {
478 return pkg;
479 }
480
481 /**
482 * Set the mapped java package.
483 *
484 * @param pcg the package to set
485 */
486 @Override
487 public void setPackage(String pcg) {
488 pkg = pcg;
489 }
490
491 /**
492 * Get the cached file handle.
493 *
494 * @return the fileHandle
495 */
496 public CachedFileHandle getFileHandle() {
497 return fileHandle;
498 }
499
500 /**
501 * Set the cached file handle.
502 *
503 * @param handle the fileHandle to set
504 */
505 public void setFileHandle(CachedFileHandle handle) {
506 fileHandle = handle;
507 }
508
509 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530510 * Returns the type of the parsed data.
511 *
512 * @return returns MODULE_DATA.
513 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530514 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530515 public ParsableDataType getParsableDataType() {
516 return ParsableDataType.MODULE_DATA;
517 }
518
519 /**
520 * Validate the data on entering the corresponding parse tree node.
521 *
522 * @throws DataModelException a violation of data model rules
523 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530524 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530525 public void validateDataOnEntry() throws DataModelException {
526 // TODO auto-generated method stub, to be implemented by parser
527 }
528
529 /**
530 * Validate the data on exiting the corresponding parse tree node.
531 *
532 * @throws DataModelException a violation of data model rules
533 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530534 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530535 public void validateDataOnExit() throws DataModelException {
536 // TODO auto-generated method stub, to be implemented by parser
537 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530538
539 /**
540 * Generates java code for module.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530541 *
542 * @throws IOException when fails to generate the source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530543 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530544 @Override
545 public void generateJavaCodeEntry() throws IOException {
546 String modPkg = JavaIdentifierSyntax.getRootPackage(getVersion(), getNameSpace().getUri(),
547 getRevision().getRevDate());
548 setPackage(modPkg);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530549
Bharat saraswal870c56f2016-02-20 21:57:16 +0530550 CachedFileHandle handle = null;
551 try {
552 FileSystemUtil.createPackage(getPackage(), getName());
553 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
554 } catch (IOException e) {
555 throw new IOException("Failed to create the source files.");
556 }
557 setFileHandle(handle);
558 addLavesAttributes();
559 addLeafListAttributes();
560 }
561
562 /**
563 * Adds leaf attributes in generated files.
564 */
565 private void addLavesAttributes() {
566
567 List<YangLeaf<?>> leaves = getListOfLeaf();
568 if (leaves != null) {
569 for (YangLeaf<?> leaf : leaves) {
570 getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
571 }
572 }
573 }
574
575 /**
576 * Adds leaf list's attributes in generated files.
577 */
578 private void addLeafListAttributes() {
579 List<YangLeafList<?>> leavesList = getListOfLeafList();
580 if (leavesList != null) {
581 for (YangLeafList<?> leafList : leavesList) {
582 getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
583 }
584 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530585 }
586
587 /**
588 * Free resources used to generate code.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530589 *
590 * @throws IOException when fails to generate source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530591 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530592 @Override
593 public void generateJavaCodeExit() throws IOException {
594 getFileHandle().close();
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530595 return;
596 }
597
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530598}