使用Archetype构建OpenDaylight项目hello
mkdir hello(后来看次步多余!)
cd hello
root@odl_ubuntu:/mnt/hello# mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/opendaylight.release/ -DarchetypeCatalog=remote -DarchetypeVersion=1.3.4-Carbon
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: :
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.4-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 /mnt/hello/hello/pom.xml
[INFO] Project created from Archetype in dir: /mnt/hello/hello
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20:41 min
[INFO] Finished at: 2018-11-24T05:28:24-08:00
[INFO] ------------------------------------------------------------------------
root@odl_ubuntu:/mnt/hello/hello# mvn clean install -Dmaven.test.failure.ignore=true -DskipTests
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for hello 0.1.0-SNAPSHOT:
[INFO]
[INFO] ODL :: org.opendaylight.hello :: hello-api ......... SUCCESS [ 36.626 s]
[INFO] ODL :: org.opendaylight.hello :: hello-impl ........ SUCCESS [ 19.921 s]
[INFO] ODL :: org.opendaylight.hello :: hello-cli ......... SUCCESS [ 17.254 s]
[INFO] ODL :: org.opendaylight.hello :: hello-features .... SUCCESS [ 15.303 s]
[INFO] ODL :: org.opendaylight.hello :: hello-karaf ....... SUCCESS [01:25 min]
[INFO] ODL :: org.opendaylight.hello :: hello-artifacts ... SUCCESS [ 3.388 s]
[INFO] ODL :: org.opendaylight.hello :: hello-it .......... SUCCESS [01:04 min]
[INFO] hello .............................................. SUCCESS [ 47.560 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:54 min
[INFO] Finished at: 2018-11-24T05:38:30-08:00
[INFO] ------------------------------------------------------------------------
root@odl_ubuntu:/mnt/hello/hello/karaf/target/assembly/etc# vim org.apache.karaf.management.cfg
rmiRegistryHost=127.0.0.1
rmiServerHost=127.0.0.1
Apache Karaf starting up. Press Enter to open the shell now...
100% [========================================================================]
\_____ \ ______ ____ ____ \______ \ _____ ___.__.| | |__| ____ | |___/ |_
/ | \\____ \_/ __ \ / \ | | \\__ \< | || | | |/ ___\| | \ __\
/ | \ |_> > ___/| | \| ` \/ __ \\___ || |_| / /_/ > 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.
2018-11-24 05:54:22,312 | INFO | Event Dispatcher | BlueprintBundleTracker | 173 - org.opendaylight.controller.blueprint - 0.6.4.Carbon | Creating blueprint container for bundle org.opendaylight.hello.impl_0.1.0.SNAPSHOT [208] with paths [bundleentry://208.fwk1752203484/org/opendaylight/blueprint/impl-blueprint.xml]
2018-11-24 05:54:22,320 | INFO | Event Dispatcher | BlueprintContainerImpl | 15 - org.apache.aries.blueprint.core - 1.6.1 | Bundle org.opendaylight.hello.impl/0.1.0.SNAPSHOT is waiting for dependencies [(objectClass=org.opendaylight.controller.md.sal.binding.api.DataBroker)]
2018-11-24 05:54:23,461 | INFO | rint Extender: 3 | HelloProvider | 208 - org.opendaylight.hello.impl - 0.1.0.SNAPSHOT | HelloProvider Session Initiated
2018-11-24 05:54:23,543 | INFO | ntAdminThread #8 | BlueprintBundleTracker | 173 - org.opendaylight.controller.blueprint - 0.6.4.Carbon | Blueprint container for bundle org.opendaylight.hello.impl_0.1.0.SNAPSHOT [208] was successfully created
opendaylight-user@root>
![](https://img2018.cnblogs.com/blog/1494842/201811/1494842-20181124221222888-374424365.png)
Add any new things that you are doing in your implementation by using the HelloProvider.init()method. Its analogous to an Activator.
现在编辑hello.yang,增加如下黑体内容:
root@odl_ubuntu:/mnt/hello/hello/api/src/main/yang# vim 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 greeting {
type string;
}
}
}
}
然后再编译一遍:
root@odl_ubuntu:/mnt/hello/hello# mvn clean install -Dmaven.test.failure.ignore=true -DskipTests
[INFO] Attaching 'src/site/site.xml' site descriptor with classifier 'site'.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for hello 0.1.0-SNAPSHOT:
[INFO]
[INFO] ODL :: org.opendaylight.hello :: hello-api ......... SUCCESS [ 36.888 s]
[INFO] ODL :: org.opendaylight.hello :: hello-impl ........ SUCCESS [ 19.039 s]
[INFO] ODL :: org.opendaylight.hello :: hello-cli ......... SUCCESS [ 18.147 s]
[INFO] ODL :: org.opendaylight.hello :: hello-features .... SUCCESS [ 38.558 s]
[INFO] ODL :: org.opendaylight.hello :: hello-karaf ....... SUCCESS [01:10 min]
[INFO] ODL :: org.opendaylight.hello :: hello-artifacts ... SUCCESS [ 2.869 s]
[INFO] ODL :: org.opendaylight.hello :: hello-it .......... SUCCESS [ 51.920 s]
[INFO] hello .............................................. SUCCESS [ 41.035 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:44 min
[INFO] Finished at: 2018-11-25T01:20:37-08:00
[INFO] ------------------------------------------------------------------------
编译成功后在如下目录自动创建出了如下impl-blueprint.xml文件:
root@odl_ubuntu:/mnt/hello/hello/impl/src/main/resources/org/opendaylight/blueprint# ll
total 12
drwxr-xr-x 2 root root 4096 Nov 24 05:28 ./
drwxr-xr-x 3 root root 4096 Nov 24 05:28 ../
-rw-r--r-- 1 root root 842 Nov 24 05:28 impl-blueprint.xml
现在编辑这个文件,新增如下黑色字体:
<?xml version="1.0" encoding="UTF-8"?>
<!-- vi: set et smarttab sw=4 tabstop=4: -->
<!--
Copyright © 2017 No 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
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
odl:use-default-for-reference-types="true">
<reference id="dataBroker"
interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"
odl:type="default" />
<reference id="rpcRegistry"
interface="org.opendaylight.controller.sal.binding.api.RpcProviderRegistry"/>
<bean id="provider"
class="org.opendaylight.hello.impl.HelloProvider"
init-method="init" destroy-method="close">
<argument ref="dataBroker" />
<argument ref="rpcRegistry" />
</bean>
</blueprint>
然后新建文件HelloWorldImpl.java并输入如下内容(注意java文件开头的注释一定要格式规范,否则编译不过!),以实现RPC API:
root@odl_ubuntu:/mnt/hello/hello/impl/src/main/java/org/opendaylight/hello/impl# vim HelloWorldImpl.java
/*
* Copyright © 2016 Cisco Systems 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.hello.impl;
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 Future<RpcResult<HelloWorldOutput>> helloWorld(HelloWorldInput input) {
HelloWorldOutputBuilder helloBuilder = new HelloWorldOutputBuilder();
helloBuilder.setGreeting("Hello " + input.getName());
return RpcResultBuilder.success(helloBuilder.build()).buildFuture();
}
root@odl_ubuntu:/mnt/hello/hello/impl/src/main/java/org/opendaylight/hello/impl# vim HelloProvider.java
/*
* Copyright © 2017 No 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.hello.impl;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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 {
private static final Logger LOG = LoggerFactory.getLogger(HelloProvider.class);
private final DataBroker dataBroker;
private final RpcProviderRegistry rpcProviderRegistry;
private RpcRegistration<HelloService> serviceRegistration;
public HelloProvider(final DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) {
this.dataBroker = dataBroker;
this.rpcProviderRegistry = rpcProviderRegistry;
}
/**
* Method called when the blueprint container is created.
*/
public void init() {
LOG.info("HelloProvider Session Initiated");
serviceRegistration = rpcProviderRegistry.addRpcImplementation(HelloService.class, new HelloWorldImpl());
}
/**
* Method called when the blueprint container is destroyed.
*/
public void close() {
serviceRegistration.close();
LOG.info("HelloProvider Closed");
}
}
再次编译:
root@odl_ubuntu:/mnt/hello/hello# mvn clean install -Dmaven.test.failure.ignore=true -DskipTests
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for hello 0.1.0-SNAPSHOT:
[INFO]
[INFO] ODL :: org.opendaylight.hello :: hello-api ......... SUCCESS [ 47.681 s]
[INFO] ODL :: org.opendaylight.hello :: hello-impl ........ SUCCESS [ 18.801 s]
[INFO] ODL :: org.opendaylight.hello :: hello-cli ......... SUCCESS [ 16.287 s]
[INFO] ODL :: org.opendaylight.hello :: hello-features .... SUCCESS [ 16.883 s]
[INFO] ODL :: org.opendaylight.hello :: hello-karaf ....... SUCCESS [ 58.413 s]
[INFO] ODL :: org.opendaylight.hello :: hello-artifacts ... SUCCESS [ 2.421 s]
[INFO] ODL :: org.opendaylight.hello :: hello-it .......... SUCCESS [ 35.376 s]
[INFO] hello .............................................. SUCCESS [ 33.026 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:57 min
[INFO] Finished at: 2018-11-25T03:44:34-08:00
[INFO] ------------------------------------------------------------------------
下面可以开始测试了:
root@odl_ubuntu:/mnt/hello/hello/karaf/target/assembly/etc# vim org.apache.karaf.management.cfg
rmiRegistryHost=127.0.0.1
rmiServerHost=127.0.0.1
root@odl_ubuntu:/mnt/hello/hello/karaf/target/assembly/bin# ./karaf
Apache Karaf starting up. Press Enter to open the shell now...
100% [========================================================================]
Karaf started in 62s. Bundle stats: 295 active, 295 total
________ ________ .__ .__ .__ __
\_____ \ ______ ____ ____ \______ \ _____ ___.__.| | |__| ____ | |___/ |_
/ | \\____ \_/ __ \ / \ | | \\__ \< | || | | |/ ___\| | \ __\
/ | \ |_> > ___/| | \| ` \/ __ \\___ || |_| / /_/ > 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>
现在通过浏览器访问http://10.0.0.9:8181/apidoc/explorer/index.html
单击新增加的hello后面的Expand Operations:
在Value中输入: {
"hello:input"
: {
"name"
:
"hello"
}}
![](https://img2018.cnblogs.com/blog/1494842/201811/1494842-20181125195846314-1541650526.png)
Try it Out!