Perl クライアントアプリケーションで NETCONF サーバーにリクエストを送信する
NETCONF Perl クライアントアプリケーションでは、NETCONF サーバーへの接続を確立した後、クライアントアプリケーションは、Junos OS を実行しているデバイス上で運用コマンドまたは設定コマンドを実行して、運用情報を要求したり、設定を変更したりできます。NETCONF Perl APIは、CLI運用モードコマンドとNETCONF設定操作に対応する一連のメソッドをサポートしています。コマンドを実行するために、クライアントアプリケーションはそのコマンドに対応するPerlメソッドを呼び出します。
詳細については、以下のセクションを参照してください。
Junos OSコマンドとNETCONF操作のPerlメソッドへのマッピング
ほとんどの操作コマンドには、対応するJunos XMLリクエストタグがあります。 Junos XML APIエクスプローラを使用して、操作コマンド用のJunos XMLリクエストタグを見つけることができます。CLI で Junos XML リクエストタグを表示することもできます。リクエストタグを取得したら、対応するPerlメソッド名にマッピングできます。
CLI内のコマンドに対するJunos XMLリクエストタグを表示するには、コマンドを発行し、 | display xml rpc オプションを含めます。以下の例では、 show route コマンドのrequestタグを表示します。
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>
運用コマンドのリクエストタグをPerlメソッド名にマッピングできます。メソッド名を派生させるには、リクエストタグ内のハイフンをアンダースコアに置き換え、山括弧を削除します。たとえば、 <get-route-information> リクエストタグは get_route_information メソッド名にマップされます。
同様に、NETCONFプロトコルの操作も同じ方法でPerlメソッド名にマッピングされます。たとえば、 <edit-config> 操作は edit_config メソッド名にマップされます。
メソッドオプションの提供
Perl メソッドは 1 つ以上のオプションを持つことができます。次のセクションでは、NETCONF Perl クライアントアプリケーションでメソッドのオプションを定義するためにアプリケーションが使用する表記法について説明します。
オプションのないメソッドは、次の
get_autoinstallation_status_informationメソッドのエントリのように、$NO_ARGSとして定義されます。## Method : get_autoinstallation_status_information ## Returns: <autoinstallation-status-information> ## Command: "show system autoinstallation status" get_autoinstallation_status_information => $NO_ARGS,
オプションなしでメソッドを呼び出すには、次の例のように、クライアント アプリケーションはメソッド名の後に空の括弧を付きます。
$jnx->get_autoinstallation_status_information();
固定形式オプションは、タイプ
$TOGGLEとして定義されます。次の例では、get_ancp_neighbor_informationメソッドにはbriefとdetailという 2 つの固定形式オプションがあります。## Method : get_ancp_neighbor_information ## Returns: <ancp-neighbor-information> ## Command: "show ancp neighbor" get_ancp_neighbor_information => { brief => $TOGGLE, detail => $TOGGLE, }メソッドを呼び出すときに固定形式オプションを含めるには、次の例のように、オプションを文字列
'True'に等しく設定します。$jnx->get_ancp_neighbor_information(brief => 'True');
注:リリース依存の NETCONF Perl ディストリビューションを使用する場合、メソッドを呼び出すときに固定形式オプションを含めるには、オプションを値 1 (1) に等しく設定します。
変数値を持つオプションは、タイプ
$STRINGとして定義されます。次の例では、get_cos_drop_profile_informationメソッドはprofile_name引数を取ります。## 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, },メソッドを呼び出すときに変数値を含めるには、次の例のように値を一重引用符で囲みます。
$jnx->get_cos_drop_profile_information(profile_name => 'user-drop-profile');
設定ステートメントまたは対応するタグ要素のセットは、タイプ
$DOMとして定義されます。次の例では、get_configメソッドは一連の設定ステートメント(2つの属性)を受け取ります。'get_config' => { 'source' => $DOM_STRING, 'source_url' => $URL_STRING, 'filter' => $DOM },DOM オブジェクトは XML コードです。
my $xml_string = " <filter type=\"subtree\"> <configuration> <protocols> <bgp></bgp> </protocols> </configuration> </filter> "; my %queryargs = ( 'source' => "running", 'filter' => $xml_string, );これにより、以下のRPCリクエストが生成されます。
<rpc message-id='1'> <get-config> <source> <running/> </source> <filter type="subtree"> <configuration> <protocols> <bgp></bgp> </protocols> </configuration> </filter> </get-config> </rpc>
メソッドには、固定形式オプション、変数値を持つオプション、および一連の設定ステートメントの組み合わせを含めることができます。たとえば、 get_forwarding_table_information メソッドには、4 つの固定形式オプションと、変数値を持つ 5 つのオプションがあります。
## 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, },
リクエストの送信
次のコードは、NETCONFサーバーに設定要求を送信する推奨方法と、エラー状態の処理方法を示しています。$jnx変数は、NET::Netconf::Managerオブジェクトとして定義されています。edit_configuration.plサンプルスクリプトから取得したサンプルコードは、候補の設定をロックし、設定変更を読み込み、変更をコミットした後、設定データベースのロックを解除してNETCONFサーバーから切断します。完全なedit_configuration.plスクリプトは、https://github.com/Juniper/netconf-perl にあるNETCONF Perl GitHubリポジトリのexamples/edit_configurationディレクトリにあります。
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);