blob: 836ad77c430c9808d8f41dd63c7b2c214ec70fae [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 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053074public class YangModule extends YangNode
75 implements YangLeavesHolder, YangDesc, YangReference, Parsable, CodeGenerator {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053076
77 /**
78 * Name of the module.
79 */
80 private String name;
81
82 /**
83 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053084 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053085 * The "contact" statement provides contact information for the module. The
86 * argument is a string that is used to specify contact information for the
87 * person or persons to whom technical queries concerning this module should
88 * be sent, such as their name, postal address, telephone number, and
89 * electronic mail address.
90 */
91 private String contact;
92
93 /**
94 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +053095 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +053096 * The "description" statement takes as an argument a string that contains a
97 * human-readable textual description of this definition. The text is
98 * provided in a language (or languages) chosen by the module developer; for
99 * the sake of interoperability.
100 */
101 private String description;
102
103 /**
104 * List of YANG modules imported.
105 */
106 private List<YangImport> importList;
107
108 /**
109 * List of YANG sub-modules included.
110 */
111 private List<YangInclude> includeList;
112
113 /**
114 * List of leaves at root level in the module.
115 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530116 private List<YangLeaf> listOfLeaf;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530117
118 /**
119 * List of leaf-lists at root level in the module.
120 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530121 private List<YangLeafList> listOfLeafList;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530122
123 /**
124 * Name space of the module.
125 */
126 private YangNameSpace nameSpace;
127
128 /**
129 * Reference:RFC 6020.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530130 *
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530131 * The "organization" statement defines the party responsible for this
132 * module. The argument is a string that is used to specify a textual
133 * description of the organization(s) under whose auspices this module was
134 * developed.
135 */
136 private String organization;
137
138 /**
139 * Prefix to refer to the objects in module.
140 */
141 private String prefix;
142
143 /**
144 * Reference of the module.
145 */
146 private String reference;
147
148 /**
149 * Revision info of the module.
150 */
151 private YangRevision revision;
152
153 /**
154 * YANG version.
155 */
156 private byte version;
157
158 /**
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530159 * Package of the generated java code.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530160 */
161 private String pkg;
162
163 /**
164 * Cached Java File Handle.
165 */
166 private CachedFileHandle fileHandle;
167
168 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530169 * Create a YANG node of module type.
170 */
171 public YangModule() {
172 super(YangNodeType.MODULE_NODE);
173 }
174
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530175 /* (non-Javadoc)
176 * @see org.onosproject.yangutils.datamodel.YangNode#getName()
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530177 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530178 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530179 public String getName() {
180 return name;
181 }
182
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530183 /* (non-Javadoc)
184 * @see org.onosproject.yangutils.datamodel.YangNode#setName(java.lang.String)
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530185 */
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530186 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530187 public void setName(String moduleName) {
188 name = moduleName;
189 }
190
191 /**
192 * Get the contact details of the module owner.
193 *
194 * @return the contact details of YANG owner.
195 */
196 public String getContact() {
197 return contact;
198 }
199
200 /**
201 * Set the contact details of the module owner.
202 *
203 * @param contact the contact details of YANG owner.
204 */
205 public void setContact(String contact) {
206 this.contact = contact;
207 }
208
209 /**
210 * Get the description of module.
211 *
212 * @return the description of YANG module.
213 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530214 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530215 public String getDescription() {
216 return description;
217 }
218
219 /**
220 * Set the description of module.
221 *
222 * @param description set the description of YANG module.
223 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530224 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530225 public void setDescription(String description) {
226 this.description = description;
227 }
228
229 /**
230 * Get the list of imported modules.
231 *
232 * @return the list of imported modules.
233 */
234 public List<YangImport> getImportList() {
235 return importList;
236 }
237
238 /**
239 * prevent setting the import list from outside.
240 *
241 * @param importList the import list to set.
242 */
243 private void setImportList(List<YangImport> importList) {
244 this.importList = importList;
245 }
246
247 /**
248 * Add the imported module information to the import list.
249 *
250 * @param importedModule module being imported.
251 */
252 public void addImportedInfo(YangImport importedModule) {
253
254 if (getImportList() == null) {
255 setImportList(new LinkedList<YangImport>());
256 }
257
258 getImportList().add(importedModule);
259
260 return;
261 }
262
263 /**
264 * Get the list of included sub modules.
265 *
266 * @return the included list of sub modules.
267 */
268 public List<YangInclude> getIncludeList() {
269 return includeList;
270 }
271
272 /**
273 * Set the list of included sub modules.
274 *
275 * @param includeList the included list to set.
276 */
277 private void setIncludeList(List<YangInclude> includeList) {
278 this.includeList = includeList;
279 }
280
281 /**
282 * Add the included sub module information to the include list.
283 *
284 * @param includeModule submodule being included.
285 */
286 public void addIncludedInfo(YangInclude includeModule) {
287
288 if (getIncludeList() == null) {
289 setIncludeList(new LinkedList<YangInclude>());
290 }
291
292 getIncludeList().add(includeModule);
293 return;
294 }
295
296 /**
297 * Get the list of leaves in module.
298 *
299 * @return the list of leaves.
300 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530301 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530302 public List<YangLeaf> getListOfLeaf() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530303 return listOfLeaf;
304 }
305
306 /**
307 * Set the list of leaf in module.
308 *
309 * @param leafsList the list of leaf to set.
310 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530311 private void setListOfLeaf(List<YangLeaf> leafsList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530312 listOfLeaf = leafsList;
313 }
314
315 /**
316 * Add a leaf in module.
317 *
318 * @param leaf the leaf to be added.
319 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530320 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530321 public void addLeaf(YangLeaf leaf) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530322 if (getListOfLeaf() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530323 setListOfLeaf(new LinkedList<YangLeaf>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530324 }
325
326 getListOfLeaf().add(leaf);
327 }
328
329 /**
330 * Get the list of leaf-list from module.
331 *
332 * @return the list of leaf-list.
333 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530334 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530335 public List<YangLeafList> getListOfLeafList() {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530336 return listOfLeafList;
337 }
338
339 /**
340 * Set the list of leaf-list in module.
341 *
342 * @param listOfLeafList the list of leaf-list to set.
343 */
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530344 private void setListOfLeafList(List<YangLeafList> listOfLeafList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530345 this.listOfLeafList = listOfLeafList;
346 }
347
348 /**
349 * Add a leaf-list in module.
350 *
351 * @param leafList the leaf-list to be added.
352 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530353 @Override
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530354 public void addLeafList(YangLeafList leafList) {
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530355 if (getListOfLeafList() == null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530356 setListOfLeafList(new LinkedList<YangLeafList>());
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530357 }
358
359 getListOfLeafList().add(leafList);
360 }
361
362 /**
363 * Get the name space of module elements.
364 *
365 * @return the nameSpace.
366 */
367 public YangNameSpace getNameSpace() {
368 return nameSpace;
369 }
370
371 /**
372 * Set the name space of module elements.
373 *
374 * @param nameSpace the nameSpace to set.
375 */
376 public void setNameSpace(YangNameSpace nameSpace) {
377 this.nameSpace = nameSpace;
378 }
379
380 /**
381 * Get the modules organization.
382 *
383 * @return the organization.
384 */
385 public String getOrganization() {
386 return organization;
387 }
388
389 /**
390 * Set the modules organization.
391 *
392 * @param org the organization to set.
393 */
394 public void setOrganization(String org) {
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530395 organization = org;
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530396 }
397
398 /**
399 * Get the prefix.
400 *
401 * @return the prefix
402 */
403 public String getPrefix() {
404 return prefix;
405 }
406
407 /**
408 * Set the prefix.
409 *
410 * @param prefix the prefix to set.
411 */
412 public void setPrefix(String prefix) {
413 this.prefix = prefix;
414 }
415
416 /**
417 * Get the textual reference.
418 *
419 * @return the reference.
420 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530421 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530422 public String getReference() {
423 return reference;
424 }
425
426 /**
427 * Set the textual reference.
428 *
429 * @param reference the reference to set.
430 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530431 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530432 public void setReference(String reference) {
433 this.reference = reference;
434 }
435
436 /**
437 * Get the revision.
438 *
439 * @return the revision.
440 */
441 public YangRevision getRevision() {
442 return revision;
443 }
444
445 /**
446 * Set the revision.
447 *
448 * @param revision the revision to set.
449 */
450 public void setRevision(YangRevision revision) {
451 this.revision = revision;
452 }
453
454 /**
455 * Get the version.
456 *
457 * @return the version.
458 */
459 public byte getVersion() {
460 return version;
461 }
462
463 /**
464 * Set the version.
465 *
466 * @param version the version to set.
467 */
468 public void setVersion(byte version) {
469 this.version = version;
470 }
471
472 /**
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530473 * Get the mapped java package.
474 *
475 * @return the java package
476 */
477 @Override
478 public String getPackage() {
479 return pkg;
480 }
481
482 /**
483 * Set the mapped java package.
484 *
485 * @param pcg the package to set
486 */
487 @Override
488 public void setPackage(String pcg) {
489 pkg = pcg;
490 }
491
492 /**
493 * Get the cached file handle.
494 *
495 * @return the fileHandle
496 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530497 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530498 public CachedFileHandle getFileHandle() {
499 return fileHandle;
500 }
501
502 /**
503 * Set the cached file handle.
504 *
505 * @param handle the fileHandle to set
506 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530507 @Override
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530508 public void setFileHandle(CachedFileHandle handle) {
509 fileHandle = handle;
510 }
511
512 /**
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530513 * Returns the type of the parsed data.
514 *
515 * @return returns MODULE_DATA.
516 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530517 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530518 public ParsableDataType getParsableDataType() {
519 return ParsableDataType.MODULE_DATA;
520 }
521
522 /**
523 * Validate the data on entering the corresponding parse tree node.
524 *
525 * @throws DataModelException a violation of data model rules
526 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530527 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530528 public void validateDataOnEntry() throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530529 /*
530 * Module is root in the data model tree, hence there is no entry
531 * validation
532 */
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530533 }
534
535 /**
536 * Validate the data on exiting the corresponding parse tree node.
537 *
538 * @throws DataModelException a violation of data model rules
539 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530540 @Override
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530541 public void validateDataOnExit() throws DataModelException {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530542 /*
543 * TODO: perform symbol linking for the imported or included YANG info.
544 * TODO: perform symbol resolution for referred YANG entities.
545 */
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530546 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530547
548 /**
549 * Generates java code for module.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530550 *
551 * @throws IOException when fails to generate the source files.
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530552 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530553 @Override
554 public void generateJavaCodeEntry() throws IOException {
555 String modPkg = JavaIdentifierSyntax.getRootPackage(getVersion(), getNameSpace().getUri(),
556 getRevision().getRevDate());
557 setPackage(modPkg);
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530558
Bharat saraswal870c56f2016-02-20 21:57:16 +0530559 CachedFileHandle handle = null;
560 try {
561 FileSystemUtil.createPackage(getPackage(), getName());
562 handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
563 } catch (IOException e) {
564 throw new IOException("Failed to create the source files.");
565 }
566 setFileHandle(handle);
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530567 }
568
569 @Override
570 public void generateJavaCodeExit() throws IOException {
571 addLeavesAttributes();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530572 addLeafListAttributes();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530573 getFileHandle().close();
574 return;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530575 }
576
577 /**
578 * Adds leaf attributes in generated files.
579 */
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530580 private void addLeavesAttributes() {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530581
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530582 List<YangLeaf> leaves = getListOfLeaf();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530583 if (leaves != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530584 for (YangLeaf leaf : leaves) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530585 getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
586 }
587 }
588 }
589
590 /**
591 * Adds leaf list's attributes in generated files.
592 */
593 private void addLeafListAttributes() {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530594 List<YangLeafList> leavesList = getListOfLeafList();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530595 if (leavesList != null) {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +0530596 for (YangLeafList leafList : leavesList) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530597 getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
598 }
599 }
Vinod Kumar S67e7be62016-02-11 20:13:28 +0530600 }
601
Vinod Kumar S1ede5ab2016-02-09 20:14:53 +0530602}