从 Yang 模型生成 xml/json

Abh*_*hek 5 java xml json ietf-netmod-yang

我一直在尝试找到一些可以从java中的 yang 模型生成示例 xml/json 数据的东西,例如对于 xsd,有一些工具可以生成示例 xml。

我尝试过 Pyang: 1. 它是用 Python 编写的。2. 转换后,它给出了 yin 格式,它相当于 yang 规格的 xml。例如。对于以下 yang 代码,如果我使用 pyang 将其转换为 YIN:

 leaf templateSendPeriod {
      when "../exportProtocol!='netflow-v5'";
      type uint16;
      default 60;
      units seconds;
    }
Run Code Online (Sandbox Code Playgroud)

这就是我得到的——

 <leaf name="templateSendPeriod">
      <when condition="../exportProtocol!='netflow-v5'"/>
      <type name="uint16"/>
      <default value="60"/>
      <units name="seconds"/>
    </leaf>
Run Code Online (Sandbox Code Playgroud)

相反我想要的是

<templateSendPeriod></templateSendPeriod>
Run Code Online (Sandbox Code Playgroud)

这样我就可以获得 xml,输入详细信息并针对相同的 yang 进行验证。

KSp*_*KSp 1

你可以这样做,首先声明你的模型

// module name
module napalm-star-wars {

    // boilerplate
    yang-version "1";
    namespace "https://napalm-yang.readthedocs.io/napalm-star-wars";

    prefix "napalm-star-wars";

    // identity to unequivocally identify the faction an individual belongs to
    identity AFFILIATION {
      description "To which group someone belongs to";
    }

    identity EMPIRE {
      base AFFILIATION;
      description "Affiliated to the empire";
    }

    identity REBEL_ALLIANCE {
      base AFFILIATION;
      description "Affiliated to the rebel alliance";
    }

    // new type to enforce correctness of the data
    typedef age {
      type uint16 {
        range 1..2000;
      }
    }

    // this grouping will all the personal data we will assign to individuals
    grouping personal-data {
        leaf name {
            type string;
        }
        leaf age {
            type age;
        }
        leaf affiliation {
            type identityref {
                base napalm-star-wars:AFFILIATION;
            }
        }
    }

    // this is the root object defined by the model
    container universe {
        list individual {
            // identify each individual by using the name as key
            key "name";

            // each individual will have the elements defined in the grouping
            uses personal-data;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对模型进行树表示

$ pyang -f tree napalm-star-wars.yang
module: napalm-star-wars
    +--rw roster
        +--rw individual* [name]
           +--rw name           string
           +--rw age?           age
           +--rw affiliation?   identityref
Run Code Online (Sandbox Code Playgroud)

稍后在你的 python 代码中使用它:

>>> import napalm_star_wars
>>>
>>> sw = napalm_star_wars.napalm_star_wars()
>>>
>>> obi = sw.universe.individual.add("Obi-Wan Kenobi")
>>> obi.affiliation = "REBEL_ALLIANCE"
>>> obi.age = 57
>>>
>>> luke = sw.universe.individual.add("Luke Skywalker")
>>> luke.affiliation = "REBEL_ALLIANCE"
>>> luke.age = 19 
Run Code Online (Sandbox Code Playgroud)

在这里您可以得到根据您的选择获取 json 或 xml 的答案。

import json
>>> print(json.dumps(sw.get(), indent=4))
Run Code Online (Sandbox Code Playgroud)