# net-cfg-yang-parser **Repository Path**: movee/net-cfg-yang-parser ## Basic Information - **Project Name**: net-cfg-yang-parser - **Description**: 解析厂商配置文件内容,结构化为oc yang模型 - **Primary Language**: Go - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-03-06 - **Last Updated**: 2023-10-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # net-cfg-yang-parser #### 介绍 解析厂商配置文件内容,结构化为openconfig yang模型 本项目实现了一个使用非常简易的厂商网络配置文件openconfig yang结构化功能。 用户只需要按照配置文件结构编写一个解析规则文件,项目就能按照规则文件提取相应厂商设备的配置文件内容,构建openconfig yang模型表达的数据结构 #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. 生成oc yang数据结构文件 使用openconfig/ygot项目提供的generator工具编译oc yang文件,生成相应的yang数据结构文件 ``` % cd ygot-0.25.7/generator % go build % ./generator -path=yang -output_file=oc/oc.go -package_name=oc -generate_fakeroot -fakeroot_name=device -compress_paths=true -shorten_enum_leaf_names -typedef_enum_with_defmod -exclude_modules=ietf-interfaces yang/openconfig-interfaces.yang yang/openconfig-if-ip.yang ``` 2. 编写厂商规则文件 根据厂商的配置文件结构和上面步骤生成的yang数据结构文件,编写相应的规则文件,如华为设备的接口配置片段: ``` # interface 100GE1/0/0 mtu 9000 description TO-S12516CR-36.Int_H5/0/13 # interface 100GE1/0/1 mtu 9000 description TO-S12516CR-36.Int_H5/0/17 ``` 可以编写相应的规则文件为: ```json { "rule": [ { "pattern": ["interface"], "yang-path": "interfaces/interface", "key": { "index": 1, "pattern": "(.*)" }, "paramters": [ { "index": 1, "pattern": "(.*)", "yang-path": "config/name|name" } ], "children": [ { "pattern": ["mtu"], "parameters": [ { "index": 1, "pattern": "(.*)", "yang-path": "config/mtu" } ] }, { "pattern": ["description"], "parameters": [ { "index": 1, "pattern": "(.*)", "yang-path": "config/description" } ] } ] } ] } ``` 3. 解析厂商配置文件为oc yang模型 ```go var dev = &oc.Device{} func main() { // 规则文件名 ruleFile := "./rule/huawei_rule.json" cfgFile := "./cfgfile/HUAWEI_interfaces.cfg" huawei.ParseCfgFile(ruleFile, cfgFile, dev) // 解析后的值 s, _ := json.Marshal(dev) log.Printf("dev: %s", s) } ``` 解析结果为: ```text 2023/05/14 19:19:25 dev: {"Interface":{"100GE1/0/0":{"AdminStatus":0,"Aggregation":null,"Counters":null,"Description":"TO-S12516CR-36.Int_H5/0/13","Enabled":null,"Ethernet":null,"HoldTime":null,"Ifindex":null,"LastChange":null,"Mtu":9000,"Name":null,"OperStatus":0,"RoutedVlan":null,"Subinterface":null,"Type":0},"100GE1/0/1":{"AdminStatus":0,"Aggregation":null,"Counters":null,"Description":"TO-S12516CR-36.Int_H5/0/17","Enabled":null,"Ethernet":null,"HoldTime":null,"Ifindex":null,"LastChange":null,"Mtu":9000,"Name":null,"OperStatus":0,"RoutedVlan":null,"Subinterface":null,"Type":0}},"Vlan":null} ```