【问题标题】:Generate xml/json from Yang model从 Yang 模型生成 xml/json
【发布时间】:2016-06-22 05:14:40
【问题描述】:

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

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

 leaf templateSendPeriod {
      when "../exportProtocol!='netflow-v5'";
      type uint16;
      default 60;
      units seconds;
    }

这就是我得到的-

 <leaf name="templateSendPeriod">
      <when condition="../exportProtocol!='netflow-v5'"/>
      <type name="uint16"/>
      <default value="60"/>
      <units name="seconds"/>
    </leaf>

我想要的是

<templateSendPeriod></templateSendPeriod>

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

【问题讨论】:

  • Pyang 可以为你生成 xml 骨架。使用这个命令:pyang -f sample-xml-skeleton YANG_FILE -o OUTPUT_FILE.

标签: java json xml ietf-netmod-yang


【解决方案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;
        }
    }
}

对模型进行树状表示

$ 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

稍后在你的 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 

在这里,您将获得根据您的选择获取 json 或 xml 的答案..

import json
>>> print(json.dumps(sw.get(), indent=4))

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 2012-04-17
    • 2023-03-14
    • 1970-01-01
    • 2018-06-30
    • 2017-02-14
    • 2014-02-09
    • 2011-04-24
    • 2011-04-19
    相关资源
    最近更新 更多