blob: 8eda6175359bb1404693f92ecbb2b7710354e2f2 [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;
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/*-
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053033 * Reference:RFC 6020.
34 * The "module" statement defines the module's name,
35 * and groups all statements that belong to the module together. The "module"
36 * statement's argument is the name of the module, followed by a block of
37 * sub statements that hold detailed module information.
38 * The module's sub statements
39 *
40 * +--------------+---------+-------------+-----------------------+
41 * |sub statement | section | cardinality | data model mapping |
42 * +--------------+---------+-------------+-----------------------+
43 * | anyxml | 7.10 | 0..n | not supported |
44 * | augment | 7.15 | 0..n | child nodes |
45 * | choice | 7.9 | 0..n | child nodes |
46 * | contact | 7.1.8 | 0..1 | string |
47 * | container | 7.5 | 0..n | child nodes |
48 * | description | 7.19.3 | 0..1 | string |
49 * | deviation | 7.18.3 | 0..n | TODO |
50 * | extension | 7.17 | 0..n | TODO |
51 * | feature | 7.18.1 | 0..n | TODO |
52 * | grouping | 7.11 | 0..n | child nodes |
53 * | identity | 7.16 | 0..n | TODO |
54 * | import | 7.1.5 | 0..n | list of import info |
55 * | include | 7.1.6 | 0..n | list of include info |
56 * | leaf | 7.6 | 0..n | list of leaf info |
57 * | leaf-list | 7.7 | 0..n | list of leaf-list info|
58 * | list | 7.8 | 0..n | child nodes |
59 * | namespace | 7.1.3 | 1 | string/uri |
60 * | notification | 7.14 | 0..n | TODO |
61 * | organization | 7.1.7 | 0..1 | string |
62 * | prefix | 7.1.4 | 1 | string |
63 * | reference | 7.19.4 | 0..1 | string |
64 * | revision | 7.1.9 | 0..n | revision |
65 * | rpc | 7.13 | 0..n | TODO |
66 * | typedef | 7.3 | 0..n | child nodes |
67 * | uses | 7.12 | 0..n | child nodes |
68 * | YANG-version | 7.1.2 | 0..1 | int |
69 * +--------------+---------+-------------+-----------------------+
70 */
71
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053072/**
73 * Data model node to maintain information defined in YANG module.
74 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053075public class YangModule extends YangNode
76 implements YangLeavesHolder, YangDesc, YangReference, Parsable, CodeGenerator {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053077
78 /**
79 * Name of the module.
80 */
81 private String name;
82
83 /**
84 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053085 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053086 * The "contact" statement provides contact information for the module. The
87 * argument is a string that is used to specify contact information for the
88 * person or persons to whom technical queries concerning this module should
89 * be sent, such as their name, postal address, telephone number, and
90 * electronic mail address.
91 */
92 private String contact;
93
94 /**
95 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053096 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053097 * The "description" statement takes as an argument a string that contains a
98 * human-readable textual description of this definition. The text is
99 * provided in a language (or languages) chosen by the module developer; for
100 * the sake of interoperability.
101 */
102 private String description;
103
104 /**
105 * List of YANG modules imported.
106 */
107 private List<YangImport> importList;
108
109 /**
110 * List of YANG sub-modules included.
111 */
112 private List<YangInclude> includeList;
113
114 /**
115 * List of leaves at root level in the module.
116 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530117 private List<YangLeaf> listOfLeaf;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530118
119 /**
120 * List of leaf-lists at root level in the module.
121 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530122 private List<YangLeafList> listOfLeafList;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530123
124 /**
125 * Name space of the module.
126 */
127 private YangNameSpace nameSpace;
128
129 /**
130 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530131 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530132 * The "organization" statement defines the party responsible for this
133 * module. The argument is a string that is used to specify a textual
134 * description of the organization(s) under whose auspices this module was
135 * developed.
136 */
137 private String organization;
138
139 /**
140 * Prefix to refer to the objects in module.
141 */
142 private String prefix;
143
144 /**
145 * Reference of the module.
146 */
147 private String reference;
148
149 /**
150 * Revision info of the module.
151 */
152 private YangRevision revision;
153
154 /**
155 * YANG version.
156 */
157 private byte version;
158
159 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530160 * Package of the generated java code.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530161 */
162 private String pkg;
163
164 /**
165 * Cached Java File Handle.
166 */
167 private CachedFileHandle fileHandle;
168
169 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530170 * Create a YANG node of module type.
171 */
172 public YangModule() {
173 super(YangNodeType.MODULE_NODE);
174 }
175
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530176 /* (non-Javadoc)
177 * @see org.onosproject.yangutils.datamodel.YangNode#getName()
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530178 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530179 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530180 public String getName() {
181 return name;
182 }
183
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530184 /* (non-Javadoc)
185 * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530186 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530187 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530188 public void setName(String moduleName) {
189 name = moduleName;
190 }
191
192 /**
193 * Get the contact details of the module owner.
194 *
195 * @return the contact details of YANG owner.
196 */
197 public String getContact() {
198 return contact;
199 }
200
201 /**
202 * Set the contact details of the module owner.
203 *
204 * @param contact the contact details of YANG owner.
205 */
206 public void setContact(String contact) {
207 this.contact = contact;
208 }
209
210 /**
211 * Get the description of module.
212 *
213 * @return the description of YANG module.
214 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530215 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530216 public String getDescription() {
217 return description;
218 }
219
220 /**
221 * Set the description of module.
222 *
223 * @param description set the description of YANG module.
224 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530225 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530226 public void setDescription(String description) {
227 this.description = description;
228 }
229
230 /**
231 * Get the list of imported modules.
232 *
233 * @return the list of imported modules.
234 */
235 public List<YangImport> getImportList() {
236 return importList;
237 }
238
239 /**
240 * prevent setting the import list from outside.
241 *
242 * @param importList the import list to set.
243 */
244 private void setImportList(List<YangImport> importList) {
245 this.importList = importList;
246 }
247
248 /**
249 * Add the imported module information to the import list.
250 *
251 * @param importedModule module being imported.
252 */
253 public void addImportedInfo(YangImport importedModule) {
254
255 if (getImportList() == null) {
256 setImportList(new LinkedList<YangImport>());
257 }
258
259 getImportList().add(importedModule);
260
261 return;
262 }
263
264 /**
265 * Get the list of included sub modules.
266 *
267 * @return the included list of sub modules.
268 */
269 public List<YangInclude> getIncludeList() {
270 return includeList;
271 }
272
273 /**
274 * Set the list of included sub modules.
275 *
276 * @param includeList the included list to set.
277 */
278 private void setIncludeList(List<YangInclude> includeList) {
279 this.includeList = includeList;
280 }
281
282 /**
283 * Add the included sub module information to the include list.
284 *
285 * @param includeModule submodule being included.
286 */
287 public void addIncludedInfo(YangInclude includeModule) {
288
289 if (getIncludeList() == null) {
290 setIncludeList(new LinkedList<YangInclude>());
291 }
292
293 getIncludeList().add(includeModule);
294 return;
295 }
296
297 /**
298 * Get the list of leaves in module.
299 *
300 * @return the list of leaves.
301 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530302 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530303 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530304 return listOfLeaf;
305 }
306
307 /**
308 * Set the list of leaf in module.
309 *
310 * @param leafsList the list of leaf to set.
311 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530312 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530313 listOfLeaf = leafsList;
314 }
315
316 /**
317 * Add a leaf in module.
318 *
319 * @param leaf the leaf to be added.
320 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530321 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530322 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530323 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530324 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530325 }
326
327 getListOfLeaf().add(leaf);
328 }
329
330 /**
331 * Get the list of leaf-list from module.
332 *
333 * @return the list of leaf-list.
334 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530335 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530336 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530337 return listOfLeafList;
338 }
339
340 /**
341 * Set the list of leaf-list in module.
342 *
343 * @param listOfLeafList the list of leaf-list to set.
344 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530345 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530346 this.listOfLeafList = listOfLeafList;
347 }
348
349 /**
350 * Add a leaf-list in module.
351 *
352 * @param leafList the leaf-list to be added.
353 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530354 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530355 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530356 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530357 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530358 }
359
360 getListOfLeafList().add(leafList);
361 }
362
363 /**
364 * Get the name space of module elements.
365 *
366 * @return the nameSpace.
367 */
368 public YangNameSpace getNameSpace() {
369 return nameSpace;
370 }
371
372 /**
373 * Set the name space of module elements.
374 *
375 * @param nameSpace the nameSpace to set.
376 */
377 public void setNameSpace(YangNameSpace nameSpace) {
378 this.nameSpace = nameSpace;
379 }
380
381 /**
382 * Get the modules organization.
383 *
384 * @return the organization.
385 */
386 public String getOrganization() {
387 return organization;
388 }
389
390 /**
391 * Set the modules organization.
392 *
393 * @param org the organization to set.
394 */
395 public void setOrganization(String org) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530396 organization = org;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530397 }
398
399 /**
400 * Get the prefix.
401 *
402 * @return the prefix
403 */
404 public String getPrefix() {
405 return prefix;
406 }
407
408 /**
409 * Set the prefix.
410 *
411 * @param prefix the prefix to set.
412 */
413 public void setPrefix(String prefix) {
414 this.prefix = prefix;
415 }
416
417 /**
418 * Get the textual reference.
419 *
420 * @return the reference.
421 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530422 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530423 public String getReference() {
424 return reference;
425 }
426
427 /**
428 * Set the textual reference.
429 *
430 * @param reference the reference to set.
431 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530432 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530433 public void setReference(String reference) {
434 this.reference = reference;
435 }
436
437 /**
438 * Get the revision.
439 *
440 * @return the revision.
441 */
442 public YangRevision getRevision() {
443 return revision;
444 }
445
446 /**
447 * Set the revision.
448 *
449 * @param revision the revision to set.
450 */
451 public void setRevision(YangRevision revision) {
452 this.revision = revision;
453 }
454
455 /**
456 * Get the version.
457 *
458 * @return the version.
459 */
460 public byte getVersion() {
461 return version;
462 }
463
464 /**
465 * Set the version.
466 *
467 * @param version the version to set.
468 */
469 public void setVersion(byte version) {
470 this.version = version;
471 }
472
473 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530474 * Get the mapped java package.
475 *
476 * @return the java package
477 */
478 @Override
479 public String getPackage() {
480 return pkg;
481 }
482
483 /**
484 * Set the mapped java package.
485 *
486 * @param pcg the package to set
487 */
488 @Override
489 public void setPackage(String pcg) {
490 pkg = pcg;
491 }
492
493 /**
494 * Get the cached file handle.
495 *
496 * @return the fileHandle
497 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530498 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530499 public CachedFileHandle getFileHandle() {
500 return fileHandle;
501 }
502
503 /**
504 * Set the cached file handle.
505 *
506 * @param handle the fileHandle to set
507 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530508 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530509 public void setFileHandle(CachedFileHandle handle) {
510 fileHandle = handle;
511 }
512
513 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530514 * Returns the type of the parsed data.
515 *
516 * @return returns MODULE_DATA.
517 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530518 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530519 public ParsableDataType getParsableDataType() {
520 return ParsableDataType.MODULE_DATA;
521 }
522
523 /**
524 * Validate the data on entering the corresponding parse tree node.
525 *
526 * @throws DataModelException a violation of data model rules
527 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530528 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530529 public void validateDataOnEntry() throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530530 /*
531 * Module is root in the data model tree, hence there is no entry
532 * validation
533 */
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530534 }
535
536 /**
537 * Validate the data on exiting the corresponding parse tree node.
538 *
539 * @throws DataModelException a violation of data model rules
540 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530541 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530542 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530543 /*
544 * TODO: perform symbol linking for the imported or included YANG info.
545 * TODO: perform symbol resolution for referred YANG entities.
546 */
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530547 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530548
549 /**
550 * Generates java code for module.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530551 *
552 * @throws IOException when fails to generate the source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530553 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530554 @Override
555 public void generateJavaCodeEntry() throws IOException {
556 String modPkg = JavaIdentifierSyntax.getRootPackage(getVersion(), getNameSpace().getUri(),
557 getRevision().getRevDate());
558 setPackage(modPkg);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530559
Bharat saraswal870c56f2016-02-20 21:57:16 +0530560 CachedFileHandle handle = null;
561 try {
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530562 FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
Bharat saraswal870c56f2016-02-20 21:57:16 +0530563 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530564 handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530565 } catch (IOException e) {
566 throw new IOException("Failed to create the source files.");
567 }
568 setFileHandle(handle);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530569 addLeavesAttributes();
570 addLeafListAttributes();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530571 }
572
573 @Override
574 public void generateJavaCodeExit() throws IOException {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530575 getFileHandle().close();
576 return;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530577 }
578
579 /**
580 * Adds leaf attributes in generated files.
581 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530582 private void addLeavesAttributes() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530583
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530584 List<YangLeaf> leaves = getListOfLeaf();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530585 if (leaves != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530586 for (YangLeaf leaf : leaves) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530587 getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
588 }
589 }
590 }
591
592 /**
593 * Adds leaf list's attributes in generated files.
594 */
595 private void addLeafListAttributes() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530596 List<YangLeafList> leavesList = getListOfLeafList();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530597 if (leavesList != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530598 for (YangLeafList leafList : leavesList) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530599 getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
600 }
601 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530602 }
603
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530604}