YANG Tools:YANG to Java Mapping

1 YANG to JAVA mapping


Package name


The package name consists from following parts:
  • opendaylight prefix - every package name starts with prefix org.opendaylight.yang.gen.v1 which is hardcoded in BindingGeneratorUtil.moduleNamespaceToPackageName().
  • YANG version - is specified through module substatement yang-version
  • namespace - equals to value of module subelement namespace argument value. The namespace characters : / : - @ $ # ' * + , ; = and character group:/) are replaced with periods (.).
  • revision - concatenation of word rev and value of module subelement revision argument value without leading zeros before month and day (e.g. rev201379)
After the package name is generated then it is checked if it contains any JAVA key words or digits. If it is so then before this tokens is adden underscore (_). List of key words which are prefixed with underscore:

As an example suppose following yang model:
module module {
 
    namespace "urn:2:case#module"
    prefix "sbd";
 
    organization "OPEN DAYLIGHT";
    contact "http://www.whatever.com/";
 
    revision <font color="violet">2013-07-09</font color> {        
    }
}
Package name will be org.opendaylight.yang.gen.v1.urn:2:case#module.rev201379
and after replacing digits and JAVA keywords
org.opendaylight.yang.gen.v1.urn._2._case.module.rev201379




Getters and setters name

In some cases are YANG elements generated as getter or setter methods. This methods are created through class MethodSignatureBuilder
The process for getter is:
  • name of YANG element is converted to JAVA class name style
  • the word get is added as preffix
  • return type of the getter method is set to element's type substatement value

The process for setter is:
  • name of YANG element is converted to JAVA class name style
  • word set is added as preffix
  • input parameter name is set to element's name converted to JAVA parameter style
  • return parameter is set to void



Module

YANG module is converted to JAVA as two JAVA classes. Each of the class is in the separate JAVA file. The names of JAVA files are composed as follows:
<YANG_module_name><Sufix>.java where <sufix> can be Data or Service.
YANGJAVA
module module {
 
    namespace "urn:module";
    prefix "sbd";
 
    organization "OPEN DAYLIGHT";
    contact "http://www.whatever.com/";    
 
    revision 2013-07-09 {
    }
}
ModuleData.java
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
public interface ModuleData {
}
ModuleService.java
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
public interface ModuleService {
}


Data Interface

Data Interface has a mapping similar to container, but contains only top level nodes defined in module.

Service Interface

Service Interface serves to describe RPC contract defined in the module. This RPC contract is defined by rpc statements.


Typedef

YANG typedef statement is mapped to JAVA class. Typedef may contain following substatement:
SubstatementHow si argument mapped to JAVA
typeis mapped as class attribute
descriptonisn't mapped
unitsisn't mapped
defaultisn't mapped


Rpc

Rpc is mapped to JAVA as method of class ModuleService.java.
Rpc's substatement are mapped as follows:
Rpc substatementmapped to JAVA
inputinterface
outputinterface


YANGJAVA
rpc rpc-test1 {
    output {
        leaf lf-output {
            type string;
        }
    }
    input {
        leaf lf-input {
            type string;
        }        
    }
}
ModuleService.java
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
 
import java.util.concurrent.Future;
import org.opendaylight.yangtools.yang.common.RpcResult;
 
public interface ModuleService {
 
    Future<RpcResult<RpcTest1Output>> rpcTest1(RpcTest1Input input);
 
}
RpcTest1Input.java
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
 
public interface RpcTest1Input {
 
    String getLfInput();
 
}
RpcTest1Output.java
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
 
public interface RpcTest1Output {
 
    String getLfOutput();
 



Notification

Notification is mapped to the JAVA interface which extends Notification interface.
Example of notification mapping:
YANGJAVA
 notification notif {
 }
package org.opendaylight.yang.gen.v1.urn.module.rev201379;
 
 
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Notification;
 
public interface Notif extends DataObject, Augmentable<Notif>, Notification {}



https://wiki.opendaylight.org/view/YANG_Tools:YANG_to_Java_Mapping#Package_name



评论

此博客中的热门博文

openflow switch(I)

OpenDaylight架构简介