| 1 | # Net::Netconf::Device - Implements a remote Netconf device | ||
| 2 | |||
| 3 | package Net::Netconf::Device; | ||
| 4 | use strict; | ||
| 5 | use Net::Netconf::Trace; | ||
| 6 | use Net::Netconf::Constants; | ||
| 7 | use Net::Netconf::Access; | ||
| 8 | use XML::LibXML; | ||
| 9 | use XML::LibXML::SAX; | ||
| 10 | use XML::LibXML::SAX::Parser; | ||
| 11 | use Net::Netconf::SAXHandler; | ||
| 12 | use File::Basename; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | no strict 'subs'; | ||
| 16 | require Exporter; | ||
| 17 | |||
| 18 | use vars qw(@EXPORT); | ||
| 19 | use vars qw(@ISA); | ||
| 20 | use vars qw($AUTOLOAD); | ||
| 21 | |||
| 22 | our $VERSION ='1.02'; | ||
| 23 | my $NO_ARGS = bless {}, 'NO_ARGS'; | ||
| 24 | my $TOGGLE = bless { 1 => 1 }, 'TOGGLE'; | ||
| 25 | my $TOGGLE_NO = bless {}, 'TOGGLE'; | ||
| 26 | my $STRING = bless {}, 'STRING'; | ||
| 27 | my $DOM = bless {}, 'DOM'; | ||
| 28 | my $ATTRIBUTE = bless {}, 'ATTRIBUTE'; | ||
| 29 | |||
| 30 | my $URL_STRING = bless {}, 'URL_STRING'; | ||
| 31 | my $DOM_STRING = bless {}, 'DOM_STRING'; | ||
| 32 | my $message_id = 1; # message-id for the rpc request | ||
| 33 | |||
| 34 | # Capabilities supported | ||
| 35 | my $capability = 'http://xml.juniper.net/netconf/junos/1.0'; | ||
| 36 | |||
| 37 | my %methods = ( | ||
| 38 | 'get_xnm_information' => { | ||
| 39 | 'type' => $STRING, | ||
| 40 | 'namespace' => $STRING | ||
| 41 | }, | ||
| 42 | 'get' => { | ||
| 43 | filter => $DOM | ||
| 44 | }, | ||
| 45 | 'lock_config' => { | ||
| 46 | 'tag_name' => 'lock', | ||
| 47 | 'target' => $DOM_STRING, | ||
| 48 | 'target_url' => $URL_STRING | ||
| 49 | }, | ||
| 50 | 'unlock_config' => { | ||
| 51 | 'tag_name' => 'unlock', | ||
| 52 | 'target' => $DOM_STRING, | ||
| 53 | 'target_url' => $URL_STRING | ||
| 54 | }, | ||
| 55 | 'kill_session' => { | ||
| 56 | 'session_id' => $STRING | ||
| 57 | }, | ||
| 58 | 'close_session' => $NO_ARGS, | ||
| 59 | 'get_config' => { | ||
| 60 | 'source' => $DOM_STRING, | ||
| 61 | 'source_url' => $URL_STRING, | ||
| 62 | 'filter' => $DOM | ||
| 63 | }, | ||
| 64 | 'edit_config' => { | ||
| 65 | 'operation' => $ATTRIBUTE, | ||
| 66 | 'target' => $DOM_STRING, | ||
| 67 | 'default_operation' => $STRING, | ||
| 68 | 'test-option' => $STRING, | ||
| 69 | 'error-option' => $STRING, | ||
| 70 | 'url' => $STRING, | ||
| 71 | 'config' => $DOM, | ||
| 72 | 'config-text' => $STRING | ||
| 73 | }, | ||
| 74 | 'copy_config' => { | ||
| 75 | 'target' => $DOM_STRING, | ||
| 76 | 'target_url' => $URL_STRING, | ||
| 77 | 'source' => $DOM_STRING, | ||
| 78 | 'source_url' => $URL_STRING, | ||
| 79 | }, | ||
| 80 | 'delete_config' => { | ||
| 81 | 'target' => $DOM_STRING, | ||
| 82 | 'target_url' => $URL_STRING, | ||
| 83 | }, | ||
| 84 | 'discard_changes' => $NO_ARGS, | ||
| 85 | 'commit' => { | ||
| 86 | 'confirmed' => $TOGGLE, | ||
| 87 | 'confirm_timeout' => $STRING | ||
| 88 | }, | ||
| 89 | 'validate' => { | ||
| 90 | 'source' => $DOM_STRING, | ||
| 91 | 'source_url' => $URL_STRING, | ||
| 92 | }, | ||
| 93 | 'get_database_status_information' => $NO_ARGS, | ||
| 94 | ); | ||
| 95 | |||
| 96 | #client's capabilities | ||
| 97 | my %capabilities = ( | ||
| 98 | 'urn:ietf:params:xml:ns:netconf:base:1.0' => | ||
| 99 | [ 'get', 'get_config', 'edit_config', 'copy_config', 'delete_config', | ||
| 100 | 'lock_config', 'unlock_config', 'close_session', 'kill_session', | ||
| 101 | 'discard_changes'], | ||
| 102 | 'urn:ietf:params:xml:ns:netconf:capability:candidate:1.0' => [ 'commit' ], | ||
| 103 | 'urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0' => [], | ||
| 104 | 'urn:ietf:params:xml:ns:netconf:capability:validate:1.0' => [ 'validate' ], | ||
| 105 | 'urn:ietf:params:xml:ns:netconf:capability:url:1.0?protocol=http,ftp,file' => [], | ||
| 106 | ); | ||
| 107 | |||
| 108 | # get the message id | ||
| 109 | sub get_msg_id { | ||
| 110 | return $message_id++; | ||
| 111 | } | ||
| 112 | |||
| 113 | # Create a new Netconf device object | ||
| 114 | # Connect to the Netconf server | ||
| 115 | sub new | ||
| 116 | { | ||
| 117 | my ($class, %args) = @_; | ||
| 118 | my $self = { %args }; | ||
| 119 | |||
| 120 | # Make sure we have 'login', 'hostname' and 'password' | ||
| 121 | croak 'missing information <hostname>' unless exists $args{'hostname'}; | ||
| 122 | croak 'missing information <login>' unless exists $args{'login'}; | ||
| 123 | croak 'missing information <password>' unless exists $args{'password'}; | ||
| 124 | |||
| 125 | # SAX Parser | ||
| 126 | my $handler = new Net::Netconf::SAXHandler('ErrorContext' => 5); | ||
| 127 | my $saxparser = new XML::LibXML::SAX::Parser('Handler' => $handler) | ||
| 128 | || croak 'Cannot create parser: ' . $!; | ||
| 129 | $self->{'sax_parser'} = $saxparser; | ||
| 130 | $self->{'handler'} = $handler; | ||
| 131 | |||
| 132 | # DOM parser | ||
| 133 | my $domparser = XML::LibXML->new(); | ||
| 134 | $self->{'dom_parser'}= $domparser; | ||
| 135 | |||
| 136 | # Bless ourselves | ||
| 137 | $class = ref($class) || $class; | ||
| 138 | bless $self, $class; | ||
| 139 | |||
| 140 | # Connection state | ||
| 141 | $self->{'conn_state'} = Net::Netconf::Constants::NC_STATE_DISCONN; | ||
| 142 | |||
| 143 | # Default trace level: WARNING_LEVEL | ||
| 144 | $self->{'debug_level'} = Net::Netconf::Trace::WARNING_LEVEL unless | ||
| 145 | $self->{'debug_level'}; | ||
| 146 | |||
| 147 | # Create the trace object | ||
| 148 | $self->{'trace_obj'} = new Net::Netconf::Trace($self->{'debug_level'}); | ||
| 149 | |||
| 150 | $self->{'methods'} = \%methods; | ||
| 151 | |||
| 152 | # Now bring up the connection | ||
| 153 | return $self->connect() unless $self->{'do_not_connect'}; | ||
| 154 | $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | # Open a connection to the Netconf server | ||
| 158 | sub connect | ||
| 159 | { | ||
| 160 | my($self) = @_; | ||
| 161 | my $conn; | ||
| 162 | my $trace_obj = $self->{'trace_obj'}; | ||
| 163 | $trace_obj->trace(Net::Netconf::Trace::TRACE_LEVEL, 'preparing to connect'); | ||
| 164 | |||
| 165 | # If we are already connected, we have the connection object | ||
| 166 | # in $self->{conn_obj} | ||
| 167 | return $self if $self->{'conn_obj'}; | ||
| 168 | |||
| 169 | eval { | ||
| 170 | $conn = new Net::Netconf::Access($self); # $self = ssh , connect to ssh | ||
| 171 | }; | ||
| 172 | |||
| 173 | if ($@ || !ref($conn)) { | ||
| 174 | croak 'unable to create Access object' . $@; | ||
| 175 | } | ||
| 176 | |||
| 177 | eval 'use ' . ref($conn); | ||
| 178 | if ($@) { | ||
| 179 | croak 'unable to create Access object using ' . $self->{'access'} . $@; | ||
| 180 | } | ||
| 181 | |||
| 182 | $self->{'conn_obj'} = $conn; | ||
| 183 | unless (defined ($conn->connect())) { | ||
| 184 | croak 'unable to connect to the Netconf server'; | ||
| 185 | } | ||
| 186 | |||
| 187 | $self->send_and_recv_hello(); | ||
| 188 | $self; | ||
| 189 | } | ||
| 190 | |||
| 191 | # - If the user specifies client capabilities, it uses that to generate | ||
| 192 | # the client hello. Else, it uses the default capability list given | ||
| 193 | # by Net::Netconf::Constants::NC_DEFAULT_CAP. | ||
| 194 | # - Does the hello exchange | ||
| 195 | # - Parses the server hello to get the server capabilities | ||
| 196 | # - Initializes the Netconf API based on the server capabilities | ||
| 197 | sub send_and_recv_hello | ||
| 198 | { | ||
| 199 | my($self) = shift; | ||
| 200 | my $msg; | ||
| 201 | my $conn = $self->{'conn_obj'}; | ||
| 202 | my $traceobj = $self->{'trace_obj'}; | ||
| 203 | my @server_capabilities; | ||
| 204 | |||
| 205 | # Generate the client capability using the capabilities passed to it | ||
| 206 | my $client_capability = <<EOF; | ||
| 207 | <hello> | ||
| 208 | <capabilities> | ||
| 209 | EOF | ||
| 210 | |||
| 211 | my $cap = $self->{'client_capabilities'}; | ||
| 212 | if ($cap) { # Client has specified some capability use that | ||
| 213 | foreach my $capelmt (@$cap) { | ||
| 214 | $client_capability .= <<EOF; | ||
| 215 | <capability>$capelmt</capability> | ||
| 216 | EOF | ||
| 217 | } | ||
| 218 | } else { # Use the default capability | ||
| 219 | my $default_capability = Net::Netconf::Constants::NC_DEFAULT_CAP; | ||
| 220 | $client_capability .= " $default_capability\n"; | ||
| 221 | } | ||
| 222 | $client_capability .= <<EOF; | ||
| 223 | </capabilities> | ||
| 224 | </hello> | ||
| 225 | EOF | ||
| 226 | |||
| 227 | |||
| 228 | # Send our capabilities to the Netconf server | ||
| 229 | # Get the server capability | ||
| 230 | my $server_cap = $self->send_and_recv_rpc($client_capability, | ||
| 231 | Net::Netconf::Constants::NC_HELLO_TAG, | ||
| 232 | Net::Netconf::Constants::NC_STATE_HELLO_SENT, | ||
| 233 | Net::Netconf::Constants::NC_STATE_HELLO_RECVD); | ||
| 234 | |||
| 235 | $self->{'conn_state'} = Net::Netconf::Constants::NC_STATE_CONN; | ||
| 236 | |||
| 237 | # Tracing what is going on | ||
| 238 | $traceobj->trace(Net::Netconf::Trace::DEBUG_LEVEL, <<EOF); | ||
| 239 | Client capability sent: | ||
| 240 | $client_capability | ||
| 241 | Server capability received: | ||
| 242 | $server_cap | ||
| 243 | EOF | ||
| 244 | |||
| 245 | # Parse the server hello to extract the capabilities | ||
| 246 | |||
| 247 | eval { | ||
| 248 | $self->{'sax_parser'}->parse_string($server_cap); | ||
| 249 | }; | ||
| 250 | |||
| 251 | if ($@) { | ||
| 252 | $self->report_error(0, 'error in parsing server capability'); | ||
| 253 | $self->disconnect(); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | # Now save the session-id and server capabilities | ||
| 258 | $self->{'session_id'} = $self->{'handler'}->{'session_id'}; | ||
| 259 | @{$self->{'server_capabilities'}} = @{$self->{'handler'}->{'parsed_cap'}}; | ||
| 260 | |||
| 261 | # See if any errors were emitted and save them | ||
| 262 | $self->{'found_rpc_errors'} = $self->{'handler'}->{'found_error'}; | ||
| 263 | if ($self->{'found_rpc_errors'}) { | ||
| 264 | %{$self->{'rpc_errors'}} = (%{$self->{'handler'}->{'rpc_errors'}}); | ||
| 265 | } | ||
| 266 | $self; | ||
| 267 | } | ||
| 268 | |||
| 269 | # This returns the server capability for Netconf session | ||
| 270 | sub get_server_cap | ||
| 271 | { | ||
| 272 | my ($self) = @_; | ||
| 273 | return @{$self->{'server_capabilities'}}; | ||
| 274 | } | ||
| 275 | |||
| 276 | # Disconnect from the Netconf server | ||
| 277 | # Free up resources | ||
| 278 | sub disconnect | ||
| 279 | { | ||
| 280 | my($self) = @_; | ||
| 281 | my $conn = $self->{'conn_obj'}; | ||
| 282 | #carp "Disconnecting\n"; | ||
| 283 | $conn->disconnect if ($conn and $self->{'conn_state'} != | ||
| 284 | Net::Netconf::Constants::NC_STATE_DISCONN && not $conn->eof); | ||
| 285 | $self->{'conn_obj'} = undef; | ||
| 286 | } | ||
| 287 | |||
| 288 | # Helper function for sending and receiving Netconf commands and responses | ||
| 289 | # $xml = the xml string to send | ||
| 290 | # $endtag = we should read data till we find this tag (optional) | ||
| 291 | # $state_sent = state after we sent the data (optional) | ||
| 292 | # $state_recv = state after we receive data (optional) | ||
| 293 | sub send_and_recv_rpc | ||
| 294 | { | ||
| 295 | my($self, $xml, $endtag, $state_sent, $state_recv) = @_; | ||
| 296 | return unless ($xml); | ||
| 297 | my $conn = $self->{'conn_obj'}; | ||
| 298 | my $traceobj = $self->{'trace_obj'}; | ||
| 299 | my $in = ''; | ||
| 300 | my $msg; | ||
| 301 | return unless ($xml); | ||
| 302 | |||
| 303 | # Set the default values | ||
| 304 | $state_sent = Net::Netconf::Constants::NC_STATE_REQ_SENT unless ($state_sent); | ||
| 305 | $state_recv = Net::Netconf::Constants::NC_STATE_REQ_RECVD unless ($state_recv); | ||
| 306 | $endtag = Net::Netconf::Constants::NC_REPLY_TAG unless ($endtag); | ||
| 307 | |||
| 308 | # Send the request to the Netconf server | ||
| 309 | eval { | ||
| 310 | unless ($conn->send($xml)) { | ||
| 311 | carp 'failed to send user request'; | ||
| 312 | return; | ||
| 313 | }; | ||
| 314 | }; | ||
| 315 | if ($@) { | ||
| 316 | $self->report_error(0, 'connection to Netconf server lost'); | ||
| 317 | $self->disconnect(); | ||
| 318 | return undef; | ||
| 319 | } | ||
| 320 | |||
| 321 | $self->{'conn_state'} = $state_sent; | ||
| 322 | |||
| 323 | # Get a response from the Netconf server | ||
| 324 | $in = $self->read_rpc( $endtag ); | ||
| 325 | if (!defined $in) { | ||
| 326 | carp 'failed to recv user response'; | ||
| 327 | $self->report_error(0, 'connection to Netconf server lost'); | ||
| 328 | $self->disconnect(); | ||
| 329 | return undef; | ||
| 330 | } | ||
| 331 | |||
| 332 | $self->parse_response( $in ); | ||
| 333 | } | ||
| 334 | |||
| 335 | # Helper function to read RPC response until end tag | ||
| 336 | sub read_rpc | ||
| 337 | { | ||
| 338 | my $self = shift; | ||
| 339 | my $endtag = shift; | ||
| 340 | |||
| 341 | my $conn = $self->{'conn_obj'}; | ||
| 342 | my $in = ''; | ||
| 343 | |||
| 344 | while ( $self->{'conn_state'} != Net::Netconf::Constants::NC_STATE_HELLO_RECVD ) | ||
| 345 | { | ||
| 346 | if ($conn->eof) { | ||
| 347 | $self->report_error(0, 'connection to Netconf server lost'); | ||
| 348 | $self->disconnect(); | ||
| 349 | return undef; | ||
| 350 | } | ||
| 351 | |||
| 352 | eval { | ||
| 353 | $in .= $conn->recv(); | ||
| 354 | }; | ||
| 355 | if ($@) { | ||
| 356 | $self->report_error(0, 'connection to Netconf server lost'); | ||
| 357 | $self->disconnect(); | ||
| 358 | return undef; | ||
| 359 | } | ||
| 360 | |||
| 361 | # Check to see if you received the end-tag | ||
| 362 | if ($in =~ /<\/\s*$endtag\s*>/gs) | ||
| 363 | { | ||
| 364 | $self->{'conn_state'} = Net::Netconf::Constants::NC_STATE_HELLO_RECVD; | ||
| 365 | } | ||
| 366 | elsif ($conn->eof) | ||
| 367 | { | ||
| 368 | $self->report_error(0, 'connection to Netconf server lost'); | ||
| 369 | $self->disconnect(); | ||
| 370 | return undef; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | return $in; | ||
| 375 | } | ||
| 376 | |||
| 377 | # Once we receive the response from the server, we call this method before passing | ||
| 378 | # the response to the parser. We strip the response off the namespace values. | ||
| 379 | sub parse_response | ||
| 380 | { | ||
| 381 | my ($self, $response) = @_; | ||
| 382 | return unless $response; | ||
| 383 | $self->{'found_rpc_errors'} = 0; | ||
| 384 | $self->{'rpc_errors'} = undef; | ||
| 385 | # For junos:style, junos:format stuff which gives error while parsing | ||
| 386 | $response =~ s/junos://g; | ||
| 387 | $self->{'server_response'} = $response; | ||
| 388 | eval { | ||
| 389 | $self->{'sax_parser'}->parse_string($response); | ||
| 390 | }; | ||
| 391 | if ($@) { #parse error | ||
| 392 | carp 'error in parsing server response ' . "\n" . $response . "\n" . $@; | ||
| 393 | return $self->{'server_response'}; | ||
| 394 | } | ||
| 395 | |||
| 396 | # Save the total number of <rpc-error>s received and error information | ||
| 397 | |||
| 398 | $self->{'found_rpc_errors'} = $self->{'handler'}->{'found_error'}; #Net::Netconf::SAXHandler::found_error; | ||
| 399 | # found_error an attribute of SAXHandler | ||
| 400 | |||
| 401 | $self->{'no_error'} = $self->{'handler'}->{'no_error'};#Net::Netconf::SAXHandler::no_error; | ||
| 402 | #no_error an attribute of SAXHandler | ||
| 403 | |||
| 404 | # We should not get both <rpc-error> and <ok/> | ||
| 405 | #if ($self->{'found_rpc_errors'} && $self->{'no_error'}) { | ||
| 406 | # carp 'error in server response: has both <rpc-error> and <ok/> ' . | ||
| 407 | #"\n" . $response . "\n" . $@; | ||
| 408 | #return $self->{'server_response'}; | ||
| 409 | #} | ||
| 410 | |||
| 411 | if ($self->{'found_rpc_errors'}) { | ||
| 412 | %{$self->{'rpc_errors'}} = (%{$self->{'handler'}->{'rpc_errors'}});#Net::Netconf::SAXHandler::rpc_errors); | ||
| 413 | } | ||
| 414 | # Then we return the response as a string | ||
| 415 | return $self->{'server_response'}; | ||
| 416 | } | ||
| 417 | |||
| 418 | # This creates a DOM tree from the server response and returns it | ||
| 419 | # This is useful for <get> and <get-config> operations | ||
| 420 | sub get_dom | ||
| 421 | { | ||
| 422 | #print "inside dom object"; | ||
| 423 | my($self) = @_; | ||
| 424 | # Create a DOM object and return that. | ||
| 425 | # Server response is in: $self->{server_response} | ||
| 426 | my $doc = $self->{'dom_parser'}->parse_string($self->{'server_response'}); | ||
| 427 | $self->{'doc'} = $doc; | ||
| 428 | return $self->{'doc'}; | ||
| 429 | } | ||
| 430 | |||
| 431 | # This returns the session ID for this Netconf session | ||
| 432 | sub get_session_id | ||
| 433 | { | ||
| 434 | my ($self) = @_; | ||
| 435 | return $self->{'session_id'}; | ||
| 436 | } | ||
| 437 | |||
| 438 | # This returns the number of errors returned by the server | ||
| 439 | sub has_error | ||
| 440 | { | ||
| 441 | my ($self) = shift; | ||
| 442 | return $self->{'found_rpc_errors'}; | ||
| 443 | } | ||
| 444 | |||
| 445 | # This returns the first <rpc-error> emitted by the server as a hash reference. | ||
| 446 | # This also prints the errors in debug mode | ||
| 447 | sub get_first_error | ||
| 448 | { | ||
| 449 | my($self) = shift; | ||
| 450 | return unless ($self->{'found_rpc_errors'}); | ||
| 451 | $self->print_error_info(1); | ||
| 452 | return $self->{'rpc_errors'}{1}; | ||
| 453 | } | ||
| 454 | |||
| 455 | # This returns all the errors emitted by the server as a hash reference | ||
| 456 | # This also prints the errors in debug mode | ||
| 457 | sub get_all_errors | ||
| 458 | { | ||
| 459 | my($self) = @_; | ||
| 460 | return unless ($self->{'found_rpc_errors'}); | ||
| 461 | my $indx = 1; | ||
| 462 | while ($indx <= $self->{'found_rpc_errors'}) { | ||
| 463 | $self->print_error_info($indx); | ||
| 464 | $indx++; | ||
| 465 | } | ||
| 466 | return $self->{'rpc_errors'}; | ||
| 467 | } | ||
| 468 | |||
| 469 | # This prints the server error messages if in DEBUG_LEVEL | ||
| 470 | sub print_error_info | ||
| 471 | { | ||
| 472 | my $self = shift; | ||
| 473 | confess 'Usage: ' . __PACKAGE__ . '::print_error_info <key>' unless @_ == 1; | ||
| 474 | my $key = @_; | ||
| 475 | my $trace_obj = $self->{'trace_obj'}; | ||
| 476 | my @error_tag = ( | ||
| 477 | 'error_type', | ||
| 478 | 'error_tag', | ||
| 479 | 'error_severity', | ||
| 480 | 'error_path', | ||
| 481 | 'error_message', | ||
| 482 | 'error_info'); | ||
| 483 | $trace_obj->trace(Net::Netconf::Trace::DEBUG_LEVEL, | ||
| 484 | 'Server response HAD ERRORS ....'); | ||
| 485 | foreach my $tag (@error_tag) { | ||
| 486 | if ($self->{'rpc_errors'}{$key}{$tag}) { | ||
| 487 | $trace_obj->trace(Net::Netconf::Trace::DEBUG_LEVEL, | ||
| 488 | $tag . '=' . $self->{'rpc_errors'}{$key}{$tag}); | ||
| 489 | } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | # This prints the error message and quits the program based on $level | ||
| 494 | # $level > 0 means this is a fatal error, so quit | ||
| 495 | sub report_error | ||
| 496 | { | ||
| 497 | my $self = shift; | ||
| 498 | confess 'Usage: ' . __PACKAGE__ . '::report_error <level> <message>' | ||
| 499 | unless @_ == 2; | ||
| 500 | my ($level, $msg) = @_; | ||
| 501 | if ($level) { | ||
| 502 | $self->{'conn_obj'} = undef; | ||
| 503 | $self->{'trace_obj'} = undef; | ||
| 504 | # $self->{'sax_parser'} = undef; | ||
| 505 | croak $msg; | ||
| 506 | } | ||
| 507 | carp $msg; | ||
| 508 | } | ||
| 509 | |||
| 510 | #This function generate rpc and call send_and recv_rpc() method | ||
| 511 | sub generate_rpc | ||
| 512 | { | ||
| 513 | my ($self,$fn, %args) = @_; | ||
| 514 | my $bindings = $methods{ $fn }; | ||
| 515 | my $output = ""; | ||
| 516 | my $tag = ""; | ||
| 517 | my $attrs = ""; | ||
| 518 | my $url = ""; | ||
| 519 | my $msg_id = &get_msg_id; | ||
| 520 | |||
| 521 | foreach my $field (keys(%args)) { | ||
| 522 | my $type = $bindings->{ $field }; | ||
| 523 | my $value = $args{ $field }; | ||
| 524 | |||
| 525 | ($tag = $field) =~ s/_/-/g; | ||
| 526 | |||
| 527 | if (ref($type) eq 'TOGGLE' || ref($value) eq 'TOGGLE' || $value eq 'True') { | ||
| 528 | if ($value ne '0') { | ||
| 529 | $output .= " <$tag/>\n"; | ||
| 530 | } | ||
| 531 | |||
| 532 | } elsif (ref($value) eq 'TOGGLE_NO') { | ||
| 533 | if ($value eq '0') { | ||
| 534 | $output .= " <no-$tag/>\n"; | ||
| 535 | } else { | ||
| 536 | $output .= " <$tag/>\n"; | ||
| 537 | } | ||
| 538 | |||
| 539 | } elsif (ref($type) eq 'STRING') { | ||
| 540 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 541 | |||
| 542 | } elsif (ref($type) eq 'URL_STRING') { | ||
| 543 | ($tag, $url) = split(/[-_]/, $field); | ||
| 544 | $output .= " <${tag}><${url}>${value}</${url}></${tag}>\n"; | ||
| 545 | }elsif (ref($type) eq 'DOM_STRING') { | ||
| 546 | if ($value =~ '<\/') { | ||
| 547 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 548 | } else { | ||
| 549 | $output .= " <${tag}><${value}/></${tag}>\n"; | ||
| 550 | } | ||
| 551 | }elsif ($type =~ /(\d)+\.\.(\d)+/) { | ||
| 552 | $output .= " <${tag}>${value}</${tag}>\n"; | ||
| 553 | |||
| 554 | } elsif (ref($type) eq 'DOM') { | ||
| 555 | $output .= $value->toString; | ||
| 556 | $output .= "\n"; | ||
| 557 | |||
| 558 | } elsif (ref($type) eq 'ATTRIBUTE') { | ||
| 559 | $attrs .= " ${tag}=\"${value}\"\n"; | ||
| 560 | |||
| 561 | } elsif (ref($value)) { | ||
| 562 | $output .= $value->toString; | ||
| 563 | $output .= "\n"; | ||
| 564 | |||
| 565 | } else { | ||
| 566 | $output .= " <${field}>${value}</${field}>\n"; | ||
| 567 | } | ||
| 568 | } | ||
| 569 | if ($bindings->{'tag_name'}) { | ||
| 570 | $fn = $bindings->{'tag_name'}; | ||
| 571 | } | ||
| 572 | |||
| 573 | ($tag = $fn) =~ s/_/-/g; | ||
| 574 | |||
| 575 | if ($output) { | ||
| 576 | $output = "<rpc message-id=\'$msg_id\'>\n <${tag}${attrs}>\n${output} </${tag}>\n</rpc>\n"; | ||
| 577 | } | ||
| 578 | else { | ||
| 579 | $output = "<rpc message-id=\'$msg_id\'>\n <${tag}${attrs}/>\n</rpc>\n"; | ||
| 580 | } | ||
| 581 | $self->{'request'} = $output; | ||
| 582 | my $response = $self->send_and_recv_rpc($output); | ||
| 583 | my $traceobj = $self->{'trace_obj'}; | ||
| 584 | $traceobj->trace(Net::Netconf::Trace::DEBUG_LEVEL,<<EOF); | ||
| 585 | SERVER REQUEST: | ||
| 586 | $output | ||
| 587 | SERVER RESPONSE: | ||
| 588 | $response | ||
| 589 | EOF | ||
| 590 | } | ||
| 591 | |||
| 592 | # This will be removed after the <get-config> accepts parameters in any order | ||
| 593 | sub get_config | ||
| 594 | { | ||
| 595 | my ($self,%args) = @_; | ||
| 596 | my $msg_id = &get_msg_id; | ||
| 597 | my $request = '<rpc message-id=\'' . $msg_id . '\'>'; | ||
| 598 | $request .= '<get-config> <source>'; | ||
| 599 | if (exists $args{'source_url'}) { | ||
| 600 | $request .= '<url> <'; | ||
| 601 | $request .= $args{'source_url'}; | ||
| 602 | $request .= '/> </url> </source>'; | ||
| 603 | } elsif (exists $args{'source'}) { | ||
| 604 | $request .= '<'; | ||
| 605 | $request .= $args{'source'}; | ||
| 606 | $request .= '/> </source>'; | ||
| 607 | } | ||
| 608 | $request .= $args{'filter'}; | ||
| 609 | $request .= '</get-config> </rpc>'; | ||
| 610 | $self->send_and_recv_rpc($request); | ||
| 611 | } | ||
| 612 | |||
| 613 | # This will be removed after the <edit-config> accepts parameters in any order | ||
| 614 | sub edit_config | ||
| 615 | { | ||
| 616 | my ($self,%args) = @_; | ||
| 617 | my $msg_id = &get_msg_id; | ||
| 618 | my $request = '<rpc message-id=\'' . $msg_id; | ||
| 619 | if (exists $args{'operation'}) { | ||
| 620 | $request = 'operation=\'' . $args{'operation'}; | ||
| 621 | } | ||
| 622 | $request .= '\'>'; | ||
| 623 | $request .= '<edit-config> <target>'; | ||
| 624 | if (exists $args{'target'}) { | ||
| 625 | $request .= '<'; | ||
| 626 | $request .= $args{'target'}; | ||
| 627 | $request .= '/> </target>'; | ||
| 628 | } | ||
| 629 | |||
| 630 | if (exists $args{'default_operation'}) { | ||
| 631 | $request .= '<default-operation>'; | ||
| 632 | $request .= $args{'default_operation'}; | ||
| 633 | $request .= '</default-operation>'; | ||
| 634 | } | ||
| 635 | |||
| 636 | if (exists $args{'test_option'}) { | ||
| 637 | $request .= '<test-option>'; | ||
| 638 | $request .= $args{'test_option'}; | ||
| 639 | $request .= '</test-option>'; | ||
| 640 | } | ||
| 641 | |||
| 642 | if (exists $args{'error_option'}) { | ||
| 643 | $request .= '<error-option>'; | ||
| 644 | $request .= $args{'error_option'}; | ||
| 645 | $request .= '</error-option>'; | ||
| 646 | } | ||
| 647 | |||
| 648 | if (exists $args{'url'}) { | ||
| 649 | $request .= '<url>' . $args{'url'} . '</url>'; | ||
| 650 | } elsif (exists $args{'config-text'}) { | ||
| 651 | $request .= '<config-text>' . $args{'config-text'} . '</config-text>'; | ||
| 652 | } else { | ||
| 653 | $request .= '<config>' . $args{'config'} . '</config>'; | ||
| 654 | } | ||
| 655 | $request .= '</edit-config> </rpc>'; | ||
| 656 | $self->send_and_recv_rpc($request); | ||
| 657 | } | ||
| 658 | |||
| 659 | sub AUTOLOAD | ||
| 660 | { | ||
| 661 | my ($self, @args) = @_; | ||
| 662 | my $fn = substr($AUTOLOAD, rindex($AUTOLOAD, '::') + 2); | ||
| 663 | return if $AUTOLOAD =~ /::DESTROY$/; | ||
| 664 | $self->generate_rpc($fn, @args); | ||
| 665 | } | ||
| 666 | |||
| 667 | 1; | ||
| 668 | |||
| 669 | __END__ |