Fixes bugs regarding alarm deletion

Adds healthcheck script to be used in devops MON Dockerfile
Removes unused dependencies

Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Change-Id: Ia353c0ae9ee6a502667c04c96f1bc7f3e9337ced
diff --git a/osm_mon/cmd/mon_healthcheck.py b/osm_mon/cmd/mon_healthcheck.py
new file mode 100644
index 0000000..412410b
--- /dev/null
+++ b/osm_mon/cmd/mon_healthcheck.py
@@ -0,0 +1,85 @@
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
+#         http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
+
+import logging
+import subprocess
+import sys
+import uuid
+
+import requests
+
+from osm_mon.core.message_bus.consumer import Consumer
+
+log = logging.getLogger(__name__)
+
+
+def main():
+    # Check Kafka
+    if not _processes_running():
+        sys.exit(1)
+    if not _is_kafka_ok():
+        sys.exit(1)
+    if not _is_prometheus_exporter_ok():
+        sys.exit(1)
+    sys.exit(0)
+
+
+def _processes_running():
+    def _contains_process(processes, process_name):
+        for row in processes:
+            if process_name in row:
+                return True
+        return False
+    processes_to_check = ['osm-mon-collector', 'osm-mon-evaluator', 'osm-mon-server']
+    ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
+    processes_running = ps.split('\n')
+    for p in processes_to_check:
+        if not _contains_process(processes_running, p):
+            return False
+    return True
+
+
+def _is_prometheus_exporter_ok():
+    try:
+        r = requests.get('http://localhost:8000')
+        r.raise_for_status()
+        return True
+    except Exception:
+        log.exception("MON Prometheus exporter is not running")
+        return False
+
+
+def _is_kafka_ok():
+    try:
+        common_consumer = Consumer("mon-healthcheck-" + str(uuid.uuid4()))
+        topics = ['alarm_request', 'vim_account']
+        common_consumer.subscribe(topics)
+        common_consumer.poll()
+        common_consumer.close(autocommit=False)
+        return True
+    except Exception:
+        log.exception("MON can not connect to Kafka")
+        return False
+
+
+if __name__ == '__main__':
+    main()
diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py
index 56a1275..a41c0fd 100644
--- a/osm_mon/core/database.py
+++ b/osm_mon/core/database.py
@@ -23,8 +23,9 @@
 ##
 
 import logging
+import uuid
 
-from peewee import CharField, TextField, FloatField, Model
+from peewee import CharField, TextField, FloatField, Model, AutoField
 from playhouse.db_url import connect
 
 from osm_mon.core.settings import Config
@@ -37,6 +38,7 @@
 
 
 class BaseModel(Model):
+    id = AutoField(primary_key=True)
 
     class Meta:
         database = db
@@ -54,6 +56,7 @@
 
 
 class Alarm(BaseModel):
+    uuid = CharField(unique=True)
     name = CharField()
     severity = CharField()
     threshold = FloatField()
@@ -81,7 +84,7 @@
         """Saves vim credentials. If a record with same uuid exists, overwrite it."""
         exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
         if exists:
-            vim_credentials.uuid = exists.uuid
+            vim_credentials.id = exists.id
         vim_credentials.save()
         return vim_credentials
 
@@ -94,7 +97,9 @@
     def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
                    vnf_member_index, nsr_id) -> Alarm:
         """Saves alarm."""
+        # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
         alarm = Alarm()
+        alarm.uuid = str(uuid.uuid4())
         alarm.name = name
         alarm.threshold = threshold
         alarm.operation = operation
@@ -107,11 +112,11 @@
         alarm.save()
         return alarm
 
-    def delete_alarm(self, alarm_id) -> None:
+    def delete_alarm(self, alarm_uuid) -> None:
         alarm = (Alarm.select()
-                 .where(Alarm.alarm_id == alarm_id)
+                 .where(Alarm.uuid == alarm_uuid)
                  .get())
-        alarm.delete()
+        alarm.delete_instance()
 
     def get_vim_type(self, vim_account_id) -> str:
         """Get the vim type that is required by the message."""
diff --git a/osm_mon/core/models/delete_alarm_resp.json b/osm_mon/core/models/delete_alarm_resp.json
index 5b931f2..d6fec9f 100644
--- a/osm_mon/core/models/delete_alarm_resp.json
+++ b/osm_mon/core/models/delete_alarm_resp.json
@@ -21,7 +21,7 @@
 {
   "schema_version": { "type": "string" },
   "schema_type": { "type": "string" },
-  "alarm_deletion_response":
+  "alarm_delete_response":
   {
     "correlation_id": { "type": "integer" },
     "alarm_uuid": { "type": "string" },
diff --git a/osm_mon/core/response.py b/osm_mon/core/response.py
index 8a864be..7e49a4f 100644
--- a/osm_mon/core/response.py
+++ b/osm_mon/core/response.py
@@ -83,8 +83,8 @@
     def delete_alarm_response(self, **kwargs) -> dict:
         """Generate a response for a delete alarm request."""
         delete_alarm_resp = {"schema_version": schema_version,
-                             "schema_type": "alarm_deletion_response",
-                             "alarm_deletion_response": {
+                             "schema_type": "alarm_delete_response",
+                             "alarm_delete_response": {
                                  "correlation_id": kwargs['cor_id'],
                                  "alarm_uuid": kwargs['alarm_id'],
                                  "status": kwargs['status']}}
diff --git a/osm_mon/evaluator/evaluator.py b/osm_mon/evaluator/evaluator.py
index 7613e0b..9591946 100644
--- a/osm_mon/evaluator/evaluator.py
+++ b/osm_mon/evaluator/evaluator.py
@@ -157,7 +157,7 @@
         # Generate and send response
         resp_message = response.generate_response(
             'notify_alarm',
-            alarm_id=alarm.id,
+            alarm_id=alarm.uuid,
             vdu_name=alarm.vdur_name,
             vnf_member_index=alarm.vnf_member_index,
             ns_id=alarm.nsr_id,
diff --git a/osm_mon/server/server.py b/osm_mon/server/server.py
index 89ba4e8..854d926 100755
--- a/osm_mon/server/server.py
+++ b/osm_mon/server/server.py
@@ -1,22 +1,25 @@
-# Copyright 2017 Intel Research and Development Ireland Limited
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
 # *************************************************************
+
 # This file is part of OSM Monitoring module
-# All Rights Reserved to Intel Corporation
-#
-# Licensed under the Apache License, Version 2.0 (the "License"); you
-# may not use this file except in compliance with the License. You may
-# obtain a copy of the License at
-#
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
 #         http://www.apache.org/licenses/LICENSE-2.0
-#
+
 # Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
-# implied. See the License for the specific language governing
-# permissions and limitations under the License.
-#
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
 # For those usages not covered by the Apache License, Version 2.0 please
-# contact: helena.mcgough@intel.com or adrian.hoban@intel.com
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
 """A common KafkaConsumer for all MON plugins."""
 
 import json
@@ -91,7 +94,7 @@
                         response = response_builder.generate_response('create_alarm_response',
                                                                       cor_id=alarm_details['correlation_id'],
                                                                       status=True,
-                                                                      alarm_id=alarm.id)
+                                                                      alarm_id=alarm.uuid)
                     except Exception:
                         log.exception("Error creating alarm: ")
                         response = response_builder.generate_response('create_alarm_response',
@@ -100,18 +103,18 @@
                                                                       alarm_id=None)
                 if message.key == "delete_alarm_request":
                     alarm_details = values['alarm_delete_request']
-                    response_builder = ResponseBuilder()
                     alarm_uuid = alarm_details['alarm_uuid']
+                    response_builder = ResponseBuilder()
                     cor_id = alarm_details['correlation_id']
                     try:
                         self.database_manager.delete_alarm(alarm_uuid)
-                        response = response_builder.generate_response('create_alarm_response',
+                        response = response_builder.generate_response('delete_alarm_response',
                                                                       cor_id=cor_id,
                                                                       status=True,
                                                                       alarm_id=alarm_uuid)
                     except Exception:
-                        log.exception("Error creating alarm: ")
-                        response = response_builder.generate_response('create_alarm_response',
+                        log.exception("Error deleting alarm: ")
+                        response = response_builder.generate_response('delete_alarm_response',
                                                                       cor_id=cor_id,
                                                                       status=False,
                                                                       alarm_id=alarm_uuid)