什么是YANG?
YANG是一种数据建模语言。YANG模型定义了数据的层次化结构,可用于基于网络配置管理协议(例如NETCONF/RESTCONF)的操作,包括配置、状态数据、远程过程调用和通知。YANG相对于SNMP的模型MIB,更有层次化,能够区分配置和状态,可扩展性强。
YANG模型简介
YANG是一种语言,是用来建立数据模型的语言。建模语言之前就存在,YANG作为新一代的语言,定位为Yet Another Next Generation。
YANG用于为网络配置管理协议(例如NETCONF,RESTCONF)使用的配置数据、状态数据、远程过程调用和通知进行模型化。通过YANG描述数据结构、数据完整性约束、数据操作,形成了一个个YANG模型(或者叫YANG文件)。
YANG在以下RFC标准中定义:
- RFC 6020:2010年IETF组织对YANG进行了第一次标准定义。YANG是NETCONF(网络配置协议)的数据建模语言。
- RFC 6021:2010年IETF定义了网络通信技术里面常用的各种数据类型。我们在建立自己的YANG模型的时候,可以导入使用这些预先定义好的网络数据类型,无需重新定义。
- RFC 6991:2013年IETF在[RFC6021]基础上新增补充了YANG模型的数据类型。
- RFC 7950:2016年IETF发布了YANG1.1版本,矫正了在初始版本[RFC6020]中的歧义和缺陷。
随着标准化的推行,YANG正逐渐成为业界主流的数据描述规范,标准组织、厂商、运营商、OTT纷纷定义各自的YANG模型。如图,设备上集成了YANG模型并作为Server端,网络管理员可以利用NETCONF协议或RESTCONF协议统一管理、配置、监控已经支持YANG的各类网络设备,从而简化网络运维管理,降低运维成本。
基于NETCONF/RESTCONF和YANG的网络管理架构
为什么选择YANG?
2002年IAB(因特网架构委员会)会议提出SNMP在配置管理上有不少劣势,从而触发了NETCONF的诞生。NETCONF协议标准化了,但却没有对数据内容标准化,从而触发了更优秀的模型语言YANG的出现,使得数据模型更加简单易懂。
YANG相对于SNMP的模型MIB,更有层次化,能够区分配置和状态,可扩展性强。
这里先取if-mib文件部分内容来直观感受下。MIB是一个平铺的表,一个IfEntry下所有元素都并排的列在一起,也区分不了哪些属于配置数据,哪些属于状态数据。
ifEntry OBJECT-TYPE SYNTAX IfEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "An entry containing management information applicable to a particular interface." INDEX { ifIndex } ::= { ifTable 1 } IfEntry ::= SEQUENCE { ifIndex InterfaceIndex, ifDescr DisplayString, ifType IANAifType, ifMtu Integer32, ifSpeed Gauge32, ifPhysAddress PhysAddress, ifAdminStatus INTEGER, ifOperStatus INTEGER, ifLastChange TimeTicks, ifInOctets Counter32, ifInUcastPkts Counter32, ifInNUcastPkts Counter32, -- deprecated ifInDiscards Counter32, ifInErrors Counter32, ifInUnknownProtos Counter32, ifOutOctets Counter32, ifOutUcastPkts Counter32, ifOutNUcastPkts Counter32, -- deprecated ifOutDiscards Counter32, ifOutErrors Counter32, ifOutQLen Gauge32, -- deprecated ifSpecific OBJECT IDENTIFIER -- deprecated }
然后取huawei-ifm.yang的部分内容做一个简单的YANG模型组成介绍。模型开头部分是描述huawei-ifm.yang的基本信息,字段含义见表格。其中Container,list,leaf则是YANG的定义的一些模型节点类型,这些节点类型可以用来清晰地划分层次。
元素 |
描述 |
---|---|
module |
YANG将数据模型构建为模块,模块名与YANG文件名一致。 一个模块可以从其他模块中导入数据,也可以从子模块中引用数据。 外部模块的引入(import & include):“include”声明允许模块或者子模块引用子模块中的材料,“import”声明允许引用其他模块中定义的材料。 |
namespace |
模块的名字空间,是全球唯一的URI。在对数据的XML编码过程中会使用到名字空间。 |
prefix |
namespace的简写,简写唯一。 |
organization |
YANG归属组织名。 |
contact |
YANG模块的联系信息。 |
description |
YANG模块的功能描述。 |
revision |
YANG模块的版本信息,提供了模块的编辑版本历史。 |
container |
容器节点,用来描述若干相关节点的集合。 |
list |
列表节点,定义了列表条目序列,每个条目就像一个结构体或者一个记录实例,由其关键叶节点的值(key值)唯一识别。 |
leaf |
叶节点。一个叶节点包含简单的数据,如整形数据或字符串。 |
YANG模型示例:
module huawei-ifm { namespace "urn:huawei:yang:huawei-ifm"; prefix ifm; import huawei-pub-type { prefix pub-type; } organization "Huawei Technologies Co., Ltd."; contact "Huawei Industrial Base Bantian, Longgang Shenzhen 518129 People's Republic of China Website: https://www.huawei.com Email: support@huawei.com"; description "Common interface management, which includes the public configuration of interfaces."; revision 2020-06-10 { description "Add units attribute."; reference "Huawei private."; } container auto-recovery-times { description "List of automatic recovery time configuration."; list auto-recovery-time { key "error-down-type"; description "Configure automatic recovery time."; leaf error-down-type { type error-down-type; description "Cause of the error-down event."; leaf time-value { type uint32 { range "30..86400"; } units "s"; mandatory true; description "Delay for the status transition from down to up."; } } }
使用pyang工具可以把YANG模型转换成YANG Tree视图,就是整个YANG模型以一棵树的形式展示。在树中,rw代表状态数据,ro代表配置数据。
module: huawei-ifm +--rw ifm +--rw auto-recovery-times | +--rw auto-recovery-time* [error-down-type] | +--rw error-down-type error-down-type | +--rw time-value uint32 +--ro static-dimension-ranges | +--ro static-dimension-range* [type] | +--ro type port-type | +--ro chassis-range? string | +--ro slot-range? string | +--ro card-range? string | +--ro port-range? string
所以YANG具有简单易懂、层次化、模块化、可复用、可扩展很多优点,当然它的优点不仅仅这些,欢迎大家交流指出。
YANG和YIN
设备解析模型时用YIN(YANG Independent Notation)模型文件。YIN是XML表达方式的YANG,YIN与YANG之间使用不同的表达方法但包含等价的信息。
YANG和YIN的转换示例
用YIN是为了利用各编程语言中现有的XML解析器等工具。这些工具可用来进行数据过滤和验证,自动生成代码和文件或者其他任务,这样可以提升设备解析YANG模型的效率。
一个简单的YANG报文示例
示例:查询数据库中IFM特性的接口配置,RPC应答报文返回查询到的接口信息。
RPC请求
<?xml version="1.0" encoding="utf-8"?> <rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="831"> <get> <filter type="subtree"> <ifm:ifm xmlns:ifm="urn:huawei:yang:huawei-ifm"> <ifm:interfaces> <ifm:interface/> </ifm:interfaces> </ifm:ifm> </filter> </get> </rpc>
RPC应答:查询并返回了10GE1/0/1接口的配置。
<?xml version="1.0" encoding="utf-8"?> <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <ifm xmlns="urn:huawei:yang:huawei-ifm"> <interfaces> <interface> <name>10GE1/0/1</name> <index>4</index> <class>main-interface</class> <type>10GE</type> <position>0/0/0</position> <number>1/0/1</number> <admin-status>up</admin-status> <link-protocol>ethernet</link-protocol> <statistic-enable>true</statistic-enable> <mtu>1500</mtu> <spread-mtu-flag>false</spread-mtu-flag> <vrf-name>_public_</vrf-name> <dynamic> <oper-status>up</oper-status> <physical-status>up</physical-status> <link-status>up</link-status> <mtu>1500</mtu> <bandwidth>100000000</bandwidth> <ipv4-status>up</ipv4-status> <ipv6-status>down</ipv6-status> <is-control-flap-damp>false</is-control-flap-damp> <mac-address>00e0-fc12-3456</mac-address> <line-protocol-up-time>2019-05-25T02:33:46Z</line-protocol-up-time> <is-offline>false</is-offline> <link-quality-grade>good</link-quality-grade> </dynamic> <mib-statistics> <receive-byte>0</receive-byte> <send-byte>0</send-byte> <receive-packet>363175</receive-packet> <send-packet>61660</send-packet> <receive-unicast-packet>66334</receive-unicast-packet> <receive-multicast-packet>169727</receive-multicast-packet> <receive-broad-packet>127122</receive-broad-packet> <send-unicast-packet>61363</send-unicast-packet> <send-multicast-packet>0</send-multicast-packet> <send-broad-packet>299</send-broad-packet> <receive-error-packet>0</receive-error-packet> <receive-drop-packet>0</receive-drop-packet> <send-error-packet>0</send-error-packet> <send-drop-packet>0</send-drop-packet> </mib-statistics> <common-statistics> <stati-interval>300</stati-interval> <in-byte-rate>40</in-byte-rate> <in-bit-rate>320</in-bit-rate> <in-packet-rate>2</in-packet-rate> <in-use-rate>0.01%</in-use-rate> <out-byte-rate>0</out-byte-rate> <out-bit-rate>0</out-bit-rate> <out-packet-rate>0</out-packet-rate> <out-use-rate>0.00%</out-use-rate> <receive-byte>0</receive-byte> <send-byte>0</send-byte> <receive-packet>363183</receive-packet> <send-packet>61662</send-packet> <receive-unicast-packet>66334</receive-unicast-packet> <receive-multicast-packet>169727</receive-multicast-packet> <receive-broad-packet>127122</receive-broad-packet> <send-unicast-packet>61363</send-unicast-packet> <send-multicast-packet>0</send-multicast-packet> <send-broad-packet>299</send-broad-packet> <receive-error-packet>0</receive-error-packet> <receive-drop-packet>0</receive-drop-packet> <send-error-packet>0</send-error-packet> <send-drop-packet>0</send-drop-packet> <send-unicast-bit>0</send-unicast-bit> <receive-unicast-bit>0</receive-unicast-bit> <send-multicast-bit>0</send-multicast-bit> <receive-multicast-bit>0</receive-multicast-bit> <send-broad-bit>0</send-broad-bit> <receive-broad-bit>0</receive-broad-bit> <send-unicast-bit-rate>0</send-unicast-bit-rate> <receive-unicast-bit-rate>0</receive-unicast-bit-rate> <send-multicast-bit-rate>0</send-multicast-bit-rate> <receive-multicast-bit-rate>0</receive-multicast-bit-rate> <send-broad-bit-rate>0</send-broad-bit-rate> <receive-broad-bit-rate>0</receive-broad-bit-rate> <send-unicast-packet-rate>0</send-unicast-packet-rate> <receive-unicast-packet-rate>0</receive-unicast-packet-rate> <send-multicast-packet-rate>0</send-multicast-packet-rate> <receive-multicast-packet-rate>0</receive-multicast-packet-rate> <send-broadcast-packet-rate>0</send-broadcast-packet-rate> <receive-broadcast-packet-rate>0</receive-broadcast-packet-rate> </common-statistics> </interface> </ifm> </data>
获取HUAWEI YANG
目前华为设备支持的YANG模型主要分为:
- HUAWEI-YANG:华为私有YANG模型。模型命名以huawei开头。
- IETF-YANG:IETF标准组织定义的公有YANG模型。模型命名一般以ietf开头,也有非ietf开头的命名。
- OPENCONFIG-YANG:OPENCONFIG标准组织定义的公有YANG模型,一般也称为OC YANG。模型命名以openconfig开头。
可以通过登录华为技术支持网站获取YANG模型:
- 运营商用户登录到https://support.huawei.com,浏览或搜索产品名称。在 页签选择需要的版本,下载YANG文件,一般命名为“产品名称+版本号+yang”。
- 企业用户登录到https://support.huawei.com/enterprise/,浏览或搜索产品名称。在产品的 页签选择需要的版本,下载YANG文件,一般命名为“产品名称+版本号+yang”。
- 作者: 黄刚
- 最近更新: 2024-02-26
- 浏览次数: 52678
- 平均得分: