目錄
所用環境
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-自學筆記
有問題請下方評論,轉載請注明出處,并附有原文連結,謝謝!如有侵權,請及時聯系。