Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Creating and Executing a NETCONF Java Application

You can use the NETCONF Java toolkit to create Java applications to connect to a device, open a NETCONF session, and create and execute operational and configuration requests. After installing the NETCONF Java toolkit, which is described in Downloading and Installing the NETCONF Java Toolkit, the general procedure is:

  1. Create a Java program that includes code to connect to a device and to execute the desired operations or requests.

  2. Compile the Java code and execute the program.

These steps are reviewed in detail in the following sections:

Creating a NETCONF Java Toolkit Program File

NETCONF Java toolkit programs have the same generic framework. To create a basic NETCONF Java toolkit program:

  1. Create a .java file.

    The filename should be identical to the class name, excluding the extension. For example, the ShowChassis class is saved in the file ShowChassis.java.

  2. Create the general boilerplate, which includes the code for import statements, the class declaration, and the Java method, main().
  3. Within main(), create a Device object and call the connect() method.

    This also creates a default NETCONF session with the NETCONF server over SSHv2.

  4. Execute operational and configuration requests by executing RPCs and performing NETCONF operations on the Device object.

    For example, to execute an operational request to retrieve chassis inventory information from the device, include the following line of code:

  5. Add code to print, parse, or take action on RPC replies received from the NETCONF server.

    The following line of code prints the RPC reply in XML format to standard output:

  6. Close the device and release resources by calling the close() method on the Device object.

Sample NETCONF Java Toolkit Program

The following sample code illustrates a simple NETCONF Java toolkit program, ShowChassis.java, which connects to a device and executes an operational request for chassis inventory information:

Compiling and Executing a NETCONF Java Toolkit Program File

To execute a NETCONF Java toolkit program, compile the code and run the program from the configuration management server. You need a Java compiler to compile the source code and to create an executable program.

  1. Compile the Java source code to create a Java class file containing Java bytecode.

    For example, to compile the ShowChassis.java file using the javac compiler included in the Java Development Kit (JDK) from Oracle Corporation, issue the following command on the command line of the configuration management server:

    This creates the ShowChassis.class file.

  2. Execute the program.