2017-02-10 32 views
1

目前,我正在與YANG一起作爲(遺留)Python項目的一部分。YANG - 非必需容器建模

我有點被困在定義模式的任務中,該模式將被用於驗證數據,組織爲Python字典。 如果可能,我會「喜歡」保持當前的結構,因爲很多代碼都在使用這些數據。

的「不變」的數據塊:

"namespace": {      # Mandatory 
    "management": {     # Optional 
     "interfaces": {    # Mandatory 
      "m0": {     # Optional 
       "leaf1": "..." 
      } 
     } 
    }, 
    "benchmark": {     # Optional 
     "interfaces": {    # Mandatory 
      "b0": {     # Optional 
       "leaf1": "...", 
       "leaf2": "..." 
      }, 
      "b1": {     # Optional 
       "leaf1": "...", 
       "leaf2": "..." 
      } 
     } 
    } 
} 

我的問題是,一切都標記爲「可選」(在本例中)將被模擬成一個容器,但似乎他們不能被定義爲可選(即:強制false;)根據RFC6020

因此,我定義了一個使用列表的模型。這意味着Python的快譯通的一些節點(管理,標杆,M0,B0,B1)現在列出元素,不能在當前的方式來訪問,如:data['namespace']['management']...

修改後的示例如下:

"namespace": [ 
    { 
     "desc": "management", 
     "interfaces": [ 
      { 
       "leaf1": "..." 
      } 
     ] 
    }, 
    { 
     "desc": "benchmark", 
     "interfaces": [ 
      { 
       "leaf1": "...", 
       "leaf2": "..." 
      }, 
      { 
       "leaf1": "...", 
       "leaf2": "..." 
      } 
     ] 
    } 
] 

的描述(段從我目前的)洋型號:

list namespace { 
    description "Namespace definitions."; 
    key desc; 

    leaf desc { type string; } 

    uses leaf-definitions; 

    list interfaces { 
     key leaf1; 
     uses leaf-definitions; 
    } 
} 

驗證是成功的,數據轉換(本身)是沒有問題的,但它造成了一大堆的斷碼。

這導致了我的問題(S):

  1. 我是正確 - 是楊總的容器強制?
  2. 是否有另一種方法來模擬這種情況? (沒有打破「太多」)

我非常感謝您的意見,因爲我對楊來說比較陌生!

+0

是否有什麼特別的原因讓你直接在當前YANG模型的'namespace'列表中使用'leaf-definitions''語句?我認爲'leaf-definitions'是某種分組,它定義了接口所需的葉子,而不是整個名稱空間。 –

+0

不是。我主要想把這個例子減到最少,另外對於大多數可能的葉子都有一個分組。所以'leaf-definitions'只是一個佔位符。但是我的計劃是爲了這個目的使用(至少)兩個分組(一個用於一般命名空間離開,一個用於界面離開)。我最關心的是從「容器到列表」的變化引入了額外的工作量,這對於讓遺留代碼再次運行將是必需的。 – Trollokia

回答

0

我是否正確 - 在YANG中的容器總是強制性的?

恰恰相反。它們始終是可選的,除非它們包含強制性節點(強制性葉,具有最小元素> 0的列表或葉列表等)。換句話說,容器從它們的後代繼承這個屬性。這當然只適用於非在場容器。具有強制兒童的存在容器不會繼承此屬性,因爲這會破壞其目的(存在)。你可能錯過了強制節點的定義在RFC6020:

A mandatory node is one of: 

    o A leaf, choice, or anyxml node with a "mandatory" statement with 
     the value "true". 

    o A list or leaf-list node with a "min-elements" statement with a 
     value greater than zero. 

    o A container node without a "presence" statement, which has at 
     least one mandatory node as a child. 

這應該已經是你的第二個問題有幫助。

是否有另一種方法來模擬此場景? (不中斷「太多」)

濫用存在容器。他們總是可選的。您也可以通過向非在場容器引入一些強制性子女來避免使用這些列表。根據你的初始數據:

module mandatory-optional-branch { 
    namespace "org:example:mandatory-optional-branch"; 
    prefix "mob"; 

    grouping leafs { 
    leaf leaf1 {type string;} 
    leaf leaf2 {type string;} 
    } 

    list namespace { // mandatory 
    config false; 
    min-elements 1; 
    max-elements 1; 
    container management { // optional by nature 
     presence "I have mandatory children, but am not mandatory. Yay for me. 
       Of course my presence should have some meaning."; 
     list interfaces { // mandatory 
     min-elements 1; 
     max-elements 1; 
     container m0 { // optional - no mandatory node children 
      leaf leaf1 {type string;} 
     } 
     } 
    } 
    container benchmark { // optional by nature 
     presence "Same as 'management' above."; 
     list interfaces { // mandatory 
     min-elements 1; 
     max-elements 1; 
     container b0 { // optional - no mandatory node children 
      uses leafs; 
     } 
     container b1 { // optional - no mandatory node children 
      uses leafs; 
     } 
     } 
    } 
    } 
} 
+0

@Trollokia,我誤解了你的初始數據(因此模型是一個錯誤),但它應該讓你開始。 – predi

+0

感謝predi!這確實讓我開始了 - 當我重新閱讀RFC時,我看到了「強制節點(/容器)」定義,但很不確定這是否可能是一種解決方案。我會回報。 – Trollokia

+0

事實上,我在層次結構中使用了強制性的葉子,但是使用「存在性」,即使這樣也行得通。測試了兩個版本,成功 - 謝謝! – Trollokia

相關問題