OpenDaylight開發hello-world項目之功能實現


OpenDaylight開發hello-world項目之開發環境搭建

OpenDaylight開發hello-world項目之開發工具安裝

OpenDaylight開發hello-world項目之代碼框架搭建

OpenDaylight開發hello-world項目之功能實現

 

來到最后的功能實現的步驟,功能實現其實很簡單,添加一個yang文件,編譯,添加接口實現代碼,編譯,ok,搞定收工。

 

yang文件編寫

yang文件簡單理解為是定義接口和傳入參數的文件,在hello world項目中定了一個接口叫hello-world,需要傳入的參數是:input標簽中的name變量,類型為string,輸出的信息為outpu標簽中定義的greeting變量,類型也是string。

 

module example {
    yang-version 1;
    namespace "urn:opendaylight:params:xml:ns:yang:example";
    prefix "example";

    revision "2015-01-05" {
        description "Initial revision of example model";
    }

    rpc hello-world {
        input {
            leaf name {
                type string;
            }
        }
        output {
            leaf greeting {
                type string;
            }
        }
    }
}

 

定義好yang文件之后編譯,odl的框架會自動生成很多代碼,包括rpc中函數的定義,文件的引用等。

mvn clean install -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true
[INFO] 
[INFO] --- maven-site-plugin:3.6:attach-descriptor (generate-site) @ example-aggregator ---
[INFO] Attaching 'src/site/site.xml' site descriptor with classifier 'site'.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] ODL :: org.opendaylight.example :: example-api ..... SUCCESS [ 30.916 s]
[INFO] ODL :: org.opendaylight.example :: example-impl .... SUCCESS [ 4.513 s]
[INFO] ODL :: org.opendaylight.example :: example-cli ..... SUCCESS [ 4.574 s]
[INFO] ODL :: org.opendaylight.example :: example-features SUCCESS [ 20.799 s]
[INFO] ODL :: org.opendaylight.example :: example-karaf ... SUCCESS [01:16 min]
[INFO] ODL :: org.opendaylight.example :: example-artifacts SUCCESS [ 5.072 s]
[INFO] ODL :: org.opendaylight.example :: example-it ...... SUCCESS [ 7.690 s]
[INFO] example ............................................ SUCCESS [ 14.771 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:53 min
[INFO] Finished at: 2019-09-12T17:02:48+08:00
[INFO] Final Memory: 209M/483M
[INFO] ------------------------------------------------------------------------

 

hello world 函數

 編譯順利完成之后,首先將該RPC注冊到系統當中去。編寫impl-blueprint.xml文件,注冊ExampleProvider。

 

<?xml version="1.0" encoding="UTF-8"?>
<!-- vi: set et smarttab sw=4 tabstop=4: -->
<!--
Copyright © 2017 worker and others. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v1.0 which accompanies this distribution,
and is available at http://www.eclipse.org/legal/epl-v10.html
-->

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
  xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
  odl:use-default-for-reference-types="true">

  <bean id="provider"
    class="org.opendaylight.example.impl.ExampleProvider">
  </bean>
  
  <odl:rpc-implementation ref="provider"/>

</blueprint>

 

 

注冊好函數之后,最后實現RPC的處理函數。獲取input輸入的內容,然后返回 ‘hello’ + input的內容。

package org.opendaylight.example.impl;

import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.example.rev150105.ExampleService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.example.rev150105.HelloWorldInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.example.rev150105.HelloWorldOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.example.rev150105.HelloWorldOutputBuilder;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import java.util.concurrent.Future;


public class ExampleProvider implements ExampleService {

    @Override
    public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
        HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();
        helloBuilder.setGreeting("Hello " + input.getName());
        return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
    }


}

 

 編譯文件

mvn clean install -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true
[INFO] --- maven-site-plugin:3.6:attach-descriptor (generate-site) @ example-aggregator ---
[INFO] Attaching 'src/site/site.xml' site descriptor with classifier 'site'.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] ODL :: org.opendaylight.example :: example-api ..... SUCCESS [ 35.665 s]
[INFO] ODL :: org.opendaylight.example :: example-impl .... SUCCESS [  5.435 s]
[INFO] ODL :: org.opendaylight.example :: example-cli ..... SUCCESS [  4.453 s]
[INFO] ODL :: org.opendaylight.example :: example-features  SUCCESS [ 26.418 s]
[INFO] ODL :: org.opendaylight.example :: example-karaf ... SUCCESS [01:52 min]
[INFO] ODL :: org.opendaylight.example :: example-artifacts SUCCESS [  5.602 s]
[INFO] ODL :: org.opendaylight.example :: example-it ...... SUCCESS [ 18.424 s]
[INFO] example ............................................ SUCCESS [ 22.318 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:00 min
[INFO] Finished at: 2019-08-04T17:31:48+08:00
[INFO] Final Memory: 207M/483M
[INFO] ------------------------------------------------------------------------

 

啟動ODL

編譯通過之后,在karaf文件夾下啟動ODL,hello-world模塊生效。

 

 

 

 

 查看ODL的api文檔

當啟動ODL之后,可以查看該ODL所有的api文檔,在瀏覽器中輸入 地址:http://localhost:8181/apidoc/explorer/index.html,從中能夠找新鮮出爐的example模塊。

 

 

 

測試接口

在這個api文檔中,可以直接測試接口的可用性,按照輸入的格式將內容輸入,然后try it out!

 

 

 

使用Postman測試接口

postman是最常見的測試工具,輸入restful api的地址,請求方法,body體,認證方式,然后send。 

 

 

 

ODL hello-world模塊的學習重點不是功能實現,而是ODL開發模塊的過程和套路,其中很多深入的內容並沒有介紹全面,主要的注意力還是放在流程上,如何安裝環境,構建框架代碼,編譯,實現功能代碼等。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM