Monitoring: ns export metric; ns new alarm 84/6284/1
authorlombardofr <lombardo@everyup.it>
Mon, 18 Jun 2018 13:54:24 +0000 (15:54 +0200)
committerlombardofr <lombardo@everyup.it>
Mon, 18 Jun 2018 13:54:24 +0000 (15:54 +0200)
Change-Id: Ie428b53d320d1a2cee67bb776dada7988d6d32d3
Signed-off-by: lombardofr <lombardo@everyup.it>
instancehandler/template/instance_list.html
instancehandler/template/instance_list_ns.html
instancehandler/template/modal/instance_export_metric.html [new file with mode: 0644]
instancehandler/template/modal/instance_new_alarm.html [new file with mode: 0644]
instancehandler/urls.py
instancehandler/views.py
lib/osm/osmclient/client.py
static/src/instancehandler/instance_list.js

index ff76215..9baae4c 100644 (file)
@@ -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 %}
index 7743043..dd84d47 100644 (file)
                             <ul class="dropdown-menu">
                                 <li><a href="#"
                                        onclick="javascript:performAction('{% url 'projects:instances:action' instance_id=i|get:'_id' project_id=project_id type=type %}')">
-                                    <i class="fa fa-magic"></i>Exec NS Primitive</a></li>
+                                    <i class="fa fa-magic"></i> Exec NS Primitive</a></li>
                                 <li>
                                     <a href="{% url 'projects:instances:ns_operations' project_id=project_id type=type instance_id=i|get:'_id' %}">
-                                        <i class="fa fa-list"></i>Active operations</a></li>
+                                        <i class="fa fa-list"></i> Active operations</a></li>
+                                <li class="divider"></li>
+                                <li><a href="#"
+                                       onclick="javascript:newAlarmNs('{% url 'projects:instances:ns_create_alarm' instance_id=i|get:'_id' project_id=project_id type=type %}')">
+                                    <i class="far fa-bell"></i> New Alarm</a></li>
+                                <li><a href="#"
+                                       onclick="javascript:exportMetricNs('{% url 'projects:instances:ns_export_metric' instance_id=i|get:'_id' project_id=project_id type=type %}')">
+                                    <i class="far fa-chart-bar"></i> Export metric</a></li>
                                 <li class="divider"></li>
                                 <li>
                                     <a href="javascript:deleteNs('{% url 'projects:instances:delete' instance_id=i|get:'_id' project_id=project_id type=type %}', true)">
-                                        <i class="far fa-trash-alt" style="color:red" ></i>Force delete</a></li>
+                                        <i class="far fa-trash-alt" style="color:red" ></i> Force delete</a></li>
                             </ul>
 
 
diff --git a/instancehandler/template/modal/instance_export_metric.html b/instancehandler/template/modal/instance_export_metric.html
new file mode 100644 (file)
index 0000000..cd3d1b5
--- /dev/null
@@ -0,0 +1,75 @@
+<div class="modal" id="modal_instance_export_metric" xmlns="http://www.w3.org/1999/html">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                    <span aria-hidden="true">×</span></button>
+                <h4 class="modal-title">Export metric</h4>
+            </div>
+
+            <form id="formExportMetricNS" action="" class="form-horizontal" method="post" enctype="multipart/form-data">
+                {% csrf_token %}
+                <div class="modal-body" id="modal_instance_export_metric">
+
+
+                    <div class="form-group">
+                        <label for="metric_name" class="col-sm-3">Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="metric_name" name="metric_name" required>
+                        </div>
+                        <label for="metric_name" class="col-sm-3">Metric Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="metric_name" name="metric_name" required>
+                        </div>
+                    </div>
+
+
+                    <div class="form-group">
+                        <label for="vnf_member_index" class="col-sm-3">VNF Member index *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="vnf_member_index" name="vnf_member_index" required>
+                        </div>
+                        <label for="vdu_name" class="col-sm-3">VDU Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="vdu_name" name="vdu_name" required>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+
+
+                        <label for="correlation_id" class="col-sm-3">Correlation</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="correlation_id" name="correlation_id" >
+                        </div>
+
+                    </div>
+                    <div class="form-group">
+                        <label for="collection_unit" class="col-sm-3">Collection Unit</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="collection_unit" name="collection_unit" >
+                        </div>
+                        <label for="collection_period" class="col-sm-3">Collection period </label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="collection_period" name="collection_period" >
+                        </div>
+
+                    </div>
+                    <div class="form-group">
+
+
+                    </div>
+
+
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
+                    <button class="btn btn-primary pull-right">Create</button>
+                </div>
+            </form>
+
+        </div>
+        <!-- /.modal-content -->
+    </div>
+    <!-- /.modal-dialog -->
+</div>
diff --git a/instancehandler/template/modal/instance_new_alarm.html b/instancehandler/template/modal/instance_new_alarm.html
new file mode 100644 (file)
index 0000000..fe5fbfe
--- /dev/null
@@ -0,0 +1,83 @@
+<div class="modal" id="modal_instance_new_alarm" xmlns="http://www.w3.org/1999/html">
+    <div class="modal-dialog">
+        <div class="modal-content">
+            <div class="modal-header">
+                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                    <span aria-hidden="true">×</span></button>
+                <h4 class="modal-title">Create Alarm</h4>
+            </div>
+
+            <form id="formAlarmNS" action="" class="form-horizontal" method="post" enctype="multipart/form-data">
+                {% csrf_token %}
+                <div class="modal-body" id="modal_instance_new_alarm_body">
+
+
+                    <div class="form-group">
+                        <label for="alarm_name" class="col-sm-3">Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="alarm_name" name="alarm_name" required>
+                        </div>
+                        <label for="metric_name" class="col-sm-3">Metric Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="metric_name" name="metric_name" required>
+                        </div>
+                    </div>
+
+
+                    <div class="form-group">
+                        <label for="vnf_member_index" class="col-sm-3">VNF Member index *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="vnf_member_index" name="vnf_member_index" required>
+                        </div>
+                        <label for="vdu_name" class="col-sm-3">VDU Name *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="vdu_name" name="vdu_name" required>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+
+                        <label for="statistic" class="col-sm-3">Statistic *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="statistic" name="statistic" required>
+                        </div>
+                        <label for="correlation_id" class="col-sm-3">Correlation</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="correlation_id" name="correlation_id" >
+                        </div>
+
+                    </div>
+                    <div class="form-group">
+                        <label for="threshold_value" class="col-sm-3">Threshold Value *</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="threshold_value" name="threshold_value" required>
+                        </div>
+                        <label for="statistic" class="col-sm-3">Severity</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="severity" name="severity" >
+                        </div>
+
+                    </div>
+                    <div class="form-group">
+                        <label for="operation" class="col-sm-3">Operation</label>
+                        <div class="col-sm-3">
+                            <input class="form-control input-sm" id="operation" name="operation" >
+                        </div>
+
+
+
+                    </div>
+
+
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
+                    <button class="btn btn-primary pull-right">Create</button>
+                </div>
+            </form>
+
+        </div>
+        <!-- /.modal-content -->
+    </div>
+    <!-- /.modal-dialog -->
+</div>
index 9474f1e..054c3ba 100644 (file)
@@ -24,6 +24,8 @@ urlpatterns = [
     url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)/action$', views.action, name='action'),
     url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)/operation$', views.ns_operations, name='ns_operations'),
     url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)/operation/(?P<op_id>[0-9a-z-]+)', views.ns_operation, name='ns_operation'),
+    url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)/monitoring/alarm$', views.create_alarm, name='ns_create_alarm'),
+    url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)/monitoring/metric$', views.export_metric, name='ns_export_metric'),
     url(r'^(?P<type>[ns|vnf]+)/(?P<instance_id>[0-9a-z-]+)', views.show, name='show'),
 
 ]
index c9808d6..d835de6 100644 (file)
@@ -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)
+
+
index d91d297..e9248ec 100644 (file)
@@ -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:
index 47d4957..b4c0edc 100644 (file)
@@ -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