Skip to content
Snippets Groups Projects

yang2rdf

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

Requirements

Set-up

  1. Clone this repository:

    git clone
  2. Install the dependencies:

    poetry install

Instructions

Converting one or more YANG modules to RDF

  1. Convert the YANG module(s) to RDF:

    poetry run python -m yang2rdf.convert_yang /path/to/yang [--engine {morphkgc,rmlmapper}] [--force]

    Where:

    • /path/to/yang is the path to either a single YANG module or a directory containing multiple YANG modules.
    • --engine or -e specifies the engine to use for conversion. Options are morphkgc (default) or rmlmapper.
    • --force or -f overwrites the output file if it exists.

    If using rmlmapper, you need to download the jar file and place it in the base directory.

    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 any RML engine that supports either YARRRML or RML mappings. In this example, we will show how to use either Morph-KGC or RMLMapper.

    • Option 1: Convert the YIN file to RDF using Morph-KGC:

      poetry run python -m morph_kgc mapping/config.ini

      This will generate a file output/knowledge-graph.nt containing the RDF triples.

    • Option 2: Convert the YIN file to RDF using RMLMapper:

      1. Download the latest RMLMapper jar file from the official repository and place it in the base directory.

      2. Run RMLMapper with the following command:

        java -jar rmlmapper-*.jar --mapping mapping/mapping.ttl --output output/knowledge-graph.nt --serialization ntriples

        This will generate a file out.nt containing the RDF triples.

      3. Fix the %2F characters in the URIs:

        sed -i -E ':loop; s/(<[^>]*)%2F/\1\//g; t loop' output/knowledge-graph.nt
  4. (Optional) 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# > output/knowledge-graph.ttl

Converting instance data to RDF

  1. Convert the instance data to RDF:

    poetry run python -m yang2rdf.convert_instance -f nt /path/to/instance.xml /path/to/rdf-modules

    Where:

    • /path/to/instance.xml is the path to the instance data file.
    • /path/to/rdf-modules is the path to the directory containing the YANG modules converted into RDF using convert_yang.

URI Patterns

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

Element URI
Module http://yang.eurecom.fr/module/$(@module-name)
SubModule http://yang.eurecom.fr/module/$(@module-name)/submodule/$(@id)
Module Revision http://yang.eurecom.fr/module/$(@module-name)/revision/$(@id)
Type Definition http://yang.eurecom.fr/module/$(@module-name)/typedef/$(@id)
List http://yang.eurecom.fr/module/$(@module-name)/list/$(@id)
Enumeration http://yang.eurecom.fr/module/$(@module-name)/enum/$(@id)
Bit http://yang.eurecom.fr/module/$(@module-name)/bit/$(@id)
Type http://yang.eurecom.fr/module/$(@module-name)/type/$(@id)
Leaf List http://yang.eurecom.fr/module/$(@module-name)/leaflist/$(@id)
Leaf http://yang.eurecom.fr/module/$(@module-name)/leaf/$(@id)
Container http://yang.eurecom.fr/module/$(@module-name)/container/$(@id)
Identity http://yang.eurecom.fr/module/$(@module-name)/identity/$(@id)
Notification http://yang.eurecom.fr/module/$(@module-name)/notification/$(@id)
RPC http://yang.eurecom.fr/module/$(@module-name)/rpc/$(@id)
Input http://yang.eurecom.fr/module/$(@module-name)/input/$(@id)
Output http://yang.eurecom.fr/module/$(@module-name)/output/$(@id)
AnyData http://yang.eurecom.fr/module/$(@module-name)/anydata/$(@id)
AnyXML http://yang.eurecom.fr/module/$(@module-name)/anyxml/$(@id)
Choice http://yang.eurecom.fr/module/$(@module-name)/choice/$(@id)
Case http://yang.eurecom.fr/module/$(@module-name)/case/$(@id)
Deviation http://yang.eurecom.fr/module/$(@module-name)/deviation/$(@id)
Deviate http://yang.eurecom.fr/module/$(@module-name)/deviate/$(@id)
Feature http://yang.eurecom.fr/module/$(@module-name)/feature/$(@id)
Extension http://yang.eurecom.fr/module/$(@module-name)/extension/$(@id)
Action http://yang.eurecom.fr/module/$(@module-name)/action/$(@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>

Converting YARRRML to RML

The YARRRML mappings have been converted to RML mappings using yatter. The RML mappings are stored in the mapping/mapping.ttl file.

To convert the YARRRML mappings to RML, run the following command:

poetry run python -m yatter -i mapping/mapping.yaml -o mapping/mapping.ttl -f RML