一、什么是YANG语言
YANG is Yet Another Next Generation modeling language for Network Configuration Protocol. Itis first designed for state synchronization between devices and state storage but is also used for service layer abstraction.YANG是网络配置协议的另一种下一代建模语言。它最初被设计 用于设备和状态存储之间的状态同步 ,但也用于服务层抽象。
Martin Björklund. YANG - a Data Modeling Language for the Network Configuration
二、YANG语言数据结构
1.Module
YANG的顶层结构是Module
A module contains• statements to express meta information about the module (namespace, prefix, date revision)• statements to import dependent modules• statements to define data trees一个模块包含
•表达模块元信息的语句(命名空间,前缀,日期修订)
•导入依赖模块的语句
•定义数据树的语句
子声明类型:
Module规范和导入示例
module example1 {
namespace "urn:examples:example1";
prefix "example1";
revision "2021-09-15" {
description "Initial revision.";
}
typedef score {
type uint8 {
range "0..100";
}
}
...
}
module example2 {
namespace "urn:examples:example2";
prefix "example2";
revision "2021-09-15" {
description "Initial revision.";
}
import "example1" {
prefix "abc";
}
...
typedef score-s {
type "abc:score" {
range "90..100";
}
}
}
2.类型系统
YANG语言用于将数据模型指定为树状结构
Types for leaf and leaf-list nodes:• built-in types• types defined by ``typedef'' statementTypes for non-leaf nodes:• container• list叶节点和叶列表节点的类型:
•内置类型
•由“typedef”语句定义的类型
非叶节点类型:
•容器
•列表
内置类型:
示例程序:
我们使用下面的示例来说明如何为不同类型的节点构建数据树
该代码建立如下的树形结构:
每个模块都有一个隐式根节点 root,按名称查找顶级节点。
容器节点 a 有一个实例,并按成员名查找子树
一个列表节点 b 包含多个实例
列表中的实例是一个数据容器,它按成员名查找子树
叶节点 c 的类型为“string”,值为字符串
叶列表节点 d 的类型为“int32”,值是一个32位整数列表
3.顶级类型
(1)Data Tree
任何数据树语句(container, list, leaf, leaf-list, etc.)都会在Module中创建一个数据树
(2)RPC
RPC是用RPC语句创建的
•input语句中的数据树指定输入格式
•output语句中的数据树指定输出格式
(3)Notifications
使用notification语句创建通知
定义方法示例:
4.Config v.s. Operational
在数据树中,可以使用config参数指定它是属于配置和操作数据树,还是只属于操作数据树
5.List
YANG uses list with keys to realize maps
YANG 使用 带有 keys 的 List 来实现 maps
6.代码重用
The grouping statement allows the same subtree structure to be reused
分组语句允许重用相同的子树结构
右边的模型重用了左边的模型中的 grouping 结构中声明的子树类型
7.使用 Augmentation 进行扩展
YANG allows a new module to extend an old module's data/rpc/notification tree
YANG允许新模块扩展旧模块的data/rpc/notification树