Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All
     

    Related Documentation

     

    Example: Using the NETCONF Java Toolkit to Execute 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().
      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 {
          }
      }
    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.

      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.

    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.

      XML rpc_reply = device.executeRPC(“get-chassis-inventory”);
      
    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:

      System.out.println(rpc_reply.toString());
    6. 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:

    1. Compile the GetChassisInventory.java file.
      >javac GetChassisInventory.java
    2. Execute the resulting 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 sshuser@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);
    
     

    Related Documentation

     

    Published: 2013-07-26