YANG To Java Conversion – How To Create Maven Project In Eclipse

My objective was to learn YANG and how it maps to Java. I was going through this RFC on YANG & NETCONF, when I realized that it will add more value if I do some hands on alongside. So my agenda was to:

  • Create a Maven Project in Eclipse.
  • Write a pom.xml to automatically generate java files corresponding to .yang files.
  • Write a sample .yang file where I can write YANG code.
  • Run build commands to generate java source code.

Here is the step by step guide to accomplish YANG to Java conversion in Eclipse by creating a maven project.

Step 1: Create a Maven Project in Eclipse

Go to File -> New -> Project & select Maven Project.

Note: You need to have m2eclipse plugin installed to create a maven project.

Create maven project in eclipse

Figure 1

Create maven project in eclipse

Figure 2

Create maven project in eclipse

Figure 3

Carefully see the details filled in Figure 3. Under Artifact section, you can give Group Id and Artifact Id of your choice, leave the version as it is & input packaging as bundle (even though this option is not available in drop down).

Under Parent Project, input Group Id, Artifact Id and Version as shown in the figure. This is done so that pom.xml in our maven project automatically inherits the dependencies from it’s parent’s pom.xml i.e. org.opendaylight.controller.sample.

Click on Finish and the maven project will be created in eclipse.

Step 2: Have a look at pom.xml

This is how pom.xml of your maven project would look like:

pom.xml

Step 3: Enhance pom.xml to convert YANG to Java

Add following piece of code in pom.xml after </packaging> closing tag and before </project> closing tag.

Step 4: Write YANG File

Create a source folder as src/main/yang and write your .yang file – sample yang file is given below:

Step 5: Build & Generate Java Source Code

From command-line, go to the maven project dir & run “mvn clean install”.

Step 6: Have a look at the generated Java file

Yang to Java Mapping

Note: Click on images to have a better view.

Alternative to Step 3 is to put following code in your pom.xml:

Comments

  1. WANG FENG says

    When i do as what you do in step 1 ,there will be a error with attribute in pom.xml .The error as following:
    Error(s) found in manifest configuration (org.apache.felix:maven-bundle-plugin:2.3.7:bundle:default-bundle:package)

  2. shrenik says

    Getting the following error after first step
    “Project build error: Non-resolvable parent POM: Could not find artifact org.opendaylight.controller.samples:saf-samples:pom:1.1-SNAPSHOT and ‘parent.relativePath’ points at wrong local POM”

  3. Rahil says

    while executing ‘mvn clean install’ it gives error
    “Non-resolvable parent POM for Yang-03:Yang-03:0.0.1-SNAPSHOT: Could not find artifact org.opendaylight.controller.sample:sal-samples:pom:1.1-SNAPSHOT and ‘parent.relativePath’ points at wrong local POM @ line 3, column 11″

    What is missing? Have you resolved it?

Leave a Reply

Your email address will not be published. Required fields are marked *