From 430e59055cd6b4213829fc2dc28feaa222b1e698 Mon Sep 17 00:00:00 2001 From: Flaviu Mates Date: Thu, 1 Aug 2019 15:45:00 +0300 Subject: [PATCH] TT#64011 - Implement NCOS LNP Carrier patterns support in UI/API * Create page for Lnp Carrier Patterns in Panel UI * Add create/edit/delete functionalities for Lnp Carrier Patterns * Create API enpoints for Lnp Carrier Patterns Change-Id: I7e98b330128bf5423382a109fd316e5331f985bd --- .../Panel/Controller/API/NcosLnpPatterns.pm | 131 ++++++++++++++ .../Controller/API/NcosLnpPatternsItem.pm | 137 +++++++++++++++ lib/NGCP/Panel/Controller/NCOS.pm | 160 ++++++++++++++++++ lib/NGCP/Panel/Form/NCOS/LnpPatternAPI.pm | 23 +++ lib/NGCP/Panel/Role/API/NcosLnpPatterns.pm | 109 ++++++++++++ share/templates/ncos/lnp_pattern_list.tt | 29 ++++ share/templates/ncos/pattern_list.tt | 1 + 7 files changed, 590 insertions(+) create mode 100644 lib/NGCP/Panel/Controller/API/NcosLnpPatterns.pm create mode 100644 lib/NGCP/Panel/Controller/API/NcosLnpPatternsItem.pm create mode 100644 lib/NGCP/Panel/Form/NCOS/LnpPatternAPI.pm create mode 100644 lib/NGCP/Panel/Role/API/NcosLnpPatterns.pm create mode 100644 share/templates/ncos/lnp_pattern_list.tt diff --git a/lib/NGCP/Panel/Controller/API/NcosLnpPatterns.pm b/lib/NGCP/Panel/Controller/API/NcosLnpPatterns.pm new file mode 100644 index 0000000000..5f727a8b3e --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/NcosLnpPatterns.pm @@ -0,0 +1,131 @@ +package NGCP::Panel::Controller::API::NcosLnpPatterns; +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); + + +sub allowed_methods{ + return [qw/GET POST OPTIONS HEAD/]; +} + +sub api_description { + return 'NCOS Lnp Patterns define rules within Lnp Lists.'; +}; + +sub query_params { + return [ + { + param => 'ncos_lnp_list_id', + description => 'Filter for Lnp patterns belonging to a specific NCOS Lnp List.', + query => { + first => sub { + my $q = shift; + { ncos_lnp_list_id => $q }; + }, + second => sub {}, + }, + }, + ]; +} + +use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::NcosLnpPatterns/; + +sub resource_name{ + return 'ncoslnppatterns'; +} + +sub dispatch_path{ + return '/api/ncoslnppatterns/'; +} + +sub relation{ + return 'http://purl.org/sipwise/ngcp-api/#rel-ncoslnppatterns'; +} + +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller/], +}); + +sub GET :Allow { + my ($self, $c) = @_; + my $page = $c->request->params->{page} // 1; + my $rows = $c->request->params->{rows} // 10; + { + my $items = $self->item_rs($c); + (my $total_count, $items, my $items_rows) = $self->paginate_order_collection($c, $items); + my (@embedded, @links); + my $form = $self->get_form($c); + for my $item (@$items_rows) { + push @embedded, $self->hal_from_item($c, $item, $form); + push @links, Data::HAL::Link->new( + relation => 'ngcp:'.$self->resource_name, + href => sprintf('/%s%d', $c->request->path, $item->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/'), + Data::HAL::Link->new(relation => 'self', href => sprintf('/%s?page=%s&rows=%s', $c->request->path, $page, $rows)); + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows)); + } + + 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; +} + +sub create_item { + my ($self, $c, $resource, $form, $process_extras) = @_; + + my $item; + my $schema = $c->model('DB'); + try { + my $lnp_list_rs = $schema->resultset('ncos_lnp_list')->search({ + id => $resource->{ncos_lnp_list_id}, + }); + my $lnp_list = $lnp_list_rs->first; + unless($lnp_list) { + $c->log->error("invalid ncos_lnp_list_id '$$resource{ncos_lnp_list_id}'"); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid ncos_lnp_list_id, lnp list does not exist"); + return; + } + + $item = $lnp_list->ncos_lnp_pattern_lists->create($resource); + } catch($e) { + $c->log->error("failed to create a ncos lnp carrier pattern: $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create a ncos lnp carrier pattern."); + return; + } + + return $item; +} + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/NcosLnpPatternsItem.pm b/lib/NGCP/Panel/Controller/API/NcosLnpPatternsItem.pm new file mode 100644 index 0000000000..d4887ce6db --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/NcosLnpPatternsItem.pm @@ -0,0 +1,137 @@ +package NGCP::Panel::Controller::API::NcosLnpPatternsItem; +use NGCP::Panel::Utils::Generic qw(:all); + +use Sipwise::Base; + +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); + +use NGCP::Panel::Utils::ValidateJSON qw(); +require Catalyst::ActionRole::ACL; +require NGCP::Panel::Role::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +sub allowed_methods{ + return [qw/GET OPTIONS HEAD PATCH PUT DELETE/]; +} + +use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::NcosLnpPatterns/; + +sub resource_name{ + return 'ncoslnppatterns'; +} + +sub dispatch_path{ + return '/api/ncoslnppatterns/'; +} + +sub relation{ + return 'http://purl.org/sipwise/ngcp-api/#rel-ncoslnppatterns'; +} + +__PACKAGE__->set_config({ + allowed_roles => [qw/admin reseller/], +}); + +sub GET :Allow { + my ($self, $c, $id) = @_; + { + last unless $self->valid_id($c, $id); + my $item = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, ncoslnppattern => $item); + + my $hal = $self->hal_from_item($c, $item); + + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + (map { # XXX Data::HAL must be able to generate links with multiple relations + s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|r =~ + s/rel=self/rel="item self"/r; + } $hal->http_headers), + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub PATCH :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $json = $self->get_valid_patch_data( + c => $c, + id => $id, + media_type => 'application/json-patch+json', + ); + last unless $json; + + my $item = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, ncoslnppattern => $item); + my $old_resource = { $item->get_inflated_columns }; + my $resource = $self->apply_patch($c, $old_resource, $json); + last unless $resource; + + my $form = $self->get_form($c); + $item = $self->update_item($c, $item, $old_resource, $resource, $form); + last unless $item; + + $guard->commit; + + $self->return_representation($c, 'item' => $item, 'form' => $form, 'preference' => $preference ); + } + return; +} + +sub PUT :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $item = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, ncoslnppattern => $item); + my $resource = $self->get_valid_put_data( + c => $c, + id => $id, + media_type => 'application/json', + ); + last unless $resource; + my $old_resource = { $item->get_inflated_columns }; + + my $form = $self->get_form($c); + $item = $self->update_item($c, $item, $old_resource, $resource, $form); + last unless $item; + + $guard->commit; + + $self->return_representation($c, 'item' => $item, 'form' => $form, 'preference' => $preference ); + } + return; +} + +sub DELETE :Allow { + my ($self, $c, $id) = @_; + + my $guard = $c->model('DB')->txn_scope_guard; + { + my $item = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, ncoslnppattern => $item); + + $item->delete; + + $guard->commit; + + $c->response->status(HTTP_NO_CONTENT); + $c->response->body(q()); + } + return; +} + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/NCOS.pm b/lib/NGCP/Panel/Controller/NCOS.pm index 72abdea1b0..515473040b 100644 --- a/lib/NGCP/Panel/Controller/NCOS.pm +++ b/lib/NGCP/Panel/Controller/NCOS.pm @@ -579,7 +579,167 @@ sub lnp_create :Chained('pattern_list') :PathPart('lnp/create') :Args(0) :Allowe ); } +sub lnp_pattern_list :Chained('lnp_base') :PathPart('lnp_patterns') :CaptureArgs(0) { + my ( $self, $c ) = @_; + + my $lnp_id = $c->stash->{lnp_result}->id; + my $pattern_rs = $c->model('DB')->resultset('ncos_lnp_pattern_list')->search({ncos_lnp_list_id => $lnp_id}); + $c->stash(lnp_pattern_rs => $pattern_rs); + $c->stash(lnp_pattern_base_uri => + $c->uri_for_action("/ncos/lnp_pattern_root", [$c->req->captures->[0], $c->req->captures->[1]])); + + $c->stash->{lnp_pattern_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [ + { name => 'id', search => 1, title => $c->loc('#') }, + { name => 'pattern', search => 1, title => $c->loc('Pattern') }, + { name => 'description', search => 1, title => $c->loc('Description') }, + ]); + + $c->stash( template => 'ncos/lnp_pattern_list.tt' ); +} + +sub lnp_pattern_root :Chained('lnp_pattern_list') :PathPart('') :Args(0) { + my ($self, $c) = @_; +} + +sub lnp_pattern_ajax :Chained('lnp_pattern_list') :PathPart('lnp_pattern_ajax') :Args(0) { + my ($self, $c) = @_; + + my $resultset = $c->stash->{lnp_pattern_rs}; + NGCP::Panel::Utils::Datatables::process($c, $resultset, $c->stash->{lnp_pattern_dt_columns}); + $c->detach( $c->view("JSON") ); +} + +sub lnp_pattern_create :Chained('lnp_pattern_list') :PathPart('create') :Args(0) { + my ($self, $c) = @_; + + my $posted = ($c->request->method eq 'POST'); + my $form = NGCP::Panel::Form::get("NGCP::Panel::Form::NCOS::Pattern", $c); + $form->process( + posted => $posted, + params => $c->request->params, + ); + NGCP::Panel::Utils::Navigation::check_form_buttons( + c => $c, + form => $form, + fields => {}, + back_uri => $c->req->uri, + ); + if($posted && $form->validated) { + try { + unless($c->stash->{lnp_result}) { + NGCP::Panel::Utils::Message::error( + c => $c, + error => "Invalid ncos_lnp_list_id, lnp list does not exist", + desc => $c->loc('Failed to create NCOS pattern'), + ); + } + + $c->stash->{lnp_pattern_rs}->create($form->values); + NGCP::Panel::Utils::Message::info( + c => $c, + desc => $c->loc('NCOS pattern successfully created'), + ); + } catch($e) { + NGCP::Panel::Utils::Message::error( + c => $c, + error => $e, + desc => $c->loc('Failed to create NCOS pattern'), + ); + } + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{lnp_pattern_base_uri}); + } + + $c->stash( + close_target => $c->stash->{lnp_pattern_base_uri}, + form => $form, + create_flag => 1 + ); +} + +sub lnp_pattern_base :Chained('lnp_pattern_list') :PathPart('') :CaptureArgs(1) { + my ($self, $c, $pattern_id) = @_; + + unless($pattern_id && is_int($pattern_id)) { + NGCP::Panel::Utils::Message::error( + c => $c, + log => 'Invalid NCOS LNP pattern id detected', + desc => $c->loc('Invalid NCOS LNP pattern id detected'), + ); + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{lnp_pattern_base_uri}); + } + + my $res = $c->stash->{lnp_pattern_rs}->find($pattern_id); + unless(defined($res)) { + NGCP::Panel::Utils::Message::error( + c => $c, + log => 'NCOS LNP pattern does not exist', + desc => $c->loc('NCOS LNP pattern does not exist'), + ); + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{lnp_pattern_base_uri}); + } + $c->stash(lnp_pattern_result => $res); +} + +sub lnp_pattern_edit :Chained('lnp_pattern_base') :PathPart('edit') { + my ($self, $c) = @_; + + my $posted = ($c->request->method eq 'POST'); + my $form = NGCP::Panel::Form::get("NGCP::Panel::Form::NCOS::Pattern", $c); + $form->process( + posted => $posted, + params => $c->request->params, + item => $c->stash->{lnp_pattern_result}, + ); + NGCP::Panel::Utils::Navigation::check_form_buttons( + c => $c, + form => $form, + fields => {}, + back_uri => $c->req->uri, + ); + if($posted && $form->validated) { + try { + $c->stash->{lnp_pattern_result}->update($form->values); + NGCP::Panel::Utils::Message::info( + c => $c, + data => { $c->stash->{lnp_pattern_result}->get_inflated_columns }, + desc => $c->loc('NCOS pattern successfully updated'), + ); + } catch($e) { + NGCP::Panel::Utils::Message::error( + c => $c, + error => $e, + desc => $c->loc('Failed to update NCOS pattern'), + ); + } + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{lnp_pattern_base_uri}); + } + + $c->stash( + close_target => $c->stash->{lnp_pattern_base_uri}, + form => $form, + edit_flag => 1 + ); +} +sub lnp_pattern_delete :Chained('lnp_pattern_base') :PathPart('delete') { + my ($self, $c) = @_; + + try { + $c->stash->{lnp_pattern_result}->delete; + NGCP::Panel::Utils::Message::info( + c => $c, + data => { $c->stash->{lnp_pattern_result}->get_inflated_columns }, + desc => $c->loc('NCOS pattern successfully deleted'), + ); + } catch ($e) { + NGCP::Panel::Utils::Message::error( + c => $c, + error => $e, + desc => $c->loc('Failed to delete NCOS pattern'), + ); + }; + NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{lnp_pattern_base_uri}); +} 1; diff --git a/lib/NGCP/Panel/Form/NCOS/LnpPatternAPI.pm b/lib/NGCP/Panel/Form/NCOS/LnpPatternAPI.pm new file mode 100644 index 0000000000..bbe4582700 --- /dev/null +++ b/lib/NGCP/Panel/Form/NCOS/LnpPatternAPI.pm @@ -0,0 +1,23 @@ +package NGCP::Panel::Form::NCOS::LnpPatternAPI; + +use HTML::FormHandler::Moose; +extends 'NGCP::Panel::Form::NCOS::Pattern'; + +has_field 'ncos_lnp_list_id' => ( + type => 'PosInteger', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['The ncos list this pattern belongs to.'] + }, +); + +has_block 'fields' => ( + tag => 'div', + class => [qw/modal-body/], + render_list => [qw/pattern description ncos_lnp_list_id/], +); + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/NcosLnpPatterns.pm b/lib/NGCP/Panel/Role/API/NcosLnpPatterns.pm new file mode 100644 index 0000000000..f55f9a69e0 --- /dev/null +++ b/lib/NGCP/Panel/Role/API/NcosLnpPatterns.pm @@ -0,0 +1,109 @@ +package NGCP::Panel::Role::API::NcosLnpPatterns; +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 _item_rs { + my ($self, $c) = @_; + + my $item_rs = $c->model('DB')->resultset('ncos_lnp_pattern_list'); + if($c->user->roles eq "admin") { + } elsif($c->user->roles eq "reseller") { + $item_rs = $item_rs->search({ + 'ncos_level.reseller_id' => $c->user->reseller_id + },{ + join => {'ncos_lnp_list' => 'ncos_level'}, + }); + } + return $item_rs; +} + +sub get_form { + my ($self, $c) = @_; + return NGCP::Panel::Form::get("NGCP::Panel::Form::NCOS::LnpPatternAPI", $c); +} + +sub hal_from_item { + my ($self, $c, $item, $form) = @_; + my %resource = $item->get_inflated_columns; + + my $hal = Data::HAL->new( + 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 => 'collection', href => sprintf("/api/%s/", $self->resource_name)), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $item->id)), + Data::HAL::Link->new(relation => 'ngcp:ncoslists', href => sprintf("/api/ncoslists/%d", $item->ncos_lnp_list_id)), + ], + relation => 'ngcp:'.$self->resource_name, + ); + + $form //= $self->get_form($c); + + $self->validate_form( + c => $c, + resource => \%resource, + form => $form, + run => 0, + ); + + $resource{id} = int($item->id); + $hal->resource({%resource}); + return $hal; +} + +sub item_by_id { + my ($self, $c, $id) = @_; + my $item_rs = $self->item_rs($c); + return $item_rs->find($id); +} + +sub update_item { + my ($self, $c, $item, $old_resource, $resource, $form) = @_; + + $form //= $self->get_form($c); + return unless $self->validate_form( + c => $c, + form => $form, + resource => $resource, + ); + + my $lnp_list_rs = $c->model('DB')->resultset('ncos_lnp_list')->search({ + id => $resource->{ncos_lnp_list_id}, + }); + my $lnp_list = $lnp_list_rs->first; + unless($lnp_list) { + $c->log->error("invalid ncos_lnp_list_id '$$resource{ncos_lnp_list_id}'"); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid ncos_lnp_list_id, lnp list does not exist"); + return; + } + + my $dup_item = $lnp_list->ncos_lnp_pattern_lists->search({ + pattern => $resource->{pattern}, + })->first; + if($dup_item && $dup_item->id != $item->id) { + $c->log->error("ncos pattern '$$resource{pattern}' already exists for ncos_lnp_list_id '$$resource{ncos_lnp_list_id}'"); + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "NCOS pattern already exists for given ncos lnp list id"); + return; + } + + $item->update($resource); + + return $item; +} + +1; +# vim: set tabstop=4 expandtab: diff --git a/share/templates/ncos/lnp_pattern_list.tt b/share/templates/ncos/lnp_pattern_list.tt new file mode 100644 index 0000000000..18b23f7e2c --- /dev/null +++ b/share/templates/ncos/lnp_pattern_list.tt @@ -0,0 +1,29 @@ +[% site_config.title = c.loc('LNP Patterns for [_1]', lnp_result.lnp_provider.name) -%] + +[% + helper.name = c.loc('LNP Pattern'); + helper.identifier = 'lnp_pattern'; + helper.messages = messages; + helper.dt_columns = lnp_pattern_dt_columns; + helper.length_change = 1; + + helper.close_target = close_target; + helper.create_flag = create_flag; + helper.edit_flag = edit_flag; + helper.form_object = form; + + helper.ajax_uri = c.uri_for_action( "/ncos/lnp_pattern_ajax", [c.req.captures.0, c.req.captures.1] ); + helper.tmpuri = c.uri_for_action( "/ncos/lnp_pattern_root", [c.req.captures.0, c.req.captures.1] ); + + UNLESS c.user.read_only; + helper.dt_buttons = [ + { name = c.loc('Edit'), uri = helper.tmpuri _ "/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, + { name = c.loc('Delete'), uri = helper.tmpuri _ "/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' }, + ]; + helper.top_buttons = [ + { name = c.loc('Create LNP Pattern Entry'), uri = c.uri_for_action( "/ncos/lnp_pattern_create", c.req.captures ), icon = 'icon-star' }, + ]; + END; + + PROCESS 'helpers/datatables.tt'; +-%] diff --git a/share/templates/ncos/pattern_list.tt b/share/templates/ncos/pattern_list.tt index d9fbde7c11..e03f491025 100644 --- a/share/templates/ncos/pattern_list.tt +++ b/share/templates/ncos/pattern_list.tt @@ -74,6 +74,7 @@ helper.dt_buttons = [ { name = c.loc('Edit'), uri = helper.tmpuri _ "/'+full.id+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' }, { name = c.loc('Delete'), uri = helper.tmpuri _ "/'+full.id+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' }, + { name = c.loc('Patterns'), uri = helper.tmpuri _ "/'+full.id+'/lnp_patterns", class = 'btn-small btn-tertiary', icon = 'icon-list' }, ]; helper.top_buttons = [ { name = c.loc('Create LNP Entry'), uri = c.uri_for_action( "/ncos/lnp_create", [c.req.captures.0] ), icon = 'icon-star' },