Brocade-BSC
view release on metacpan or search on metacpan
lib/Brocade/BSC.pm view on Meta::CPAN
#
# parameter hash | YAML label | default value
# -------------- | ----------- | -------------
# ipAddr | ctrlIpAddr | 127.0.0.1
# portNum | ctrlPortNum | 8181
# adminName | ctrlUname | admin
# adminPassword | ctrlPswd | admin
# timeout | timeout | 5
Returns new I<Brocade::BSC> object.
=cut
sub new {
my $caller = shift;
my %params = @_;
my $yamlcfg;
if ($params{cfgfile} && ( -e $params{cfgfile})) {
$yamlcfg = YAML::LoadFile($params{cfgfile});
}
my $self = {
ipAddr => '127.0.0.1',
portNum => '8181',
adminName => 'admin',
adminPassword => 'admin',
timeout => 5
};
if ($yamlcfg) {
$yamlcfg->{ctrlIpAddr}
&& ($self->{ipAddr} = $yamlcfg->{ctrlIpAddr});
$yamlcfg->{ctrlPortNum}
&& ($self->{portNum} = $yamlcfg->{ctrlPortNum});
$yamlcfg->{ctrlUname}
&& ($self->{adminName} = $yamlcfg->{ctrlUname});
$yamlcfg->{ctrlPswd}
&& ($self->{adminPassword} = $yamlcfg->{ctrlPswd});
$yamlcfg->{timeout}
&& ($self->{timeout} = $yamlcfg->{timeout});
}
map { $params{$_} && ($self->{$_} = $params{$_}) }
qw(ipAddr portNum adminName adminPassword timeout);
bless $self;
}
# Method ===============================================================
# _http_req : semi-private; send HTTP request to BSC Controller
# Parameters: $method (string, req) HTTP verb
# : $urlpath (string, req) path for REST request
# : $data (string, opt)
# : $headerref (hash ref, opt)
# Returns : HTTP::Response
#
sub _http_req {
my $self = shift;
my ($method, $urlpath, $data, $headerref) = @_;
my %headers = $headerref ? %$headerref : ();
my $url = "http://$$self{ipAddr}:$$self{portNum}$urlpath";
my $ua = LWP::UserAgent->new;
$ua->timeout($self->{timeout});
my $req = HTTP::Request->new($method => $url);
while (my($header, $value) = each %headers) {
$req->header($header => $value);
}
if ($data) {
$req->content($data);
}
$req->authorization_basic($$self{adminName}, $$self{adminPassword});
return $ua->request($req);
}
# Method ===============================================================
#
=item B<as_json>
# Returns pretty-printed JSON string representing BSC object.
=cut
sub as_json {
my $self = shift;
my $json = new JSON->canonical->allow_blessed->convert_blessed;
return $json->pretty->encode($self);
}
# Method ===============================================================
#
=item B<get_nodes_operational_list>
# Returns : BSC::Status
# : reference to an array of node names
=cut
sub get_nodes_operational_list {
my $self = shift;
my @nodeNames = ();
my $status = new Brocade::BSC::Status;
my $urlpath = "/restconf/operational/opendaylight-inventory:nodes";
my $resp = $self->_http_req('GET', $urlpath);
if ($resp->code == HTTP_OK) {
if ($resp->content =~ /\"nodes\"/) {
my $nodes = decode_json($resp->content)->{nodes}->{node};
if (! $nodes) {
$status->code($BSC_DATA_NOT_FOUND);
}
else {
foreach (@$nodes) {
push @nodeNames, $_->{id};
}
$status->code($BSC_OK);
}
}
else {
$status->code($BSC_DATA_NOT_FOUND);
}
}
else {
$status->http_err($resp);
}
return ($status, \@nodeNames);
( run in 1.183 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )