...
List is also like container that can hold many nodes by logically grouping. The only difference is, list can have multiple instances whereas container has only one instance.
Java mapping
In java, list acts as a class which can hold information contained within. A class of the list is formed only when list has nodes in it. In addition to that, list's parent holder will have list information by creating the list information in java List so that many instances of the class can be stored in it.
...
In the below example the list holder is also a list with the same name. In such cases the complete path is defined for attribute in parent, in order to make sure that they are not referring to themselves. This case is same for any class generating YANG constructs.
Example
| Code Block | ||
|---|---|---|
| ||
File : acme-system.yang
module acme-system {
namespace "http://acme.example.com/system";
prefix "acme";
organization "ACME Inc.";
contact "joe@acme.example.com";
description
"The module for entities implementing the ACME system.";
revision 2007-06-09 {
description "Initial revision.";
}
list login {
key "name";
list login {
key "name";
leaf name {
type string;
}
leaf full-name {
type string;
}
leaf class {
type string;
}
}
leaf name {
type string;
}
}
.
.
.
} |
| Code Block | ||
|---|---|---|
| ||
File : Login.java
public interface Login extends AugmentationHolder {
String name();
String fullName();
String addThisBeforeClass();
interface LoginBuilder {
String name();
String fullName();
String addThisBeforeClass();
LoginBuilder name(String name);
LoginBuilder fullName(String fullName);
LoginBuilder addThisBeforeClass(String addThisBeforeClass);
Login build();
}
}
File : UserBuilderLoginBuilder.java
public class LoginBuilder implements Login.LoginBuilder {
private String name;
private String fullName;
private String addThisBeforeClass;
@Override
public String name() {
return name;
}
@Override
public String fullName() {
return fullName;
}
@Override
public String addThisBeforeClass() {
return addThisBeforeClass;
}
@Override
public LoginBuilder name(String name) {
this.name = name;
return this;
}
@Override
public LoginBuilder fullName(String fullName) {
this.fullName = fullName;
return this;
}
@Override
public LoginBuilder addThisBeforeClass(String addThisBeforeClass) {
this.addThisBeforeClass = addThisBeforeClass;
return this;
}
@Override
public Login build() {
return new LoginImpl(this);
}
public LoginBuilder() {
}
public final class LoginImpl implements Login {
private List<AugmentedInfo> augmentedInfoList = new ArrayList<>();
private String name;
private String fullName;
private String addThisBeforeClass;
@Override
public String name() {
return name;
}
@Override
public String fullName() {
return fullName;
}
@Override
public String addThisBeforeClass() {
return addThisBeforeClass;
}
@Override
public int hashCode() {
return Objects.hash(name, fullName, addThisBeforeClass);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof LoginImpl) {
LoginImpl other = (LoginImpl) obj;
return
Objects.equals(name, other.name) &&
Objects.equals(fullName, other.fullName) &&
Objects.equals(addThisBeforeClass, other.addThisBeforeClass);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("name", name)
.add("fullName", fullName)
.add("addThisBeforeClass", addThisBeforeClass)
.toString();
}
public LoginImpl(LoginBuilder builderObject) {
this.name = builderObject.name();
this.fullName = builderObject.fullName();
this.addThisBeforeClass = builderObject.addThisBeforeClass();
}
@Override
public void addAugmentation(AugmentedInfo value) {
getAugmentedInfoList().add(value);
}
@Override
public List<AugmentedInfo> getAugmentedInfoList() {
return augmentedInfoList;
}
@Override
public void removeAugmentation() {
getAugmentedInfoList().clear();
}
}
}
Generated java files for list's holder(another list):
File : UserLogin.java
public interface Login extends AugmentationHolder {
String name();
List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609.acmesystem.login.Login> login();
interface LoginBuilder {
String name();
List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609.acmesystem.login.Login> login();
LoginBuilder name(String name);
LoginBuilder login(List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609.acmesystem
.login
.Login> login);
Login build();
}
}
File : UserBuilderLoginBuilder.java
public class LoginBuilder implements Login.LoginBuilder {
private String name;
private List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609.acmesystem.login.Login> login;
@Override
public String name() {
return name;
}
@Override
public List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609.acmesystem.login
.Login> login() {
return login;
}
@Override
public LoginBuilder name(String name) {
this.name = name;
return this;
}
@Override
public LoginBuilder login(List<org.onosproject.yang.gen.v1.http.acme.example.com.system.rev20070609
.acmesystem
.login.Login> login) {
this.login = login;
return this;
}
.
.
.
public final class LoginImpl implements Login {
.
.
.
}
} |
...
During YANG to java conversion, the nodes under grouping are completely copied, wherever uses is present. Later the java file generation takes place according to the new yang nodes added. Grouping and uses itself will not have any impact in java file generation.
Example
| Code Block | ||
|---|---|---|
| ||
File : Test.yang
module Test {
.
.
.
grouping Percentage {
leaf mark{
type String;
}
}
container classroom {
leaf student{
type String;
}
uses Percentage;
}
} |
...
Interface file which extends choice marker interface
Builder class which implements the builder interface and impl class which implements the interface
- Impl class includes overridden methods, hashcode, equals, toString methods.
Example
| Code Block | ||
|---|---|---|
| ||
File : link.yang
module link {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container link {
choice interfaceType {
case ethernerType {
leaf ethernet { type string; }
}
case p2pType {
leaf p2p { type string; }
}
}
}
} |
...
When input is present and no output statement.
When input has only one leaf/leaf-list. In this case method signature will have return type as “void” and member attribute with type of leaf.
When input has only one YANG construct. In this case method signature will have return type as “void” and a class will be generated for construct which will be used as the type of method’s attribute..
When input has multiple leaf/leaf-list/YANG construct, one class will be generated for input and that will be used as type of method’s attribute and return type will be void.
When no input and output statement is present.
When output has only one leaf/leaf-list. In this case method signature will have return type as type of leaf and no member attribute.
When output has only one YANG construct. In this case method signature will have return type as class which is generated for construct and no method attribute will be generated.
- When output has multiple leaf/leaf-list/YANG construct, one class will be generated for output and that will be used as type of method’s return type.
- When input is present and output statement is present.
When input has only one leaf/leaf-list and output has only leaf/leaf-list. In this case method signature will have return type as type of leaf/list of output and member attribute with type of leaf/leaf-lists type input.
When input has only one YANG construct and output has one YANG construct. In this case method signature will have return type as generated class of outputs construct and a class will be generated for inputs construct which will be used as the type of method’s attribute.
- When input has multiple leaf/leaf-list/YANG construct, and output has leaf/leaf-list/YANG construct one class will be generated for input and that will be used as type of method’s attribute and class will be generated for output which will be used as return type of method.
Example
| Code Block | ||
|---|---|---|
| ||
File: sfc.yang
module Sfc {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
rpc SFP {
input {
leaf port {
type string;
}
}
output {
leaf path {
type string;
}
}
}
} |
...
Manager Extends ListenerRegistry with event and eventListener.
Example
| Code Block | ||
|---|---|---|
| ||
File : ospf.yang
module ospf {
namespace "http://example.com/ospf";
prefix "ospf";
notification test {
leaf event-class {
type string;
}
leaf severity {
type string;
}
}
} |
...
these apis will be providing augmentation functionalities for augmented nodes. These class will be keeping a list of augmentedInfo , which is nothing but a list of augment nodes which are augmenting this node.
Example
| Code Block | ||
|---|---|---|
| ||
File : Test.yang
module Test {
yang-version 1;
namespace "http://huawei.com";
prefix Ant;
description "Interval before a route is declared invalid";
container interface {
leaf ifType {
type string;
}
}
augment "/Test/interface" {
leaf ds0ChannelNumber {
type int16;
}
}
} |
...
YANG | Description | JAVA |
|---|---|---|
binary | Any binary data | To be implemented |
bits | A set of bits or flags | To be implemented |
boolean | "True" or "false" | boolean |
decimal64 | 64-bit signed decimal number | To be implemented |
empty | A leaf that does not have any value | boolean |
enumeration | Enumerated strings | Enum class will be generated |
identityref | A reference to an abstract identity | To be implemented |
instance-identifier | References a data tree node | To be implemented |
int8 | 8-bit signed integer | byte |
int16 | 16-bit signed integer | short |
int32 | 32-bit signed integer | int |
int64 | 64-bit signed integer | long |
leafref | A reference to a leaf instance | To be implemented |
string | Human-readable string | String |
uint8 | 8-bit unsigned integer | short |
uint16 | 16-bit unsigned integer | int |
uint32 | 32-bit unsigned integer | long |
uint64 | 64-bit unsigned integer | BigInteger |
union | Choice of member types | Union class will be generated |
Example
| Code Block | ||
|---|---|---|
| ||
leaf one {
type string;
}
leaf two {
type int32;
}
leaf-list three {
type boolean;
}
leaf-list four {
type int16;
} |
...
For a given typedef node one class file will be generated which will have an attribute with the base type of typedef. There will be a constructor and a getter method, of method and implementation of hashcode, equals and toString methods.
Example
| Code Block | ||
|---|---|---|
| ||
File : test.yang
module test {
yang-version 1;
namespace "http://huawei.com";
prefix "test";
typedef percent {
type uint8;
description "Percentage";
}
leaf completed {
type percent;
}
} |
...
For a given enumeration node one enum file will be generated which will have all the enum as its attributes. There will be a constructor and a getter method for the values.
Example
| Code Block | ||
|---|---|---|
| ||
File: test.yang
module Test {
yang-version 1;
namespace "http://huawei.com";
prefix Ant;
description "Interval before a route is declared invalid";
leaf packetType {
type enumeration {
enum "unbounded";
enum ZERO;
enum two;
enum four;
}
}
} |
...
For a given union node one class file will be generated which will have all the an attribute with the type union is having. There will be a constructor , getter method, of method, fromString, HashCode, equals and ToString methods for the values.
Example
| Code Block | ||
|---|---|---|
| ||
File : test.yang
module test {
yang-version 1;
namespace "http://huawei.com";
prefix "test";
typedef ip-address {
type union {
type int32;
type uint32;
}
}
} |
...