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 Set Configuration Commands

This NETCONF Java toolkit program demonstrates the loadSetConfiguration() method, which updates the configuration using a set of Junos OS configuration mode commands.

Requirements

  • Routing, switching, or security device running Junos OS Release 11.4 or later.
  • 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 Device class contains the loadSetConfiguration() and loadSetFile() methods, which load configuration data as a set of Junos OS configuration mode commands on devices running Junos OS Release 11.4 or a later release. For each configuration element, you can specify the complete statement path in the command, or you can use navigation commands , such as edit, to move around the configuration hierarchy as you do in CLI configuration mode. The NETCONF Java toolkit converts the command set to the equivalent RPC in XML that can be processed by the NETCONF server on devices running Junos OS. Junos OS executes the configuration instructions line by line.

The method syntax is:

public void loadSetConfiguration (String setCommands)
public void loadSetFile (String filePath)

The loadSetConfiguration() method takes as an argument the configuration command string that you would enter in Junos OS CLI configuration mode. For example, to add the ftp statement at the [edit system services] hierarchy level, you use the set system services ftp command. The loadSetFile() method takes as an argument the path of the file containing the set of configuration commands.

You can also use both methods to load multiple commands. To load multiple commands using the loadSetConfiguration() method, you can either list the commands as a single string and separate them with the \n newline sequence, or you can execute the method separately for each command. To load multiple commands using the loadSetFile() method, place each command on a separate line in the file.

Note: When using the loadSetConfiguration() method with navigation commands, you should list the commands as a single string and separate them with the \n newline sequence. You cannot call the loadSetConfiguration() method with a single navigation command such as up.

The program in this example loads two configuration commands, which merge two statements into the candidate configuration on a device running Junos OS Release 11.4. The first command, set system services ftp, adds the ftp statement at the [edit system services] hierarchy level. The second command, set interfaces ge-0/0/0 disable, adds the disable statement at the [edit interfaces ge-0/0/0] hierarchy level. The relevant statements in the program code are:

String system_config = "set system services ftp";
String interfaces_config = "set interfaces ge-0/0/0 disable"; 

device.loadSetConfiguration(system_config);
device.loadSetConfiguration(interfaces_config);

Configuration

Creating the Java Program

Step-by-Step Procedure

To construct the Java program file:

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

  2. Add the code to the file and update the environment-specific variables such as the remote host IP address, username, password, and <rpc-reply> tag elements.

    The complete Java code for the LoadSetConfig.java program is presented here.

    If you load the set of commands from a file, create a file containing the commands, and replace the two loadSetConfiguration() method calls with a call to the loadSetFile() method.

    /*LoadSetConfig*/
    import java.io.IOException;
    import javax.xml.parsers.ParserConfigurationException;
    import net.juniper.netconf.Device;
    import net.juniper.netconf.CommitException;
    import net.juniper.netconf.LoadException;
    import net.juniper.netconf.NetconfException;
    import net.juniper.netconf.XML;
    import org.xml.sax.SAXException;
    
    public class LoadSetConfig {
        public static void main(String args[]) throws NetconfException,
                  ParserConfigurationException, SAXException, IOException {
            
            String system_config = "set system services ftp";
            String interfaces_config = "set interfaces ge-0/0/0 disable"; 
            
            Device device = new Device("10.10.1.1","admin","PaSsWoRd",null);
            
            try {
                device.connect();
                System.out.println("Connection successful.");
    
                if (device.lockConfig()) {
                    System.out.println("Configuration successfully locked.");
                    try {
                       System.out.println("Updating configuration.");        	
                       device.loadSetConfiguration(system_config);
                       device.loadSetConfiguration(interfaces_config);
                       System.out.println("Committing configuration.");        	
                       device.commit();
                    }
                    catch (LoadException e) {
                        System.out.println("LoadException occurred: " + e.getMessage());
                    }
                    catch (CommitException e) {
                        System.out.println("CommitException occurred: " + e.getMessage());
                    }
                    device.unlockConfig();
                    device.close();       
                }
                else { 
                    System.out.println("Error - cannot lock configuration");
                }
            }
            catch (NetconfException e) {
                System.out.println("Could not connect to device: " + e.getMessage());
            }
        }
    }

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 LoadSetConfig.java file.
    >javac LoadSetConfig.java
  2. Execute the resulting LoadSetConfig program.
    >java LoadSetConfig

Verification

To confirm that the program is working properly:

Verifying Program Execution

Purpose

Verify that the LoadSetConfig program runs correctly.

Action

If the program executes successfully, it establishes a connection and creates a NETCONF session with the specified device. The program merges the new statements with the candidate configuration on the device and commits the configuration.

>java LoadSetConfig
Connection successful.
Configuration successfully locked.
Updating configuration.
Committing configuration.

Verifying the Configuration Changes

Purpose

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

Action

admin@host> show configuration system services
ftp;
netconf {
    ssh;
}
admin@host> show configuration interfaces
ge-0/0/0 {
    disable;
}

Verifying the Commit

Purpose

Additionally, you can review the commit log to verify that the commit was successful. On a device running Junos OS, issue the show system commit operational mode command to view the commit log. In this example, the log confirms that the user admin committed the candidate configuration in a NETCONF session at the given date and time.

Action

Issue the show system commit operational mode command and review the commit log.

admin@host> show system commit
0   2011-09-02 14:16:44 PDT by admin via netconf
1   2011-07-08 14:33:46 PDT by root via other
 

Related Documentation

 

Published: 2013-07-26

 

Related Documentation

 

Published: 2013-07-26