因為研究的關係開始接觸基本的 YANG model

最近 ONOS 釋出了 onos-yang-tools 工具,他是 maven 以及 buck 的一個 plugin,可以在編譯階段將 YANG model 轉換成 Java API。

轉換的詳細規則以及英文版說明如下連結:

https://wiki.onosproject.org/display/ONOS/YANG+utils

 

透過 YANG Model 撰寫 Service

若要透過 YANG model 產生一套 API 並使用 API 來撰寫 Service,我們需要:

  1. YANG model code
  2. ONOS YANG tools
  3. ONOS maven/buck bundle (project)

製作方法如下:

透過 onos-create-app 產生出一個簡單的 maven project

$ onos-create-app
...
Define value for property 'groupId': : tw.takeshi
Define value for property 'artifactId': : test-service
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': tw.takeshi: :
Confirm properties configuration:
groupId: tw.takeshi
artifactId: test-service
version: 1.0-SNAPSHOT
package: tw.takeshi
Y: : y
...
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25.890 s
[INFO] Finished at: 2016-10-27T17:10:21+08:00
[INFO] Final Memory: 14M/177M
[INFO] ------------------------------------------------------------------------

 

更改 pom.xml

在 pom 裡面新增 yang 的 plugin 以及設定,這一塊寫在 <plugins></plugins>

<plugin>
    <groupId>org.onosproject</groupId>
    <artifactId>onos-yang-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <configuration>
                <classFileDir>src/main/java</classFileDir>
            </configuration>
            <goals>
                <goal>yang2java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <yangFilesDir>src/main/yang</yangFilesDir>
        <classFileDir>src/main/java</classFileDir>
    </configuration>
</plugin>

<configuration> 中,設定了 YANG 檔案放置的位置以及產生的 java 檔案應該要在哪邊,我們直接設定於 src/main 中即可。

編輯 YANG 檔案

假設我們現在要撰寫一個簡單的 Device Service,我們可以定義下面的 YANG Model,將這個檔案放到 src/main/yang 中

module device-service {

    yang-version 1;
    namespace "urn:tw:takeshi:device-service";
    prefix nb;

    revision 2016-10-27 {
        description "ONOS device service model";
    }

    list device {
        key device-id;

        leaf device-id {
            type uint64;
        }

        leaf of-version {
            type string;
        }

        list port {
            key port-no;

            leaf port-no {
                type uint8;
            }

            leaf port-mac {
                type string;
            }
        }
    }
}

最後透過 mvn clean install 即可產生出 Java Code

.
├── org
    └── onosproject
        └── yang
            └── gen
                └── v1
                    └── urn
                        └── tw
                            └── takeshi
                                └── device
                                    └── service
                                        └── rev20161027
                                            ├── DeviceService.java
                                            ├── DeviceServiceOpParam.java
                                            ├── deviceservice
                                            │   ├── DefaultDevice.java
                                            │   ├── Device.java
                                            │   ├── device
                                            │   │   ├── DefaultPort.java
                                            │   │   ├── Port.java
                                            │   │   └── package-info.java
                                            │   └── package-info.java
                                            └── package-info.java

 

Share Your Thought