Refactor codes for notify util
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / utils / notificationsutil.py
index 0a0e1f2..8a38fd6 100644 (file)
@@ -22,31 +22,31 @@ from rest_framework import status
 
 from lcm.nf import const
 from lcm.pub.database.models import SubscriptionModel
-from lcm.pub.database.models import (
-    VmInstModel, NetworkInstModel,
-    PortInstModel, StorageInstModel, VNFCInstModel
-)
+from lcm.pub.database.models import VmInstModel
+from lcm.pub.database.models import NetworkInstModel
+from lcm.pub.database.models import PortInstModel
+from lcm.pub.database.models import StorageInstModel
+from lcm.pub.database.models import VNFCInstModel
 from lcm.pub.utils.timeutil import now_time
 from lcm.pub.utils.enumutil import enum
 
 logger = logging.getLogger(__name__)
 
-NOTIFY_TYPE = enum(lCM_OP_OCC="VnfLcmOperationOccurrenceNotification",
-                   CREATION="VnfIdentifierCreationNotification",
-                   DELETION="VnfIdentifierDeletionNotification")
+NOTIFY_TYPE = enum(
+    lCM_OP_OCC="VnfLcmOperationOccurrenceNotification",
+    CREATION="VnfIdentifierCreationNotification",
+    DELETION="VnfIdentifierDeletionNotification"
+)
 
 
 class NotificationsUtil(object):
-    def __init__(self):
-        pass
-
     def send_notification(self, notification):
         logger.info("Send Notifications to the callbackUri")
         filters = {
             "operationState": "operation_states",
             "operation": "operation_types"
         }
-        subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
+        subscriptions_filter = {v + "__contains": notification[k] for k, v in list(filters.items())}
 
         subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
         if not subscriptions.exists():
@@ -65,18 +65,20 @@ class NotificationsUtil(object):
                 try:
                     self.post_notification(callbackUri, auth_info, notification)
                 except Exception as e:
-                    logger.error("Failed to post notification: %s", e.message)
+                    logger.error("Failed to post notification: %s", e.args[0])
 
     def post_notification(self, callbackUri, auth_info, notification):
         params = auth_info.get("paramsBasic", {})
         username = params.get("userName")
         password = params.get("password")
         logger.info("Sending notification to %s", callbackUri)
-        resp = requests.post(callbackUri,
-                             data=notification,
-                             auth=HTTPBasicAuth(username, password))
+        resp = requests.post(
+            callbackUri,
+            data=notification,
+            auth=HTTPBasicAuth(username, password)
+        )
         if resp.status_code != status.HTTP_204_NO_CONTENT:
-            raise Exception("Notify %s failed: %s" % (callbackUri, resp.text))
+            logger.error("Notify %s failed: %s", callbackUri, resp.text)
 
 
 def set_affected_vnfcs(affected_vnfcs, nfinstid, changetype):
@@ -136,7 +138,7 @@ def set_ext_connectivity(ext_connectivity, nfinstid):
             },
             'cpInstanceId': port.portid  # TODO: port.cpinstanceid is not initiated when create port resource.
         })
-    for network_id, ext_link_ports in ext_connectivity_map.items():
+    for network_id, ext_link_ports in list(ext_connectivity_map.items()):
         networks = NetworkInstModel.objects.filter(networkid=network_id)
         net_name = networks[0].name if networks else network_id
         network_resource = {
@@ -171,10 +173,9 @@ def set_affected_vss(affected_vss, nfinstid, changetype):
 
 
 def get_notification_status(operation_state):
-    notification_status = const.LCM_NOTIFICATION_STATUS.START
-    if operation_state in const.RESULT_RANGE:
-        notification_status = const.LCM_NOTIFICATION_STATUS.RESULT
-    return notification_status
+    if operation_state == const.OPERATION_STATE_TYPE.STARTING:
+        return const.LCM_NOTIFICATION_STATUS.START
+    return const.LCM_NOTIFICATION_STATUS.RESULT
 
 
 def prepare_notification(nfinstid, jobid, operation, operation_state):
@@ -208,10 +209,12 @@ def prepare_notification(nfinstid, jobid, operation, operation_state):
 
 
 def prepare_notification_data(nfinstid, jobid, changetype, operation):
-    data = prepare_notification(nfinstid=nfinstid,
-                                jobid=jobid,
-                                operation=operation,
-                                operation_state=const.OPERATION_STATE_TYPE.COMPLETED)
+    data = prepare_notification(
+        nfinstid=nfinstid,
+        jobid=jobid,
+        operation=operation,
+        operation_state=const.OPERATION_STATE_TYPE.COMPLETED
+    )
 
     set_affected_vnfcs(data['affectedVnfcs'], nfinstid, changetype)
     set_affected_vls(data['affectedVirtualLinks'], nfinstid, changetype)