行业标准clis与杨的缺失桥梁

ntc-rosetta的Python项目详细描述


全国过渡委员会罗塞塔

ntc椆u rosetta利用yangify来实现一组“驱动程序”,它们可以:

  1. 将网络设备的本机配置/状态转换为符合yang模型的结构化数据
  2. 将符合YANG模型的数据结构转换为网络设备的本机配置/数据结构
  3. 合并配置

解析

>>> from ntc_rosetta import get_driver
>>>
>>> ios = get_driver("ios", "openconfig")
>>> ios_driver = ios()
>>> with open("data/ios/config.txt", "r") as f:
>>>     config = f.read()
>>> print(config)
interface FastEthernet1
   description This is Fa1
   shutdown
   exit
!
vlan 10
   name prod
   no shutdown
   exit
!
vlan 20
   name dev
   shutdown
   exit
!
>>> parsed = ios_driver.parse(native={"dev_conf": config})
>>> import json
>>> print(json.dumps(parsed.raw_value(), indent=4))
{
    "openconfig-interfaces:interfaces": {
        "interface": [
            {
                "name": "FastEthernet1",
                "config": {
                    "name": "FastEthernet1",
                    "type": "iana-if-type:ethernetCsmacd",
                    "description": "This is Fa1",
                    "enabled": false
                },
                "subinterfaces": {
                    "subinterface": [
                        {
                            "index": 1,
                            "config": {
                                "index": 1,
                                "description": "This is Fa1.1"
                            }
                        },
                        {
                            "index": 2,
                            "config": {
                                "index": 2,
                                "description": "This is Fa1.2"
                            }
                        }
                    ]
                }
            },
        ]
    },
    "openconfig-network-instance:network-instances": {
        "network-instance": [
            {
                "name": "default",
                "config": {
                    "name": "default"
                },
                "vlans": {
                    "vlan": [
                        {
                            "vlan-id": 10,
                            "config": {
                                "vlan-id": 10,
                                "name": "prod",
                                "status": "ACTIVE"
                            }
                        },
                        {
                            "vlan-id": 20,
                            "config": {
                                "vlan-id": 20,
                                "name": "dev",
                                "status": "SUSPENDED"
                            }
                        }
                    ]
                }
            }
        ]
    }
}

翻译

>>> from ntc_rosetta import get_driver
>>> 
>>> ios = get_driver("ios", "openconfig")
>>> ios_processor = ios()
>>> data = {
>>>     "openconfig-interfaces:interfaces": {
>>>         "interface": [
>>>             {
>>>                 "name": "FastEthernet1",
>>>                 "config": {
>>>                     "name": "FastEthernet1",
>>>                     "type": "iana-if-type:ethernetCsmacd",
>>>                     "description": "This is Fa1",
>>>                     "enabled": False
>>>                 },
>>>                 "subinterfaces": {
>>>                     "subinterface": [
>>>                         {
>>>                             "index": 1,
>>>                             "config": {
>>>                                 "index": 1,
>>>                                 "description": "This is Fa1.1"
>>>                             }
>>>                         },
>>>                         {
>>>                             "index": 2,
>>>                             "config": {
>>>                                 "index": 2,
>>>                                 "description": "This is Fa1.2"
>>>                             }
>>>                         }
>>>                     ]
>>>                 }
>>>             },
>>>         ]
>>>     },
>>>     "openconfig-network-instance:network-instances": {
>>>         "network-instance": [
>>>             {
>>>                 "name": "default",
>>>                 "config": {
>>>                     "name": "default"
>>>                 },
>>>                 "vlans": {
>>>                     "vlan": [
>>>                         {
>>>                             "vlan-id": 10,
>>>                             "config": {
>>>                                 "vlan-id": 10,
>>>                                 "name": "prod",
>>>                                 "status": "ACTIVE"
>>>                             }
>>>                         },
>>>                         {
>>>                             "vlan-id": 20,
>>>                             "config": {
>>>                                 "vlan-id": 20,
>>>                                 "name": "dev",
>>>                                 "status": "SUSPENDED"
>>>                             }
>>>                         }
>>>                     ]
>>>                 }
>>>             }
>>>         ]
>>>     }
>>> }
>>> native = ios_processor.translate(candidate=data)
>>> print(native)
interface FastEthernet1
   description This is Fa1
   shutdown
   exit
!
interface FastEthernet1.1
   description This is Fa1.1
   exit
!
interface FastEthernet1.2
   description This is Fa1.2
   exit
!
interface FastEthernet3
   description This is Fa3
   no shutdown
   switchport mode access
   switchport access vlan 10
   exit
!
interface FastEthernet4
   shutdown
   switchport mode trunk
   switchport trunk allowed vlan 10,20
   exit
!
vlan 10
   name prod
   no shutdown
   exit
!
vlan 20
   name dev
   shutdown
   exit
!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何比较二维交错数组中列中元素的长度   java依赖javafx是安全的。util。回电话?   用于格式化二进制数的正则表达式,在Java中,从右到左每4个字符使用空格   java我应该在基于Springboot的后端使用headless模式吗?   java Hibernate获取数据。list()已去润滑,表未映射。怎么办?   java Spring引导存储库,包括示例、规范和页面   java使用UUID。randomUUID()和多次调用方法?   每个客户端请求的Java DatagramSocket不同线程   字符串中的java表达式到arraylist   数组循环遍历word文档和图像,并存储到SQL java   java wsimport Xauthfile错误   java在适配器中返回相同的片段   charAt()方法如何从字符串中提取数字并将其放入Java中的新字符串中?   java我想在两部手机之间建立hfp连接   java如何添加priceAfterDiscount的公共方法,该方法返回折扣后的价格