1.install JDK(需要1.7+版本)
下载JDK:jdk-7u79-linux-x64.tar.gz
解压文件到/usr/local/目录下。
在/etc/profile文件下添加环境变量:
export JAVA_HOME=/usr/local/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
使用以下命令使环境变量生效:
source /etc/profile
使用以下命令检查安装是否完成。
root@todd-pc:/home/todd/Downloads# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
2.install maven(需要3.1.1+版本)
下载maven:apache-maven-3.3.3-bin.tar.gz
解压文件到/usr/local/目录下。
在/etc/profile文件最下方添加环境变量:
export M2_HOME=/usr/local/apache-maven-3.3.3
export PATH=$PATH:$M2_HOME/bin
使用以下命令使环境变量生效:
source /etc/profile
使用以下命令检查安装是否完成。
#mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /usr/local/apache-maven-3.3.3
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/local/jdk1.7.0_79/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-32-generic", arch: "amd64", family: "unix"
3.执行以下命令删除寄存的repository
#rm ~/.m2/repository
4.执行以下命令 创建apl的archetype:
#mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/public/ -DarchetypeCatalog=https://nexus.opendaylight.org/content/repositories/public/archetype-catalog.xml
创建archetype时按照以下进行填写:
Define value for property 'groupId': : org.opendaylight.hello
Define value for property 'artifactId': : hello
Define value for property 'version': 1.0-SNAPSHOT: : 1.0.0
Define value for property 'package': org.opendaylight.hello: :
Nov 05, 2015 8:14:54 PM org.apache.velocity.runtime.log.JdkLogChute log
INFO: FileResourceLoader : adding path '.'
Define value for property 'classPrefix': Hello: :
Define value for property 'copyright': : jftt,Inc
5.执行以下命令创建settings.xml文件
#cp -n ~/.m2/settings.xml{,.orig} ; \wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/stable/lithium/settings.xml > ~/.m2/settings.xml
6.进行archetype的编译。
#cd hello
#mvn clean install
......
[INFO] Reactor Summary:
[INFO]
[INFO] hello-api .......................................... SUCCESS [ 18.534 s]
[INFO] hello-impl ......................................... SUCCESS [ 12.576 s]
[INFO] hello-features ..................................... SUCCESS [05:06 min]
[INFO] hello-karaf ........................................ SUCCESS [ 57.203 s]
[INFO] hello-artifacts .................................... SUCCESS [ 0.739 s]
[INFO] hello .............................................. SUCCESS [ 2.068 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:46 min
[INFO] Finished at: 2015-11-05T16:52:43+08:00
[INFO] Final Memory: 95M/414M
[INFO] ------------------------------------------------------------------------
7.定义helllo-world rpc。修改api/src/main/yang/hello.yang文件如下:
module hello {
yang-version 1;
namespace "urn:opendaylight:params:xml:ns:yang:hello";
prefix "hello";
revision "2015-01-05" {
description "Initial revision of hello model";
}
rpc hello-world {
input {
leaf name {
type string;
}
}
output {
leaf greating {
type string;
}
}
}
}
8.编译API yang文件。在api目录下执行以下命令进行API的编译。
#mvn clean install
......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 43.319 s
[INFO] Finished at: 2015-11-05T20:23:50+08:00
[INFO] Final Memory: 37M/377M
[INFO] ------------------------------------------------------------------------
9.将服务注册到MD-SAL中。修改impl/src/main/java/org/opendaylight/hello/impl/HelloProvider.java:
package org.opendaylight.hello.impl;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloProvider implements BindingAwareProvider, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);
private RpcRegistration<HelloService> helloService;
@Override
public void onSessionInitiated(ProviderContext session) {
LOG.info("HelloProvider Session Initiated");
helloService = session.addRpcImplementation(HelloService.class, new HelloWorldImpl());
}
@Override
public void close() throws Exception {
LOG.info("HelloProvider Closed");
if (helloService != null) {
helloService.close();
}
}
}
10.实现API定义的rpc请求。新建impl/src/main/java/org/opendaylight/hello/impl/HelloWorldImpl.java文件:
package org.opendaylight.hello.impl;
import java.util.concurrent.Future;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutputBuilder;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
public class HelloWorldImpl implements HelloService {
@Override
public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
HelloWorldOutputBuilder helloBuilder = new
HelloWorldOutputBuilder();
helloBuilder.setGreating("Hello " + input.getName());
return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
}
}
10.在工程的根目录进行编译。
#mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] hello-api .......................................... SUCCESS [ 18.548 s]
[INFO] hello-impl ......................................... SUCCESS [ 12.503 s]
[INFO] hello-features ..................................... SUCCESS [05:36 min]
[INFO] hello-karaf ........................................ SUCCESS [01:06 min]
[INFO] hello-artifacts .................................... SUCCESS [ 0.271 s]
[INFO] hello .............................................. SUCCESS [ 2.208 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:22 min
[INFO] Finished at: 2015-11-05T20:42:39+08:00
[INFO] Final Memory: 94M/432M
[INFO] ------------------------------------------------------------------------
确认kar文件已经生成:
#ll ./features/target/hello-features-1.0.0.kar
-rw-r--r-- 1 root root 42832 Nov 5 20:41 ./features/target/hello-features-1.0.0.kar
11.下载opendaylight pre-build 版本,并解压运行。
#./distribution-karaf-0.3.2-Lithium-SR2/bin/karaf
________ ________ .__ .__ .__ __
\_____ \ ______ ____ ____ \______ \ _____ ___.__.| | |__| ____ | |___/ |_
/ | \\____ \_/ __ \ / \ | | \\__ \< | || | | |/ ___\| | \ __\
/ | \ |_> > ___/| | \| ` \/ __ \\___ || |_| / /_/ > Y \ |
\_______ / __/ \___ >___| /_______ (____ / ____||____/__\___ /|___| /__|
\/|__| \/ \/ \/ \/\/ /_____/ \/
Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown OpenDaylight.
opendaylight-user@root>
12.安装刚才生成的kar文件。
opendaylight-user@root>kar:install file:/home/todd/opendaylight/hello/features/target/hello-features-1.0.0.kar
opendaylight-user@root>kar:list
KAR Name
--------------------
hello-features-1.0.0
opendaylight-user@root>feature:list -i
Name | Version | Installed | Repository | Description
---------------------------------------------------------------------------------------------------------------------------------------------------------------
......
odl-hello-api | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: api
odl-hello | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello
odl-hello-rest | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: REST
odl-hello-ui | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: UI
13.发送rest请求,确认feature安装成功。
#curl -X POST -u admin:admin -H "Content-type:application/json" -d "{"input":{"name":"world"}}" http://localhost:8181/restconf/operations/hello:hello-world
{"output":{"greating":"Hello world"}}
下载JDK:jdk-7u79-linux-x64.tar.gz
解压文件到/usr/local/目录下。
在/etc/profile文件下添加环境变量:
export JAVA_HOME=/usr/local/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
使用以下命令使环境变量生效:
source /etc/profile
使用以下命令检查安装是否完成。
root@todd-pc:/home/todd/Downloads# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
2.install maven(需要3.1.1+版本)
下载maven:apache-maven-3.3.3-bin.tar.gz
解压文件到/usr/local/目录下。
在/etc/profile文件最下方添加环境变量:
export M2_HOME=/usr/local/apache-maven-3.3.3
export PATH=$PATH:$M2_HOME/bin
使用以下命令使环境变量生效:
source /etc/profile
使用以下命令检查安装是否完成。
#mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /usr/local/apache-maven-3.3.3
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/local/jdk1.7.0_79/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-32-generic", arch: "amd64", family: "unix"
3.执行以下命令删除寄存的repository
#rm ~/.m2/repository
4.执行以下命令 创建apl的archetype:
#mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/public/ -DarchetypeCatalog=https://nexus.opendaylight.org/content/repositories/public/archetype-catalog.xml
创建archetype时按照以下进行填写:
Define value for property 'groupId': : org.opendaylight.hello
Define value for property 'artifactId': : hello
Define value for property 'version': 1.0-SNAPSHOT: : 1.0.0
Define value for property 'package': org.opendaylight.hello: :
Nov 05, 2015 8:14:54 PM org.apache.velocity.runtime.log.JdkLogChute log
INFO: FileResourceLoader : adding path '.'
Define value for property 'classPrefix': Hello: :
Define value for property 'copyright': : jftt,Inc
5.执行以下命令创建settings.xml文件
#cp -n ~/.m2/settings.xml{,.orig} ; \wget -q -O - https://raw.githubusercontent.com/opendaylight/odlparent/stable/lithium/settings.xml > ~/.m2/settings.xml
6.进行archetype的编译。
#cd hello
#mvn clean install
......
[INFO] Reactor Summary:
[INFO]
[INFO] hello-api .......................................... SUCCESS [ 18.534 s]
[INFO] hello-impl ......................................... SUCCESS [ 12.576 s]
[INFO] hello-features ..................................... SUCCESS [05:06 min]
[INFO] hello-karaf ........................................ SUCCESS [ 57.203 s]
[INFO] hello-artifacts .................................... SUCCESS [ 0.739 s]
[INFO] hello .............................................. SUCCESS [ 2.068 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:46 min
[INFO] Finished at: 2015-11-05T16:52:43+08:00
[INFO] Final Memory: 95M/414M
[INFO] ------------------------------------------------------------------------
7.定义helllo-world rpc。修改api/src/main/yang/hello.yang文件如下:
module hello {
yang-version 1;
namespace "urn:opendaylight:params:xml:ns:yang:hello";
prefix "hello";
revision "2015-01-05" {
description "Initial revision of hello model";
}
rpc hello-world {
input {
leaf name {
type string;
}
}
output {
leaf greating {
type string;
}
}
}
}
8.编译API yang文件。在api目录下执行以下命令进行API的编译。
#mvn clean install
......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 43.319 s
[INFO] Finished at: 2015-11-05T20:23:50+08:00
[INFO] Final Memory: 37M/377M
[INFO] ------------------------------------------------------------------------
9.将服务注册到MD-SAL中。修改impl/src/main/java/org/opendaylight/hello/impl/HelloProvider.java:
package org.opendaylight.hello.impl;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloProvider implements BindingAwareProvider, AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);
private RpcRegistration<HelloService> helloService;
@Override
public void onSessionInitiated(ProviderContext session) {
LOG.info("HelloProvider Session Initiated");
helloService = session.addRpcImplementation(HelloService.class, new HelloWorldImpl());
}
@Override
public void close() throws Exception {
LOG.info("HelloProvider Closed");
if (helloService != null) {
helloService.close();
}
}
}
10.实现API定义的rpc请求。新建impl/src/main/java/org/opendaylight/hello/impl/HelloWorldImpl.java文件:
package org.opendaylight.hello.impl;
import java.util.concurrent.Future;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloWorldOutputBuilder;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
public class HelloWorldImpl implements HelloService {
@Override
public Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
HelloWorldOutputBuilder helloBuilder = new
HelloWorldOutputBuilder();
helloBuilder.setGreating("Hello " + input.getName());
return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
}
}
10.在工程的根目录进行编译。
#mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] hello-api .......................................... SUCCESS [ 18.548 s]
[INFO] hello-impl ......................................... SUCCESS [ 12.503 s]
[INFO] hello-features ..................................... SUCCESS [05:36 min]
[INFO] hello-karaf ........................................ SUCCESS [01:06 min]
[INFO] hello-artifacts .................................... SUCCESS [ 0.271 s]
[INFO] hello .............................................. SUCCESS [ 2.208 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:22 min
[INFO] Finished at: 2015-11-05T20:42:39+08:00
[INFO] Final Memory: 94M/432M
[INFO] ------------------------------------------------------------------------
确认kar文件已经生成:
#ll ./features/target/hello-features-1.0.0.kar
-rw-r--r-- 1 root root 42832 Nov 5 20:41 ./features/target/hello-features-1.0.0.kar
11.下载opendaylight pre-build 版本,并解压运行。
#./distribution-karaf-0.3.2-Lithium-SR2/bin/karaf
________ ________ .__ .__ .__ __
\_____ \ ______ ____ ____ \______ \ _____ ___.__.| | |__| ____ | |___/ |_
/ | \\____ \_/ __ \ / \ | | \\__ \< | || | | |/ ___\| | \ __\
/ | \ |_> > ___/| | \| ` \/ __ \\___ || |_| / /_/ > Y \ |
\_______ / __/ \___ >___| /_______ (____ / ____||____/__\___ /|___| /__|
\/|__| \/ \/ \/ \/\/ /_____/ \/
Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown OpenDaylight.
opendaylight-user@root>
12.安装刚才生成的kar文件。
opendaylight-user@root>kar:install file:/home/todd/opendaylight/hello/features/target/hello-features-1.0.0.kar
opendaylight-user@root>kar:list
KAR Name
--------------------
hello-features-1.0.0
opendaylight-user@root>feature:list -i
Name | Version | Installed | Repository | Description
---------------------------------------------------------------------------------------------------------------------------------------------------------------
......
odl-hello-api | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: api
odl-hello | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello
odl-hello-rest | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: REST
odl-hello-ui | 1.0.0 | x | odl-hello-1.0.0 | OpenDaylight :: hello :: UI
13.发送rest请求,确认feature安装成功。
#curl -X POST -u admin:admin -H "Content-type:application/json" -d "{"input":{"name":"world"}}" http://localhost:8181/restconf/operations/hello:hello-world
{"output":{"greating":"Hello world"}}