YANG 数据建模语言详解

在这里插入图片描述

1. YANG 概念

YANG (Yet Another Next Generation) 是网络配置建模语言:

  • 用于定义网络设备的配置和状态数据结构
  • 支持 NETCONF/RESTCONF 协议的数据操作
  • 提供数据约束和验证规则
  • 广泛应用于 SDN、网络自动化和设备管理

核心价值:使网络配置标准化、可验证、自动化

2. YANG 核心原理

  • 树状结构:数据组织为层次化树形结构
  • 强类型系统:定义严格的数据类型
  • 约束规范:通过 must/when 定义业务规则
  • RPC支持:定义远程过程调用接口
  • 通知机制:支持事件通知模型
  • 模块化设计:支持模块复用和扩展

3. YANG 语法规范

基本结构

module example-system {
  namespace "urn:ietf:params:xml:ns:yang:example-system";
  prefix sys;

  container system {
    leaf hostname {
      type string;
      description "Device hostname";
    }
    
    list interface {
      key "name";
      leaf name {
        type string;
      }
      leaf ip-address {
        type inet:ip-address;
      }
    }
  }
}

核心元素

元素功能示例
module定义模块module network { ... }
container容器节点container interfaces { ... }
leaf叶子节点leaf hostname { type string; }
list列表节点list interface { key "name"; ... }
leaf-list叶子列表leaf-list dns-server { type inet:ip-address; }
choice选择分支choice protocol { case tcp; case udp; }
typedef类型定义typedef port-number { type uint16 { range 1..65535; } }
rpc远程调用rpc reboot { input { ... } output { ... } }
notification事件通知notification interface-down { ... }

数据类型

类型描述示例
string字符串type string;
uint8/16/32/64无符号整数type uint16;
int8/16/32/64有符号整数type int32;
boolean布尔值type boolean;
enumeration枚举值type enumeration { enum up; enum down; }
identityref身份引用type identityref { base interface-type; }
leafref叶子引用type leafref { path "/interfaces/interface/name"; }

4. Python YANG 处理工具

主要工具

  1. pyang:YANG 验证和转换工具
  2. ncclient:NETCONF 客户端库
  3. yangson:纯 Python YANG 数据模型处理器
  4. pyangbind:从 YANG 生成 Python 绑定

安装

pip install pyang ncclient

5. YANG 处理流程

Python处理
pyang
pyangbind
ncclient
ncclient
验证语法
模型验证
生成Python类
生成数据绑定/文档
NETCONF配置
配置设备
NETCONF查询
查询状态
YANG模型

6. 应用示例

示例1:创建YANG模型

输入 (network.yang):

module network {
  namespace "urn:ietf:params:xml:ns:yang:network";
  prefix net;

  container devices {
    list device {
      key "name";
      leaf name {
        type string;
      }
      leaf ip-address {
        type inet:ip-address;
        mandatory true;
      }
      leaf status {
        type enumeration {
          enum up;
          enum down;
          enum testing;
        }
        default up;
      }
    }
  }
  
  rpc reboot {
    input {
      leaf device-name {
        type leafref {
          path "/devices/device/name";
        }
      }
    }
    output {
      leaf result {
        type string;
      }
    }
  }
}

示例2:使用pyang验证模型

# 验证语法
pyang network.yang

# 生成树形图
pyang -f tree network.yang

树形图输出:

module: network
  +--rw devices
     +--rw device* [name]
        +--rw name          string
        +--rw ip-address    inet:ip-address
        +--rw status?       enumeration
  +---x reboot
     +---w input
     |  +---w device-name    -> /devices/device/name
     +--ro output
        +--ro result?        string

示例3:使用ncclient配置设备

Python 配置代码:

from ncclient import manager

# 连接设备
with manager.connect(
    host='192.168.1.1',
    port=830,
    username='admin',
    password='secret',
    hostkey_verify=False
) as m:

    # 构建配置XML
    config = """
    <config>
        <devices xmlns="urn:ietf:params:xml:ns:yang:network">
            <device>
                <name>router01</name>
                <ip-address>192.168.1.1</ip-address>
                <status>up</status>
            </device>
        </devices>
    </config>
    """
    
    # 发送配置
    m.edit_config(target='running', config=config)
    
    # 调用RPC
    rpc = """
    <reboot xmlns="urn:ietf:params:xml:ns:yang:network">
        <device-name>router01</device-name>
    </reboot>
    """
    response = m.dispatch(rpc)
    print(response.xml)

RPC响应:

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <result xmlns="urn:ietf:params:xml:ns:yang:network">Reboot initiated</result>
</rpc-reply>

示例4:查询设备状态

Python 查询代码:

# 继续使用上面的连接

# 构建查询
filter = """
<devices xmlns="urn:ietf:params:xml:ns:yang:network">
    <device>
        <name>router01</name>
    </device>
</devices>
"""

# 发送查询
response = m.get_config(source='running', filter=('subtree', filter))
print(response.xml)

查询结果:

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <data>
    <devices xmlns="urn:ietf:params:xml:ns:yang:network">
      <device>
        <name>router01</name>
        <ip-address>192.168.1.1</ip-address>
        <status>up</status>
      </device>
    </devices>
  </data>
</rpc-reply>

示例5:使用pyangbind处理数据

from pyangbind.lib.serialise import pybindJSONDecoder
import json

# 加载YANG模型(需先编译)
# pyang --plugindir $PWD/pyangbind/plugin -f pybind -o network.py network.yang

from network import network

# 创建模型实例
model = network()

# 添加设备
device = model.devices.device.add("router01")
device.ip_address = "192.168.1.1"
device.status = "up"

# 序列化为JSON
json_data = model.devices.device["router01"].get(filter=True)
print(json.dumps(json_data, indent=2))

# 反序列化
new_device = {
    "name": "switch01",
    "ip-address": "10.0.0.1",
    "status": "down"
}

pybindJSONDecoder.load_ietf_json(
    new_device, 
    None, None, 
    obj=model.devices.device.add("switch01")
)

print("添加的设备:", model.devices.device["switch01"].name)

输出:

{
  "name": "router01",
  "ip-address": "192.168.1.1",
  "status": "up"
}
添加的设备: switch01

7. YANG 在自动化运维中的典型应用

1. 网络设备配置管理

module cisco-ios {
  container interfaces {
    list interface {
      key "name";
      leaf name {
        type string;
      }
      leaf description {
        type string;
      }
      leaf enabled {
        type boolean;
        default true;
      }
    }
  }
}

2. 服务配置建模

module vpn-service {
  container vpn {
    list instance {
      key "name";
      leaf name {
        type string;
      }
      leaf type {
        type enumeration {
          enum site-to-site;
          enum remote-access;
        }
      }
      leaf-list endpoints {
        type inet:ip-address;
      }
    }
  }
}

3. 监控数据模型

module system-monitoring {
  container metrics {
    leaf cpu-usage {
      type percentage;
      config false; // 状态数据,不可配置
    }
    leaf memory-usage {
      type percentage;
      config false;
    }
    
    notification high-usage {
      leaf resource {
        type enumeration {
          enum cpu;
          enum memory;
        }
      }
      leaf value {
        type percentage;
      }
    }
  }
}

8. 最佳实践

  1. 模块化设计

    module network-base {
      // 基础类型和分组
    }
    
    module network-devices {
      import network-base {
        prefix base;
      }
      // 设备特定扩展
    }
    
  2. 命名规范

    • 模块名:小写加连字符 (acme-network)
    • 叶子名:小写驼峰 (ipAddress)
    • 容器名:单数名词 (device)
  3. 数据验证

    leaf mtu {
      type uint16 {
        range 68..9216;
      }
      must ". >= 1500" {
        error-message "MTU must be at least 1500";
      }
    }
    
  4. 文档注释

    leaf ospf-enabled {
      type boolean;
      default true;
      description "Enable OSPF routing protocol";
      reference "RFC 2328 - OSPF Version 2";
    }
    
  5. 版本控制

    revision 2023-11-15 {
      description "Added BGP support";
    }
    

9. 工具生态系统

工具用途示例命令
pyang模型验证/转换pyang -f tree model.yang
yanglint高级验证yanglint model.yang
gnmicgNMI 客户端gnmic get --path /interfaces
confd配置引擎confd --yangpath ./yang
sysrepoYANG 运行时sysrepoctl -i model.yang

10. YANG 与其他格式对比

特性YANGJSON SchemaXSD
主要用途网络配置API数据验证XML验证
协议支持NETCONF/RESTCONFHTTP/RESTSOAP
数据类型网络专用类型基础类型XML类型
扩展性强(分组/继承)有限中等
工具生态网络设备专用通用通用

总结

YANG 是现代网络自动化的核心技术:

  • 标准化建模:统一网络设备配置接口
  • 强约束验证:确保配置正确性
  • 自动化支持:与 NETCONF/RESTCONF 完美集成
  • 跨厂商兼容:支持多厂商设备统一管理

核心应用场景:

  1. 网络设备配置管理
  2. SDN控制器数据建模
  3. 网络服务编排
  4. 配置审计和验证
  5. 网络状态监控

通过 Python 工具链(pyang + ncclient):

  • 可以轻松实现 YANG 模型的验证和处理
  • 与网络设备进行配置交互
  • 构建网络自动化流水线

掌握 YANG 是成为高级网络自动化工程师的关键技能,能够显著提升网络运维的效率和质量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值