OpenDaylight提供了一個(gè)例子:Toaster(烤面包機(jī)),借這個(gè)例子學(xué)習(xí)OpenDaylight的特性。在Toaster中,真正有關(guān)烤面包機(jī)相關(guān)的代碼較少,它只是一種OpenDaylight具體思想的展示品,已有相關(guān)文章對(duì)其進(jìn)行了分析。現(xiàn)在我們舉一個(gè)永恒的示例:Hello World。
一、應(yīng)用程序類型
OpenDaylight應(yīng)用程序開發(fā),分如下兩種:
(1)如果提供API的接口能夠滿足當(dāng)前需求,那么只需基于REST API開發(fā)External System(外置式應(yīng)用程序);
(2)如果提供的API接口不能滿足當(dāng)前需求,則需要進(jìn)行Internal System(內(nèi)置式應(yīng)用程序)的開發(fā),可以理解為插件。
二、應(yīng)用程序開發(fā)流程
對(duì)于External System來說,我們?cè)诮榻B基于REST API編程時(shí)再進(jìn)行介紹。下面我們來看下,Internal System的開發(fā)流程如下圖所示:
(1)生成項(xiàng)目骨架(Archetype)
Archetype是一個(gè)Maven項(xiàng)目模板工具包,使用Archetype來生成項(xiàng)目骨架,可以使得開發(fā)人員在開發(fā)新的應(yīng)用程序時(shí),使用與ODL風(fēng)格一致的最佳實(shí)踐方法。
(2)定義模型(model)
YANG在OpenDaylight中做為建模語言使用,MD-SAL中M即是YANG。YANG的使用已不局限于NETCONF。YANG模型定義之后,通過YANG TOOLS可以自動(dòng)生成部分代碼,但對(duì)于Model對(duì)應(yīng)的具體實(shí)現(xiàn)需要添加相關(guān)業(yè)務(wù)邏輯。
執(zhí)行mvn clean install -Dmaven.test.failure.ignore=true
(3)代碼實(shí)現(xiàn)
①實(shí)現(xiàn)Impl:添加業(yè)務(wù)邏輯(上圖藍(lán)色部分);
②配置blueprint.xml:xml文件定義和描述了不同組件的組裝,它是Blueprint編程模型的關(guān)鍵。規(guī)范描述了組件如何獲得實(shí)例,如何捆綁到一起形成一個(gè)運(yùn)行模塊。
(4)構(gòu)建安裝
執(zhí)行 mvn clean install命令,可以將代碼打包并安裝到倉庫中。
(5)測(cè)試
OpenDaylight提供了API測(cè)試頁面,可以在該頁面直接進(jìn)行API的測(cè)試,地址為:http://localhost:8181/apidoc/explorer/index.html
三、Hello示例
1.構(gòu)建Hello項(xiàng)目
步驟1 執(zhí)行構(gòu)建項(xiàng)目命令:
mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeRepository=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/ -DarchetypeCatalog=http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/archetype-catalog.xml
輸入Hello交互信息:
Define value for property 'groupId': org.opendaylight.hello
Define value for property 'artifactId': hello
[INFO] Using property: version = 0.1.0-SNAPSHOT
Define value for property 'package' org.opendaylight.hello: :
Define value for property 'classPrefix' Hello: : hello
Define value for property 'copyright': No
[INFO] Using property: copyrightYear = 2017
Confirm properties configuration:
groupId: org.opendaylight.hello
artifactId: hello
version: 0.1.0-SNAPSHOT
package: org.opendaylight.hello
classPrefix: hello
copyright: No
copyrightYear: 2017
Y: : Y
安裝成功的信息提示:
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: opendaylight-startup-archetype:1.3.0-Carbon
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.opendaylight.hello
[INFO] Parameter: artifactId, Value: hello
[INFO] Parameter: version, Value: 0.1.0-SNAPSHOT
[INFO] Parameter: package, Value: org.opendaylight.hello
[INFO] Parameter: packageInPathFormat, Value: org/opendaylight/hello
[INFO] Parameter: classPrefix, Value: hello
[INFO] Parameter: package, Value: org.opendaylight.hello
[INFO] Parameter: version, Value: 0.1.0-SNAPSHOT
[INFO] Parameter: copyright, Value: No
[INFO] Parameter: groupId, Value: org.opendaylight.hello
[INFO] Parameter: artifactId, Value: hello
[INFO] Parameter: copyrightYear, Value: 2017
[WARNING] Don't override file D:\\CODE\\odlexample\\hello\\hello\\pom.xml
[INFO] Project created from Archetype in dir: D:\\CODE\\odlexample\\hello\\hello
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 45.526 s
步驟2. 構(gòu)建Hello項(xiàng)目,執(zhí)行命令
mvn clean install -Dmaven.test.failure.ignore=true
[INFO] --- maven-site-plugin:3.6:attach-descriptor (generate-site) @ hello-aggregator ---
[INFO] Attaching 'src\\site\\site.xml' site descriptor with classifier 'site'.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] ODL :: org.opendaylight.hello :: hello-api ......... SUCCESS [ 19.565 s]
[INFO] ODL :: org.opendaylight.hello :: hello-impl ........ SUCCESS [ 8.826 s]
[INFO] ODL :: org.opendaylight.hello :: hello-cli ......... SUCCESS [ 8.878 s]
[INFO] ODL :: org.opendaylight.hello :: hello-features .... SUCCESS [15:41 min]
[INFO] ODL :: org.opendaylight.hello :: hello-karaf ....... SUCCESS [ 41.676 s]
[INFO] ODL :: org.opendaylight.hello :: hello-artifacts ... SUCCESS [ 1.794 s]
[INFO] ODL :: org.opendaylight.hello :: hello-it .......... SUCCESS [03:38 min]
[INFO] hello .............................................. SUCCESS [ 23.209 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21:07 min
生成的項(xiàng)目骨架為:
生成的代碼文件為:
2.定義模型
步驟1 修改 api/src/main/yang/hello.yang,增加簡單的Hello RPC信息:
步驟2 執(zhí)行命令:mvn clean install -Dmaven.test.failure.ignore=true
3.代碼實(shí)現(xiàn)
步驟1 實(shí)現(xiàn)Impl:
步驟2 配置blueprint.xml
在文件impl-blueprint.xml( src/main/resources/org/opendaylight/blueprint)注冊(cè)RPC引用,增加粗體部分信息。
注:在 Blueprint XML 中,bean 元素將定義一個(gè) bean 管理器。用于對(duì)象構(gòu)造的參數(shù)由 argument 元素指定;注入的屬性則由 property 子元素指定。property 元素指定要注入的屬性的名稱和值。屬性名與 Java 類中的 setter 方法名對(duì)應(yīng)。例如,如果屬性名為 foo,那么對(duì)應(yīng)的 setter 方法為 setFoo(arg)。屬性名和對(duì)應(yīng)的 setter 方法名遵循 JavaBeans 規(guī)范中定義的屬性設(shè)計(jì)模式。
步驟3 類HelloProvider中init方法中綁定HelloService和HelloWorldImpl
4.構(gòu)建安裝、啟動(dòng)測(cè)試
步驟1 執(zhí)行命令 mvn clean install
步驟2 啟動(dòng)OpenDaylight
\\CODE\\odlexample\\hello\\hello\\karaf\\target\\assembly\\bin>karaf
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=256M; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512M; support was removed in 8.0
Apache Karaf starting up. Press Enter to open the shell now...
100% [========================================================================]
Karaf started in 29s. Bundle stats: 294 active, 294 total
步驟3 瀏覽器打開如下地址(admin\\admin):
http://localhost:8181/apidoc/explorer/index.html
-
API
+關(guān)注
關(guān)注
2文章
1554瀏覽量
63277 -
編程
+關(guān)注
關(guān)注
88文章
3673瀏覽量
94690 -
REST
+關(guān)注
關(guān)注
0文章
33瀏覽量
9560
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
華為游戲服務(wù)示例代碼教程案例
如何從零開始學(xué)OpenDaylight

OpenDaylight中的OSGi

OpenDaylight中的DataStore是什么

Vivado HDL編寫示例
自己編寫函數(shù)示例代碼很難嗎?分享幾個(gè)示例!

評(píng)論