Added a Common KafkaConsumer for all of the plugins
[osm/MON.git] / core / message_bus / producer.py
index 1b0de7a..4aee831 100644 (file)
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: prithiv.mohan@intel.com or adrian.hoban@intel.com
 ##
-
 '''
 This is a kafka producer app that interacts with the SO and the plugins of the
 datacenters like OpenStack, VMWare, AWS.
 '''
 
-__author__ = "Prithiv Mohan"
-__date__   = "06/Sep/2017"
-
-
 from kafka import KafkaProducer as kaf
 from kafka.errors import KafkaError
 import logging
@@ -38,6 +33,9 @@ import os
 from os import listdir
 from jsmin import jsmin
 
+__author__ = "Prithiv Mohan"
+__date__ = "06/Sep/2017"
+
 json_path = os.path.join(os.pardir+"/models/")
 
 
@@ -45,7 +43,7 @@ class KafkaProducer(object):
 
     def __init__(self, topic):
 
-        self._topic= topic
+        self._topic = topic
 
         if "BROKER_URI" in os.environ:
             broker = os.getenv("BROKER_URI")
@@ -58,10 +56,10 @@ class KafkaProducer(object):
         is already running.
         '''
 
-        self.producer = kaf(key_serializer=str.encode,
-                   value_serializer=lambda v: json.dumps(v).encode('ascii'),
-                   bootstrap_servers=broker, api_version=(0,10))
-
+        self.producer = kaf(
+            key_serializer=str.encode,
+            value_serializer=lambda v: json.dumps(v).encode('ascii'),
+            bootstrap_servers=broker, api_version=(0, 10))
 
     def publish(self, key, value, topic=None):
         try:
@@ -80,237 +78,224 @@ class KafkaProducer(object):
 
     def create_alarm_request(self, key, message, topic):
 
-       #External to MON
+        # External to MON
 
-        payload_create_alarm = jsmin(open(os.path.join(json_path,
-                                         'create_alarm.json')).read())
+        payload_create_alarm = jsmin(
+            open(os.path.join(json_path, 'create_alarm.json')).read())
         self.publish(key,
-                value=json.dumps(payload_create_alarm),
-                topic='alarm_request')
+                     value=json.dumps(payload_create_alarm),
+                     topic='alarm_request')
 
     def create_alarm_response(self, key, message, topic):
 
-       #Internal to MON
+        # Internal to MON
 
-        payload_create_alarm_resp = jsmin(open(os.path.join(json_path,
-                                         'create_alarm_resp.json')).read())
+        payload_create_alarm_resp = jsmin(
+            open(os.path.join(json_path, 'create_alarm_resp.json')).read())
 
         self.publish(key,
-                value = message,
-                topic = 'alarm_response')
+                     value=message,
+                     topic='alarm_response')
 
     def acknowledge_alarm(self, key, message, topic):
 
-       #Internal to MON
+        # Internal to MON
 
-        payload_acknowledge_alarm = jsmin(open(os.path.join(json_path,
-                                         'acknowledge_alarm.json')).read())
+        payload_acknowledge_alarm = jsmin(
+            open(os.path.join(json_path, 'acknowledge_alarm.json')).read())
 
         self.publish(key,
-                value = json.dumps(payload_acknowledge_alarm),
-                topic = 'alarm_request')
+                     value=json.dumps(payload_acknowledge_alarm),
+                     topic='alarm_request')
 
     def list_alarm_request(self, key, message, topic):
 
-        #External to MON
+        # External to MON
 
-        payload_alarm_list_req = jsmin(open(os.path.join(json_path,
-                                      'list_alarm_req.json')).read())
+        payload_alarm_list_req = jsmin(
+            open(os.path.join(json_path, 'list_alarm_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_alarm_list_req),
-                topic='alarm_request')
+                     value=json.dumps(payload_alarm_list_req),
+                     topic='alarm_request')
 
     def notify_alarm(self, key, message, topic):
 
-        payload_notify_alarm = jsmin(open(os.path.join(json_path,
-                                          'notify_alarm.json')).read())
+        payload_notify_alarm = jsmin(
+            open(os.path.join(json_path, 'notify_alarm.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='alarm_response')
+                     value=message,
+                     topic='alarm_response')
 
     def list_alarm_response(self, key, message, topic):
 
-        payload_list_alarm_resp = jsmin(open(os.path.join(json_path,
-                                             'list_alarm_resp.json')).read())
+        payload_list_alarm_resp = jsmin(
+            open(os.path.join(json_path, 'list_alarm_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='alarm_response')
-
+                     value=message,
+                     topic='alarm_response')
 
     def update_alarm_request(self, key, message, topic):
 
-      # External to Mon
+        # External to Mon
 
-        payload_update_alarm_req = jsmin(open(os.path.join(json_path,
-                                        'update_alarm_req.json')).read())
+        payload_update_alarm_req = jsmin(
+            open(os.path.join(json_path, 'update_alarm_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_update_alarm_req),
-                topic='alarm_request')
-
+                     value=json.dumps(payload_update_alarm_req),
+                     topic='alarm_request')
 
     def update_alarm_response(self, key, message, topic):
 
-        # Internal to Mon 
+        # Internal to Mon
 
-        payload_update_alarm_resp = jsmin(open(os.path.join(json_path,
-                                        'update_alarm_resp.json')).read())
+        payload_update_alarm_resp = jsmin(
+            open(os.path.join(json_path, 'update_alarm_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='alarm_response')
-
+                     value=message,
+                     topic='alarm_response')
 
     def delete_alarm_request(self, key, message, topic):
 
-      # External to Mon
+        # External to Mon
 
-        payload_delete_alarm_req = jsmin(open(os.path.join(json_path,
-                                        'delete_alarm_req.json')).read())
+        payload_delete_alarm_req = jsmin(
+            open(os.path.join(json_path, 'delete_alarm_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_delete_alarm_req),
-                topic='alarm_request')
+                     value=json.dumps(payload_delete_alarm_req),
+                     topic='alarm_request')
 
     def delete_alarm_response(self, key, message, topic):
 
-      # Internal to Mon
+        # Internal to Mon
 
-        payload_delete_alarm_resp = jsmin(open(os.path.join(json_path,
-                                               'delete_alarm_resp.json')).read())
+        payload_delete_alarm_resp = jsmin(
+            open(os.path.join(json_path, 'delete_alarm_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='alarm_response')
-
-
+                     value=message,
+                     topic='alarm_response')
 
     def create_metrics_request(self, key, message, topic):
 
         # External to Mon
 
-        payload_create_metrics_req = jsmin(open(os.path.join(json_path,
-                                                'create_metric_req.json')).read())
+        payload_create_metrics_req = jsmin(
+            open(os.path.join(json_path, 'create_metric_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_create_metrics_req),
-                topic='metric_request')
-
+                     value=json.dumps(payload_create_metrics_req),
+                     topic='metric_request')
 
     def create_metrics_resp(self, key, message, topic):
 
         # Internal to Mon
 
-        payload_create_metrics_resp = jsmin(open(os.path.join(json_path,
-                                                 'create_metric_resp.json')).read())
+        payload_create_metrics_resp = jsmin(
+            open(os.path.join(json_path, 'create_metric_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='metric_response')
-
+                     value=message,
+                     topic='metric_response')
 
     def read_metric_data_request(self, key, message, topic):
 
         # External to Mon
 
-        payload_read_metric_data_request = jsmin(open(os.path.join(json_path,
-                                                      'read_metric_data_req.json')).read())
+        payload_read_metric_data_request = jsmin(
+            open(os.path.join(json_path, 'read_metric_data_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_read_metric_data_request),
-                topic='metric_request')
-
+                     value=json.dumps(payload_read_metric_data_request),
+                     topic='metric_request')
 
     def read_metric_data_response(self, key, message, topic):
 
         # Internal to Mon
 
-        payload_metric_data_response = jsmin(open(os.path.join(json_path,
-                                                  'read_metric_data_resp.json')).read())
+        payload_metric_data_response = jsmin(
+            open(os.path.join(json_path, 'read_metric_data_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='metric_response')
-
+                     value=message,
+                     topic='metric_response')
 
     def list_metric_request(self, key, message, topic):
 
-        #External to MON
+        # External to MON
 
-        payload_metric_list_req = jsmin(open(os.path.join(json_path,
-                                             'list_metric_req.json')).read())
+        payload_metric_list_req = jsmin(
+            open(os.path.join(json_path, 'list_metric_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_metric_list_req),
-                topic='metric_request')
+                     value=json.dumps(payload_metric_list_req),
+                     topic='metric_request')
 
     def list_metric_response(self, key, message, topic):
 
-      #Internal to MON
+        # Internal to MON
 
-        payload_metric_list_resp = jsmin(open(os.path.join(json_path,
-                                              'list_metrics_resp.json')).read())
+        payload_metric_list_resp = jsmin(
+            open(os.path.join(json_path, 'list_metrics_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='metric_response')
-
+                     value=message,
+                     topic='metric_response')
 
     def delete_metric_request(self, key, message, topic):
 
-      # External to Mon
+        # External to Mon
 
-        payload_delete_metric_req = jsmin(open(os.path.join(json_path,
-                                               'delete_metric_req.json')).read())
+        payload_delete_metric_req = jsmin(
+            open(os.path.join(json_path, 'delete_metric_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_delete_metric_req),
-                topic='metric_request')
-
+                     value=json.dumps(payload_delete_metric_req),
+                     topic='metric_request')
 
     def delete_metric_response(self, key, message, topic):
 
-      # Internal to Mon
+        # Internal to Mon
 
-        payload_delete_metric_resp = jsmin(open(os.path.join(json_path,
-                                                'delete_metric_resp.json')).read())
+        payload_delete_metric_resp = jsmin(
+            open(os.path.join(json_path, 'delete_metric_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='metric_response')
-
+                     value=message,
+                     topic='metric_response')
 
     def update_metric_request(self, key, message, topic):
 
         # External to Mon
 
-        payload_update_metric_req = jsmin(open(os.path.join(json_path,
-                                               'update_metric_req.json')).read())
+        payload_update_metric_req = jsmin(
+            open(os.path.join(json_path, 'update_metric_req.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_update_metric_req),
-                topic='metric_request')
-
+                     value=json.dumps(payload_update_metric_req),
+                     topic='metric_request')
 
     def update_metric_response(self, key, message, topic):
 
         # Internal to Mon
 
-        payload_update_metric_resp = jsmin(open(os.path.join(json_path,
-                                                'update_metric_resp.json')).read())
+        payload_update_metric_resp = jsmin(
+            open(os.path.join(json_path, 'update_metric_resp.json')).read())
 
         self.publish(key,
-                value=message,
-                topic='metric_response')
+                     value=message,
+                     topic='metric_response')
 
     def access_credentials(self, key, message, topic):
 
-        payload_access_credentials = jsmin(open(os.path.join(json_path,
-                                                'access_credentials.json')).read())
+        payload_access_credentials = jsmin(
+            open(os.path.join(json_path, 'access_credentials.json')).read())
 
         self.publish(key,
-                value=json.dumps(payload_access_credentials),
-                topic='access_credentials')
+                     value=json.dumps(payload_access_credentials),
+                     topic='access_credentials')