Opendaylight子项目构建
1 前置安装
1.1 git 工具
sudo apt-get install git
1.2 JDK 1.8 安装配置
(1)官网下载jdk-8u162-linux-x64.tar.gz并拖入虚拟机桌面
(2)创建安装目录,并解压
cd /usr/local
sudo mkdir java
cd java
sudo mkdir jvm
sudo tar -zxvf ~/Desktop/jdk-8u162-linux-x64.tar.gz -C /usr/local/java/jvm
(3)配置
sudo gedit /etc/profile
#末尾加上如下
#set Java environment
export JAVA_HOME=/usr/local/java/jvm/jdk1.8.0_162
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
#同样,需要使用命令使环境变量立即生效
source /etc/profile
(4)检验
java -version
(5)多jdk问题处理
#注册
sudo update-alternatives --install /usr/bin/java java /usr/local/java/jvm/jdk1.8.0_162/bin/java 300
#注意,然后java –version 一下,没有的话,
sudo update-alternatives --install /usr/lib/java java /usr/local/java/jvm/jdk1.8.0_162/bin/java 300
#切换
sudo update-alternatives --config java
#选择jdk1.8.0_162,这里是输入序号,我的电脑是输入2.
1.3 Maven 安装
(1)官网下载并拖入虚拟机桌面apache-maven-3.6.0-bin.tar
(2)创建安装目录,并解压
cd /opt
sudo mkdir maven
sudo tar -zxvf ~/Desktop/apache-maven-3.6.0-bin.tar.gz -C /opt/maven
(3)配置
sudo gedit /etc/profile
#jdk配置之前
#set maven environment
export MAVEN_HOME=/opt/maven/apache-maven-3.6.0
export PATH=${MAVEN_HOME}/bin:$PATH
#同样,需要使用命令使环境变量立即生效
source /etc/profile
(4)检查
mvn -v
2 发行版本下载安装
说明:这里要先下载编译安装发行版本的原因在于,它会在~/.m2/repository目录下生成依赖包,而后面修改原型子项目中POM中的一些版本信息的时候可以参考对应目录下的包版本
2.1 配置maven settings.xml
cp -n ~/.m2/settings.xml{,.orig} ; wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/master/settings.xml > ~/.m2/settings.xml
2.2 下载源码(这个过程会耗费比较久的时间)
sudo git clone https://git.opendaylight.org/gerrit/p/integration/distribution.git
cd distribution
sudo git checkout stable/oxygen
git branch
#现在好像仓库里面的pom文件又变化了,要么别切分支要么切到弗版本
mvn clean install -DskipTests
2.3 添加一些基础特性
cd karaf/target/assembly/bin
./karaf
feature:install odl-l2switch-switch
feature:install odl-openflowplugin-flow-services
feature:install odl-mdsal-all
feature:install odl-dlux-core odl-dluxapps-nodes odl-dluxapps-topology odl-dluxapps-yangui odl-dluxapps-yangvisualizer odl-dluxapps-yangman
3 子项目构建
3.1 利用项目原型来创键项目
说明:这里的项目原型迁移了,组id发生了变化,同时这里下载下来的项目原型的版本是最高版本,以前的版本的原型拿不到(比如Oxygen),同时官网doc的很多信息已经过时
(1)这里的DarchetypeVersion版本可能会随时间变化而变化,快照版本基本隔几个月close,可以参考的version。
mvn archetype:generate
-DarchetypeGroupId=org.opendaylight.archetypes
-DarchetypeArtifactId=opendaylight-startup-archetype
-DarchetypeCatalog=remote
-DarchetypeVersion=1.1.0-SNAPSHOT
(2)修改POM中的版本(修改到stable/oxygen)
用idea打开子项目,修改version信息
Oxygen要修改的(这里可能不是全部):
<artifactId>single-feature-parent</artifactId> 版本到3.1.4
<artifactId>odlparent-lite</artifactId> 版本到3.1.4
<artifactId>feature-repo-parent</artifactId>版本到3.1.4
<artifactId>karaf4-parent</artifactId> 版本到 3.1.4
<artifactId>mdsal-artifacts</artifactId> 版本到1.7.5
<artifactId>binding-parent</artifactId> 版本到0.12.5-SNAPSHOT
利用~/.m2/repository来找到要修改的版本举例:
要修改的为扫描二维码关注公众号,回复: 12924271 查看本文章
<groupId>org.opendaylight.netconf</groupId>
<artifactId>restconf-artifacts</artifactId>
那么查看的路径
~/.m2/repository/org/opendaylight/netconf/restconf-artifacts
(3)编译安装子项目
cd 子项目名
mvn clean install
(4)idea支持
mvn idea:idea
3.2 构建一个简单的hello RPC项目
(1)下载子项目
说明:生成指令见3.1(1)
#按提示,输入要构建的子项目信息
Define value for property 'groupId': : com.ict.hello
Define value for property 'artifactId': : hello
Define value for property 'version'= 0.1.0-SNAPSHOT
Define value for property 'package': com.ict.hello: :
Define value for property 'classPrefix': Hello
Define value for property 'copyright': : Copyright (c) 2018 ict
(2)修改版本
说明:修改方法见3.1(2)
(3)编译安装子项目
cd hello
mvn clean install
(4)idea支持,完成后,用idea打开ipr
mvn idea:idea
(5)构建yang模型
1)编辑api/src/main/yang/hello.yang
module hello {
yang-version 1.1;
namespace "urn:opendaylight:params:xml:ns:yang:hello";
prefix "hello";
revision "2018-05-17" { // TODO change revision to date of model creation
description "Initial revision of hello model";
}
rpc hello-world {
input {
leaf name {
type string;
}
}
output {
leaf greating {
type string;
}
}
}
}
2)利用yang tool生成api
cd api
mvn clean install
(6)修改HelloProvider实现yang生成的HelloServce接口
1)编辑impl/src/main/java/com.ict.hello.impl
public class HelloProvider implements HelloService {
private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);
private final DataBroker dataBroker;
public HelloProvider(final DataBroker dataBroker) {
this.dataBroker = dataBroker;
}
/**
* Method called when the blueprint container is created.
*/
public void init() {
LOG.info("HelloProvider Session Initiated");
}
/**
* Method called when the blueprint container is destroyed.
*/
public void close() {
LOG.info("HelloProvider Closed");
}
@Override
public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();
helloBuilder.setGreating("Hello World " + input.getName());
return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
}
2)编译
cd impl
mvn clean install
(7)修改编辑impl/src/main/java/resources/blueprint.xml
说明:这里利用其来进行依赖注入
<!--末尾添加-->
<odl:rpc-implementation ref="provider"/>
(8)编译打包
建议:注释掉pom.xml里面的it,它在test的时候比较费时间
cd hello
mvn clean install
(9)启动简单检查
cd karaf/target/assembly/bin
./karaf
log:display | grep hello
feature:list | grep hello
(10)利用postman测试
1)请求类型及地址
POST
http://your_ip:8181/restconf/operations/hello:hello-world
说明:这里的ip地址是controller的IP地址,helloworld前缀和rpc hello-world在yang文件中定义。
2)修改Authorization
类型选择Basic Auth
Username:admin
Password:admin
3)Body,raw,json(application/json)
{"input": {
"name": "Andrew"
}
}
4)结果
{
"output": {
"greating": "Hello World Andrew"
}
}
3.3 将子项目添加到发行版本控制器中
(1)复制特征相关文件到发行版目录下
#子项目路径/karaf/target/assembly/system/子项目组名路径
#复制到
#distribution odl目录/karaf/target/assembly/system/子项目组名路径
#example:(以com.ict.hello为例,因为这里这个包下只有一个项目,所有我直接移动了ict目录)
sudo cp -r /opt/ODLWorkspace/hello/karaf/target/assembly/system/com/ict /opt/ODL/integration/distribution/karaf/target/assembly/system/com
(2)启动发行版本odl,添加特性
cd odl目录/karaf/target/assembly/bin
./karaf
feature:repo-add mvn:com.ict.hello/features-hello/0.1.0-SNAPSHOT/xml/features
feature:install odl-restconf-all
feature:install odl-hello
(3)利用yang ui来检查插件
#启动控制器后,浏览器打开
http://localhost:8181/index.html#/yangui/index
#Expendall 查看是否有hello
#点击
hello
opreational
hello-world
#input 输入,然后点击send
#这里output有时候不知道为啥,看不到输出,点击history查看
3.4 调试
#idea添加远程监听端口5005
#(这里的odl目录可以是发行版本也可以是自己的子项目)
#给代码打上断点
cd odl目录/karaf/target/assembly/bin
./karaf debug
3.5 关于自己的子项目没有一些发行版拥有的特性的解决
#在项目的features-项目名称中直接添加
#比如
<dependency>
<groupId>org.opendaylight.l2switch</groupId>
<artifactId>odl-l2switch-switch</artifactId>
<version>0.7.5-SNAPSHOT</version>
<type>xml</type>
<classifier>features</classifier>
</dependency>
<dependency>
<groupId>org.opendaylight.openflowplugin</groupId>
<artifactId>odl-openflowplugin-flow-services</artifactId>
<version>0.6.5-SNAPSHOT</version>
<type>xml</type>
<classifier>features</classifier>
</dependency>
<dependency>
<groupId>org.opendaylight.controller</groupId>
<artifactId>odl-mdsal-all</artifactId>
<version>1.7.5-SNAPSHOT</version>
<type>xml</type>
<classifier>features</classifier>
</dependency>
#重新mvn clean install
#但是它不是自动安装,只是不用再在karaf中add,但是依旧需要install.
#因为可以直接利用发行版本来调试,所以建议将自己开发的插件加入到发行版本,来调试
#在修改的时候记得uninstall