odl-boron-sr3之hello rpc

来源:互联网 发布:网络小精灵国语版58网 编辑:程序博客网 时间:2024/06/05 18:57

环境安装,请看这里。
之前的文章,已经建立了hello,详情看这里。
现在做一个hello的rpc。官方文档看这里。

环境:odl-boron-sr3和mvn和java

ps:本篇文章只有过程,没有细致讲解(因为我也不太懂呀)
1.添加rpc api:
编辑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 {        input {            leaf name {                type string;            }        }        output {            leaf greeting {                type string;            }        }    }}

2.进到api文件夹执行mvn clean install
3.实现rpc api:
进入impl/src/main/java/org/bupt/siwind/hello/impl,然后新建文件HelloImpl.java并填入下面代码。
ps:最前面的/**/不要删,可能回报错。

/* * Copyright © 2016 Pn, 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.bupt.siwind.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.HelloInput;import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloOutput;import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.hello.rev150105.HelloOutputBuilder;import org.opendaylight.yangtools.yang.common.RpcResult;import org.opendaylight.yangtools.yang.common.RpcResultBuilder;public class HelloImpl implements HelloService {    @Override    public Future<RpcResult<HelloOutput>> hello(HelloInput input) {        HelloOutputBuilder helloBuilder = new HelloOutputBuilder();        helloBuilder.setGreeting("Hello " + input.getName());        return RpcResultBuilder.success(helloBuilder.build()).buildFuture();    }}

再编辑HelloProvider.java,修改为下面代码(init函数启动时会调用hello rpc):
ps:最前面的/**/不要删,可能回报错。

/* * Copyright © 2016 Pn, 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.bupt.siwind.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() {    serviceRegistration = rpcProviderRegistry.addRpcImplementation(HelloService.class, new HelloImpl());        LOG.info("helloProvider Session Initiated");    }    /**     * Method called when the blueprint container is destroyed.     */    public void close() {    serviceRegistration.close();        LOG.info("helloProvider Closed");    }}

3.注册rpc api:
编辑impl/src/main/resources/org/opendaylight/blueprint下的impl-blueprint.xml,修改为下面代码:
ps:最前面的/**/不要删,可能回报错。

<?xml version="1.0" encoding="UTF-8"?><!-- vi: set et smarttab sw=4 tabstop=4: --><!--Copyright © 2016 Pn, Inc.  and others. All rights reserved.This program and the accompanying materials are made available under theterms 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.bupt.siwind.hello.impl.HelloProvider"    init-method="init" destroy-method="close">    <argument ref="dataBroker" />    <argument ref="rpcRegistry" />  </bean></blueprint>

4.到impl文件夹中执行mvn clean install
5.到hello文件夹中执行mvn clean install
6.运行./karaf/target/assembly/bin/karaf
执行log:display | grep Hello输出没有error,就说明成功了。
7.测试rest,用postman或相关软件。
url:http://127.0.0.1:8181/restconf/operations/hello:hello
method:post
basic auth:admin admin
body:

{"input": {    "name": "Songqiu"  }}
原创粉丝点击