Opendaylight 之 HelloWorld

万里长征第一步!!!!
最近因为导师的要求开始研究opendaylight!!!

参考:
http://www.cnblogs.com/FrankZhou2017/p/7236035.html
官网:https://wiki.opendaylight.org/view/OpenDaylight_Controller:MD-SAL:Startup_Project_Archetype

一. 通过mvn命令构建项目
1.执行如下命令(参考官网):

mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype \
-DarchetypeRepository=http://nexus.opendaylight.org/content/repositories/<Snapshot-Type>/ \
-DarchetypeCatalog=remote -DarchetypeVersion=<Archetype-Version>
  • For the current Master (Carbon): Snapshot-Type=opendaylight.snapshot
    Archetype-Version=1.4.0-SNAPSHOT
  • For the Carbon snapshot:Snapshot-Type=opendaylight.release
    Archetype-Version=1.3.0-Carbon
  • For Boron “SR0”:Snapshot-Type=opendaylight.release
    Archetype-Version=1.2.0-Boron
  • For Boron SR1:Snapshot-Type=opendaylight.release
    Archetype-Version=1.2.1-Boron-SR1
  • For Boron SR2:Snapshot-Type=opendaylight.release
    Archetype-Version=1.2.2-Boron-SR2
  • For the Boron snapshot:Snapshot-Type=opendaylight.snapshot
    Archetype-Version=1.2.2-SNAPSHOT

在这里我使用的是boron-sr2,所以使用:Snapshot-Type=opendaylight.release Archetype-Version=1.2.2-Boron-SR2

在此之前根据网上的教程使用其他构建ODL项目的命令,均会出现以下错误,搜索了好久都不知道是因为什么。
这里写图片描述

2.输入交互信息:
这里写图片描述
其中’classPrefix’ Helloworld::
${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}表示将helloworld中的第一个字母h由小写改成大写,否则后续生成的HelloworldProvider.java的第一个字母会是小写的,需要手动的将此文件名和文件中的相关部分由小写改成大写(直接使用小写的文件名,我不知道是否可以,没有试。因为看到一些教程上面没有执行这条信息,生成的是小写的文件名,后面修改后,莫名的变成了大写,觉得有点奇怪,所以特此记录下,直接在交互信息这里修改,省去了后续的麻烦)

3.使用如下命令构建helloworld项目:

ubuntu@ubuntu:~$ cd helloworld/
ubuntu@ubuntu:~/helloworld$ mvn clean install -DskipTests

如下图所示:
这里写图片描述

二.增加 helloword PRC api
1.编辑修改 api/src/main/yang/helloworld.yang,增加内容如下:

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

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

  //新增的部分  
    rpc hello-world {
        input {
            leaf name {
                type string;
            }
        }
        output {
            leaf greeting {
                type string;
            }
        }
    } 
}

2.执行 mvn clean install -DskipTests

3.实现RPC api
在impl/src/main/java/org/opendaylight/helloworld/impl/下创建HelloWorldImpl.java
内容如下:

/*
 * Copyright © 2016 Cisco Systems 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
 */

package org.opendaylight.helloworld.impl;

import java.util.concurrent.Future;

import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev150105.HelloworldService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev150105.HelloWorldInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev150105.HelloWorldOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev150105.HelloWorldOutputBuilder;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;

public class HelloWorldImpl implements HelloworldService {

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

}

4.绑定MD-SAL
(1)修改文件/impl/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml
修改内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- vi: set et smarttab sw=4 tabstop=4: -->
<!--
Copyright © 2016 Yoyodyne, Inc.  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">

  <reference id="dataBroker"
    interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"
    odl:type="default" />

  <reference id="rpcRegistry"
     interface="org.opendaylight.controller.sal.binding.api.RpcProviderRegistry"/>

  <bean id="provider"
    class="org.opendaylight.helloworld.impl.HelloworldProvider"
    init-method="init" destroy-method="close">
    <argument ref="dataBroker" />
    <argument ref="rpcRegistry" />
  </bean>

</blueprint>

(2)修改impl/src/main/java/org/opendaylight/helloworld/impl/文件下的HelloWorldProvider.java

/*
 * Copyright © 2016 Yoyodyne, Inc.  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
 */
package org.opendaylight.helloworld.impl;

import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev150105.HelloworldService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloworldProvider {

    private static final Logger LOG = LoggerFactory.getLogger(HelloworldProvider.class);

    private final DataBroker dataBroker;
    private final RpcProviderRegistry rpcProviderRegistry;
    private RpcRegistration<HelloworldService> serviceRegistration;    

    public HelloworldProvider(final DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) {
        this.dataBroker = dataBroker;
        this.rpcProviderRegistry = rpcProviderRegistry;
    }

    /**
     * Method called when the blueprint container is created.
     */
    public void init() {
        LOG.info("HelloworldProvider Session Initiated");
        serviceRegistration = rpcProviderRegistry.addRpcImplementation(HelloworldService.class, new HelloWorldImpl());
    }

    /**
     * Method called when the blueprint container is destroyed.
     */
    public void close() {
        serviceRegistration.close();
        LOG.info("HelloworldProvider Closed");
    }
}

5.修改完成后,再次执行 mvn clean install -DskipTests
这里可能会出现错误 “Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (check-license) on project hello-impl: You have 1 Checkstyle violation”。
这说明在刚才修改的HelloworldProvide.java和HelloWorldImpl.java中出现错误,可以根据提示,进行相应的修改,一般为语法错误。
但是当出现如下所示错误:显示错误出现在HelloWorldImpl.java中。
这里写图片描述
解决办法是:查看HelloWorldImpl.java中头部注释的格式是否正确,严格按照如下所示格式,在第二行之后需要空格
这里写图片描述

三.验证
1.启动opendaylight
2.在浏览器中打开
http://localhost:8181/apidoc/explorer/index.html
3.选择”hello(2015-01-05)”—”POST /operations/hello:hello-worls”
在输入框中输入{“hello:input”: { “name”:”hello”}}
点击“Try it out”
得到如下结果
这里写图片描述

四.集成到发行版中
使用的opendaylight版本为:0.5.2-Boron-SR2
参考:
http://www.sdnlab.com/16902.html
http://www.cnblogs.com/FrankZhou2017/p/7251168.html
http://www.sdnlab.com/17863.html

1.将hello工程复制到ODL发行版中

cd /opt/odlboron/distribution-karaf-0.5.2-Boron-SR2/system/org/opendaylight
sudo mkdir hello
sudo cp -r ~/.m2/repository/org/opendaylight/hello/* /opt/odlboron/distribution-karaf-0.5.2-Boron-SR2/system/org/opendaylight/hello/

2.启动ODL

cd /opt/odlboron/distribution-karaf-0.5.2-Boron-SR2/
./bin/karaf

3.将Hello 仓库添加到featue list中

opendaylight-user@root>feature:repo-add mvn:org.opendaylight.hello/hello-features/0.1.0-SNAPSHOT/xml/features

其中mvn:org.opendaylight.hello/hello-features/0.1.0-SNAPSHOT/xml/features的具体值在\hello\karaf\target\assembly\etc\org.apache.karaf.features.cfg 文件的最后部分:
这里写图片描述

4.查看hello是否安装并安装
这里写图片描述

如果要将hello移除feature list,可以使用
feature:repo-remove mvn:org.opendaylight.hello/hello-features/0.1.0-SNAPSHOT/xml/features

5.在webUI上查看是否成功将hello集成到ODL中
登录页面http://127.0.0.1:8181/index.html,可在YANG UI/API中找到相应的功能,进行测试

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值