updated json port-test and cx-test scripts

This commit is contained in:
Jed Reynolds
2018-09-25 17:12:13 -07:00
parent 1565175012
commit ae611efc62
2 changed files with 296 additions and 132 deletions

View File

@@ -14,9 +14,10 @@ use HTTP::Request;
use LWP;
use LWP::UserAgent;
use Data::Dumper;
use Time::HiRes qw(usleep);
use JSON;
use lib '/home/lanforge/scripts';
use LANforge::JsonUtils qw(logg err json_request get_links_from get_thru json_post get_port_names);
use LANforge::JsonUtils qw(logg err json_request get_links_from get_thru json_post get_port_names flatten_list);
package main;
# Default values for ye ole cmd-line args.
@@ -51,39 +52,103 @@ $::HostUri = "http://$Host:$Port";
my $uri = "/shelf/1";
my $rh = json_request($uri);
my $ra_links = get_links_from($rh, 'resources');
my @links2= ();
my $ra_alias_links = [];
# TODO: make this a JsonUtils::list_ports()
for $uri (@$ra_links) {
$uri =~ s{/resource}{/port}g;
$uri .= "/list";
#logg("requesting $uri");
$rh = json_request($uri);
#print Dumper($rh);
push( @$ra_alias_links, @{get_port_names($rh, 'interfaces')});
push(@links2, @{get_links_from($rh, 'interfaces')});
#logg("\nfound: ");
#logg(@links2);
}
#print Dumper($ra_alias_links);
my @ports_up= ();
# TODO: make this a JsonUtils::list_ports()
$uri = "/port/1/3/list?fields=alias,device,down,phantom,port";
#logg("requesting $uri");
$rh = json_request($uri);
flatten_list($rh, 'interfaces');
for my $rh_p (keys %{$rh->{'flat_list'}}) {
if (!$rh->{'flat_list'}->{$rh_p}->{'down'}) {
push(@ports_up, $rh_p);
}
}
# find first station
my $rh_sta;
for my $rh_alias_link (@$ra_alias_links) {
#print Dumper($rh_alias_link);
if ($rh_alias_link->{'alias'} =~/^sta\d+/) {
$rh_sta=$rh_alias_link;
last;
for my $rh_up (@ports_up) {
my $eid = $rh->{'flat_list'}->{$rh_up}->{'port'};
my @hunks = split(/[.]/, $eid);
if ($hunks[1]) {
$rh_sta = $rh_up;
}
}
if (!defined $rh_sta) {
die("Unable to find a virtual station. Is one up?");
}
# delete old CXes and old endpoints
# TODO: collect_cx_names
my $rh_cxlist = json_request("/cx/list");
my @cx_names = ();
for my $cx_name (sort keys %$rh_cxlist) {
#print " cx_name[$cx_name]";
next if (ref $rh_cxlist->{$cx_name} ne "HASH");
next if (!defined $rh_cxlist->{$cx_name}->{"name"});
next if ($rh_cxlist->{$cx_name}->{"name"} eq "uri");
push(@cx_names, $rh_cxlist->{$cx_name}->{"name"});
}
print "\nRemoving cx: ";
$uri = "/cli-json/rm_cx";
for my $cx_name (sort @cx_names) {
print "$cx_name ";
$rh = {
"test_mgr" => "all",
"cx_name" => $cx_name
};
json_post($uri, $rh);
}
sleep 1;
my $rh_endplist = json_request("/endp/list");
print "\nRemoving endpoints: ";
flatten_list($rh_endplist, 'endpoint');
#print Dumper($rh_endplist->{'flat_list'});
#sleep 10;
my @endp_names = ();
for my $ep_name (keys %{$rh_endplist->{'flat_list'}}) {
next if (!defined $ep_name);
next if ($ep_name eq "");
next if ((ref $ep_name) eq "ARRAY");
next if (!defined $rh_endplist->{'flat_list'}->{$ep_name}->{"name"});
next if ($rh_endplist->{'flat_list'}->{$ep_name}->{"name"} eq "");
#print "epn:".Dumper($rh_endplist->{'flat_list'}->{$ep_name}->{"name"});
push(@endp_names, $ep_name);
}
$uri = "/cli-json/rm_endp";
for my $ep_name (@endp_names) {
if (!defined $ep_name || $ep_name =~/^\s*$/ || (ref $ep_name) eq "ARRAY") {
print " skipping ep_name[$ep_name]";
print Dumper(\$ep_name);
next;
}
print "[$ep_name] ";
#usleep(500000);
$rh = { "endp_name" => $ep_name };
json_post($uri, $rh);
}
print "\nRefreshing...";
my $h = {"endpoint"=>"all"};
json_request("/cli-json/nc_show_endpoints", $h);
sleep 1;
$h = {"test_mgr"=>"all", "cross_connect"=>"all"};
json_request("/cli-json/show_cxe", $h);
# assume resource 1, eth1 is present, and create an endpoint to it
# -A and -B are expected convention for endpoint names
# create 10 endpoints
my $rh_ports = json_request("/port/1/3/list");
flatten_list($rh_ports, 'interfaces');
my $rh_endp_A = {
'alias' => 'udp_ex-A',
'alias' => 'udp_json',
'shelf' => 1,
'resource' => 1,
'port' => 'b1000', # or eth1
@@ -96,15 +161,12 @@ my $rh_endp_A = {
'payload_pattern' => 'increasing',
'multi_conn' => 0
};
json_post("/cli-json/add_endp", $rh_endp_A);
my $resourceB_uri = $rh_sta->{'uri'};
my ($resourceB) = $resourceB_uri =~ m{/port/1/(\d+)/};
my $rh_endp_B = {
'alias' => 'udp_ex-B',
'alias' => 'udp_json',
'shelf' => 1,
'resource' => $resourceB,
'port' => $rh_sta->{'alias'},
'resource' => 3,
'port' => 'unset',
'type' => 'lf_udp',
'ip_port' => -1,
'is_rate_bursty' => 'NO',
@@ -114,41 +176,116 @@ my $rh_endp_B = {
'payload_pattern' => 'increasing',
'multi_conn' => 0
};
json_post("/cli-json/add_endp", $rh_endp_B);
my $rh_cx = {
"alias" => 'udp_json',
"test_mgr" => 'default_tm',
'tx_endp' => '',
'rx_endp' => ''
};
sleep(2);
my $rh_cx ={
'alias' => 'udp_ex',
'test_mgr' => 'default_tm',
'tx_endp' => 'udp_ex-A',
'rx_endp' => 'udp_ex-B'
};
json_post("/cli-json/add_cx", $rh_cx);
$rh_cx = {
'test_mgr' => 'default_tm',
'cx_name' => 'udp_ex',
'milliseconds'=> 1000,
};
json_post("/cli-json/set_cx_report_timer", $rh_cx);
$h = {"endpoint"=>"all"};
json_request("/cli-json/nc_show_endpoints", $h);
$h = {"test_mgr"=>"all", "cross_connect"=>"all"};
json_request("/cli-json/show_cxe", $h);
sleep 1;
print "\nConstructing new Endpoints: ";
my $num_ports = scalar keys(%{$rh_ports->{'flat_list'}});
my $num_cx = 0;
for my $rh_p (values %{$rh_ports->{'flat_list'}}) {
last if ($num_cx >= ($num_ports-1));
next if ($rh_p->{'alias'} !~ /^v*sta/);
my $end_a_alias = "udp_json_$num_cx-A";
my $end_b_alias = "udp_json_$num_cx-B";
my $port_b = $rh_p->{'alias'};
print "$port_b ";
$rh_endp_B->{'port'} = $port_b;
$rh_endp_B->{'alias'} = $end_b_alias;
$rh_endp_A->{'alias'} = $end_a_alias;
$num_cx++;
json_post("/cli-json/add_endp", $rh_endp_A);
json_post("/cli-json/add_endp", $rh_endp_B);
}
print "\nRefreshing...";
$h = {"endpoint"=>"all"};
json_request("/cli-json/nc_show_endpoints", $h);
sleep 1;
print "\nConstructing new CX: ";
$num_cx = 0;
for my $rh_p (values %{$rh_ports->{'flat_list'}}) {
last if ($num_cx >= ($num_ports-1));
next if ($rh_p->{'alias'} !~ /^v*sta/);
my $end_a_alias = "udp_json_${num_cx}-A";
my $end_b_alias = "udp_json_${num_cx}-B";
my $port_b = $rh_p->{'alias'};
my $cx_alias = "udp_json_".$num_cx;
$rh_cx->{'alias'} = $cx_alias;
$rh_cx->{'tx_endp'} = $end_a_alias;
$rh_cx->{'rx_endp'} = $end_b_alias;
json_post("/cli-json/add_cx", $rh_cx);
print " $cx_alias";
$num_cx++;
}
print "\nRefreshing...";
$h = {"endpoint"=>"all"};
json_request("/cli-json/nc_show_endpoints", $h);
$rh_cxlist = json_request("/cx/list");
@cx_names = ();
for my $cx_name (sort keys %$rh_cxlist) {
next if (ref $rh_cxlist->{$cx_name} ne "HASH");
next if (!defined $rh_cxlist->{$cx_name}->{"name"});
push(@cx_names, $rh_cxlist->{$cx_name}->{"name"});
}
for my $cx_alias (sort @cx_names) {
my $rh_cx_t = {
'test_mgr' => 'default_tm',
'cx_name' => $cx_alias,
'milliseconds'=> 1000,
};
json_post("/cli-json/set_cx_report_timer", $rh_cx_t);
}
print "\nRefreshing...";
$h = {"endpoint"=>"all"};
json_request("/cli-json/nc_show_endpoints", $h);
sleep 1;
$h = {"test_mgr"=>"all", "cross_connect"=>"all"};
json_request("/cli-json/show_cxe", $h);
my $set_state = {
'test_mgr' => 'default_tm',
'cx_name' => 'udp_ex',
'cx_state' => 'RUNNING'
};
json_post("/cli-json/set_cx_state", $set_state);
my $rh_result;
for(my $i = 0; $i < 60; $i++) {
$rh_result = json_request("/cx/udp_ex?fields=eid,state");
print Dumper($rh_result);
sleep(1);
@cx_names = ();
$rh_cxlist = json_request("/cx/list");
for my $cx_name (sort keys %$rh_cxlist) {
next if (ref $rh_cxlist->{$cx_name} ne "HASH");
next if (!defined $rh_cxlist->{$cx_name}->{"name"});
push(@cx_names, $rh_cxlist->{$cx_name}->{"name"});
}
print "\nStarting: ";
for my $cxname (@cx_names) {
print " $cxname";
$set_state->{'cx_name'} = $cxname;
json_post("/cli-json/set_cx_state", $set_state);
}
sleep 10;
$set_state = {
'test_mgr' => 'default_tm',
'cx_name' => 'udp_ex',
'cx_state' => 'STOPPED'
};
json_post("/cli-json/set_cx_state", $set_state);
print "\nStopping: ";
for my $cxname (@cx_names) {
$set_state->{'cx_name'} = $cxname;
print " $cxname";
json_post("/cli-json/set_cx_state", $set_state);
}
print "...done\n";
#

View File

@@ -17,7 +17,7 @@ use LWP::UserAgent;
use Data::Dumper;
use JSON;
use lib '/home/lanforge/scripts';
use LANforge::JsonUtils qw(logg err json_request get_links_from get_thru json_post get_port_names);
use LANforge::JsonUtils qw(logg err json_request get_links_from get_thru json_post get_port_names flatten_list);
package main;
# Default values for ye ole cmd-line args.
@@ -51,35 +51,41 @@ my $rh = json_request($uri);
my $ra_links = get_links_from($rh, 'resources');
my @links2= ();
my $ra_alias_links = [];
# TODO: make this a JsonUtils::list_ports()
for $uri (@$ra_links) {
$uri =~ s{/resource}{/port}g;
$uri .= "/list";
#logg("requesting $uri");
$uri .= "/list?fields=_links,device,alias,port";
print "$uri\n";
$rh = json_request($uri);
#print Dumper($rh);
push( @$ra_alias_links, @{get_port_names($rh, 'interfaces')});
push(@links2, @{get_links_from($rh, 'interfaces')});
#logg("\nfound: ");
#logg(@links2);
if (defined $rh->{'interfaces'}) {
flatten_list($rh, 'interfaces');
#push(@$ra_alias_links, keys(%{$rh->{'flat_list'}}));
push( @$ra_alias_links, get_port_names($rh, 'interfaces'));
#push(@links2, keys(%{$rh->{'flat_list'}}));
push(@links2, @{get_links_from($rh, 'interfaces')});
}
}
#print Dumper($ra_alias_links);
# destroy stations on resource 3, 7, 8
# destroy stations on resource 3
my @radios = ();
my @destroy_me = ();
for my $rh_alias_link (@$ra_alias_links) {
push(@destroy_me, $rh_alias_link)
if (($rh_alias_link->{'uri'} =~m{^/port/1/[3]/})
&& ($rh_alias_link->{'alias'} =~m{^v*sta}));
push(@radios, $rh_alias_link)
if (($rh_alias_link->{'uri'} =~m{^/port/1/[3]/})
&& ($rh_alias_link->{'alias'} =~m{^wiphy}));
for my $rh_link (@$rh_alias_link) {
if ( ($rh_link->{'uri'} =~m{^/port/1/[3]/})
&& ($rh_link->{'device'} =~m{^sta})) {
push(@destroy_me, $rh_link);
}
push(@radios, $rh_link)
if (($rh_link->{'uri'} =~m{^/port/1/[3]/})
&& ($rh_link->{'device'} =~m{^wiphy}));
}
}
logg("\nDestroying these: ");
#print Dumper(@destroy_me);
for my $rh_target (@destroy_me) {
my $alias = $rh_target->{'alias'};
my $alias = $rh_target->{'device'};
my @hunks = split(/[\/]/, $rh_target->{'uri'});
# TODO: create JsonUtils::rm_vlan($eid, $alias)
@@ -87,22 +93,31 @@ for my $rh_target (@destroy_me) {
my $rh_data = {
'shelf'=>1,
'resource'=>$hunks[3],
# 'port'=>'z'.$alias, # use this to force pre_exec check
'port'=>$alias,
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
#'suppress_postexec_cli'=>'true',
#'suppress_postexec_method'=>'true'
'port'=>$alias
};
logg(" $alias");
my $rh_response = json_post("/cli-json/rm_vlan", $rh_data);
usleep (25000);
usleep (15000);
}
my $rh_update = {
'shelf'=>1, 'resource'=>'all', 'port'=>'all', 'flags'=>'0x1'
'shelf'=>1, 'resource'=>3, 'port'=>'all', 'flags'=>'0x1'
};
logg(" updating ");
logg("\nRefreshing: ");
my $rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
my $remaining = 1;
while ($remaining > 0) {
$rh = json_request("/port/1/3/list");
flatten_list($rh, 'interfaces');
$remaining = 0;
for my $name (keys %{$rh->{'flat_list'}}) {
$remaining ++
if ($name =~ /^v*sta/);
}
print "Remaining stations: $remaining, ";
sleep 1;
}
# this really should poll for ports to wait for them to disappear
sleep 3;
@@ -117,16 +132,19 @@ my $range;
my $num_sta = 160;
my $radio_num;
my $radio_counter = 0;
$rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
# add_sta + ht20 -ht40 -ht80 -create_admin_down
# flags=142609408&mode=8
print "\nAdding stations...\n";
for $rh_radio (@radios) {
$radio_name = $rh_radio->{'alias'};
my @hunks = split(/[\/]/, $rh_radio->{'uri'});
($radio_num) = $radio_name =~ /wiphy(\d+)/;
$resource = $hunks[3];
$range = ($resource * 1000) + ($radio_num * 100);
logg("\n/cli-json/add_sta = ");
logg("\n/cli-json/add_sta on 1.$resource.$radio_name\n");
for (my $i = $range; $i < ($range+$num_sta); $i++) {
# TODO: create JsonUtils::add_sta($eid, $alias...)
my $rh_data = {
@@ -135,29 +153,26 @@ for $rh_radio (@radios) {
#'radio'=>'x'.$radio_name, # use to prompt radio not found error
'radio'=>$radio_name,
'sta_name'=>'sta'.$radio_counter,
#'alias'=>'vsta'.$i, # deprecated, use set_port_alias
#'alias'=>'vsta'.$i, # deprecated, use set_port + interest.set_alias
#'flags'=>68862086144, # has port-down set
'flags'=>142609408,
'ssid'=>'idtest-1200-wpa2',
'key'=>'idtest-1200-wpa2',
'mac'=>'xx:xx:xx:xx:*:xx',
'mode'=>0,
'rate'=>'DEFAULT',
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
'suppress_postexec_cli'=>'true',
'suppress_postexec_method'=>'true'
'rate'=>'DEFAULT'
};
#print Dumper($rh_data);
logg("1/$resource/$radio_name -> sta$radio_counter");
logg(" sta$radio_counter");
my $rh_response = json_post("/cli-json/add_sta", $rh_data);
usleep(25000);
usleep(15000);
$radio_counter +=1;
}
$rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
sleep 1;
}
logg(" updating ");
logg("\nUpdating aliases ");
$rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
sleep 2;
sleep 3;
$radio_counter = 0;
for $rh_radio (@radios) {
$radio_name = $rh_radio->{'alias'};
@@ -166,54 +181,66 @@ for $rh_radio (@radios) {
$resource = $hunks[3];
$range = ($resource * 10000) + ($radio_num * 1000);
# set_port - port up, enable dhcp
# current_flags=2147483648&interest=16386
for (my $i = $range; $i < ($range+$num_sta); $i++) {
print "sta$radio_counter = vsta$i [ $range .. ".($range+$num_sta)."] 1/$resource/$radio_num $radio_name \n";
print "sta$radio_counter:vsta$i ";
#my $eidname = "1.$resource.sta$radio_counter";
# set port up + dhcp + alias
my $rh_data = {
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
'suppress_postexec_cli'=>'true',
'suppress_postexec_method'=>'true',
'shelf'=>1,
'resource'=>$resource,
'port'=>'sta'.$radio_counter,
'current_flags'=>2147483648,
'interest'=>20480,
'alias'=>'vsta'.$i
};
$rh_response = json_post("/cli-json/set_port", $rh_data);
usleep(10000);
# set port up + dhcp
$rh_data = {
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
'suppress_postexec_cli'=>'true',
'suppress_postexec_method'=>'true',
'shelf'=>1,
'resource'=>$resource,
'port'=>'sta'.$radio_counter,
'cmd_flags'=>0,
'current_flags'=>2147483648,
#'suppress_postexec'=>'true',
'interest'=>16386
};
# TODO: create JsonUtils::set_dhcp($eid, $alias, $on_off)
my $rh_response = json_post("/cli-json/set_port", $rh_data);
$radio_counter+=1;
usleep(10000);
}
}
logg(" updating ");
$radio_counter = 0;
for $rh_radio (@radios) {
$radio_name = $rh_radio->{'alias'};
my @hunks = split(/[\/]/, $rh_radio->{'uri'});
($radio_num) = $radio_name =~ /wiphy(\d+)/;
$resource = $hunks[3];
$range = ($resource * 10000) + ($radio_num * 1000);
for (my $i = $range; $i < ($range+$num_sta); $i++) {
print "sta$radio_counter:vsta$i ";
#my $eidname = "1.$resource.sta$radio_counter";
# set port up + dhcp + alias
my $rh_data = {
'shelf'=>1,
'resource'=>$resource,
'port'=>'sta'.$radio_counter,
'current_flags'=>0,
'interest'=>8388608,
};
my $rh_response = json_post("/cli-json/set_port", $rh_data);
$radio_counter+=1;
usleep(10000);
}
}
logg("\nRefreshing after setting up... ");
$rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
sleep 2;
for $uri (@$ra_links) {
$uri =~ s{/resource}{/port}g;
$uri .= "/list"
if ($uri !~ m{/list$});
$rh = json_request($uri);
push( @$ra_alias_links, @{get_port_names($rh, 'interfaces')});
push(@links2, @{get_links_from($rh, 'interfaces')});
sleep 1;
# wait on ports up
my $ports_still_down = 1;
while ($ports_still_down > 0) {
$rh = json_request("/port/1/3/list?fields=_links,port,device,down");
flatten_list($rh, 'interfaces');
$ports_still_down=0;
for my $rh_p (values %{$rh->{'flat_list'}}) {
next unless $rh_p->{'device'} =~ /^sta/;
#print "$rh_p->{'device'} is $rh_p->{'down'} ";
$ports_still_down++
if ($rh_p->{'down'});
}
print "ports down: $ports_still_down ";
$rh_response = json_post("/cli-json/nc_show_ports", $rh_update);
sleep 1;
}
# ports down
@@ -228,8 +255,8 @@ for my $port_uri (@links2) {
my @hunks = split(/\./, $port);
my $resource = $hunks[1];
my %post = (
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
#'suppress_preexec_cli'=>'false',
#'suppress_preexec_method'=>'false',
#'suppress_postexec_cli'=>'false',
#'suppress_postexec_method'=>'false',
"shelf" => 1,
@@ -256,10 +283,10 @@ for my $port_uri (@links2) {
my $resource = $hunks[1];
# 'shelf=1&resource=2&port=vap2000&cmd_flags=0&current_flags=0&interest=8388610'
my %post = (
'suppress_preexec_cli'=>'false',
'suppress_preexec_method'=>'false',
'suppress_postexec_cli'=>'false',
'suppress_postexec_method'=>'false',
#'suppress_preexec_cli'=>'false',
#'suppress_preexec_method'=>'false',
#'suppress_postexec_cli'=>'false',
#'suppress_postexec_method'=>'false',
"shelf" => 1,
"resource" => 0+$resource,
"port" => $device,