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:
public XML executeRPC (String rpcContent) public XML executeRPC (net.juniper.netconf.XML rpc) public XML executeRPC (org.w3c.dom.Document rpcDoc)
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).
Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); try { XML rpc_reply = device.executeRPC("get-chassis-inventory"); System.out.println(rpc_reply.toString()); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); // additional processing for exception } device.close();
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:
public String runCLICommand (String command)
The following code snippet sends the CLI operational mode command show chassis hardware
to the NETCONF server on a device running
Junos OS:
Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); try { cli_reply = device.runCliCommand("show chassis hardware"); System.out.println(cli_reply); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); // additional processing for exception } device.close();
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:
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.
Include the appropriate import statements, and the code for the class declaration and the Java method,
main()
.import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import net.juniper.netconf.Device; import net.juniper.netconf.NetconfException; import net.juniper.netconf.XML; import org.xml.sax.SAXException; public class GetChassisInventory { public static void main(String args[]) throws NetconfException, ParserConfigurationException, SAXException, IOException { } }
Within
main()
, create aDevice
object and call theconnect()
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.
Device device = new Device("10.10.1.1", "admin", "PaSsWoRd", null); device.connect();
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.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 therpc_reply
variable.XML rpc_reply = device.executeRPC(“get-chassis-inventory”);
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:
System.out.println(rpc_reply.toString());
Close the device and release resources by calling the
close()
method on the device object.device.close();
Results
The complete program is:
/*GetChassisInventory*/ import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import net.juniper.netconf.Device; import net.juniper.netconf.NetconfException; import net.juniper.netconf.XML; import org.xml.sax.SAXException; public class GetChassisInventory { public static void main(String args[]) throws NetconfException, ParserConfigurationException, SAXException, IOException { Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); XML rpc_reply = device.executeRPC("get-chassis-inventory"); System.out.println(rpc_reply.toString()); device.close(); } }
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:
Compile the GetChassisInventory.java file.
> javac GetChassisInventory.java
Execute the GetChassisInventory program.
> java GetChassisInventory
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.
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos"> <chassis-inventory xmlns="http://xml.juniper.net/junos/11.2R1/junos-chassis"> <chassis junos:style="inventory"> <name>Chassis</name> <serial-number>12345</serial-number> <description>M7i</description> <chassis-module> ...output omitted... </chassis> </chassis-inventory> </rpc-reply>
Troubleshooting
Troubleshooting NETCONF Exceptions
Problem
A NETCONF exception occurs, and you see the following error message:
Exception in thread "main" net.juniper.netconf.NetconfException: There was a problem while connecting to 10.10.1.1:830 at net.juniper.netconf.Device.createNetconfSession(Device.java:344) at net.juniper.netconf.Device.connect(Device.java:225) at GetChassisInventory.main(GetChassisInventory.java:14)
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:
user@host> show configuration system services ftp; netconf { ssh; }
If the netconf
configuration hierarchy is absent,
issue the following statements in configuration mode to enable NETCONF
over SSH on the default port:
[edit] user@host# set system services netconf ssh user@host# commit
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:
user@host> show configuration system services netconf { ssh { port 12345; } }
To correct the connection issue, include the new port
number in the Device
arguments.
Device device = new Device("10.10.1.1", "admin", "PaSsWoRd", null, 12345);
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:
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.
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.
/*ExecuteCLICommand*/ import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import net.juniper.netconf.Device; import net.juniper.netconf.NetconfException; import net.juniper.netconf.XML; import org.xml.sax.SAXException; public class ExecuteCLICommand { public static void main(String args[]) throws NetconfException, ParserConfigurationException, SAXException, IOException { String cli = "show chassis hardware"; Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); try { String cli_reply = device.runCliCommand(cli); System.out.println(cli_reply); } catch (Exception e) { System.out.println("exception: " + e.getMessage()); // additional processing for exception } device.close(); } }
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:
Compile the ExecuteCLICommand.java file.
> javac ExecuteCLICommand.java
Execute the ExecuteCLICommand program.
> java ExecuteCLICommand
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.
Hardware inventory: Item Version Part number Serial number Description Chassis 30010 M7I Midplane REV 03 710-008761 CB3874 M7i Midplane Power Supply 0 Rev 04 740-008537 PG10715 AC Power Supply Routing Engine REV 07 740-009459 1000445584 RE-5.0 CFEB REV 07 750-010464 CM4612 Internet Processor II FPC 0 E-FPC PIC 0 REV 06 750-002971 CB0032 4x OC-3 SONET, MM PIC 1 REV 02 750-002982 HS2878 1x Tunnel PIC 2 REV 08 750-005724 CL9084 2x OC-3 ATM-II IQ, MM PIC 3 REV 12 750-012838 DJ1107 4x 1GE(LAN), IQ2 Xcvr 0 REV 01 740-013111 7303405 SFP-T Xcvr 1 REV 01 740-013111 7303391 SFP-T Xcvr 2 REV 01 740-013111 7303350 SFP-T Xcvr 3 REV 01 740-013111 7303420 SFP-T FPC 1 E-FPC PIC 2 REV 07 750-009487 CL5745 ASP - Integrated (Layer-2-3) PIC 3 REV 07 750-009098 CB7256 2x F/E, 100 BASE-TX Fan Tray Rear Fan Tray
On a device running Junos OS Release 11.3 or earlier release, the output contains Junos XML tag elements.
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos"> <chassis-inventory xmlns="http://xml.juniper.net/junos/11.2R1/junos-chassis"> <chassis junos:style="inventory"> <name>Chassis</name> <serial-number>30010</serial-number> <description>M7I</description> <chassis-module> <name>Midplane</name> <version>REV 03</version> <part-number>710-008761</part-number> <serial-number>CB3874</serial-number> <description>M7i Midplane</description> <model-number>CHAS-MP-M7i-1GE-S</model-number> </chassis-module> /* Output omitted for brevity */ </chassis> </chassis-inventory> </rpc-reply>
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:
<rpc-reply> <environment-information> <environment-item> <name>item-name</name> ... <temperature>temperature</temperature> </environment-item> <environment-item> <name>item-name2</name> ... <temperature>temperature</temperature> </environment-item> ... </environment-information> </rpc-reply>
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:
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.
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.
import java.io.IOException; import java.util.Arrays; import java.util.Iterator; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import net.juniper.netconf.CommitException; import net.juniper.netconf.Device; import net.juniper.netconf.LoadException; import net.juniper.netconf.NetconfException; import net.juniper.netconf.XML; import net.juniper.netconf.XMLBuilder; import org.xml.sax.SAXException; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class showTemps { public static void main(String[] args) throws LoadException, IOException, NetconfException, ParserConfigurationException, SAXException { String name="", temp=""; //Create the device Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect(); //Call executeRPC(String rpc) to send RPC and receive RPC reply XML rpc_reply = device.executeRPC("get-environment-information"); // Parse reply and only print items that have a temperature element List<String> list = Arrays.asList("environment-information","environment-item"); List itemlist = rpc_reply.findNodes(list); Iterator iter = itemlist.iterator(); while (iter.hasNext()) { Node item_node = (Node) iter.next(); NodeList child_nodes = item_node.getChildNodes(); // child_nodes contains nodes like <name> and <temperature> for (int i = 0; i < child_nodes.getLength(); i++) { Node child = child_nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals("name")) // Capture the text value in <name> node name = child.getTextContent(); if (child.getNodeName().equals("temperature")) { // Capture the text value in <temperature> node temp = child.getTextContent(); System.out.println(name + ": " + temp); } } } } device.close(); } }
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:
Compile the ShowTemps.java file.
> javac ShowTemps.java
Execute the ShowTemps program.
> java ShowTemps
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:
Intake: 25 degrees C / 77 degrees F FPC 0: 26 degrees C / 78 degrees F Power Supplies: 28 degrees C / 82 degrees F CFEB Intake: 22 degrees C / 71 degrees F CFEB Exhaust: 30 degrees C / 86 degrees F Routing Engine: 28 degrees C / 82 degrees F Routing Engine CPU: 28 degrees C / 82 degrees F