认识 YANG 文件
Module
一个YANG文件通常可以定义为一个模块(module)或者子模块(submodule)。模块和子模块都可以引用其他的模块模型文件,通过该引用能够使用其他模块定义的数据类型和结构。
模块包含多个申明,包括:
YANG的版本信息,1.0或1.1
YANG的命名空间,IETF YANG或Openconfig YANG
机构信息
联系人
文件描述
修订信息
module example-system { yang-version 1.1; namespace "urn:example:system"; prefix "sys"; organization "Example Inc."; contact "joe@example.com"; description "The module for entities implementing the Example system."; revision 2007-06-09 { description "Initial revision."; } container system { leaf host-name {…} contrainer login{…} } container X {…} }
Leaf Node
Leaf Node 用于定义一个简单指定类型的变量,使用“leaf”关键字申明。
leaf host-name { type string; description "Hostname for this system."; }
在 Leaf Node 中,仅有子声明和值,没有子节点。对应 XML 格式为:
<host-name>my.example.com</host-name>
本例中“host-name”内有两个子声明:
1)“type”含义为取值的类型,本例为“string”字符串。
2)“description”含义为描述。
Leaf List
Leaf List用于定义一个数组类型变量,使用“leaf-list”关键字申明。
leaf-list domain-search { type string; description "List of domain names to search."; }
对应 XML 格式为:
<domain-search>high.example.com</domain-search> <domain-search>low.example.com</domain-search> <domain-search>everywhere.example.com</domain-search>
本例中“domain-search”内有两个子声明:
1)“type”含义为取值的类型,本例为“string”字符串。
2)“description”含义为描述。
List Nodes
List节点用于定义一个更高层次的数据节点。一个List节点使用“key”唯一标识,可以包含多个 Leaf Node。
list user { key "name"; leaf name { type string; } leaf full-name { type string; } leaf class { type string; } }
对应 XML 格式为:
<user> <name>glocks</name> <full-name>Goldie Locks</full-name> <class>intruder</class> </user> <user> <name>snowey</name> <full-name>Snow White</full-name> <class>free-loader</class> </user> <user> <name>rzell</name> <full-name>Rapun Zell</full-name> <class>tower</class> </user>
Container Nodes
Container节点用于定义更大范围的数据集合。
Contrainer节点没有值,只有不同的子节点。这些子节点可以是container、leaf、leaf-list和list等节点。
container system { container login { leaf message { type string; description "Message given at start of login session."; } list user { key "name"; leaf name { type string; } leaf full-name { type string; } leaf class { type string; } }} }
对应 XML 格式:
<system> <login> <message>Good morning</message> </login> </system>
Grouping
Grouping用于定义可以重复使用的节点,一般和uses一起使用。
本例中target定义了leaf address和port。Container peer中声明use target,表示复用此leaf模型。
grouping target { leaf address { type inet:ip-address; description "Target IP address."; } leaf port { type inet:port-number; description "Target port number."; } } container peer { container destination { uses target; } }
数据类型
YANG模型支持的数据类型有:
1)内置的数据类型;
2)扩展的数据类型;“typedef”申明用于新定义扩展数据类型。
内置的数据类型
Name:Description
binary:Any binary data
bits:A set of bits or flags
boolean:”true” or “false”
decimal64:64-bit signed decimal number
empty:A leaf that does not have any value
enumeration:Enumerated strings
identityref:A reference to an abstract identity
instance-identifier:References a data tree node
int8:8-bit signed integer
int16:16-bit signed integer
int32:32-bit signed integer
int64:64-bit signed integer
leafref:A reference to a leaf instance
string:Human-readable string
uint8:8-bit unsigned integer
uint16:16-bit unsigned integer
uint32:32-bit unsigned integer
uint64:64-bit unsigned integer
union:Choice of member types
扩展的数据类型
typedef percent { type uint8 { range "0 .. 100"; } } leaf completed { type percent; }
配置数据和状态数据
“config”声明用于区分配置数据和状态数据:
1)config true表示配置数据。
2)config false表示状态数据。
list interface { key "name"; config true; leaf name { type string; } leaf speed { type enumeration { enum 10m; enum 100m; enum auto; } } leaf observed-speed { type uint32; config false; } }
YANG and NETCONF
YANG 文件是另一种对设备数据的描述方式。YANG是建模语言用于描述NETCONF和RESTCONF的内容层。
1)YANG 起源于 NETCONF,但不仅用于 NETCONF。虽然统一了 YANG 建模语言,但是YANG文件没有统一。
2)YANG文件可以简单分为三类:厂家私有YANG文件;IETF标准YANG;OpenConfig YANG。
3)NETCONF协议中的Config&Status Data、Notification Data、底层的RPC的消息都可以通过YANG模型来建模实现。YANG的模型文件可以通过工具转换到对应格式的XML/JSON文件,被最终的NETCONF/RESTCONF消息封装。
YANG模型使用 module-container-leaf 结构,描述设备。例如路由器的接口、属性等信息,YANG模型详细定义了字段类型和规范。
注:本例为非真实示例,YANG模型不会将整个设备做成一个YANG文件,而是根据功能拆分成多个YANG文件。
NETCONF 1.0对模型语言没有要求。NETCONF1.1确定与YANG结合。