Create and Execute a NETCONF Java Application
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:
Create a Java program that includes code to connect to a device and to execute the desired operations or requests.
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:
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.