Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Create and Execute a NETCONF Java Application

date_range 15-Jul-21

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 Download and Install 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().
    content_copy zoom_out_map
    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.

    content_copy zoom_out_map
    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:

    content_copy zoom_out_map
    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:

    content_copy zoom_out_map
    System.out.println(reply.toString());
  6. Close the device and release resources by calling the close() method on the Device object.
    content_copy zoom_out_map
    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:

content_copy zoom_out_map
/* 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, issue the following command on the command line of the configuration management server:

    content_copy zoom_out_map
    > javac ShowChassis.java
    

    This creates the ShowChassis.class file.

  2. Execute the program.
    content_copy zoom_out_map
    > java ShowChassis
    
footer-navigation