Submit a Request to the NETCONF Server in Perl Client Applications
In a NETCONF Perl client application, after establishing a connection to the NETCONF server, the client application can execute operational or configuration commands on a device running Junos OS to request operational information or change the configuration. The NETCONF Perl API supports a set of methods that correspond to CLI operational mode commands and NETCONF configuration operations. To execute a command, the client application invokes the Perl method corresponding to that command.
Beginning in Junos OS Release 16.1, the NETCONF Perl client is release-independent, is hosted on GitHub and CPAN, and can manage devices running any version of the Junos OS release. The release-independent version of the NETCONF Perl client can invoke any method that has a corresponding Junos XML request tag.
Prior to Junos OS Release 16.1, every Junos OS release included a new, release-dependent version of the NETCONF Perl client. Each version of the software supported a set of methods that corresponded to specific CLI operational mode commands and operations on configuration objects. You can view the list of operational methods supported in that version of the client by examining the files stored in the lib/Net/Netconf/Plugins/Plugin/release directory of the NETCONF Perl distribution. The set of methods that correspond to operations on configuration objects is defined in the lib/Net/Netconf/Plugins.pm file of the distribution.
See the following sections for more information:
Mapping Junos OS Commands and NETCONF Operations to Perl Methods
All operational commands that have Junos XML counterparts are listed in the Junos XML API Operational Developer Reference. You can also display the Junos XML request tag elements for any operational mode command that has a Junos XML counterpart on the CLI. Once you obtain the request tag, you can map it to the corresponding Perl method name.
To display the Junos XML request tags for a command
in the CLI, include the | display xml rpc
option after
the command. The following example displays the request tag for the show route
command:
user@host> show route | display xml rpc <rpc-reply xmlns:junos="http://xml.juniper.net/junos/15.1R1/junos"> <rpc> <get-route-information> </get-route-information> </rpc> </rpc-reply>
You can map the request tag for an operational command to a
Perl method name. To derive the method name, replace any hyphens in
the request tag with underscores, and remove the enclosing angle brackets.
For example, the <get-route-information>
request tag maps to the get_route_information
method name.
Similarly, NETCONF protocol operations map to Perl method names
in the same manner. For example, the <edit-config>
operation maps to the edit_config
method
name.
Providing Method Options
Perl methods can have one or more options. The following section describes the notation that an application uses to define a method’s options in a NETCONF Perl client application.
A method without options is defined as
$NO_ARGS
, as in the following entry for theget_autoinstallation_status_information
method:## Method : get_autoinstallation_status_information ## Returns: <autoinstallation-status-information> ## Command: "show system autoinstallation status" get_autoinstallation_status_information => $NO_ARGS,
To invoke a method without options, the client application follows the method name with an empty set of parentheses, as in the following example:
$jnx->get_autoinstallation_status_information();
A fixed-form option is defined as type
$TOGGLE
. In the following example, theget_ancp_neighbor_information
method has two fixed-form options,brief
anddetail
:## Method : get_ancp_neighbor_information ## Returns: <ancp-neighbor-information> ## Command: "show ancp neighbor" get_ancp_neighbor_information => { brief => $TOGGLE, detail => $TOGGLE, }
To include a fixed-form option when invoking a method, set the option equal to the string
'True'
, as in the following example:$jnx->get_ancp_neighbor_information(brief => 'True');
Note:When using the release-dependent NETCONF Perl distribution, to include a fixed-form option when invoking a method, set the option equal to the value 1 (one).
An option with a variable value is defined as type
$STRING
. In the following example, theget_cos_drop_profile_information
method takes theprofile_name
argument:## Method : get_cos_drop_profile_information ## Returns: <cos-drop-profile-information> ## Command: "show class-of-service drop-profile" get_cos_drop_profile_information => { profile_name => $STRING, },
To include a variable value when invoking a method, enclose the value in single quotes, as in the following example:
$jnx->get_cos_drop_profile_information(profile_name => 'user-drop-profile');
A set of configuration statements or corresponding tag elements is defined as type
$DOM
. In the following example, theget_config
method takes a set of configuration statements (along with two attributes):'get_config' => { 'source' => $DOM_STRING, 'source_url' => $URL_STRING, 'filter' => $DOM },
A DOM object is XML code:
my $xml_string = " <filter type=\"subtree\"> <configuration> <protocols> <bgp></bgp> </protocols> </configuration> </filter> "; my %queryargs = ( 'source' => "running", 'filter' => $xml_string, );
This generates the following RPC request:
<rpc message-id='1'> <get-config> <source> <running/> </source> <filter type="subtree"> <configuration> <protocols> <bgp></bgp> </protocols> </configuration> </filter> </get-config> </rpc>
A method can have a combination of fixed-form options, options
with variable values, and a set of configuration statements. For example,
the get_forwarding_table_information
method
has four fixed-form options and five options with variable values:
## Method :get_forwarding_table_information
## Returns:<forwarding-table-information>
## Command: "show route forwarding-table" get_forwarding_table_information => { detail => $TOGGLE, extensive => $TOGGLE, multicast => $TOGGLE, family => $STRING, vpn => $STRING, summary => $TOGGLE, matching => $STRING, destination => $STRING, label => $STRING, },
Submitting a Request
The following code illustrates the recommended way to send a
configuration request to the NETCONF server and shows how to handle
error conditions. The $jnx
variable is
defined to be a NET::Netconf::Manager
object.
The sample code, which is taken from the edit_configuration.pl sample script, locks the candidate configuration, loads the configuration
changes, commits the changes, and then unlocks the configuration database
and disconnects from the NETCONF server. You can view the complete edit_configuration.pl script in the examples/edit_configuration directory in the NETCONF
Perl GitHub repository at https://github.com/Juniper/netconf-perl.
my $res; # Netconf server response # connect to the Netconf server my $jnx = new Net::Netconf::Manager(%deviceinfo); unless (ref $jnx) { croak "ERROR: $deviceinfo{hostname}: failed to connect.\n"; } # Lock the configuration database before making any changes print "Locking configuration database ...\n"; my %queryargs = ( 'target' => 'candidate' ); $res = $jnx->lock_config(%queryargs); # See if you got an error if ($jnx->has_error) { print "ERROR: in processing request \n $jnx->{'request'} \n"; graceful_shutdown($jnx, STATE_CONNECTED, REPORT_FAILURE); } # Load the configuration from the given XML file print "Loading configuration from $xmlfile \n"; if (! -f $xmlfile) { print "ERROR: Cannot load configuration in $xmlfile\n"; graceful_shutdown($jnx, STATE_LOCKED, REPORT_FAILURE); } # Read in the XML file my $config = read_xml_file($xmlfile); print "\n\n$config \n\n"; %queryargs = ( 'target' => 'candidate' ); # If we are in text mode, use config-text arg with wrapped # configuration-text, otherwise use config arg with raw # XML if ($opt{t}) { $queryargs{'config-text'} = '<configuration-text>' . $config . '</configuration-text>'; } else { $queryargs{'config'} = $config; } $res = $jnx->edit_config(%queryargs); # See if you got an error if ($jnx->has_error) { print "ERROR: in processing request \n $jnx->{'request'} \n"; # Get the error my $error = $jnx->get_first_error(); get_error_info(%$error); # Disconnect graceful_shutdown($jnx, STATE_LOCKED, REPORT_FAILURE); } # Commit the changes print "Committing the <edit-config> changes ...\n"; $jnx->commit(); if ($jnx->has_error) { print "ERROR: Failed to commit the configuration.\n"; graceful_shutdown($jnx, STATE_CONFIG_LOADED, REPORT_FAILURE); } # Unlock the configuration database and # disconnect from the Netconf server print "Disconnecting from the Netconf server ...\n"; graceful_shutdown($jnx, STATE_LOCKED, REPORT_SUCCESS);
Change History Table
Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.