yang2rdf
This repository contains instructions for converting a YANG module to RDF using Morph-KGC.
Requirements
Set-up
-
Clone this repository:
git clone
-
Install the dependencies:
poetry install
Instructions
Converting one or more YANG modules to RDF
-
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 aremorphkgc
(default) orrmlmapper
. -
--force
or-f
overwrites the output file if it exists.
The converted RDF files will be saved in an
output
directory. -
Manually converting a YANG module to RDF
-
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. -
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. -
Convert the YIN file to RDF using any RML engine that supports either YARRML or R2RML 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:
-
Download the latest RMLMapper jar file from the official repository.
-
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.
-
-
-
(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
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>