Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use the NETCONF Java Toolkit to Perform Operational Tasks

Use Device Object Methods to Execute RPCs and Operational Commands

The NETCONF Java toolkit Device object has methods to request information from and perform operational tasks on remote devices. When appropriate, the methods are overloaded to take a number of different formats.

Executing RPCs

To execute a remote procedure call (RPC), call the executeRPC() method on the Device object. The executeRPC() method is overloaded to accept a String object, a net.juniper.netconf.XML object, or an org.w3c.dom.Document object as the argument. The RPC is processed by the NETCONF server, which returns the RPC reply as an XML object.

The method syntax is:

The following code snippet executes the Junos XML API get-chassis-inventory RPC using a string argument. The get-chassis-inventory RPC is equivalent to the show chassis hardware operational mode command in the Junos OS command-line interface (CLI).

Executing Operational Mode Commands

To execute an operational mode command to request information from or perform operational tasks on a device running Junos OS, call the runCliCommand() method on the Device object. The runCliCommand() method sends a Junos OS operational mode command to the NETCONF server on the remote device. The argument is a string representing the operational mode command that you would enter in the Junos OS CLI. The RPC is processed by the NETCONF server, which returns the RPC reply. Starting with Junos OS Release 11.4, the return string is the same ASCII-formatted output that you see in the Junos OS CLI. For devices running earlier versions of Junos OS, the return string contains Junos XML tag elements.

The method syntax is:

The following code snippet sends the CLI operational mode command show chassis hardware to the NETCONF server on a device running Junos OS:

Example: NETCONF Java Application for Executing an Operational Request RPC

This NETCONF Java toolkit program executes an RPC to obtain operational information from a device, which is then printed to standard output. This example serves as an instructional example for creating and executing a basic NETCONF Java toolkit program.

Requirements

  • NETCONF Java toolkit is installed on the configuration management server.

  • Client application can log in to the device where the NETCONF server resides.

  • NETCONF service over SSH is enabled on the device where the NETCONF server resides.

Overview

You can use the NETCONF Java toolkit to request operational information from a remote device. The following example illustrates how to create a NETCONF Java toolkit program to execute an operational request from the Junos XML API on a device running Junos OS. The example also explains how to compile the code, execute the program, and verify the results.

Configuration

Creating the Java Program

Step-by-Step Procedure

To construct the Java program file that contains the code for the operational request:

  1. Give the file a descriptive name.

    The filename must be the same as the class name. For this example, the file and class are named GetChassisInventory.

  2. Include the appropriate import statements, and the code for the class declaration and the Java method, main().

  3. Within main(), create a Device object and call the connect() method.

    This creates a default NETCONF session over SSHv2 with the NETCONF server. You must update the code with the appropriate arguments for connection to and authentication on your specific device.

    Having established a Device object, you can perform NETCONF operations on the device. For a complete list of available methods corresponding to NETCONF operations, refer to the NETCONF Java toolkit Javadocs.

  4. Call the executeRPC() method with the operational request RPC command as the argument.

    This example uses the Junos XML API get-chassis-inventory RPC. The reply, which is returned in XML, is stored in the rpc_reply variable.

  5. Add code to take action on the RPC reply.

    The following code converts the NETCONF server’s reply to a string and prints it to the screen:

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

Results

The complete program is:

Compiling and Running the Java Program

Step-by-Step Procedure

You need a Java compiler to compile the source code and to create an executable program.

To compile the code and run the program on the configuration management server:

  1. Compile the GetChassisInventory.java file.

  2. Execute the GetChassisInventory program.

Verification

Verifying Program Execution

Purpose

Verify that the GetChassisInventory program runs correctly.

Action

If the program executes successfully, it establishes a connection and a creates a NETCONF session with the specified device. The program sends the get-chassis-inventory RPC to the NETCONF server, and the server responds with the requested operational information enclosed in the <rpc-reply> tag element. The program prints the reply to standard out. Following is a sample RPC reply with some output omitted for brevity.

Troubleshooting

Troubleshooting NETCONF Exceptions

Problem

A NETCONF exception occurs, and you see the following error message:

NETCONF over SSH might not be enabled on the device where the NETCONF server resides, or it might be enabled on a different port.

Solution

Ensure that you have enabled NETCONF over SSH on the device where the NETCONF server resides. Since the example program does not specify a specific port number in the Device arguments, the NETCONF session is established on the default NETCONF-over-SSH port, 830. To verify whether NETCONF over SSH is enabled on the default port for a device running Junos OS, enter the following operational mode command on the remote device:

If the netconf configuration hierarchy is absent, issue the following statements in configuration mode to enable NETCONF over SSH on the default port:

If the netconf configuration hierarchy specifies a port other than the default port, include the new port number in the Device object constructor arguments. For example, the following device is configured for NETCONF over SSH on port 12345:

To correct the connection issue, include the new port number in the Device arguments.

Example: NETCONF Java Application for Executing CLI Commands

This NETCONF Java toolkit program demonstrates the runCLICommand() method, which sends the specified Junos OS operational mode command to the NETCONF server to request information from or perform operational tasks on a device running Junos OS.

Requirements

  • Routing, switching, or security device running Junos OS.

  • NETCONF Java toolkit is installed on the configuration management server.

  • Client application can log in to the device where the NETCONF server resides.

  • NETCONF service over SSH is enabled on the device where the NETCONF server resides.

Overview

The NETCONF Java toolkit Device class contains the runCliCommand() method, which takes a Junos OS CLI operational mode command and converts it to an equivalent RPC in XML that can be processed by the NETCONF server. The runCLICommand() method takes as an argument the string representing an operational mode command that you enter in the Junos OS CLI.

The following example executes the show chassis hardware command on a device running Junos OS. The return value for the method is a string. Starting with Junos OS Release 11.4, the return string is the same ASCII-formatted output that you see in the Junos OS CLI. For devices running earlier versions of Junos OS, the return string contains Junos XML tag elements.

Configuration

Creating the Java program

Step-by-Step Procedure

To construct the Java program file:

  1. Give the file a descriptive name.

    The filename must be the same as the class name. For this example, the file and class are named ExecuteCLICommand.

  2. Add the code to the file and update the environment-specific variables such as the remote host IP address, username, password, and <rpc-reply> tag elements.

    The complete Java code for the ExecuteCLICommand.java program is presented here.

Compiling and Running the Java Program

Step-by-Step Procedure

You need a Java compiler to compile the source code and to create an executable program.

To compile the code and run the program on the configuration management server:

  1. Compile the ExecuteCLICommand.java file.

  2. Execute the ExecuteCLICommand program.

Verification

Verifying Program Execution

Purpose

Verify that the ExecuteCLICommand program runs correctly.

Action

If the program executes successfully, it establishes a connection and creates a NETCONF session with the specified device. The program converts the Junos OS CLI operational mode command show chassis hardware to an RPC and sends the RPC to the NETCONF server. The server responds with the requested operational information enclosed in the <rpc-reply> tag element The program parses the RPC reply and prints the resulting chassis inventory. The following sample output is from a Juniper Networks m7i router.

On a device running Junos OS Release 11.4 or later release, the output is in ASCII-formatted text, which is identical to the output in the CLI.

On a device running Junos OS Release 11.3 or earlier release, the output contains Junos XML tag elements.

Example: NETCONF Java Application for Printing Component Temperatures

This NETCONF Java toolkit program prints the name and corresponding temperature of components on a device running Junos OS.

Requirements

  • Routing, switching, or security device running Junos OS.

  • NETCONF Java toolkit is installed on the configuration management server.

  • Client application can log in to the device where the NETCONF server resides.

  • NETCONF service over SSH is enabled on the device where the NETCONF server resides.

Overview

The following example executes the Junos XML API get-environment-information RPC, which is the equivalent of the show chassis environment operational mode command on a device running Junos OS. The program parses the RPC reply, and for all components that list a temperature, the program prints the component name and corresponding temperature.

The RPC reply format for the get-environment-information RPC request is:

To parse the reply, the program uses the findNodes() method to return a list of org.w3c.dom.Node objects. For each <environment-item> node, the program obtains a list of child nodes. If a temperature element is present in the child node list, the program prints the name and temperature of that environment item.

Configuration

Creating the Java program

Step-by-Step Procedure

To construct the Java program file:

  1. Give the file a descriptive name.

    The filename must be the same as the class name. For this example, the file and class are named ShowTemps.

  2. Add the code to the file and update the environment-specific variables such as the remote host IP address, username, password, and <rpc-reply> tag elements.

    The complete Java code for the ShowTemps.java program is presented here.

Compiling and Running the Java Program

Step-by-Step Procedure

You need a Java compiler to compile the source code and to create an executable program.

To compile the code and run the program on the configuration management server:

  1. Compile the ShowTemps.java file.

  2. Execute the ShowTemps program.

Verification

Verifying the Results

Purpose

Verify that the ShowTemps program runs correctly.

Action

If the program executes successfully, it establishes a connection and a creates a NETCONF session with the specified device. The program then executes the Junos XML API get-environment-information RPC, parses the RPC reply, and prints all environment items that contain a child node <temperature>.

The following sample output is from a Juniper Networks m7i router: