Opendaylight之MD-SAL中级教程(二)

Origin post:http://www.wyqbupt.com/2015/06/15/%e3%80%90%e5%8e%9f%e5%88%9b%e3%80%91opendaylight%e4%b9%8bmd-sal%e4%b8%ad%e7%ba%a7%e6%95%99%e7%a8%8b%ef%bc%88%e4%ba%8c%ef%bc%89/

第一步,我们需要讲我们的provider接入到MD-SAL,接触过opendaylight都知道有config subsystem,它提供了服务的周期管理,同时能够提供对JMX 和 NETCONF的接入性配置。config subsystem在MD-SAL之外而且被用来提供把应用接入MD-SAL的功能。我们首先完成配置性关系的yang文件,后面会用yangtools来生成java代码,通过这部分可以在ODL启动的时候加载相应的服务。

module ODL-skeleton-impl-config {
   yang-version 1;
   namespace "urn:opendaylight:params:xml:ns:yang:ODL-skeleton:impl:config";
   prefix "ODL-skeleton-impl-config";
   import config { prefix config; revision-date 2013-04-05; }
   import opendaylight-md-sal-binding { prefix md-sal-binding; revision-date 2013-10-28;}
   description
       "Service definition for ODL-skeleton project";
   revision "2014-12-10" {
       description
           "Initial revision";
   }
   identity ODL-skeleton-impl-config {
       base config:module-type;
       config:java-name-prefix ODL-skeletonImpl;
   }
   augment "/config:modules/config:module/config:configuration" {
       case ODL-skeleton-impl-config {
           when "/config:modules/config:module/config:type = 'ODL-skeleton-impl-config'";
           container binding-aware-broker {
               uses config:service-ref {
                   refine type {
                       mandatory true;
                       config:required-identity md-sal-binding:binding-broker-osgi-registry;
                   }
               }
           }
       }
   }
}
这里面的identity在yang的模型定义语言中作为一个标示,用于作为我们应用的provider提供的服务的全局标识,唯一的标记我们连接config subsystem。

augment用来扩展一个现有的ODL配置,增加了一个节点来标识 ODL-skeleton-impl。我们其中依赖了一个MD-SAL的服务 binding-aware-broker,这个跟MD-SAL的数据存取的datastore有关,我后面会讲到如何去用。
通过上面的yang文件,我们可以引导控制器的config subsystem解释ODL-skeleton 的配置文件


下一步写响应的配置文件ODL-skeleton-impl-config.xml
由于MD-SAL提供多种服务(每个服务其实就是一个接口),每个服务可能有多种实现,我们这个配置文件里就是需要来指定要用哪种服务的哪种实现。


<?xml version="1.0" encoding="UTF-8"?>
<!-- vi: set et smarttab sw=4 tabstop=4: -->
<snapshot>
    <required-capabilities>
        <capability>urn:opendaylight:params:xml:ns:yang:ODL-skeleton:impl:config?module=ODL-skeleton-impl-config&amp;revision=2014-12-10</capability>
        <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding?module=opendaylight-md-sal-binding&amp;revision=2013-10-28</capability>
    </required-capabilities>
    <configuration>
        <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
            <modules xmlns="urn:opendaylight:params:xml:ns:yang:controller:config">
                <module>
                    <type xmlns:prefix="urn:opendaylight:params:xml:ns:yang:ODL-skeleton:impl:config">prefix:ODL-skeleton-impl-config</type>
                    <name>ODL-skeleton-impl</name>
                    <binding-aware-broker>
                        <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-broker-osgi-registry</type>
                        <name>binding-osgi-broker</name>
                    </binding-aware-broker>
                </module>
            </modules>
        </data>
    </configuration>
</snapshot>


这里我们指定使用了binding-aware-broker service的binding-osgi-broker实现来满足需求。
这里我只加入了这一个配置型服务,当然你可以加入其他的,方法就是在标签内加
然后我们在impl模块的pom.xml写入代码生成器


<plugin>
                <groupId>org.opendaylight.yangtools</groupId>
                <artifactId>yang-maven-plugin</artifactId>
                <!-- TODO: I grabbed this from Ed's sample proj -->
                <dependencies>
                    <dependency>
                        <groupId>org.opendaylight.controller</groupId>
                        <artifactId>yang-jmx-generator-plugin</artifactId>
                        <version>${controller.config.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>config</id>
                        <goals>
                            <goal>generate-sources</goal>
                        </goals>
                        <configuration>
                            <codeGenerators>
                                <generator>
                                    <codeGeneratorClass>org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator</codeGeneratorClass>
                                    <outputBaseDir>${jmxGeneratorPath}</outputBaseDir>
                                    <additionalConfiguration>
                                        <namespaceToPackage1>urn:opendaylight:params:xml:ns:yang:controller==org.opendaylight.controller.config.yang</namespaceToPackage1>
                                    </additionalConfiguration>
                                </generator>
                                <generator>
                                    <codeGeneratorClass>org.opendaylight.yangtools.maven.sal.api.gen.plugin.CodeGeneratorImpl</codeGeneratorClass>
                                    <outputBaseDir>${salGeneratorPath}</outputBaseDir>
                                </generator>
                            </codeGenerators>
                            <inspectDependencies>true</inspectDependencies>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Yantools提供了2个code生成器,一般的生成器CodeGeneratorImpl生成产生正常API和数据传输对象的(DTO),它主要的作用是将我们定义的非配置性yang,产生相应的java文件;另一个生成器JmxGenerator产生配置性的代码,也就是对我们上面写的那个配置性yang文件作用。

改完ODl-skeleton-impl的pom.xml(具体参考的代码请看分支branch1内的代码),现在我们在ODL-skeleton-impl运行:
mvn clean generate-sources
这时我们会发现两个文件被建立了
ODLSkeletonImplModule.java这个java文件中的createInstance()创建一个ODl-skeleton-impl的实例,由这个实例来操作本应用的各个服务

ODLSkeletonImplModuleFactory.java它用来创建ODLSkeletonImplModule的实例,我们目前不用修改此文件。
下面我们修改一下ODLSkeletonImplModule的createInstance()方法


ODLSkeletonImpl provider = new  ODLSkeletonImpl();
getBindingAwareBrokerDependency().registerProvider(provider, null);
return provider;


这里创建了一个ODLSkeletonImpl 实例,BindingAwareBrokerDependency是MD-SAL已经创建的一个一个服务,用来向MD-SAL注册我们的provider。
上面的代码里我们利用了一个类 ODLSkeletonImpl,但是我们并没有实现它这个类,需要自己定义
在ODL-skeleton/ODL-skeleton-impl/src/main/java/org/opendaylight下创建目录
mkdir -p odl/skeleton
cd odl/skeleton

然后创建文件ODLSkeletonImpl.java
加入如下内容:


package org.opendaylight.odl.skeleton;
  
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class ODLSkeletonImpl implements BindingAwareProvider, AutoCloseable {
    private static final Logger LOG = LoggerFactory.getLogger(ODLSkeletonImpl.class);
 
    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub
    }
    @Override
    public void onSessionInitiated(ProviderContext arg0) {
        LOG.info("ODLSkeletonImpl initialized!");
    }
 
}















评论

此博客中的热门博文

openflow switch(I)

YANG Tools:YANG to Java Mapping

OpenDaylight架构简介