Replace supported admonitions with rst directives
[docs.git] / docs / developer-guide / yang-tools.rst
1 YANG Tools
2 ==========
3
4 Overview
5 --------
6
7 YANG Tools is set of libraries and tooling providing support for use
8 `YANG <https://tools.ietf.org/html/rfc6020>`__ for Java (or other
9 JVM-based language) projects and applications.
10
11 YANG Tools provides following features in OpenDaylight:
12
13 -  parsing of YANG sources and semantic inference of relationship across
14    YANG models as defined in
15    `RFC6020 <https://tools.ietf.org/html/rfc6020>`__
16
17 -  representation of YANG-modeled data in Java
18
19    -  **Normalized Node** representation - DOM-like tree model, which
20       uses conceptual meta-model more tailored to YANG and OpenDaylight
21       use-cases than a standard XML DOM model allows for.
22
23    -  **Java Binding** - concrete data model and classes generated from
24       YANG models, designed to provide compile-time safety when working
25       with YANG-modeled data.
26
27 -  serialization / deserialization of YANG-modeled data driven by YANG
28    models
29
30    -  XML - as defined in
31       `RFC6020 <https://tools.ietf.org/html/rfc6020>`__
32
33    -  JSON - as defined in
34       `draft-lhotka-netmod-yang-json-01 <https://tools.ietf.org/html/rfc6020>`__
35
36    -  Java Binding to Normalized Node and vice-versa
37
38 -  Integration of YANG model parsing into Maven build lifecycle and
39    support for third-party generators processing YANG models.
40
41 YANG Tools project consists of following logical subsystems:
42
43 -  **Commons** - Set of general purpose code, which is not specific to
44    YANG, but is also useful outside YANG Tools implementation.
45
46 -  **YANG Model and Parser** - YANG semantic model and lexical and
47    semantic parser of YANG models, which creates in-memory
48    cross-referenced represenation of YANG models, which is used by other
49    components to determine their behaviour based on the model.
50
51 -  **YANG Data** - Definition of Normalized Node APIs and Data Tree
52    APIs, reference implementation of these APIs and implementation of
53    XML and JSON codecs for Normalized Nodes.
54
55 -  **YANG Maven Plugin** - Maven plugin which integrates YANG parser
56    into Maven build lifecycle and provides code-generation framework for
57    components, which wants to generate code or other artefacts based on
58    YANG model.
59
60 -  **YANG Java Binding** - Mapping of YANG model to generated Java APIs.
61    Java Binding also references to set of compile-time and runtime
62    components which implements this mapping, provides generation of
63    classes and APIs based on YANG models and integrate these Java
64    Binding objects with **YANG Data** APIs and components.
65
66    -  **Models** - Set of **IETF** and **YANG Tools** models, with
67       generated Java Bindings so they could be simply consumed outside
68       of **YANG Tools**.
69
70 YANG Java Binding: Mapping rules
71 --------------------------------
72
73 This chapter covers the details of mapping YANG to Java.
74
75 .. note::
76
77     The following source code examples does not show canonical generated
78     code, but rather illustrative example. Generated classes and
79     interfaces may differ from this examples, but APIs are preserved.
80
81 General conversion rules
82 ~~~~~~~~~~~~~~~~~~~~~~~~
83
84 Package names of YANG models
85 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
87 | The package name consists of the following parts:
88
89 -  **Opendaylight prefix** - Specifies the opendaylight prefix. Every
90    package name starts with the prefix ``org.opendaylight.yang.gen.v``.
91
92 -  **Java Binding version** - Specifies the YANG Java Binding version.
93    Curent Binding version is ``1``.
94
95 -  **Namespace** - Specified by the value of ``namespace`` substatement.
96    URI is converted to package name structure.
97
98 -  **Revision** - Specifies the concatenation of word ``rev`` and value
99    of ``module`` substatements ``revision`` argument value without
100    leading zeros before month and day. For example: ``rev201379``
101
102 After the package name is generated, we check if it contains any Java
103 keywords or starts with a digit. If so, then we add an underscore before
104 the offending token.
105
106 The following is a list of keywords which are prefixed with underscore:
107
108 abstract, assert, boolean, break, byte, case, catch, char, class, const,
109 continue, default, double, do, else, enum, extends, false, final,
110 finally, float, for, goto, if, implements, import, instanceof, int,
111 interface, long, native, new, null, package, private, protected, public,
112 return, short, static, strictfp, super, switch, synchronized, this,
113 throw, throws, transient, true, try, void, volatile, while
114
115 As an example suppose following yang model:
116
117 .. code:: yang
118
119     module module {
120         namespace "urn:2:case#module";
121         prefix "sbd";
122         organization "OPEN DAYLIGHT";
123         contact "http://www.example.com/";
124         revision 2013-07-09 {
125         }
126     }
127
128 After applying rules (replacing digits and Java keywords) the resulting
129 package name is
130 ``org.opendaylight.yang.gen.v1.urn._2._case.module.rev201379``
131
132 Additional Packages
133 ^^^^^^^^^^^^^^^^^^^
134
135 In cases when YANG statement contain some of specific YANG statements
136 additional packages are generated to designate this containment. Table
137 below provides details of parent statement and nested statements, which
138 yields additional package generation:
139
140 +--------------------------------------+--------------------------------------+
141 | Parent statement                     | Substatement                         |
142 +======================================+======================================+
143 | ``list``                             | list, container, choice              |
144 +--------------------------------------+--------------------------------------+
145 | ``container``                        | list, container, choice              |
146 +--------------------------------------+--------------------------------------+
147 | ``choice``                           | leaf, list, leaf-list, container,    |
148 |                                      | case                                 |
149 +--------------------------------------+--------------------------------------+
150 | ``case``                             | list, container, choice              |
151 +--------------------------------------+--------------------------------------+
152 | rpc ``input`` or ``output``          | list, container, (choice isn’t       |
153 |                                      | supported)                           |
154 +--------------------------------------+--------------------------------------+
155 | ``notification``                     | list, container, (choice isn’t       |
156 |                                      | supported)                           |
157 +--------------------------------------+--------------------------------------+
158 | ``augment``                          | list, container, choice, case        |
159 +--------------------------------------+--------------------------------------+
160
161 Substatements are not only mapped to Java setter methods in the
162 interface representing the parent statement, but they also generate
163 packages with names consisting of the parent statement package name with
164 the parent statement name appended.
165
166 For example, this YANG model considers the container statement ``cont``
167 as the direct substatement of the module.
168
169 .. code:: yang
170
171     container cont {
172       container cont-inner {
173       }
174       list outter-list {
175         list list-in-list {
176         }
177       }
178     }
179
180 Container ``cont`` is the parent statement for the substatements
181 ``cont-inner`` and ``outter-list``. ``list outter-list`` is the parent
182 statement for substatement ``list-in-list``.
183
184 | Java code is generated in the following structure:
185
186 -  ``org.opendaylight.yang.gen.v1.urn.module.rev201379`` - package
187    contains direct substatements of module statement
188
189    -  ``Cont.java``
190
191 -  ``org.opendaylight.yang.gen.v1.urn.module.rev201379.cont`` - package
192    contains substatements of ``cont`` container statement
193
194    -  ``ContInner.java`` - interface representing container
195       ``cont-inner``
196
197    -  ``OutterList.java`` - interface representing list ``outer-list``
198
199 -  ``org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.outter.list``
200    - package contains substatements of outter-list list element
201
202    -  ``ListInList.java``
203
204 Class and interface names
205 ^^^^^^^^^^^^^^^^^^^^^^^^^
206
207 Some YANG statements are mapped to Java classes and interfaces. The name
208 of YANG element may contain various characters which aren’t permitted in
209 Java class names. Firstly whitespaces are trimmed from YANG name. Next
210 the characters space, -, \` are deleted and the subsequent letter is
211 capitalized. At the end, first letter is capitalized.
212
213 For example, ``example-name_ without_capitalization`` would map to
214 ``ExampleNameWithoutCapitalization``.
215
216 Getter and setter names
217 ^^^^^^^^^^^^^^^^^^^^^^^
218
219 In some cases, YANG statements are converted to getter and/or setter
220 methods. The process for getter is:
221
222 1. the name of YANG statement is converted to Java class name style as
223    `explained above <#_class_and_interface_names>`__.
224
225 2. the word ``get`` is added as prefix, if resulting type is
226    ``Boolean``, the name is prefixed with ``is`` prefix instead of
227    ``get``.
228
229 3. the return type of the getter method is set to Java type representing
230    substatement
231
232 The process for setter is:
233
234 1. the name of YANG statement is converted to Java class name style as
235    `explained above <#_class_and_interface_names>`__.
236
237 2. the word ``set`` is added as prefix
238
239 3. the input parameter name is set to element’s name converted to Java
240    parameter style
241
242 4. the return parameter is set to builder type
243
244 Statement specific mapping
245 ~~~~~~~~~~~~~~~~~~~~~~~~~~
246
247 module statement
248 ^^^^^^^^^^^^^^^^
249
250 YANG ``module`` statement is converted to Java as two Java classes. Each
251 of the classes is in the separate Java file. The names of Java files are
252 composed as follows: ``<module name><suffix>.java`` where ``<suffix>``
253 is either data or service.
254
255 Data Interface
256 ''''''''''''''
257
258 Data Interface has a mapping similar to container, but contains only top
259 level nodes defined in module.
260
261 Data interface serves only as marker interface for type-safe APIs of
262 ``InstanceIdentifier``.
263
264 Service Interface
265 '''''''''''''''''
266
267 Service Interface serves to describe RPC contract defined in the module.
268 This RPC contract is defined by ``rpc`` statements.
269
270 RPC implementation usually implement this interface and users of the
271 RPCs use this interface to invoke RPCs.
272
273 container statement
274 ^^^^^^^^^^^^^^^^^^^
275
276 YANG containers are mapped to Java interfaces which extend the Java
277 DataObject and Augmentable<container-interface>, where
278 container-interface is the name of the mapped interface.
279
280 For example, the following YANG:
281
282 **YANG model.**
283
284 .. code:: yang
285
286     container cont {
287
288     }
289
290 is converted into this Java:
291
292 **Cont.java.**
293
294 .. code:: java
295
296     public interface Cont extends ChildOf<...>, Augmentable<Cont> {
297     }
298
299 Leaf statement
300 ^^^^^^^^^^^^^^
301
302 Each leaf has to contain at least one type substatement. The leaf is
303 mapped to getter method of parent statement with return type equal to
304 type substatement value.
305
306 For example, the following YANG:
307
308 **YANG model.**
309
310 .. code:: yang
311
312     container cont {
313       leaf lf {
314         type string;
315       }
316     }
317
318 is converted into this Java:
319
320 **Cont.java.**
321
322 .. code:: java
323
324     public interface Cont extends DataObject, Augmentable<Cont> {
325         String getLf(); 
326     }
327
328 -  Represents ``leaf lf``
329
330 leaf-list statement
331 ^^^^^^^^^^^^^^^^^^^
332
333 Each leaf-list has to contain one type substatement. The leaf-list is
334 mapped to getter method of parent statement with return type equal to
335 List of type substatement value.
336
337 For example, the following YANG:
338
339 **YANG model.**
340
341 .. code:: yang
342
343     container cont {
344         leaf-list lf-lst {
345             type string;
346         }
347     }
348
349 is converted into this Java:
350
351 **Cont.java.**
352
353 .. code:: java
354
355     public interface Cont extends DataObject, Augmentable<Cont> {
356         List<String> getLfLst();
357     }
358
359 list statement
360 ^^^^^^^^^^^^^^
361
362 ``list`` statements are mapped to Java interfaces and a getter method is
363 generated in the interface associated with it’s parent statement. The
364 return type of getter the method is a Java List of objects implementing
365 the interface generated corresponding to the ``list statement.
366 Mapping of `list`` substatement to Java:
367
368 For example, the following YANG:
369
370 **YANG model.**
371
372 .. code:: yang
373
374     container cont {
375       list outter-list {
376         key "leaf-in-list";
377         leaf number {
378           type uint64;
379         }
380       }
381     }
382
383 The list statement ``example-list`` is mapped to the Java interface
384 ``ExampleList`` and the ``Cont`` interface (parent of ``ExampleList``)
385 contains getter method with return type ``List<ExampleList>``. The
386 presence of a ``key`` statement, triggers generation of
387 ``ExampleListKey``, which may be used to identify item in list.
388
389 The end result is this Java:
390
391 **OutterList.java.**
392
393 .. code:: java
394
395     package org.opendaylight.yang.gen.v1.urn.module.rev201379.cont;
396
397     import org.opendaylight.yangtools.yang.binding.DataObject;
398     import org.opendaylight.yangtools.yang.binding.Augmentable;
399     import Java.util.List;
400     import org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.outter.list.ListInList;
401
402     public interface OutterList extends DataObject, Augmentable<OutterList> {
403
404         List<String> getLeafListInList();
405
406         List<ListInList> getListInList();
407
408         /*
409         Returns Primary Key of Yang List Type
410         */
411         OutterListKey getOutterListKey();
412
413     }
414
415 **OutterListKey.java.**
416
417 .. code:: java
418
419     package org.opendaylight.yang.gen.v1.urn.module.rev201379.cont;
420
421     import org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.OutterListKey;
422     import Java.math.BigInteger;
423
424     public class OutterListKey {
425
426         private BigInteger _leafInList;
427
428         public OutterListKey(BigInteger _leafInList) {
429             super();
430             this_leafInList = _leafInList;
431         }
432
433         public BigInteger getLeafInList() {
434             return _leafInList;
435         }
436
437         @Override
438         public int hashCode() {
439             final int prime = 31;
440             int result = 1;
441             result = prime * result + ((_leafInList == null) ? 0 : _leafInList.hashCode());
442             return result;
443         }
444
445         @Override
446         public boolean equals(Object obj) {
447             if (this == obj) {
448                 return true;
449             }
450             if (obj == null) {
451                 return false;
452             }
453             if (getClass() != obj.getClass()) {
454                 return false;
455             }
456             OutterListKey other = (OutterListKey) obj;
457             if (_leafInList == null) {
458                 if (other._LeafInList != null) {
459                     return false;
460                 }
461             } else if(!_leafInList.equals(other._leafInList)) {
462                 return false;
463             }
464             return true;
465         }
466
467         @Override
468         public String toString() {
469             StringBuilder builder = new StringBuilder();
470             builder.append("OutterListKey [_leafInList=");
471             builder.append(_leafInList);
472             builder.append("]");
473             return builder.toString();
474         }
475     }
476
477 choice and case statements
478 ^^^^^^^^^^^^^^^^^^^^^^^^^^
479
480 A ``choice`` element is mapped in mostly the same way a ``list`` element
481 is. The ``choice`` element is mapped to and interface (marker interface)
482 and a new getter method with the return type of a Java ``List`` of this
483 marker interfaces is added to the interface corresponding to the parent
484 statement. Any ``case`` substatements are mapped to Java interfaces
485 which extend the marker interface.
486
487 For example, the following YANG:
488
489 **YANG model.**
490
491 .. code:: yang
492
493     container cont {
494         choice example-choice {
495             case foo-case {
496               leaf foo {
497                 type string;
498               }
499             }
500             case bar-case {
501                 leaf bar {
502                   type string;
503                 }
504             }
505         }
506     }
507
508 is converted into this Java:
509
510 **Cont.java.**
511
512 .. code:: java
513
514     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
515
516     import org.opendaylight.yangtools.yang.binding.DataObject;
517     import org.opendaylight.yangtools.yang.binding.Augmentable;
518     import org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.ChoiceTest;
519
520     public interface Cont extends DataObject, Augmentable<Cont> {
521
522         ExampleChoice getExampleChoice();
523
524     }
525
526 **ExampleChoice.java.**
527
528 .. code:: java
529
530     package org.opendaylight.yang.gen.v1.urn.module.rev201379.cont;
531
532     import org.opendaylight.yangtools.yang.binding.DataObject;
533
534     public interface ExampleChoice extends DataContainer {
535     }
536
537 **FooCase.java.**
538
539 .. code:: java
540
541     package org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.example.choice;
542
543     import org.opendaylight.yangtools.yang.binding.DataObject;
544     import org.opendaylight.yangtools.yang.binding.Augmentable;
545     import org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.ChoiceTest;
546
547     public interface FooCase extends ExampleChoice, DataObject, Augmentable<FooCase> {
548
549         String getFoo();
550
551     }
552
553 **BarCase.java.**
554
555 .. code:: java
556
557     package org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.example.choice;
558
559     import org.opendaylight.yangtools.yang.binding.DataObject;
560     import org.opendaylight.yangtools.yang.binding.Augmentable;
561     import org.opendaylight.yang.gen.v1.urn.module.rev201379.cont.ChoiceTest;
562
563     public interface BarCase extends ExampleChoice, DataObject, Augmentable<BarCase> {
564
565         String getBar();
566
567     }
568
569 grouping and uses statements
570 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
571
572 ``grouping`s are mapped to Java interfaces. `uses`` statements in some
573 element (using of concrete grouping) are mapped as extension of
574 interface for this element with the interface which represents grouping.
575
576 For example, the following YANG:
577
578 **YANG Model.**
579
580 .. code:: yang
581
582     grouping grp {
583       leaf foo {
584         type string;
585       }
586     }
587
588     container cont {
589         uses grp;
590     }
591
592 is converted into this Java:
593
594 **Grp.java.**
595
596 .. code:: java
597
598     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
599
600     import org.opendaylight.yangtools.yang.binding.DataObject;
601
602     public interface Grp extends DataObject {
603
604         String getFoo();
605
606     }
607
608 **Cont.java.**
609
610 .. code:: java
611
612     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
613
614     import org.opendaylight.yangtools.yang.binding.DataObject;
615     import org.opendaylight.yangtools.yang.binding.Augmentable;
616
617     public interface Cont extends DataObject, Augmentable<Cont>, Grp {
618     }
619
620 rpc, input and output statements
621 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
622
623 An ``rpc`` statement is mapped to Java as method of class
624 ``ModuleService.java``. Any substatements of an ``rpc`` are mapped as
625 follows:
626
627 +--------------------------------------+--------------------------------------+
628 | Rpc Substatement                     | Mapping                              |
629 +======================================+======================================+
630 | input                                | presence of input statement triggers |
631 |                                      | generation of interface              |
632 +--------------------------------------+--------------------------------------+
633 | output                               | presence of output statement         |
634 |                                      | triggers generation of interface     |
635 +--------------------------------------+--------------------------------------+
636
637 For example, the following YANG:
638
639 **YANG model.**
640
641 .. code:: yang
642
643     rpc rpc-test1 {
644         output {
645             leaf lf-output {
646                 type string;
647             }
648         }
649         input {
650             leaf lf-input {
651                 type string;
652             }
653         }
654     }
655
656 is converted into this Java:
657
658 **ModuleService.java.**
659
660 .. code:: java
661
662     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
663
664     import Java.util.concurrent.Future;
665     import org.opendaylight.yangtools.yang.common.RpcResult;
666
667     public interface ModuleService {
668
669         Future<RpcResult<RpcTest1Output>> rpcTest1(RpcTest1Input input);
670
671     }
672
673 **RpcTest1Input.java.**
674
675 .. code:: java
676
677     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
678
679     public interface RpcTest1Input {
680
681         String getLfInput();
682
683     }
684
685 **RpcTest1Output.java.**
686
687 .. code:: java
688
689     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
690
691     public interface RpcTest1Output {
692
693         String getLfOutput();
694
695     }
696
697 notification statement
698 ^^^^^^^^^^^^^^^^^^^^^^
699
700 ``notification`` statements are mapped to Java interfaces which extend
701 the Notification interface.
702
703 For example, the following YANG:
704
705 **YANG model.**
706
707 .. code:: yang
708
709     notification notif {
710         }
711
712 is converted into this Java:
713
714 **Notif.java.**
715
716 .. code:: java
717
718     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
719
720
721     import org.opendaylight.yangtools.yang.binding.DataObject;
722     import org.opendaylight.yangtools.yang.binding.Augmentable;
723     import org.opendaylight.yangtools.yang.binding.Notification;
724
725     public interface Notif extends DataObject, Augmentable<Notif>, Notification {
726     }
727
728 augment statement
729 ~~~~~~~~~~~~~~~~~
730
731 ``augment`` statements are mapped to Java interfaces. The interface
732 starts with the same name as the name of augmented interface with a
733 suffix corresponding to the order number of augmenting interface. The
734 augmenting interface also extends ``Augmentation<>`` with actual type
735 parameter equal to augmented interface.
736
737 For example, the following YANG:
738
739 **YANG Model.**
740
741 .. code:: yang
742
743     container cont {
744     }
745
746     augment "/cont" {
747       leaf additional-value {
748         type string;
749       }
750     }
751
752 is converted into this Java:
753
754 **Cont.java.**
755
756 .. code:: java
757
758     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
759
760     import org.opendaylight.yangtools.yang.binding.DataObject;
761     import org.opendaylight.yangtools.yang.binding.Augmentable;
762
763     public interface Cont extends DataObject, Augmentable<Cont> {
764
765     }
766
767 **Cont1.java.**
768
769 .. code:: java
770
771     package org.opendaylight.yang.gen.v1.urn.module.rev201379;
772
773     import org.opendaylight.yangtools.yang.binding.DataObject;
774     import org.opendaylight.yangtools.yang.binding.Augmentation;
775
776     public interface Cont1 extends DataObject, Augmentation<Cont> {
777
778     }
779
780 YANG Type mapping
781 ~~~~~~~~~~~~~~~~~
782
783 typedef statement
784 ^^^^^^^^^^^^^^^^^
785
786 YANG ``typedef`` statements are mapped to Java classes. A ``typedef``
787 may contain following substatements:
788
789 +--------------------------------------+--------------------------------------+
790 | Substatement                         | Behaviour                            |
791 +======================================+======================================+
792 | type                                 | determines wrapped type and how      |
793 |                                      | class will be generated              |
794 +--------------------------------------+--------------------------------------+
795 | descripton                           | Javadoc description                  |
796 +--------------------------------------+--------------------------------------+
797 | units                                | is not mapped                        |
798 +--------------------------------------+--------------------------------------+
799 | default                              | is not mapped                        |
800 +--------------------------------------+--------------------------------------+
801
802 Valid Arguments Type
803 ''''''''''''''''''''
804
805 Simple values of type argument are mapped as follows:
806
807 +--------------------------------------+--------------------------------------+
808 | YANG Type                            | Java type                            |
809 +======================================+======================================+
810 | boolean                              | Boolean                              |
811 +--------------------------------------+--------------------------------------+
812 | empty                                | Boolean                              |
813 +--------------------------------------+--------------------------------------+
814 | int8                                 | Byte                                 |
815 +--------------------------------------+--------------------------------------+
816 | int16                                | Short                                |
817 +--------------------------------------+--------------------------------------+
818 | int32                                | Integer                              |
819 +--------------------------------------+--------------------------------------+
820 | int64                                | Long                                 |
821 +--------------------------------------+--------------------------------------+
822 | string                               | String or, wrapper class (if pattern |
823 |                                      | substatement is specified)           |
824 +--------------------------------------+--------------------------------------+
825 | decimal64                            | Double                               |
826 +--------------------------------------+--------------------------------------+
827 | uint8                                | Short                                |
828 +--------------------------------------+--------------------------------------+
829 | uint16                               | Integer                              |
830 +--------------------------------------+--------------------------------------+
831 | uint32                               | Long                                 |
832 +--------------------------------------+--------------------------------------+
833 | uint64                               | BigInteger                           |
834 +--------------------------------------+--------------------------------------+
835 | binary                               | byte[]                               |
836 +--------------------------------------+--------------------------------------+
837
838 Complex values of type argument are mapped as follows:
839
840 +--------------------------------------+--------------------------------------+
841 | Argument Type                        | Java type                            |
842 +======================================+======================================+
843 | enumeration                          | generated java enum                  |
844 +--------------------------------------+--------------------------------------+
845 | bits                                 | generated class for bits             |
846 +--------------------------------------+--------------------------------------+
847 | leafref                              | same type as referenced leaf         |
848 +--------------------------------------+--------------------------------------+
849 | identityref                          | Class                                |
850 +--------------------------------------+--------------------------------------+
851 | union                                | generated java class                 |
852 +--------------------------------------+--------------------------------------+
853 | instance-identifier                  | ``org.opendaylight.yangtools.yang.bi |
854 |                                      | nding.InstanceIdentifier``           |
855 +--------------------------------------+--------------------------------------+
856
857 Enumeration Substatement Enum
858 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
859
860 The YANG ``enumeration`` type has to contain some ``enum``
861 substatements. An ``enumeration`` is mapped as Java enum type
862 (standalone class) and every YANG enum substatements is mapped to Java
863 enum’s predefined values.
864
865 An ``enum`` statement can have following substatements:
866
867 +--------------------------------------+--------------------------------------+
868 | Enum’s Substatement                  | Java mapping                         |
869 +======================================+======================================+
870 | description                          | is not mapped in API                 |
871 +--------------------------------------+--------------------------------------+
872 | value                                | mapped as input parameter for every  |
873 |                                      | predefined value of enum             |
874 +--------------------------------------+--------------------------------------+
875
876 For example, the following YANG:
877
878 **YANG model.**
879
880 .. code:: yang
881
882     typedef typedef-enumeration {
883         type enumeration {
884             enum enum1 {
885                 description "enum1 description";
886                 value 18;
887             }
888             enum enum2 {
889                 value 16;
890             }
891             enum enum3 {
892             }
893         }
894     }
895
896 is converted into this Java:
897
898 **TypedefEnumeration.java.**
899
900 .. code:: java
901
902     public enum TypedefEnumeration {
903         Enum1(18),
904         Enum2(16),
905         Enum3(19);
906
907         int value;
908
909         private TypedefEnumeration(int value) {
910             this.value = value;
911         }
912     }
913
914 Bits’s Substatement Bit
915 ^^^^^^^^^^^^^^^^^^^^^^^
916
917 The YANG ``bits`` type has to contain some bit substatements. YANG
918 ``bits`` is mapped to a Java class (standalone class) and every YANG
919 ``bits`` substatements is mapped to a boolean attribute of that class.
920 In addition, the class provides overridden versions of the Object
921 methods ``hashCode``, ``toString``, and ``equals``.
922
923 For example, the following YANG:
924
925 **YANG Model.**
926
927 .. code:: yang
928
929     typedef typedef-bits {
930       type bits {
931         bit first-bit {
932           description "first-bit description";
933             position 15;
934           }
935         bit second-bit;
936       }
937     }
938
939 is converted into this Java:
940
941 **TypedefBits.java.**
942
943 .. code:: java
944
945     public class TypedefBits {
946
947         private Boolean firstBit;
948         private Boolean secondBit;
949
950         public TypedefBits() {
951             super();
952         }
953
954         public Boolean getFirstBit() {
955             return firstBit;
956         }
957
958         public void setFirstBit(Boolean firstBit) {
959             this.firstBit = firstBit;
960         }
961
962         public Boolean getSecondBit() {
963             return secondBit;
964         }
965
966         public void setSecondBit(Boolean secondBit) {
967             this.secondBit = secondBit;
968         }
969
970         @Override
971         public int hashCode() {
972             final int prime = 31;
973             int result = 1;
974             result = prime * result +
975              ((firstBit == null) ? 0 : firstBit.hashCode());
976             result = prime * result +
977              ((secondBit == null) ? 0 : secondBit.hashCode());
978             return result;
979         }
980
981         @Override
982         public boolean equals(Object obj) {
983             if (this == obj) {
984                 return true;
985             }
986             if (obj == null) {
987                 return false;
988             }
989             if (getClass() != obj.getClass()) {
990                 return false;
991             }
992             TypedefBits other = (TypedefBits) obj;
993             if (firstBit == null) {
994                 if (other.firstBit != null) {
995                     return false;
996                 }
997             } else if(!firstBit.equals(other.firstBit)) {
998                 return false;
999             }
1000             if (secondBit == null) {
1001                 if (other.secondBit != null) {
1002                     return false;
1003                 }
1004             } else if(!secondBit.equals(other.secondBit)) {
1005                 return false;
1006             }
1007             return true;
1008         }
1009
1010         @Override
1011         public String toString() {
1012             StringBuilder builder = new StringBuilder();
1013             builder.append("TypedefBits [firstBit=");
1014             builder.append(firstBit);
1015             builder.append(", secondBit=");
1016             builder.append(secondBit);
1017             builder.append("]");
1018             return builder.toString();
1019         }
1020     }
1021
1022 Union’s Substatement Type
1023 ^^^^^^^^^^^^^^^^^^^^^^^^^
1024
1025 If the type of a ``typedef`` is ``union``, it has to contain ``type``
1026 substatements. The ``union typedef`` is mapped to class and its ``type``
1027 substatements are mapped to private class members. Every YANG union
1028 subtype gets its own Java constructor with a parameter which represent
1029 just that one attribute.
1030
1031 For example, the following YANG:
1032
1033 **YANG model.**
1034
1035 .. code:: yang
1036
1037     typedef typedef-union {
1038         type union {
1039             type int32;
1040             type string;
1041         }
1042     }
1043
1044 is converted into this Java:
1045
1046 **TypdefUnion.java.**
1047
1048 .. code:: java
1049
1050     public class TypedefUnion {
1051
1052         private Integer int32;
1053         private String string;
1054
1055         public TypedefUnion(Integer int32) {
1056             super();
1057             this.int32 = int32;
1058         }
1059
1060         public TypedefUnion(String string) {
1061             super();
1062             this.string = string;
1063         }
1064
1065         public Integer getInt32() {
1066             return int32;
1067         }
1068
1069         public String getString() {
1070             return string;
1071         }
1072
1073         @Override
1074         public int hashCode() {
1075             final int prime = 31;
1076             int result = 1;
1077             result = prime * result + ((int32 == null) ? 0 : int32.hashCode());
1078             result = prime * result + ((string == null) ? 0 : string.hashCode());
1079             return result;
1080         }
1081
1082         @Override
1083         public boolean equals(Object obj) {
1084             if (this == obj) {
1085                 return true;
1086             }
1087             if (obj == null) {
1088                 return false;
1089             }
1090             if (getClass() != obj.getClass()) {
1091                 return false;
1092             }
1093             TypedefUnion other = (TypedefUnion) obj;
1094             if (int32 == null) {
1095                 if (other.int32 != null) {
1096                     return false;
1097                 }
1098             } else if(!int32.equals(other.int32)) {
1099                 return false;
1100             }
1101             if (string == null) {
1102                 if (other.string != null) {
1103                     return false;
1104                 }
1105             } else if(!string.equals(other.string)) {
1106                 return false;
1107             }
1108             return true;
1109         }
1110
1111         @Override
1112         public String toString() {
1113             StringBuilder builder = new StringBuilder();
1114             builder.append("TypedefUnion [int32=");
1115             builder.append(int32);
1116             builder.append(", string=");
1117             builder.append(string);
1118             builder.append("]");
1119             return builder.toString();
1120         }
1121     }
1122
1123 String Mapping
1124 ^^^^^^^^^^^^^^
1125
1126 The YANG ``string`` type can contain the substatements ``length`` and
1127 ``pattern`` which are mapped as follows:
1128
1129 +--------------------------------------+--------------------------------------+
1130 | Type substatements                   | Mapping to Java                      |
1131 +======================================+======================================+
1132 | length                               | not mapped                           |
1133 +--------------------------------------+--------------------------------------+
1134 | pattern                              | | . list of string constants = list  |
1135 |                                      |   of patterns                        |
1136 |                                      | | . list of Pattern objects          |
1137 |                                      | | . static initialization block      |
1138 |                                      |   where list of Patterns is          |
1139 |                                      |   initialized from list of string of |
1140 |                                      |   constants                          |
1141 +--------------------------------------+--------------------------------------+
1142
1143 For example, the following YANG:
1144
1145 **YANG model.**
1146
1147 .. code:: yang
1148
1149     typedef typedef-string {
1150         type string {
1151             length 44;
1152             pattern "[a][.]*"
1153         }
1154     }
1155
1156 is converted into this Java:
1157
1158 **TypedefString.java.**
1159
1160 .. code:: java
1161
1162     public class TypedefString {
1163
1164         private static final List<Pattern> patterns = new ArrayList<Pattern>();
1165         public static final List<String> PATTERN`CONSTANTS = Arrays.asList("[a][.]*");
1166
1167         static {
1168             for (String regEx : PATTERN`CONSTANTS) {
1169                 patterns.add(Pattern.compile(regEx));
1170             }
1171         }
1172
1173         private String typedefString;
1174
1175         public TypedefString(String typedefString) {
1176             super();
1177             // Pattern validation
1178             this.typedefString = typedefString;
1179         }
1180
1181         public String getTypedefString() {
1182             return typedefString;
1183         }
1184
1185         @Override
1186         public int hashCode() {
1187             final int prime = 31;
1188             int result = 1;
1189             result = prime * result + ((typedefString == null) ? 0 : typedefString.hashCode());
1190             return result;
1191         }
1192
1193         @Override
1194         public boolean equals(Object obj) {
1195             if (this == obj) {
1196                 return true;
1197             }
1198             if (obj == null) {
1199                 return false;
1200             }
1201             if (getClass() != obj.getClass()) {
1202                 return false;
1203             }
1204             TypedefString other = (TypedefString) obj;
1205             if (typedefString == null) {
1206                 if (other.typedefString != null) {
1207                     return false;
1208                 }
1209             } else if(!typedefString.equals(other.typedefString)) {
1210                 return false;
1211             }
1212             return true;
1213         }
1214
1215         @Override
1216         public String toString() {
1217             StringBuilder builder = new StringBuilder();
1218             builder.append("TypedefString [typedefString=");
1219             builder.append(typedefString);
1220             builder.append("]");
1221             return builder.toString();
1222         }
1223     }
1224
1225 identity statement
1226 ~~~~~~~~~~~~~~~~~~
1227
1228 The purpose of the ``identity`` statement is to define a new globally
1229 unique, abstract, and untyped value.
1230
1231 The ``base`` substatement argument is the name of existing identity from
1232 which the new identity is derived.
1233
1234 Given that, an ``identity`` statement is mapped to Java abstract class
1235 and any ``base`` substatements are mapped as ``extends`` Java keyword.
1236 The identity name is translated to class name.
1237
1238 For example, the following YANG:
1239
1240 **YANG Model.**
1241
1242 .. code:: yang
1243
1244     identity toast-type {
1245
1246     }
1247
1248     identity white-bread {
1249        base toast-type;
1250     }
1251
1252 is converted into this Java:
1253
1254 **ToastType.java.**
1255
1256 .. code:: java
1257
1258     public abstract class ToastType extends BaseIdentity {
1259         protected ToastType() {
1260             super();
1261         }
1262     }
1263
1264 **WhiteBread.java.**
1265
1266 .. code:: java
1267
1268     public abstract class WhiteBread extends ToastType {
1269         protected WhiteBread() {
1270             super();
1271         }
1272     }
1273