Upgrade to Pro — share decks privately, control downloads, hide ads and more …

データモデルYANGの処理系を再発明した話

tjmtrhs
March 05, 2025

 データモデルYANGの処理系を再発明した話

データモデリング言語の一つにYANGがあり、ネットワーク機器のconfigやステータスのデータ形式としてよく使われています。汎用のデータのバリデーションにも使えますが、特定のソフトウェア以外で使えるライブラリが少なく採用しにくいです。そういった用途への適用を目指し、YANGのサブセットの処理系を実装してみた例をご紹介します。

tjmtrhs

March 05, 2025
Tweet

More Decks by tjmtrhs

Other Decks in Technology

Transcript

  1. © NTT Communications Corporation All Rights Reserved. 1 データモデル YANG

    の処理系を再発明した話 NTT Tech Conference 2025 LT 2025/03/05 @ docomo R&D OPEN LAB ODAIBA NTTコミュニケーションズ 田島照久 [email protected]
  2. © NTT Communications Corporation All Rights Reserved. 2 背景:モデルベース運用をしたい Hardware

    Container 本番環境 検証環境 構成 情報 NWの構造を抽象化 • 各レイヤのトポロジを グラフとして表現する 各環境へ翻訳 • 各環境の文法をテンプレ エンジンで生成する これを安全に編 集したい
  3. © NTT Communications Corporation All Rights Reserved. 3 YANG:ネットワーク管理のためのデータモデル言語 ◼

    歴史 ⚫ 2008 ~ 2010年頃、もともとはNETCONFのために作られた (RFC6020) ⚫ 細部がメンテナンスされて現バージョンに至る (RFC7950) ◼ おもな特徴 ⚫ モジュール(namespace)が定義できる ⚫ データ型の定義ができる ⚫ データ間の参照が行え、制約条件として記述できる ⚫ モジュール外から特定の要素の拡張ができる “agument”(ある種の Dependency Injection) ◼ あくまでも「モデル言語」なので、実際のデータは別に与えられる https://datatracker.ietf.org/doc/html/rfc6020 https://datatracker.ietf.org/doc/html/rfc7950
  4. © NTT Communications Corporation All Rights Reserved. 4 つまりYANGはこうやって使うことがある メーカ独立な

    抽象化されたモデル (モジュール) grouping ios-endpoints { list ios { key "device interface"; leaf device { type leafref { path "/ncs:devices/ncs:device/ncs:name"; } must "deref(current())/../ncs:device-type/ncs:cli/ncs:ned-id = 'tot-ios-id:tot-ios'" { tailf:dependency "."; error-message "Selected device is not ios switch."; } } leaf interface { type leafref { path "deref(../device)/../ncs:config/tot-ios:interface/tot-ios:name"; } } (snip) } } augment "/e2e:testbed-e2e-service/e2e:endpoints" { uses ios-endpoints; } メーカに依存したモデル (モジュール) list testbed-e2e-service { (snip) container endpoints { // for augment } }
  5. © NTT Communications Corporation All Rights Reserved. 5 つまりYANGはこうやって使うことがある(その2) IETF標準のモデル

    ietf-network IETF標準のモデル RFC8345 ietf-network-topology 参照 L1~L3まで表現できる 拡張 OSPFを表現できるモデル BGPを表現できるモデル 拡張
  6. © NTT Communications Corporation All Rights Reserved. 6 YANGの処理系事情 ◼

    Cisco NSO (買収前はTail-f ConfD) ⚫ RFC authorで発祥の地、ちゃんと全機能実装されているDB ⚫ お値段はそれなりにする ◼ pyang ⚫ pythonで実装されたオープンソースのYANGの処理系 ⚫ モジュールの構造とかをちゃんと表示してくれる ⚫ 今も開発が続いている ⚫ 対象となるデータのバリデーションをやってくれるわけでは無い ⚫ json schemaに変換する機能もあるがaugmentに対応していない ・・・以上? https://github.com/mbj4668/pyang
  7. © NTT Communications Corporation All Rights Reserved. 7 処理系を作ろう ◼

    字句解析:文字列をトークン単位に分割(単語を作る) ◼ 構文解析:トークン同士の構造を紐付ける(文を作る) ◼ 意味解析:型チェックなど(今回は対象データのバリデーション) ※YANGの全機能は目指さずオレオレサブセットを目標
  8. © NTT Communications Corporation All Rights Reserved. 8 testmodel.yang module

    basic-types { namespace "hoge"; prefix "basic-types"; // line comment /* block comment */ leaf string { type string; } leaf int8 { type int8; } } { "string": "string", "int8": 1 } testdata.json
  9. © NTT Communications Corporation All Rights Reserved. 9 字句+構文を正規表現でサボろうとするも失敗 ◼

    純粋な正規表現だとCFG(Context Free Grammar)をチェックできな いが、高級プログラミング言語の正規表現は後方参照などで拡張され ているので実装できるのでは?? →できなくは無いが、正規表現が大変になってきて挫折 それでもlex+yaccとかで「ちゃんと」作るのからは避けてサボりたい
  10. © NTT Communications Corporation All Rights Reserved. 10 token parser

    ◼ 文字列配列を一文字ずつ見て トークンの種類と切り出した 文字列を返却 ◼ ひたすらcase文 module basic-types { namespace "hoge"; prefix "basic-types"; // line comment /* block comment */ leaf string { type string; }
  11. © NTT Communications Corporation All Rights Reserved. 11 syntax parser

    ◼ 続々と流れてくるトークンを スタックに入れ、意味がひと まとまりになるとタプルとし て保存 basic-types module namespace = hoge prefix = basic-types leaf string type = string
  12. © NTT Communications Corporation All Rights Reserved. 12 compile &

    validation ◼ 各クラスを作っ て詰め込んで チェックする 以上 leaf string type = string { "string": "string", "int8": 1 }
  13. © NTT Communications Corporation All Rights Reserved. 13 余談 ◼

    BNFを書くとGitHub Copilotがコード補完してくれるが、結局自分で 書いた方が早いケースだった(人による。ちゃんとテスト書けという 話かも) ◼ YANGに沿ったデータはXMLに等価に変換できる(とされている) ⚫ そもそもNETCONFのdata-planeはXML ◼ JSONにも変換できるが、あくまでもデータを変換できる方式なだけ で、YANGと等価なJSON Schemaが得られるわけではない