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。

OpenDaylight开发hello-world项目之功能实现_第1张图片

 

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。

OpenDaylight开发hello-world项目之功能实现_第2张图片

 

"1.0" encoding="UTF-8"?>



"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">

  "provider"
    class="org.opendaylight.example.impl.ExampleProvider">
  
  
  "provider"/>

 

 

注册好函数之后,最后实现RPC的处理函数。获取input输入的内容,然后返回 ‘hello’ + input的内容。

OpenDaylight开发hello-world项目之功能实现_第3张图片

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> 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模块生效。

 

OpenDaylight开发hello-world项目之功能实现_第4张图片

 

 

 

 查看ODL的api文档

当启动ODL之后,可以查看该ODL所有的api文档,在浏览器中输入 地址:http://localhost:8181/apidoc/explorer/index.html,从中能够找新鲜出炉的example模块。

 

 OpenDaylight开发hello-world项目之功能实现_第5张图片

 

测试接口

在这个api文档中,可以直接测试接口的可用性,按照输入的格式将内容输入,然后try it out!

OpenDaylight开发hello-world项目之功能实现_第6张图片

 

 

 

使用Postman测试接口

postman是最常见的测试工具,输入restful api的地址,请求方法,body体,认证方式,然后send。 

 OpenDaylight开发hello-world项目之功能实现_第7张图片

 

 

ODL hello-world模块的学习重点不是功能实现,而是ODL开发模块的过程和套路,其中很多深入的内容并没有介绍全面,主要的注意力还是放在流程上,如何安装环境,构建框架代码,编译,实现功能代码等。

 

你可能感兴趣的:(OpenDaylight开发hello-world项目之功能实现)