YANG 1.0 and YANG 1.1 models are essential part of programming for OpenDaylight model-driven architecture. Standardized yang models are already available as maven artifacts as part of OpenDaylight project. To mention just some:
- MD-SAL pre-packaged YANG models
- Controller pre-packaged YANG models
In case your project requires special Yang model or needs to alter existing one, follow procedures below or check links below for YANG repositories.
This guide explains in detail how to create your own Yang model, use it in lighty.io project and add it into LightyController global schema context.
It is as simple as creating own text file my-model.yang
with content:
module my-model.yang {
yang-version 1.1;
namespace "urn:example:my-model";
prefix "mymodel";
revision 2018-09-14;
container device {
list server {
key name;
leaf name {
type string;
}
}
}
}
Bare yang file can not be used without generating java bindings for it. OepnDaylighty provides for this purpose maven plugin which makes this step really easy. Generated java binding code is used in your projects, so you can access model nad DataStore in type safe way.
- Create maven project structure:
my-model/src/main/yang/my-model.yang
my-model/pom.xml
- Content of pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.lighty.core</groupId>
<artifactId>lighty-binding-parent</artifactId>
<version>16.0.0-SNAPSHOT</version>
<relativePath/>
</parent>
<groupId>org.mygroup</groupId>
<artifactId>my-model</artifactId>
<version>1.0.1-SNAPSHOT</version>
</project>
On LightyController startup, it is necessary to register all YANG models with dependencies. This is how you do it:
- Add dependency on model artifact into your controller project pom.xml
<dependecy>
<groupId>org.mygroup</groupId>
<artifactId>my-model</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependecy>
- Use generated java bindings in LightyController initialization.
import io.lighty.core.controller.api.LightyController;
import io.lighty.core.controller.impl.LightyControllerBuilder;
import io.lighty.core.controller.impl.config.ControllerConfiguration;
import io.lighty.core.controller.impl.util.ControllerConfigUtils;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
public class Main {
private static final Set<YangModuleInfo> MY_MODELS = ImmutableSet.of(
org.opendaylight.yang.gen.v1.urn.example.my.model.rev180914.$YangModuleInfoImpl.getInstance()
);
public static void main(String[] args) {
ControllerConfiguration controllerConfiguration = ControllerConfigUtils
.getDefaultSingleNodeConfiguration(MY_MODELS);
LightyController lightyController = new LightyControllerBuilder()
.from(controllerConfiguration)
.build();
lightyController
.start()
.get();
}
}
If you need standardized IETF models, or include existing ones:
This repository contains example projects where you can see how YANG models are used. See also example-data-center, lighty-test-models and lighty-toaster as model maven artifacts examples in this project.