Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Navigation
 

Related Documentation

 

Example: Using the NETCONF Java Toolkit to Load and Commit a Configuration

The following example NETCONF Java toolkit program constructs a configuration hierarchy, which is then merged with the candidate configuration on the specified device. The resulting configuration is then committed. The sample configuration hierarchy is for 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 performs a load merge operation to update the candidate configuration on a device running Junos OS and then commits the new configuration. The XML hierarchy that will be added into the configuration is constructed with the XMLBuilder object and stored in the ftp_config variable. Alternatively, you can load configuration data as text and, for devices running Junos OS Release 11.4 or a later release, as a set of Junos OS configuration mode commands.

The new configuration hierarchy, which enables FTP service on the device, is:

<configuration>
     <system>
          <services>
               <ftp/>
          </services>
     </system>
</configuration>

The program code creates a new Device object and calls the connect() method. This establishes an SSHv2 connection and a default NETCONF session with the device on which the NETCONF server runs.

To prevent conflicts with other users who might simultaneously edit the candidate configuration, the code calls the lockConfig() method on the device object to lock the configuration. If the lock fails, the method generates an error message, and the program exits. If the lock is successful, the loadXMLConfiguration(ftp_config.toString(), "merge") method loads the new configuration hierarchy into the candidate configuration using the merge option. Notice that, although the configuration hierarchy is initially constructed as XML, you must convert it to a string before passing it as an argument to the loadXMLConfiguration() method.

Once the new configuration hierarchy is merged with the candidate configuration, the program attempts to commit the configuration. If the commit operation is unsuccessful, the program prints the associated error message. The program then unlocks the configuration and closes the NETCONF session and device connection.

Note: For more information about the merge and replace options for loading configuration hierarchies and statements into the candidate configuration, see the CLI User Guide.

Configuration

Creating the Java Program

Step-by-Step Procedure

To construct the Java program file that contains the code for the configuration changes and requests:

  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 EditConfig.

  2. Add the code to the file and update the environment-specific variables such as the remote host IP address, username, and password.

    The complete Java code for the EditConfig program is presented here.

    import java.io.IOException;
    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;
    
     public class EditConfig {
         public static void main(String[] args) throws LoadException, IOException,
                   NetconfException, ParserConfigurationException, SAXException {
     
    /*Build the following XML hierarchy to add to the configuration: * <configuration> * <system> * <services> * <ftp/> * </services> * </system> * </configuration> */  
    XMLBuilder builder = new XMLBuilder(); XML ftp_config = builder.createNewConfig("system", "services", "ftp");  
    //Create the device Device device = new Device("10.10.1.1","admin","PaSsWoRd",null); device.connect();  
    //Lock the configuration boolean isLocked = device.lockConfig(); if(!isLocked) { System.out.println("Could not lock configuration. Exit now."); return; }  
    //Load and commit the configuration try { device.loadXMLConfiguration(ftp_config.toString(), "merge"); device.commit(); } catch(LoadException e) { System.out.println(e.getMessage()); return; } catch(CommitException e) { System.out.println(e.getMessage()); return; }  
    //Unlock the configuration and close the device device.unlockConfig(); 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 EditConfig.java file.
    >javac EditConfig.java
  2. Execute the resulting EditConfig program.
    >java EditConfig

Verification

Verifying Program Execution

Purpose

Verify that the EditConfig 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 merges the new hierarchy with the candidate configuration on the device and commits the configuration.

You can verify that the configuration was correctly merged and committed by viewing the resulting configuration on the remote device. The ftp statement should now be in the active configuration. On a device running Junos OS, enter the following operational mode command to view the [edit system services] hierarchy:

user@host> show configuration system services
ftp;
netconf {
    ssh;
}

Troubleshooting

Troubleshooting Error Messages

Problem

The following error message is printed to the display:

Could not lock configuration. Exit now.

Solution

Another user currently has a lock on the candidate configuration. Wait until the lock is released and execute the program.

 

Related Documentation

 

Published: 2013-07-26

 

Related Documentation

 

Published: 2013-07-26