TT#36007 add /api/sipcaptures

* enables fetching captured sip data
      GET all collection returns JSON with available SIP messages
      GET item collection requires a valid call-id and
      returns pcap data generated from the packets of the call-id

Change-Id: I552ee9a312a4b9acf95bde93f6c584bbf82f9ea9
changes/03/21503/8
Kirill Solomko 7 years ago
parent 8f28e18ba3
commit 6dc1e7c5b3

@ -0,0 +1,142 @@
package NGCP::Panel::Controller::API::SIPCaptures;
use NGCP::Panel::Utils::Generic qw(:all);
use Sipwise::Base;
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use DateTime::TimeZone;
use NGCP::Panel::Utils::DateTime;
use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::SIPCaptures/;
__PACKAGE__->set_config({
allowed_roles => [qw/admin reseller subscriberadmin subscriber/],
});
sub allowed_methods{
return [qw/GET OPTIONS/];
}
sub api_description {
return 'Defines SIP packet captures for a call. A GET on item with call-id as the parameter returns pcap data as application/vnd.tcpdump.pcap.';
};
sub query_params {
return [
{
param => 'call_id',
description => 'Filter for a particular call_id',
query_type => 'string_eq',
},
{
param => 'start_ge',
description => 'Filter for data starting greater or equal the specified time stamp.',
query => {
first => sub {
my $q = shift;
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
{ 'me.timestamp' => { '>=' => $dt->epoch } };
},
second => sub {},
},
},
{
param => 'start_le',
description => 'Filter for data starting lower or equal the specified time stamp.',
query => {
first => sub {
my $q = shift;
$q .= ' 23:59:59' if($q =~ /^\d{4}\-\d{2}\-\d{2}$/);
my $dt = NGCP::Panel::Utils::DateTime::from_string($q);
{ 'me.timestamp' => { '<=' => $dt->epoch } };
},
second => sub {},
},
},
{
param => 'method',
description => 'Filter for a particular SIP method',
query_type => 'string_eq',
},
{
param => 'subscriber_id',
description => 'End time of the captured SIP data',
new_rs => sub {
my ($c, $q, $rs) = @_;
if ($c->user->roles ne "subscriber") {
my $sub = $c->model('DB')->resultset('voip_subscribers')->find($q);
if ($sub) {
$rs = $rs->search_rs({
-or => [
'me.caller_uuid' => $sub->uuid,
'me.callee_uuid' => $sub->uuid
],
});
}
}
return $rs;
}
},
{
# we handle that separately/manually in the role
param => 'tz',
description => 'Format start_time according to the optional time zone provided here, e.g. Europe/Berlin.',
},
];
}
sub GET :Allow {
my ($self, $c) = @_;
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
{
if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone");
return;
}
my $items = $self->item_rs($c);
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->all) {
push @embedded, $self->hal_from_item($c, $item, $form);
push @links, Data::HAL::Link->new(
relation => 'ngcp:'.$self->resource_name,
href => sprintf('/%s%s', $c->request->path, $item->call_id),
);
}
push @links,
Data::HAL::Link->new(
relation => 'curies',
href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}',
name => 'ngcp',
templated => true,
),
Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'),
$self->collection_nav_links($c, $page, $rows, $total_count, $c->request->path, $c->request->query_params);
my $hal = Data::HAL->new(
embedded => [@embedded],
links => [@links],
);
$hal->resource({
total_count => $total_count,
});
my $response = HTTP::Response->new(HTTP_OK, undef,
HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json);
$c->response->headers($response->headers);
$c->response->body($response->content);
return;
}
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,56 @@
package NGCP::Panel::Controller::API::SIPCapturesItem;
use NGCP::Panel::Utils::Generic qw(:all);
use Sipwise::Base;
use HTTP::Headers qw();
use HTTP::Status qw(:constants);
use File::Basename;
use File::Type;
use DateTime;
use DateTime::TimeZone;
use NGCP::Panel::Utils::Callflow;
use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::SIPCaptures/;
__PACKAGE__->set_config({
allowed_roles => [qw/admin reseller subscriberadmin subscriber/],
log_response => 0,
});
sub allowed_methods {
return [qw/GET OPTIONS HEAD/];
}
sub GET :Allow {
my ($self, $c, $id) = @_;
{
if($c->req->param('tz') && !DateTime::TimeZone->is_valid_name($c->req->param('tz'))) {
$self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Query parameter 'tz' value is not a valid time zone");
return;
}
my $packets = $self->packets_by_callid($c, $id);
unless ($packets) {
$self->error($c, HTTP_NOT_FOUND, "Non-existing call id");
last;
}
my $pcap = NGCP::Panel::Utils::Callflow::generate_pcap($packets);
last unless $pcap;
my $dt = DateTime->now();
my $file_dt = sprintf "%s_%s%s%s",
$dt->ymd, $dt->hour, $dt->minute, $dt->second;
my $filename = sprintf "%s_-%s.pcap", $file_dt, $id;
$c->response->header("Content-Disposition" => "attachment; filename=$filename");
$c->response->content_type('application/vnd.tcpdump.pcap');
$c->response->body($pcap);
}
return;
}
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,151 @@
package NGCP::Panel::Form::SIPCaptures;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use HTML::FormHandler::Widget::Block::Bootstrap;
has_field 'timestamp' => (
type => 'Text',
label => 'Timestamp',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Timestamp of the sip packet'],
},
);
has_field 'protocol' => (
type => 'Text',
label => 'Protocol',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Protocol of the sip packet'],
},
);
has_field 'transport' => (
type => 'Text',
label => 'Transport',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['IP transport of the sip packet (TCP/UDP)'],
},
);
has_field 'src_ip' => (
type => 'Text',
label => 'Source IP',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Source IP of the sip packet'],
},
);
has_field 'src_port' => (
type => 'Text',
label => 'PosInteger',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Source port of the sip packet'],
},
);
has_field 'dst_ip' => (
type => 'Text',
label => 'Destination IP',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Destination IP of the sip packet'],
},
);
has_field 'dst_port' => (
type => 'PosInteger',
label => 'Destination Port',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Destination port of the sip packet'],
},
);
has_field 'method' => (
type => 'Text',
label => 'Method',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Method of the sip packet'],
},
);
has_field 'cseq_method' => (
type => 'Text',
label => 'CSEQ Method',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['CSEQ Method of the sip packet'],
},
);
has_field 'call_id' => (
type => 'Text',
label => 'Call ID',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Call id of the sip packet'],
},
);
has_field 'from_uri' => (
type => 'Text',
label => 'From URI',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['From URI of the sip packet'],
},
);
has_field 'request_uri' => (
type => 'Text',
label => 'Request URI',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['Request URI of the sip packet'],
},
);
1;
__END__
=head1 NAME
NGCP::Panel::Form::SIPCaptures
=head1 DESCRIPTION
A helper to manipulate the sip capture forms
=head1 AUTHOR
Sipwise Development Team
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# vim: set tabstop=4 expandtab:

@ -0,0 +1,110 @@
package NGCP::Panel::Role::API::SIPCaptures;
use NGCP::Panel::Utils::Generic qw(:all);
use Sipwise::Base;
use parent 'NGCP::Panel::Role::API';
use boolean qw(true);
use Data::HAL qw();
use Data::HAL::Link qw();
use HTTP::Status qw(:constants);
sub resource_name {
return 'sipcaptures';
}
sub _item_rs {
my ($self, $c) = @_;
my $item_rs = $c->model('Storage')->resultset('messages');
if ($c->user->roles eq "admin") {
} elsif ($c->user->roles eq "reseller" ||
$c->user->roles eq "subsriberadmin") {
#TODO: possibly store reseller_id inside sipstats.messages
# as such logic becomes quite expensive on large amount of subscribers
# per reseller
my $sub_rs;
if ($c->user->roles eq "reseller") {
$sub_rs = $c->model('DB')->resultset('voip_subscribers')->search({
'contact.reseller_id' => $c->user->reseller_id
},{
join => { contract => 'contact' }
});
} else {
$sub_rs = $c->model('DB')->resultset('voip_subscribers')->search({
'contract.id' => $c->user->account_id,
},{
join => 'contract'
});
}
my @uuids = map { $_->uuid } $sub_rs->all;
$item_rs = $item_rs->search({
-or => [
'me.caller_uuid' => { -in => \@uuids },
'me.callee_uuid' => { -in => \@uuids },
],
});
} elsif ($c->user->roles eq "subscriber") {
$item_rs = $item_rs->search_rs({
-or => [
'me.caller_uuid' => $c->user->uuid,
'me.callee_uuid' => $c->user->uuid,
],
});
}
return $item_rs;
}
sub packets_by_callid {
my ($self, $c, $id) = @_;
my $item_rs = $c->model('Storage')->resultset('packets')->search({
'message.call_id' => $id
},{
join => { message_packets => 'message' }
});
return $item_rs->first ? [$item_rs->all] : undef;
}
sub get_form {
my ($self, $c) = @_;
return NGCP::Panel::Form::get("NGCP::Panel::Form::SIPCaptures", $c);
}
#
sub resource_from_item {
my ($self, $c, $item, $form) = @_;
my %resource = $item->get_inflated_columns;
my $datetime_fmt = DateTime::Format::Strptime->new(
pattern => '%F %T',
);
my $tz = $c->req->param('tz');
unless($tz && DateTime::TimeZone->is_valid_name($tz)) {
$tz = undef;
}
if($item->timestamp) {
if($tz) {
$item->timestamp->set_time_zone($tz);
}
$resource{timestamp} = $datetime_fmt->format_datetime($item->timestamp);
if ($item->timestamp->millisecond > 0.0) {
$resource{timestamp} .= '.'.$item->timestamp->millisecond;
}
}
$resource{request_uri} = $item->request_uri;
return \%resource;
}
sub get_item_id {
my ($self, $c, $item, $resource, $form) = @_;
return $item->call_id;
}
1;
# vim: set tabstop=4 expandtab:

@ -129,6 +129,7 @@ $ua = Test::Collection->new()->ua();
rtcapps => 1,
rtcnetworks => 1,
rtcsessions => 1,
sipcaptures => 1,
sms => 1,
soundfilerecordings => 1,
soundfiles => 1,

Loading…
Cancel
Save