在研究netconf的時(shí)候驰弄,YANG(RFC6020)是一定繞不過(guò)的》蠼茫花了一些時(shí)間看RFC6020玉组,有一點(diǎn)初步的理解,記錄下來(lái)方便后面查看凸舵。
1 為什么要有YANG
netconf需要對(duì)設(shè)備的配置(configuration)和狀態(tài)(state)做操作,例如編輯配置,獲取狀態(tài)台丛,因此需要一種語(yǔ)言來(lái)對(duì)configuration和state進(jìn)行建模,甚至連“操作”也可以通過(guò)YANG來(lái)建模恋博。建好的模型齐佳,最后以XML的形式進(jìn)行實(shí)例化。打個(gè)比方债沮,我需要向領(lǐng)導(dǎo)請(qǐng)假炼吴,領(lǐng)導(dǎo)說(shuō)你寫(xiě)個(gè)請(qǐng)假單,包含請(qǐng)假人的姓名疫衩,請(qǐng)假的起止時(shí)間硅蹦,請(qǐng)假事由和代理人。于是我做了一個(gè)表格闷煤,包含了上述要求童芹,并根據(jù)實(shí)際情況填入了真實(shí)信息。那么領(lǐng)導(dǎo)的描述鲤拿,就可以理解為“建募偻剩”,而我最后提交的填好內(nèi)容的表格近顷,就是將模型實(shí)例化了生音。
2 建模需要什么呢
如果要建模宁否,一定需要一些基礎(chǔ)架構(gòu),然后將要建立的模型用這些基礎(chǔ)架構(gòu)拼接出來(lái)缀遍。那么YANG提供了哪些架構(gòu)呢慕匠?
2.1 module和submodule
module的header主要是一些描述信息,而body就是data model定義的地方域醇。module可以分為submodule台谊。模塊化的好處就是方便引用。
2.2 Data Modeling Basics
這時(shí)候要敲敲黑板譬挚,因?yàn)橹攸c(diǎn)來(lái)嘍锅铅。這里介紹最最基本的四種建模架構(gòu)。
2.2.1 Leaf Nodes
一個(gè)leaf node包含且只包含一個(gè)value减宣,可以是數(shù)字或是字符串狠角,具體是什么,看關(guān)鍵字"type"后面跟什么蚪腋。leaf node下面不能掛子節(jié)點(diǎn)丰歌。
例如:
YANG Example:
leaf host-name {
type string;
description "Hostname for this system";
}
-----------------------
NETCONF XML Example:
<host-name>my.example.com</host-name>
此處用YANG定義了一個(gè)名為host-name的leaf,它包含對(duì)自己的description屉凯,有一個(gè)string類(lèi)型的值立帖。那么當(dāng)用XML實(shí)例化這個(gè)leaf的時(shí)候,就需要對(duì)host-name進(jìn)行具體的賦值悠砚。換句話(huà)說(shuō)晓勇,YANG是挖坑的,XML是填坑的灌旧,但是XML填坑用的材料的“形狀”绑咱,要和YANG定義的一樣。
2.2.2 Leaf-List Nodes
與上面的Leaf Nodes“一字之差”枢泰,多了一個(gè)“-list”描融。可以認(rèn)為L(zhǎng)eaf-List Nodes表示的是一個(gè)“數(shù)組”衡蚂,“數(shù)組”中的元素的值的type必須保持一致窿克,而且不能重復(fù)。
例如:
YANG Example:
leaf-list domain-search {
type string;
description "List of domain names to search";
}
-----------------------
NETCONF XML Example:
<domain-search>high.example.com</domain-search>
<domain-search>low.example.com</domain-search>
<domain-search>everywhere.example.com</domain-search>
和leaf node一樣毛甲,它也只定義一個(gè)value年叮,但是可以有一系列同類(lèi)型的值。例子中<domain-serarch>的值有多個(gè)玻募,但是定義和類(lèi)型都是統(tǒng)一的只损。
2.2.3 Container Nodes
“Container”可以翻譯成“集裝箱”。真正有價(jià)值的七咧,是集裝箱里面裝的貨物跃惫,而不是集裝箱本身啸蜜。但是如果沒(méi)有集裝箱,那么里面的貨物就散了辈挂。
Container的作用就是將數(shù)據(jù)層次化組織起來(lái),呈現(xiàn)出subtree的樣式裹粤。特別需要注意的是:
(1)一個(gè)空的集裝箱也是能“賣(mài)錢(qián)”的终蒂,因?yàn)楫吘故氰F皮做的,但是一個(gè)container自身是沒(méi)有“value”的遥诉;
(2)一個(gè)集裝箱容量是有限的拇泣,但是一個(gè)container可以裝多少node并沒(méi)有限制,而且這些node可以在leaf/list/leaf-list甚至是container(想起了俄羅斯套娃)中任意選取矮锈。
例如:
YANG Example:
container system {
container login {
leaf message {
type string;
description
"Message given at start of login session";
}
}
}
-----------------------
NETCONF XML Example:
<system>
<login>
<message>Good morning</message>
</login>
</system>
container system里面裝了一個(gè)container login霉翔,然后login里面有一個(gè)leaf node,就是類(lèi)型為string的“message”苞笨。
2.2.4 List Nodes
一個(gè)List node可以包含多個(gè)child node债朵,而且這些node可以在leaf/leaf-list/container中任意選取。List必須指明這些child中的一個(gè)node為key瀑凝。
例如:
YANG Example:
list user {
key "name";
leaf name {
type string;
}
leaf full-name {
type string;
}
leaf class {
type string;
}
}
-----------------------
NETCONF XML Example:
<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>
定義了一個(gè)名為“user”的list序芦,這個(gè)list中包含三個(gè)leaf:name/full-name/class。其中name被指定為key粤咪。實(shí)例化的時(shí)候谚中,key的值(也就是"name"的值)是必須不同的,其它的值(full-name/class)沒(méi)有這個(gè)要求寥枝。隨后xml實(shí)例化了三個(gè)user宪塔,都包含YANG定義的name/full-name/class,而且name都是不同的囊拜。
2.2.5 Combined Module
RFC中給出了一個(gè)綜合上述四種node的混合模式的例子如下:
// Contents of "acme-system.yang"
module acme-system {
namespace "http://acme.example.com/system";
prefix "acme";
organization "ACME Inc.";
contact "joe@acme.example.com";
description
"The module for entities implementing the ACME system.";
revision 2007-06-09 {
description "Initial revision.";
}
container system {
leaf host-name {
type string;
description "Hostname for this system";
}
leaf-list domain-search {
type string;
description "List of domain names to search";
}
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;
}
}
}
}
}
-----------------------
NETCONF XML Example:
<system>
<host-name>myyang.com</host-name>
<domain-search>high.example.com</domain-search>
<domain-search>low.example.com</domain-search>
<domain-search>everywhere.example.com</domain-search>
<login>
<message>Good Morning</message>
<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>
</login>
</system>
對(duì)YANG module的解讀:
- module的名字是acme-system
- namespace是用來(lái)唯一標(biāo)識(shí)這個(gè)YANG模型與其它YANG模型不同
- prefix是namespace的一種簡(jiǎn)寫(xiě)
- organization/contact/description都是用來(lái)描述相關(guān)信息
- revison描述版本信息某筐,可以有多個(gè)revision(一般記錄版本更新的內(nèi)容)
- module中包含一個(gè)container system
- container system包含一個(gè)leaf(host-name),一個(gè)leaf-list(domain-search)和一個(gè)container login
- container login包含一個(gè)leaf(message)冠跷,和一個(gè)list(user)
下面的XML只要按照YANG Module的規(guī)定来吩,實(shí)例化即可。
2.3 State Data
netconf需要區(qū)分configuration data和state(狀態(tài)) data蔽莱,在YANG建模的時(shí)候弟疆,對(duì)于state data需要加上"config false".例如:
list interface {
key "name";
leaf name {
type string;
}
leaf speed {
type enumeration {
enum 10m;
enum 100m;
enum auto;
}
}
leaf observed-speed {
type uint32;
config false;
}
}
好吧,10m/100m確實(shí)暴露了這篇RFC的“年齡”盗冷,現(xiàn)在交換機(jī)的端口帶寬已經(jīng)可以達(dá)到100G甚至更高了怠苔。在list interface中。speed的value type是枚舉類(lèi)型仪糖,就是說(shuō)實(shí)例化的時(shí)候只能從這里列出的三種中選擇一個(gè)柑司。對(duì)于leaf observed-speed,因?yàn)榘?config false"迫肖,因此這個(gè)leaf記錄的是state value,不可以配置攒驰。對(duì)應(yīng)于netconf的操作蟆湖,leaf speed可以<get-config>,而leaf observed-speed只能<get>玻粪。
2.4 Build-in Type
前面給leaf或是leaf-list定義類(lèi)型的時(shí)候舉的例子隅津,type后面跟的都是string。實(shí)際上string只是YANG build-in(內(nèi)建)數(shù)據(jù)類(lèi)型中的一種劲室。下面羅列一下YANG所有的build-in types
+---------------------+-------------------------------------+
| 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 |
+---------------------+-------------------------------------+
2.5 Derived Type
在實(shí)際建模中伦仍,上述的type肯定是不夠的。YANG允許用戶(hù)使用typedef來(lái)定義自己需要的type很洋,可以基于build-in type或是另外一個(gè)派生的type充蓝。
例如需要一個(gè)描述百分比的type:
YANG Example:
typedef percent {
type uint8 {
range "0 .. 100";
}
description "Percentage";
}
leaf completed {
type percent;
}
-----------------------
NETCONF XML Example:
<completed>20</completed>
- 通過(guò)typedef定義了新的type"percent",基于uint8(0-255的無(wú)符號(hào)整數(shù)),并進(jìn)一步限制取值范圍為[0,100]
- 定義了leaf completed(完成百分比)喉磁,使用的type正是上面定義的percent
- XML實(shí)例化completed時(shí)候給出的數(shù)值是20谓苟,符合percent的type定義
- 如果netconf交互的時(shí)候,completed傳的數(shù)值如果不符合YANG的描述(例如小數(shù)/負(fù)數(shù)/200)协怒,會(huì)因?yàn)闊o(wú)法通過(guò)模型check而被拒絕
2.6 Reusable Node Groups (grouping)
grouping其實(shí)不是一種數(shù)據(jù)類(lèi)型娜谊,它存在的意義只是方便在“編程”的時(shí)候被引用。所以它本身是沒(méi)有value的斤讥。grouping可以在本module被引用纱皆,或是被其它module引用。
grouping可以包含
+--------------+---------+-------------+
| substatement | section | cardinality |
+--------------+---------+-------------+
| anyxml | 7.10 | 0..n |
| choice | 7.9 | 0..n |
| container | 7.5 | 0..n |
| description | 7.19.3 | 0..1 |
| grouping | 7.11 | 0..n |
| leaf | 7.6 | 0..n |
| leaf-list | 7.7 | 0..n |
| list | 7.8 | 0..n |
| reference | 7.19.4 | 0..1 |
| status | 7.19.2 | 0..1 |
| typedef | 7.3 | 0..n |
| uses | 7.12 | 0..n |
+--------------+---------+-------------+
例如:
YANG Example:
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;
}
}
-----------------------
NETCONF XML Example:
<peer>
<destination>
<address>192.0.2.1</address>
<port>830</port>
</destination>
</peer>
回想一下芭商,當(dāng)一臺(tái)主機(jī)對(duì)外提供服務(wù)的時(shí)候派草,客戶(hù)端需要知道提供服務(wù)主機(jī)的的IP和端口信息。所以這邊可以定義一個(gè)grouping target铛楣,在里面定義leaf address和leaf port近迁。下面的container peer來(lái)uses(調(diào)用grouping的關(guān)鍵字)這個(gè)grouping。當(dāng)對(duì)<peer>實(shí)例化的時(shí)候簸州,就需要將grouping target中包含的address和port都進(jìn)行賦值鉴竭。這里的830端口,是在RFC6242(Using the NETCONF Protocol over Secure Shell)中定義的基于ssh的netconf服務(wù)端口岸浑。
這里的users可以理解為copy搏存,即把grouping的整個(gè)內(nèi)容都復(fù)制到了這個(gè)schema tree。grouping本身是沒(méi)有綁定到任何namespace的矢洲,直到某個(gè)module uses了這個(gè)grouping璧眠,那么這個(gè)grouping就被綁定到這個(gè)module了。
grouping還有一個(gè)非常好用的特性就是refine,例如要建立連接责静,既需要server的IP+port袁滥,也需要client的IP+port。因?yàn)檫@兩個(gè)的數(shù)據(jù)結(jié)構(gòu)是完全一樣的灾螃,所以可以復(fù)用题翻,并用更準(zhǔn)確的description來(lái)覆蓋grouping定義時(shí)候設(shè)置的description。
YANG Example:
container connection {
container source {
uses target {
refine "address" {
description "Source IP address";
}
refine "port" {
description "Source port number";
}
}
}
container destination {
uses target {
refine "address" {
description "Destination IP address";
}
refine "port" {
description "Destination port number";
}
}
}
}
2.7 Choices
"choice"+"case"用來(lái)描述互斥的內(nèi)容腰鬼。一個(gè)choice可以包含多個(gè)case嵌赠,每個(gè)case可以包含多個(gè)node。一般來(lái)說(shuō)在一個(gè)choice里垃喊,一個(gè)case下面的node不應(yīng)該和其他case下面的node重復(fù)。
在YANG模型和schema中可以看到choice下的所有case袜炕,而當(dāng)對(duì)它實(shí)例化之后本谜,只會(huì)出現(xiàn)一個(gè)case下面的nodes了。例如
YANG Example:
container food {
choice snack {
case sports-arena {
leaf pretzel {
type empty;
}
leaf beer {
type empty;
}
}
case late-night {
leaf chocolate {
type enumeration {
enum dark;
enum milk;
enum first-available;
}
}
}
}
}
-----------------------
NETCONF XML Example:
<food>
<pretzel/>
<beer/>
</food>
2.8 Extending Data Models (augment)
YANG允許向data module插入新的節(jié)點(diǎn)偎窘。這是一個(gè)非常有用的特性乌助,例如廠商想在公共yang上插入自己的特殊參數(shù),那么augment就可以實(shí)現(xiàn)這個(gè)需求陌知。"when"后面跟的是條件他托,即為滿(mǎn)足這個(gè)條件,就插入新的node仆葡。例如
YANG Example:
augment /system/login/user {
when "class != 'wheel'";
leaf uid {
type uint16 {
range "1000 .. 30000";
}
}
}
-----------------------
NETCONF XML Example 1:
<user>
<name>alicew</name>
<full-name>Alice N. Wonderland</full-name>
<class>drop-out</class>
<other:uid>1024</other:uid>
</user>
-----------------------
NETCONF XML Example 2:
<user>
<name>jerryr</name>
<full-name>Jerry K. Roma</full-name>
<class>wheel</class>
</user>
Example 1中因?yàn)閏lass不是"wheel"赏参,因此插入uid; Example 2中class是"wheel",所以不插入uid沿盅。
2.9 RPC Definitions
YANG可以用來(lái)定義netconf的rpc把篓,包括rpc輸入的參數(shù),輸出的參數(shù)腰涧,例如:
YANG Example:
rpc activate-software-image {
input {
leaf image-name {
type string;
}
}
output {
leaf status {
type string;
}
}
}
-----------------------
NETCONF XML Example:
<rpc message-id="101"
xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<activate-software-image xmlns="http://acme.example.com/system">
<image-name>acmefw-2.3</image-name>
</activate-software-image>
</rpc>
<rpc-reply message-id="101"
xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<status xmlns="http://acme.example.com/system">
The image acmefw-2.3 is being installed.
</status>
</rpc-reply>
定義一種叫做"activate-software-image"的rpc韧掩。當(dāng)Client發(fā)送rpc的時(shí)候,需要加上image-name窖铡,交換機(jī)發(fā)回rpc-reply的時(shí)候疗锐,需要加上activate的結(jié)果,本例中可以看到操作是成功了费彼。
2.10 Notification Definitions
Notification是一種通告機(jī)制滑臊,當(dāng)交換機(jī)上出現(xiàn)特性event(事件),交換機(jī)會(huì)主動(dòng)發(fā)給已經(jīng)建立netconf連接并訂閱了Notification的client箍铲。YANG可以定義notification简珠,例如
YANG Example:
notification link-failure {
description "A link failure has been detected";
leaf if-name {
type leafref {
path "/interface/name";
}
}
leaf if-admin-status {
type admin-status;
}
leaf if-oper-status {
type oper-status;
}
}
-----------------------
NETCONF XML Example:
<notification
xmlns="urn:ietf:params:netconf:capability:notification:1.0">
<eventTime>2007-09-01T10:00:00Z</eventTime>
<link-failure xmlns="http://acme.example.com/system">
<if-name>so-1/2/3.0</if-name>
<if-admin-status>up</if-admin-status>
<if-oper-status>down</if-oper-status>
</link-failure>
</notification>
這樣無(wú)論是接口的admin狀態(tài)(shutdown或是no shutdonw),還是鏈路狀態(tài)(down/up)發(fā)送改變的時(shí)候,都可以發(fā)送notification給client聋庵,并且?guī)狭水?dāng)前的狀態(tài)信息膘融。
3 學(xué)以致用
上面羅列了很多YANG語(yǔ)言建模的細(xì)節(jié),但是還遠(yuǎn)遠(yuǎn)沒(méi)有列全祭玉。但是如果仔細(xì)看了上面的內(nèi)容氧映,見(jiàn)到y(tǒng)ang模型應(yīng)該不會(huì)完全手足無(wú)措了。例如下面列一個(gè)華為的huawei-netconf.yang來(lái)體會(huì)一下
/*
Copyright (C) 2013-2017 Huawei Technologies Co., Ltd. All rights reserved.
*/
module huawei-netconf {
namespace "http://www.huawei.com/netconf/vrp/huawei-netconf";
prefix netconf;
include huawei-netconf-type;
include huawei-netconf-authorization;
include huawei-netconf-notification;
include huawei-netconf-authorization-type;
include huawei-netconf-notification-type;
organization
"Huawei Technologies Co.,Ltd.";
contact
"Huawei Industrial Base Bantian, Longgang Shenzhen 518129
People's Republic of China
Website: http://www.huawei.com Email: support@huawei.com";
description
"The NETCONF protocol defines a simple mechanism through which a network device can be managed, configuration data information can be retrieved, and new configuration data can be uploaded and manipulated.";
revision 2017-03-23 {
description
"Functions supported by the schema are added to the YANG file.";
reference
"Huawei private.";
}
revision 2013-01-01 {
description
"Init revision";
reference
"Huawei private.";
}
container netconf {
description
"The NETCONF protocol defines a simple mechanism through which a network device can be managed, configuration data information can be retrieved, and new configuration data can be uploaded and manipulated.";
container netconfCapabilitys {
config false;
description
"NETCONF capability list.";
list netconfCapability {
key "capabilityName version";
config false;
description
"NETCONF capability.";
leaf capabilityName {
type netconfNcaCapability;
config false;
description
"Name of the NETCONF capability.";
}
leaf version {
type netconfCapabilityVersion;
config false;
description
"Capability version number.";
}
leaf scope {
type netconfCapabilityScope;
config false;
description
"Scope of the capability.";
}
}
}
container authorization {
description
"NETCONF authorization.";
uses netconf:netconf_authorization_type;
}
container notification {
config false;
description
"notification";
uses netconf:netconf_notification_type;
}
container operationLogSwitch {
description
"Switch for RPC oper log.";
leaf get {
type boolean;
description
"Get oper type.";
}
}
}
}