From 99624b5f5ae95b0674f7f5005da04281e49ac5aa Mon Sep 17 00:00:00 2001 From: lombardofr Date: Mon, 18 Jun 2018 15:54:24 +0200 Subject: [PATCH] Monitoring: ns export metric; ns new alarm Change-Id: Ie428b53d320d1a2cee67bb776dada7988d6d32d3 Signed-off-by: lombardofr --- instancehandler/template/instance_list.html | 2 + .../template/instance_list_ns.html | 13 ++- .../modal/instance_export_metric.html | 75 +++++++++++++++++ .../template/modal/instance_new_alarm.html | 83 +++++++++++++++++++ instancehandler/urls.py | 2 + instancehandler/views.py | 43 ++++++++++ lib/osm/osmclient/client.py | 35 +++++++- static/src/instancehandler/instance_list.js | 75 +++++++++++++++++ 8 files changed, 323 insertions(+), 5 deletions(-) create mode 100644 instancehandler/template/modal/instance_export_metric.html create mode 100644 instancehandler/template/modal/instance_new_alarm.html diff --git a/instancehandler/template/instance_list.html b/instancehandler/template/instance_list.html index ff76215..9baae4c 100644 --- a/instancehandler/template/instance_list.html +++ b/instancehandler/template/instance_list.html @@ -47,6 +47,8 @@ {% include 'modal/instance_create.html' %} {% include 'modal/instance_show.html' %} {% include 'modal/instance_new_action.html' %} + {% include 'modal/instance_new_alarm.html' %} + {% include 'modal/instance_export_metric.html' %} {% endblock %} {% block resource_block %} diff --git a/instancehandler/template/instance_list_ns.html b/instancehandler/template/instance_list_ns.html index 7743043..dd84d47 100644 --- a/instancehandler/template/instance_list_ns.html +++ b/instancehandler/template/instance_list_ns.html @@ -75,14 +75,21 @@ diff --git a/instancehandler/template/modal/instance_export_metric.html b/instancehandler/template/modal/instance_export_metric.html new file mode 100644 index 0000000..cd3d1b5 --- /dev/null +++ b/instancehandler/template/modal/instance_export_metric.html @@ -0,0 +1,75 @@ + diff --git a/instancehandler/template/modal/instance_new_alarm.html b/instancehandler/template/modal/instance_new_alarm.html new file mode 100644 index 0000000..fe5fbfe --- /dev/null +++ b/instancehandler/template/modal/instance_new_alarm.html @@ -0,0 +1,83 @@ + diff --git a/instancehandler/urls.py b/instancehandler/urls.py index 9474f1e..054c3ba 100644 --- a/instancehandler/urls.py +++ b/instancehandler/urls.py @@ -24,6 +24,8 @@ urlpatterns = [ url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/action$', views.action, name='action'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/operation$', views.ns_operations, name='ns_operations'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/operation/(?P[0-9a-z-]+)', views.ns_operation, name='ns_operation'), + url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/monitoring/alarm$', views.create_alarm, name='ns_create_alarm'), + url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)/monitoring/metric$', views.export_metric, name='ns_export_metric'), url(r'^(?P[ns|vnf]+)/(?P[0-9a-z-]+)', views.show, name='show'), ] diff --git a/instancehandler/views.py b/instancehandler/views.py index c9808d6..d835de6 100644 --- a/instancehandler/views.py +++ b/instancehandler/views.py @@ -123,6 +123,47 @@ def show(request, project_id=None, instance_id=None, type=None): print result return __response_handler(request, result) +@login_required +def export_metric(request, project_id=None, instance_id=None, type=None): + + metric_data = request.POST.dict() + + client = Client() + keys = ["collection_period", + "vnf_member_index", + "metric_name", + "correlation_id", + "vdu_name", + "collection_unit"] + metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) + + response = client.ns_metric_export(instance_id, metric_data) + + return __response_handler(request, {}, None, to_redirect=False, + status=response.status_code) + +@login_required +def create_alarm(request, project_id=None, instance_id=None, type=None): + metric_data = request.POST.dict() + print metric_data + client = Client() + + + keys = ["threshold_value", + "vnf_member_index", + "metric_name", + "vdu_name", + "alarm_name", + "correlation_id", + "statistic", + "operation", + "severity"] + metric_data = dict(filter(lambda i: i[0] in keys and len(i[1]) > 0, metric_data.items())) + + result = client.ns_alarm_create(instance_id, metric_data) + return __response_handler(request, {}, None, to_redirect=False, + status=result['status'] if 'status' in result else None) + def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs): raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',') @@ -132,3 +173,5 @@ def __response_handler(request, data_res, url=None, to_redirect=None, *args, **k return redirect(url, *args, **kwargs) else: return render(request, url, data_res) + + diff --git a/lib/osm/osmclient/client.py b/lib/osm/osmclient/client.py index d91d297..e9248ec 100644 --- a/lib/osm/osmclient/client.py +++ b/lib/osm/osmclient/client.py @@ -77,7 +77,6 @@ class Client(object): json=vim_data) return None - def sdn_list(self): token = self.get_token() if token: @@ -302,6 +301,33 @@ class Client(object): return self._send_get(_url, headers=headers) return None + def ns_alarm_create(self, id, alarm_payload): + token = self.get_token() + headers = {} + if token: + headers['Authorization'] = 'Bearer {}'.format(token) + headers['Content-Type'] = 'application/json' +# headers['accept'] = 'application/json' + + _url = "{0}/test/message/alarm_request".format(self._base_path) + return self._send_post(_url, headers=headers, json=alarm_payload) + return None + + def ns_metric_export(self, id, metric_payload): + + token = self.get_token() + headers = {} + if token: + headers['Authorization'] = 'Bearer {}'.format(token) + headers['Content-Type'] = 'application/json' + #headers['accept'] = 'application/json' + print "DIO" + _url = "{0}/test/message/metric_request".format(self._base_path) + print _url + return self._send_post(_url, headers=headers, json=metric_payload) + return None + + def vnfd_list(self): token = self.get_token() if token: @@ -416,7 +442,12 @@ class Client(object): log.exception(e) #print "Exception during send POST" return {'error': 'error during connection to agent'} - return Util.json_loads_byteified(r.text) + if 'accept' in kwargs['headers']: + accept = kwargs['headers']['accept'] + if accept == 'application/json': + #print "json" + return Util.json_loads_byteified(r.text) + return r def _send_put(self, url, data=None, json=None, **kwargs): try: diff --git a/static/src/instancehandler/instance_list.js b/static/src/instancehandler/instance_list.js index 47d4957..b4c0edc 100644 --- a/static/src/instancehandler/instance_list.js +++ b/static/src/instancehandler/instance_list.js @@ -19,6 +19,17 @@ function performAction(url) { $('#modal_instance_new_action').modal('show'); } +function exportMetricNs(url) { + console.log(url) + $("#formExportMetricNS").attr("action", url); + $('#modal_instance_export_metric').modal('show'); +} + +function newAlarmNs(url) { + $("#formAlarmNS").attr("action", url); + $('#modal_instance_new_alarm').modal('show'); +} + function deleteNs(url, force) { bootbox.confirm("Are you sure want to delete?", function (result) { if (result) { @@ -141,4 +152,68 @@ $(document).ready(function () { }); }); + $("#formAlarmNS").submit(function (event) { + event.preventDefault(); //prevent default action + var post_url = $(this).attr("action"); //get form action url + var request_method = $(this).attr("method"); //get form GET/POST method + var form_data = new FormData(this); //Encode form elements for submission + console.log(post_url); + $.ajax({ + url: post_url, + type: request_method, + data: form_data, + headers: { + "Accept": 'application/json' + }, + contentType: false, + processData: false + }).done(function (response,textStatus, jqXHR) { + $('#modal_instance_new_action').modal('hide'); + bootbox.alert({ + title: "Metric", + message: "Alarm created." + }); + }).fail(function(result){ + var data = result.responseJSON; + var title = "Error " + (data.code ? data.code: 'unknown'); + var message = data.detail ? data.detail: 'No detail available.'; + bootbox.alert({ + title: title, + message: message + }); + }); + }); + + $("#formExportMetricNS").submit(function (event) { + event.preventDefault(); //prevent default action + var post_url = $(this).attr("action"); //get form action url + var request_method = $(this).attr("method"); //get form GET/POST method + var form_data = new FormData(this); //Encode form elements for submission + console.log(post_url); + $.ajax({ + url: post_url, + type: request_method, + data: form_data, + headers: { + "Accept": 'application/json' + }, + contentType: false, + processData: false + }).done(function (response,textStatus, jqXHR) { + $('#modal_instance_new_action').modal('hide'); + bootbox.alert({ + title: "Metric", + message: "Metric exported." + }); + }).fail(function(result){ + var data = result.responseJSON; + var title = "Error " + (data.code ? data.code: 'unknown'); + var message = data.detail ? data.detail: 'No detail available.'; + bootbox.alert({ + title: title, + message: message + }); + }); + }); + }); \ No newline at end of file -- 2.25.1