Opendaylight toaster beryllium 版本教程 (1)

来源:互联网 发布:淘宝个性男装店 编辑:程序博客网 时间:2024/05/16 06:54

从头开始搭建一个Opendaylight(ODL)的toaster项目,环境的搭建与调试不再赘述。
我的环境:

  • windows7 32位
  • jdk1.8.0_112
  • mvn3.3.9

首先切换到自己的workspace,使用以下命令来搭建一个mvn archetype,公司使用的是铍SR2版本,因此在官方版本库可以查到其版本号为1.1.2-Beryllium-SR2

以下代码中的-DarchetypeVersion=XXX声明了你所用的版本

mvn archetype:generate -DarchetypeGroupId=org.opendaylight.controller -DarchetypeArtifactId=opendaylight-startup-archetype -DarchetypeVersion=1.1.2-Beryllium-SR2 -DarchetypeRepository=https://nexus.opendaylight.org/content/repositories/public/ -DarchetypeCatalog=https://nexus.opendaylight.org/content/repositories/public/archetype-catalog.xml

对命令行弹出的prompt回应如下:

Define value for property 'groupId': : org.opendaylight.toasterDefine value for property 'artifactId': : toasterDefine value for property 'version':  1.0-SNAPSHOT: : 0.1.0-SNAPSHOTDefine value for property 'package':  org.opendaylight.toaster: : Define value for property 'classPrefix':  ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1)}Define value for property 'copyright': : Copyright(c) Yoyodyne, Inc.

完成之后,项目的目录就会如下所示

[Root directory]   api/   artifacts/   features/   impl/   karaf/   pom.xml

这里包含了五个bundle和一个bundle aggregator, aggregator就是pom.xml文件,这个文件说明了整个包的结构.其中我们主要注意两个bundle

  • api:我们的yang模型属于这个包, 在修改了.yang文件并且编译之后,生产的对应api文件会在这里.
  • impl: 修改.yang 文件并编译后,我们的实现文件应该在这里.

下面我们来修改api/src/main/yang/toaster.yang文件, 这个文件说明了restconf的结构.

//This file contains a YANG data definition. This data model defines//a toaster, which is based on the SNMP MIB Toaster example module toaster {    //The yang version - today only 1 version exists. If omitted defaults to 1.    yang-version 1;    //a unique namespace for this toaster module, to uniquely identify it from other modules that may have the same name.    namespace "urn:opendaylight:params:xml:ns:yang:toaster";    //a shorter prefix that represents the namespace for references used below    prefix "toaster";    //Defines the organization which defined / owns this .yang file.    organization "Netconf Central";    //defines the primary contact of this yang file.    contact      "Andy Bierman <andy@netconfcentral.org>";    //provides a description of this .yang file.    description      "YANG version of the TOASTER-MIB.";    //defines the dates of revisions for this yang file        revision "2015-01-05" {        description "Initial revision of toaster model";    }    //declares a base identity, in this case a base type for different types of toast.    identity toast-type {      description        "Base for all bread types supported by the toaster. New bread types not listed here nay be added in the future.";    }    //the below identity section is used to define globally unique identities    //Note - removed a number of different types of bread to shorten the text length.    identity white-bread {      base toast-type;       //logically extending the declared toast-type above.      description "White bread.";  //free text description of this type.    }    identity wheat-bread {      base toast-type;      description "Wheat bread.";    }    //defines a new "Type" string type which limits the length    typedef DisplayString {      type string {        length "0 .. 255";      }      description        "YANG version of the SMIv2 DisplayString TEXTUAL-CONVENTION.";      reference        "RFC 2579, section 2.";    }    // This definition is the top-level configuration "item" that defines a toaster. The "presence" flag connotes there    // can only be one instance of a toaster which, if present, indicates the service is available.    container toaster {      presence        "Indicates the toaster service is available";      description        "Top-level container for all toaster database objects.";      //Note in these three attributes that config = false. This indicates that they are operational attributes.      leaf toasterManufacturer {        type DisplayString;        config false;        mandatory true;        description          "The name of the toaster's manufacturer. For instance, Microsoft Toaster.";      }      leaf toasterModelNumber {        type DisplayString;        config false;        mandatory true;        description          "The name of the toaster's model. For instance, Radiant Automatic.";      }      leaf toasterStatus {        type enumeration {          enum "up" {            value 1;            description              "The toaster knob position is up. No toast is being made now.";          }          enum "down" {            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.";      }    }  // container toaster}// module toaster

这里面声明了各种各样的服务, 比如toaster(烤面包机)能做的面包类型: white-bread, wheat-bread, 还有toaster的配置: 它有一个manufacture,一个model number 及一个status. 然后我们在toaster文件夹的根目录下运行 mvn clean install -DskipTests -Dcheckstyle.skip=true -Dmaven.javadoc.skip=true , 等到mvn编译完成. 我们的toaster就搭建好了!

然后我们可以使用各种工具来测试我们的toaster了. 我使用的是postman, 因为它的图形化界面比较直观(好看)

在toaster的根目录下,运行 .\karaf\target\assembly\bin\karaf.bat, 等一会儿之后服务器就启动了,可以用浏览器打开localhost:8181/index.html 查看opendaylight的客户端. 帐号密码都是admin.

使用postman发送以下的http请求

HTTP Method => GETURL => http://localhost:8181/restconf/config/toaster:toasterHeader => Accept: application/json       => Authorization: Basic admin:admin

会得到

HTTP/1.1 404 Not Found

因为现在配置信息还没写到toaster的服务器里, 然后发送

HTTP Method => PUTURL => http://localhost:8181/restconf/config/toaster:toasterHeader => Content-type: application/json       => Authorization: Basic admin:adminBody =>  {    "toaster": {        "darknessFactor": 500    }}

会得到 HTTP/1.1 200 OK 并且服务器会返回你传过去的数据. 然后发送

HTTP Method => POSTURL => http://localhost:8181/restconf/operations/toaster:make-toast Header =>   Content-Type: application/yang.data+json         =>   Authorization: Basic admin:adminBody =>  {  "input" :  {     "toaster:toasterDoneness" : "10",     "toaster:toasterToastType":"wheat-bread"  }}

应该会得到

{   "errors": {       "error": [           {               "error-type": "rpc",               "error-tag": "operation-not-supported",               "error-message": "No implementation for this operation is available."           }       ]   }}

因为我们只在.yang文件里面声明了一个rpc服务,但尚未实现它. 在这第一篇教程里我们搭建了一个toaster,具体的实现方法我会在后面的教程里继续.

0 0
原创粉丝点击