Opendaylight课堂之深度剖析toaster(一)

写本篇文章的时候,odl发布的版本已经是碳版本,下面的mytoaster即采用最新版本。本篇主要参考opendaylight-wiki,这里吐槽一下odl wiki,在去年wiki更新速度特别差,即使照wiki一步一步敲代码,也很难能够把toaster完成。

一、下载源代码

下载odlparentcontroller以及netconf目录。编译顺序以及简要说明一下:

1)odlparentcontroller的父工程。

2)controller是核心内容。

3)netconf,在某个版本(可能是硼)开始restconfnetconf两个组件合并了,我们需要restconf

4)编译方法

编译方法就是按照上面顺序,执行的命令如下:

mvn clean install -DskipTests -Dskip.karaf.featureTest=true -Dmaven.test.skip=true -Dcheckstyle.skip=true  -Dmaven.javadoc.skip=true

提醒I对于odlparentnetconf这两个工程我们可以不用下载,因此odl官方每天都会发布对应的版本,我们只需要在controllerpom文件增加对这两个工程的依赖就可以了,在编译controller的时候就会把这两个工程已经编译好的版本下载下来了。(此处只是为了学习才下载)

提醒II:为了能够顺利完成toaster,第一步肯定是要先把代码编译通过,编译过程可能会出现一些错误,因人而异(网络问题),此处不再展示。

提醒III:由于odl官方每天都在构建版本,所以如果一天无法完成demo,可设置maven settings.xml文件,设置成离线模式,这样即使第二天在编译的时候也不会自动更新版本。具体设置如下,增加一个配置项:

<offline>true</offline>

提醒IV:我采用的系统是centos7.0,开发工具IDEidea-ICLinux下如何配置maven以及Idea如何创建工程不是本篇内容,请自行百度。

 

二、工程配置

为了完成这个demo需要增加一些工程配置,所有源码都会提供。

1、创建工程目录

 为了和odl官方提供的toaster区分开,demo名字改成mytoaster。我们将根目录设置在controller/opendaylight/md-sal,工程pom文件,以odl官方toaster为准,只需要修改groupIdartifactId以及工程名称。具体修改如下:

1)md-sal,目录中创建一个demo2目录,并把md-sal/samples/pom.xml拷贝到demo2中。

2)修改pom文件中groupIdartifactId以及工程名称。

 <groupId>org.opendaylight.controller.demo2</groupId>
  <artifactId>sal-demo2</artifactId>
  <packaging>pom</packaging>
 
  <modules>
    <module>mytoaster</module>
    <module>mytoaster-provider</module>
    <module>mytoaster-consumer</module>
  </modules>

2、增加feature

   odl是以feature方式,部署功能模块,所以需要增加我们自己的feature。修改文件如下:

三、定义模型--mytoaster

 众所周知,在odl中开发一个功能一般是如下三步骤:定义模型、实现服务(模型rpcnotification)、使用服务。其中实现服务和使用服务,可以理解成生产者和消费者。

需要要在demo2目录做:

1、创建目录: mkdir -p mytoaster/src/main/yang/

2、在mytoaster目录中增加pom文件(参考sample中)

  <groupId>org.opendaylight.controller.demo2</groupId>
  <version>0.0.1-SNAPSHOT</version>
  <artifactId>mytoaster</artifactId>
  <packaging>bundle</packaging>

3、yang目录中增加yang文件(参考sample中)

//mytoaster.yang
module mytoaster {
    yang-version 1;
    namespace
      "http://mytoaster.org/toaster";
 
    prefix toast;
    organization "Netconf Central";
    contact
      "Andy Bierman <andy@netconfcentral.org>";
    description
      "YANG version of the TOASTER-MIB.";
    revision "2017-10-14" {
      description
        "Toaster module in progress.";
    }
    identity mytoast-type {
      description
        "Base for all bread types supported by the toaster.
           New bread types not listed here nay be added in the
           future.";
    }
    identity white-bread {
      base toast:mytoast-type;
      description "White bread.";
    }
    identity wheat-bread {
      base mytoast-type;
      description "Wheat bread.";
    }
    identity wonder-bread {
      base mytoast-type;
      description "Wonder bread.";
    }
    identity frozen-waffle {
      base mytoast-type;
      description "Frozen waffle.";
    }
    identity frozen-bagel {
      base mytoast-type;
      description "Frozen bagel.";
    }
    identity hash-brown {
      base mytoast-type;
      description "Hash browned potatos.";
    }
    typedef MyDisplayString {
      type string {
        length "0 .. 255";
      }
      description
        "YANG version of the SMIv2 DisplayString TEXTUAL-CONVENTION.";
      reference
        "RFC 2579, section 2.";
    }
    container mytoaster {
      presence
        "Indicates the toaster service is available";
      description
        "Top-level container for all toaster database objects.";
      leaf toasterManufacturer {
        type MyDisplayString;
        config false;
        mandatory true;
        description
          "The name of the toaster's manufacturer. For instance,
                Microsoft Toaster.";
      }
      leaf toasterModelNumber {
        type MyDisplayString;
        config false;
        mandatory true;
        description
          "The name of the toaster's model. For instance,
               Radiant Automatic.";
      }
      leaf toasterStatus {
        type enumeration {
          enum "idle" {
            value 1;
            description
              "The toaster knob position is up.
                      No toast is being made now.";
          }
          enum "work" {
            value 2;
            description
              "The toaster knob position is down.
                      Toast is being made now.";
          }
        }
        config false;
        mandatory true;
        description
          "This variable indicates the current state of
               the toaster.";
      }
      
      leaf darknessFactor {
        type uint32;
        config true;
        default 1000;
        description
          "The darkness factor. Basically, the number of ms to multiple the doneness value by.";
      }
    }  // container toaster
 
    rpc make-toast {
      description
        "Make some toast.
           The toastDone notification will be sent when
           the toast is finished.
           An 'in-use' error will be returned if toast
           is already being made.
           A 'resource-denied' error will be returned
           if the toaster service is disabled.";
      input {
        leaf toasterDoneness {
          type uint32 {
            range "1 .. 10";
          }
          default '5';
          description
            "This variable controls how well-done is the
                   ensuing toast. It should be on a scale of 1 to 10.
                   Toast made at 10 generally is considered unfit
                   for human consumption; toast made at 1 is warmed
                   lightly.";
        }
 
        leaf toasterToastType {
          type identityref {
            base toast:mytoast-type;
          }
          default 'wheat-bread';
          description
            "This variable informs the toaster of the type of
                   material that is being toasted. The toaster
                   uses this information, combined with
                   toasterDoneness, to compute for how
                   long the material must be toasted to achieve
                   the required doneness.";
        }
      }
      output {
        leaf make-info {
            type string;
        }
      }
    }  // rpc make-toast
 
    rpc cancel-toast {
      description
        "Stop making toast, if any is being made.
           A 'resource-denied' error will be returned
           if the toaster service is disabled.";
    }  // rpc cancel-toast
 
    rpc restock-toaster {
        description
          "Restocks the toaster with the amount of bread specified.";
          
        input {
            leaf amountOfBreadToStock {
                type uint32;
                description
                  "Indicates the amount of bread to re-stock";
            }
        }
    }
    
    notification toasterOutOfBread {
      description
        "Indicates that the toaster has run of out bread.";
    }  // notification toasterOutOfStock
    
    notification toasterRestocked {
      description
        "Indicates that the toaster has run of out bread.";
      leaf amountOfBread {
        type uint32;
        description
          "Indicates the amount of bread that was re-stocked";
      }
    }  // notification toasterOutOfStock
    
  }  // module toaster

4、编译

定义完模型后,需要编译一下:在mytoaster目录中进行编译:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 43.220 s
[INFO] Finished at: 2017-10-21T15:42:53+08:00
[INFO] Final Memory: 82M/312M
[INFO] ------------------------------------------------------------------------
[root@localhost mytoaster]#
[root@localhost mytoaster]# mvn clean install -DskipTests -Dskip.karaf.featureTest=true -Dmaven.test.skip=true -Dcheckstyle.skip=true  -Dmaven.javadoc.skip=true
[root@localhost mytoaster]#

完成以上内容,算是迈出成功的第一步,后面章节开始实现上面rpc以及notification
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值