行业标准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,使用gui连接到另一台计算机/服务器的文件系统   运行sbt的ubuntu返回错误:“javahome需要<path>参数”   java如何在Android中处理许多ImageView而不出现内存问题?   查询中非法字符的java相同URL失败   安卓取消引用可能会产生“java”。lang.NullPointerException'   java中的indexoutofboundsexception“java.lang.ArrayIndexOutOfBoundsException”错误   xml Java将dom保存到文件>文件在程序结束后由另一个进程打开   Java的垃圾收集器是如何工作的?   Java如何筛选值(列表)   java处理字符串我怎样才能像在真实的书籍中一样在上面部分生成“小数字”呢?   java SonarQube是否有一个API来获取所有项目分析的一部分?   java startActivity(intent)什么都不做   JAVAutil。扫描器类Java   java如何从Firebase更新电子邮件?UpdateMail方法已被弃用   java Hibernate。如何正确组织带有注释的onetomany关系?   在java中获得卷标和驱动器号之间的映射(而不是FileSystemView)的解决方法是什么   java查找文件的路径