Skip to content
Snippets Groups Projects

yang2rdf

This repository contains instructions for converting a YANG module to RDF using Morph-KGC.

Requirements

Set-up

  1. Clone this repository with its submodules:
    git clone --recurse-submodules
  2. Install the dependencies:
    poetry install

Instructions

Converting one or more YANG modules to RDF

  1. Run convert-yang.sh:

    ./convert-yang.sh /path/to/yang

    Where /path/to/yang is the path to either a single YANG module or a directory containing multiple YANG modules.

    The converted RDF files will be saved in an output directory.

Manually converting a YANG module to RDF

  1. Convert the YANG module to YIN:
    poetry run pyang -f yinsolidated --overwrite -o ./mapping/source.xml -p /path/to/yang /path/to/yang/openconfig-bgp.yang
    Where /path/to/yang is the path to the directory containing the YANG modules, and /path/to/yang/openconfig-bgp.yang is the path to the YANG module to be converted to YIN.
  2. Augment the YIN file with extra attributes to be used by Morph-KGC for mapping:
    poetry run python -m yang2rdf.augment_yin --overwrite mapping/source.xml
    This will update the file mapping/openconfig-bgp.yin with the extra attributes.
  3. Convert the YIN file to RDF using Morph-KGC:
    poetry run python -m morph_kgc mapping/config.ini
  4. Convert the N-Triples file to Turtle:
    poetry run python -m rdflib.tools.rdfpipe -i ntriples -o turtle output/knowledge-graph.nt --ns=yin=http://yang.eurecom.fr/yin# --ns=rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns# --ns=rdfs=http://www.w3.org/2000/01/rdf-schema# --ns=dc=http://purl.org/dc/elements/1.1/ > output/knowledge-graph.ttl

URI Patterns

The following URI patterns are used for the different YANG elements:

Element URI
Container http://yang.eurecom.fr/module/$(@module-name)/container/$(@id)
Enum http://yang.eurecom.fr/module/$(@module-name)/enum/$(@id)
Leaf http://yang.eurecom.fr/module/$(@module-name)/leaf/$(@id)
Leaf list http://yang.eurecom.fr/module/$(@module-name)/leaf-list/$(@id)
List http://yang.eurecom.fr/module/$(@module-name)/list/$(@id)
Module http://yang.eurecom.fr/module/$(@name)
Module revision http://yang.eurecom.fr/module/$(@name)/revision/$(@date)
Submodule http://yang.eurecom.fr/submodule/$(@name)
Type http://yang.eurecom.fr/module/$(@module-name)/type/$(@id)
Type def http://yang.eurecom.fr/module/$(@module-name)/type-def/$(@id)

Due to the limitations of Morph-KGC (https://github.com/morph-kgc/morph-kgc/issues/98), the leaf URIs do not include the module/container name, and container URIs do not include the module name. This might cause duplicate URIs and collisions. To resolve this, the YIN file is modified to recursively include parent names in the URIs. This is done by the augment_yin.py script.

For example:

<yin:module name="openconfig-bgp">
   <yin:container name="bgp">
      <yin:leaf name="as" type="uint32"/>
   </yin:container>
</yin:module>

Is augmented to:

<yin:module name="openconfig-bgp">
   <yin:container name="bgp" id="openconfig-bgp_bgp">
      <yin:leaf name="router-id" id="openconfig-bgp_bgp_router-id"/>
   </yin:container>
</yin:module>