目录
所用环境
maven setting设置
参考资料
Win10版(成功)
创建项目
编译启动
修改yang文件
编译api
绑定MD-SAL
实现RPC
再次编译
结果
错误及解决办法
Linux版(未得到预期结果)
创建项目
项目目录含义
编译启动
导入项目
编写RPC
绑定MD-SAL
实现RPC
所用环境
- Win10
- VM14.0
- Ubuntu 18.04
- JDK1.8
- Maven 3.6.3
- postman
maven setting设置
cp -n ~/.m2/settings.xml{,.orig} ; wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/master/settings.xml > ~/.m2/settings.xml
参考资料
HelloWorld官方文档
Win10版(成功)
创建项目
打开cmd,进入IDEA的工作空间目录,运行以下命令
mvn archetype:generate -DarchetypeGroupId=org.opendaylight.archetypes -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeCatalog=remote -DarchetypeVersion=1.2.2

创建项目
填写项目信息
Define value for property 'groupId': 项目组名,小写,例如,org.opendaylight.helloworld
Define value for property 'artifactId': : 项目名,小写,例如,helloworld
Define value for property 'version': Sodium没有让用户选择
Define value for property 'package': 包名,例如,org.opendaylight.helloworld
Define value for property 'classPrefix': 前缀,${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}
Define value for property 'copyright': 版权信息,Copyright (c) 2015 Yoyodyne, Inc.
耗时将近3小时...
项目创建成功
编译启动
根目录下(例如,helloworld/)运行以下命令
mvn clean install -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true
耗时20分钟...
编译成功
修改setenv文件中的环境变量
设置JAVA_HOME
运行成功
修改yang文件
使用IDEA打开项目,打开helloworld.yang,添加代码如下
修改后的yang文件
rpc hello-world {
input {
leaf name {
type string;
}
}
output {
leaf greeting {
type string;
}
}
}
编译api
编译api
耗时31秒...
编译成功
绑定MD-SAL
打开impl-blueprint.xml,添加代码如下
绑定ms-sal
<reference id="rpcRegistry"
inter/>
<argument ref="rpcRegistry" />
实现RPC
在impl文件夹下新建HelloWorldImpl.java,添加代码如下
HelloWorldImpl.java
package org.opendaylight.helloworld.impl;
import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev180517.HelloWorldInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev180517.HelloWorldOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev180517.HelloWorldOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev180517.HelloworldService;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
public class HelloWorldImpl implements HelloworldService {
@Override
public ListenableFuture<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();
helloBuilder.setGreeting("Hello,"+input.getName());
return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
}
}
修改HelloworldProvide.java,添加如下代码
HelloworldProvide.java
/*
* Copyright © 2018 Copyright (c) 2015 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.BindingAwareBroker;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.helloworld.rev180517.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 BindingAwareBroker.RpcRegistration<HelloworldService> serviceRpcRegistration;
public HelloworldProvider(final DataBroker dataBroker, final RpcProviderRegistry rpcProviderRegistry) {
this.dataBroker = dataBroker;
this.rpcProviderRegistry = rpcProviderRegistry;
}
/**
* Method called when the blueprint container is created.
*/
public void init() {
serviceRpcRegistration = rpcProviderRegistry.addRpcImplementation(HelloworldService.class,new HelloWorldImpl());
LOG.info("HelloworldProvider Session Initiated");
}
/**
* Method called when the blueprint container is destroyed.
*/
public void close() {
serviceRpcRegistration.close();
LOG.info("HelloworldProvider Closed");
}
}
再次编译
再次进入根目录下(例如,helloworld/)编译整个项目,编译命令和之前的一样,耗时2分钟...
再次编译
结果
标题
错误及解决办法
HelloWorldImpl出错
错误
错误提示
解决办法:将Future改为ListenableFuture,修改IDEA关于OSGI的检查
setting中修改
Linux版(未得到预期结果)
先别照着做,博主还没有得到预期效果,有些错误。
创建项目
这里选择的是Sodium-SR2版本
mvn archetype:generate -DarchetypeGroupId=org.opendaylight.archetypes -DarchetypeArtifactId=opendaylight-startup-archetype \
-DarchetypeCatalog=remote -DarchetypeVersion=1.2.2
接下来你需要填写项目信息。
Define value for property 'groupId': 项目组名,小写,例如,org.opendaylight.helloworld
Define value for property 'artifactId': : 项目名,小写,例如,helloworld
Define value for property 'version': Sodium没有让用户选择
Define value for property 'package': 包名,例如,org.opendaylight.example
Define value for property 'classPrefix': 前缀,${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}
Define value for property 'copyright': 版权信息,Copyright (c) 2015 Yoyodyne, Inc.
填写项目信息
构建项目成功
项目目录含义
目录
文件名 | 功能 |
api | Yang模型目录 |
artifacts | 项目组件坐标管理 |
cli | 部署的配置文件 |
features | feature(组件)的组织管理目录 |
impl | 业务逻辑代码目录 |
it | 集成测试目录 |
karaf | karaf打包目录 |
pom.xml | maven项目的基本信息描述文件 |
api目录下的.yang文件
artifacts目录下的pom.xml文件
features下的组件
编译启动
mvn clean install -DskipTests -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true
- -DskipTests表示不执行测试用例
- -Dmaven.javadoc.skip=true表示跳过javadoc
- -Dcheckstyle.skip=true表示跳过checkstyle检查
官网没有加这些参数,编译项目时间长,可能出现checkstyle或其他测试等不通过,建议加参数。
编译项目
编译成功
编译过后,多了一个target目录。
编译后的项目
添加JAVA_HOME
编辑setenv文件
添加JAVA_HOME
运行OpenDaylight
运行OpenDaylight
导入项目
标题
标题
不要勾选Sources和Documentation
不要勾选Sources和Documentation,否则会很卡,只将IDEA作为编写工具,可以有一些提示。
之后一路Next即可。
编写RPC
标题
rpc hello-world {
input {
leaf name {
type string;
}
}
output {
leaf greeting {
type string;
}
}
}
绑定MD-SAL
绑定md-sal
实现RPC
创建HelloworldImpl
编写HelloworldImpl继承HelloworldService
编写HelloworldProvider
再次编译整个项目
Postman测试未得到正确结果
更多SDN相关内容,请查看:SDN-自学笔记
有问题请下方评论,转载请注明出处,并附有原文链接,谢谢!如有侵权,请及时联系。