From 93f644873b4fc72e073243f23dccbf65070e0742 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Tue, 21 Aug 2018 11:22:01 +0100
Subject: [PATCH] Fixes errors found during testing

---
 src/service/clmcservice/alertsapi/tests.py    | 31 +++++++++++++++++--
 .../clmcservice/alertsapi/utilities.py        |  8 ++---
 src/service/clmcservice/alertsapi/views.py    | 17 +++++-----
 ...an_template.tick => deadman-template.tick} |  2 +-
 ...e_template.tick => relative-template.tick} |  2 +-
 ..._template.tick => threshold-template.tick} |  0
 6 files changed, 45 insertions(+), 15 deletions(-)
 rename src/service/clmcservice/resources/TICKscript/{deadman_template.tick => deadman-template.tick} (92%)
 rename src/service/clmcservice/resources/TICKscript/{relative_template.tick => relative-template.tick} (94%)
 rename src/service/clmcservice/resources/TICKscript/{threshold_template.tick => threshold-template.tick} (100%)

diff --git a/src/service/clmcservice/alertsapi/tests.py b/src/service/clmcservice/alertsapi/tests.py
index 2c2066f..11984dc 100644
--- a/src/service/clmcservice/alertsapi/tests.py
+++ b/src/service/clmcservice/alertsapi/tests.py
@@ -26,10 +26,11 @@
 # Python standard libs
 from os import listdir
 from os.path import isfile, join
-from yaml import load
+
 
 # PIP installed libs
 import pytest
+from yaml import load
 from pyramid import testing
 from toscaparser.tosca_template import ToscaTemplate
 
@@ -136,4 +137,30 @@ class TestAlertsConfigurationAPI(object):
         :param app_config: fixture for setUp/tearDown of the web service registry
         """
 
-        pass
+        test_data_path = join(ROOT_DIR, *["resources", "tosca", "test-data", "clmc-validator", "valid"])
+
+        for test_file_path in listdir(test_data_path):
+
+            request = testing.DummyRequest()
+            alert_spec_abs_path = join(test_data_path, test_file_path)
+            print(alert_spec_abs_path)
+            with open(alert_spec_abs_path) as alert_spec:
+                request.POST['alert-spec'] = FieldStorageMock(test_file_path, alert_spec)  # a simple mock class is used to mimic the FieldStorage class
+
+                print(AlertsConfigurationAPI(request).post_alerts_specification())
+
+            break
+
+
+class FieldStorageMock(object):
+
+    def __init__(self, filename, file):
+        """
+        Used to mock the behaviour of the cgi.FieldStorage class - two attributes needed only to forward a file to the view.
+
+        :param filename: file name
+        :param file: file object
+        """
+
+        self.filename = filename
+        self.file = file
diff --git a/src/service/clmcservice/alertsapi/utilities.py b/src/service/clmcservice/alertsapi/utilities.py
index 7cec89e..4bb0d2f 100644
--- a/src/service/clmcservice/alertsapi/utilities.py
+++ b/src/service/clmcservice/alertsapi/utilities.py
@@ -98,7 +98,7 @@ class TICKScriptTemplateFiller:
 
     @staticmethod
     def _fill_threshold_template_vars(db=None, measurement=None, field=None, influx_function=None, critical_value=None,
-                                      comparison_operator=None, alert_period=None, topic_id=None, where_clause=None):
+                                      comparison_operator=None, alert_period=None, topic_id=None, where_clause=None, **kwargs):
         """
         Creates a dictionary object ready to be posted to kapacitor to create a "threshold" task from template.
 
@@ -158,7 +158,7 @@ class TICKScriptTemplateFiller:
 
     @staticmethod
     def _fill_relative_template_vars(db=None, measurement=None, field=None, critical_value=None, comparison_operator=None,
-                                     alert_period=None, topic_id=None, where_clause=None):
+                                     alert_period=None, topic_id=None, where_clause=None, **kwargs):
         """
         Creates a dictionary object ready to be posted to kapacitor to create a "relative" task from template.
 
@@ -214,7 +214,7 @@ class TICKScriptTemplateFiller:
         return template_vars
 
     @staticmethod
-    def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, where_clause=None):
+    def _fill_deadman_template_vars(db=None, measurement=None, critical_value=None, alert_period=None, topic_id=None, where_clause=None, **kwargs):
         """
         Creates a dictionary object ready to be posted to kapacitor to create a "deadman" task from template.
 
@@ -242,7 +242,7 @@ class TICKScriptTemplateFiller:
                 "value": alert_period
             },
             "throughputThreshold": {
-                "type": "int",
+                "type": "float",
                 "value": critical_value
             },
             "topicID": {
diff --git a/src/service/clmcservice/alertsapi/views.py b/src/service/clmcservice/alertsapi/views.py
index 0053d7f..70cbf7f 100644
--- a/src/service/clmcservice/alertsapi/views.py
+++ b/src/service/clmcservice/alertsapi/views.py
@@ -24,6 +24,8 @@
 
 # Python standard libs
 import logging
+from json import dumps
+from urllib.parse import urlparse
 
 # PIP installed libs
 from pyramid.httpexceptions import HTTPBadRequest
@@ -97,7 +99,7 @@ class AlertsConfigurationAPI(object):
             for trigger in policy.triggers:
                 event_id = trigger.name
                 event_type = trigger.trigger_tpl["event_type"]
-                template_id = "{0}_template".format(event_type)
+                template_id = "{0}-template".format(event_type)
                 measurement, field = trigger.trigger_tpl["metric"].split(".")
 
                 condition = trigger.trigger_tpl["condition"]
@@ -109,7 +111,7 @@ class AlertsConfigurationAPI(object):
                 where_clause = None
                 if "resource_type" in trigger.trigger_tpl["condition"]:
                     tags = condition["resource_type"]
-                    where_clause = " AND ".join(map(lambda tag_name: '"{0}"=\'{1}\''.format(tag_name, tags[tag_name]), tags))
+                    where_clause = " AND ".join(map(lambda tag_name: '"{0}"==\'{1}\''.format(tag_name, tags[tag_name]), tags))
 
                 comparison_operator = COMPARISON_OPERATORS[condition.get("comparison_operator", "gte")]  # if not specified, use "gte" (>=)
 
@@ -134,10 +136,10 @@ class AlertsConfigurationAPI(object):
                 }
 
                 # send the request and receive a response
-                response = post(kapacitor_api_tasks_url, data=kapacitor_http_request_body)
+                response = post(kapacitor_api_tasks_url, data=dumps(kapacitor_http_request_body))
                 response_content = response.json()
                 # log the response
-                log.info(response_content)
+                log.info(response_content, response.status_code)
 
                 # exttranc http handlers
                 http_handlers = trigger.trigger_tpl["action"]["implementation"]
@@ -145,11 +147,12 @@ class AlertsConfigurationAPI(object):
                 # subscribe all http handlers to the created topic
                 kapacitor_api_handlers_url = "http://localhost:9092/kapacitor/v1/alerts/topics/{0}/handlers".format(topic_id)
                 for http_handler_url in http_handlers:
-                    handler_id = "{0}.{1}.{2}".format(policy.name, event_id, http_handler_url)
+                    http_handler_host = urlparse(http_handler_url).netloc
+                    handler_id = "{0}.{1}.{2}".format(policy.name, event_id, http_handler_host)
                     kapacitor_http_request_body = fill_http_post_handler_vars(handler_id, http_handler_url)
-                    response = post(kapacitor_api_handlers_url, data=kapacitor_http_request_body)
+                    response = post(kapacitor_api_handlers_url, data=dumps(kapacitor_http_request_body))
                     response_content = response.json()
-                    log.info(response_content)
+                    log.info(response_content, response.status_code)
 
         return {"msg": "Alerts specification has been successfully validated and configured", "service_function_chain_id": sfc,
                 "service_function_chain_instance_id": sfc_instance}
diff --git a/src/service/clmcservice/resources/TICKscript/deadman_template.tick b/src/service/clmcservice/resources/TICKscript/deadman-template.tick
similarity index 92%
rename from src/service/clmcservice/resources/TICKscript/deadman_template.tick
rename to src/service/clmcservice/resources/TICKscript/deadman-template.tick
index a772b81..e28851c 100644
--- a/src/service/clmcservice/resources/TICKscript/deadman_template.tick
+++ b/src/service/clmcservice/resources/TICKscript/deadman-template.tick
@@ -4,7 +4,7 @@ var rp = 'autogen'  // default value for the retention policy
 
 var measurement string
 
-var whereClause = lambda: True  // default value is a function which returns TRUE, hence no filtering of the query result
+var whereClause = lambda: TRUE  // default value is a function which returns TRUE, hence no filtering of the query result
 
 var messageValue = 'TRUE'  // default value is TRUE, as this is what SFEMC expects as a notification for an event rule
 
diff --git a/src/service/clmcservice/resources/TICKscript/relative_template.tick b/src/service/clmcservice/resources/TICKscript/relative-template.tick
similarity index 94%
rename from src/service/clmcservice/resources/TICKscript/relative_template.tick
rename to src/service/clmcservice/resources/TICKscript/relative-template.tick
index e158ab9..d892a96 100644
--- a/src/service/clmcservice/resources/TICKscript/relative_template.tick
+++ b/src/service/clmcservice/resources/TICKscript/relative-template.tick
@@ -6,7 +6,7 @@ var measurement string
 
 var selectLambda lambda  // must be a lambda specifying the field to select e.g. "requests"
 
-var whereClause = lambda: True  // default value is a function which returns TRUE, hence no filtering of the query result
+var whereClause = lambda: TRUE  // default value is a function which returns TRUE, hence no filtering of the query result
 
 var messageValue = 'TRUE'  // default value is TRUE, as this is what SFEMC expects as a notification for an event rule
 
diff --git a/src/service/clmcservice/resources/TICKscript/threshold_template.tick b/src/service/clmcservice/resources/TICKscript/threshold-template.tick
similarity index 100%
rename from src/service/clmcservice/resources/TICKscript/threshold_template.tick
rename to src/service/clmcservice/resources/TICKscript/threshold-template.tick
-- 
GitLab