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

    Creating and Executing a NETCONF Java Toolkit Program File

    You can use the NETCONF Java toolkit 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().

      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 ShowChassis {
          public static void main(String args[]) throws NetconfException, 
                    ParserConfigurationException, SAXException, IOException {
      
          }
      }
    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.

      Device device = new Device("hostname", "username", "password", null); 
      device.connect();
    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:

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

      System.out.println(reply.toString());
    6. Close the device and release resources by calling the close() method on the Device object.

      device.close();
      

    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:

    /* ShowChassis.java */
    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 ShowChassis {
        public static void main(String args[]) throws NetconfException, 
                  ParserConfigurationException, SAXException, IOException {
            
            //Create the device object and establish a NETCONF session
            Device device = new Device("hostname", "username", "password", null);
            device.connect();
            
            //Send RPC and receive RPC reply as XML
            XML rpc_reply = device.executeRPC("get-chassis-inventory");
    
            //Print the RPC reply and close the device
            System.out.println(rpc_reply.toString());
            device.close();
        }
    }

    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, you would issue the following command on the command line of the configuration management server:

      >javac ShowChassis.java

      This creates the ShowChassis.class file.

    2. Execute the resulting program.
      >java ShowChassis

    Published: 2013-07-26