Add log and comment
[modeling/etsicatalog.git] / catalog / packages / biz / vnf_pkg_subscription.py
index 29ef92e..9876961 100644 (file)
@@ -16,19 +16,19 @@ import ast
 import json
 import logging
 import os
-import requests
 import uuid
-
 from collections import Counter
+
+import requests
+from requests.auth import HTTPBasicAuth
 from rest_framework import status
 
 from catalog.packages import const
 from catalog.pub.database.models import VnfPkgSubscriptionModel
-from catalog.pub.exceptions import VnfPkgSubscriptionException,\
+from catalog.pub.exceptions import VnfPkgSubscriptionException, \
     VnfPkgDuplicateSubscriptionException, SubscriptionDoesNotExistsException
 from catalog.pub.utils.values import ignore_case_get
 
-
 logger = logging.getLogger(__name__)
 
 ROOT_FILTERS = {
@@ -45,10 +45,12 @@ def is_filter_type_equal(new_filter, existing_filter):
 
 
 class CreateSubscription(object):
-
+    """
+    Create subscription info
+    """
     def __init__(self, data):
         self.data = data
-        self.filter = ignore_case_get(self.data, "filters", {})
+        self.filter = ignore_case_get(self.data, "filter", {})
         self.callback_uri = ignore_case_get(self.data, "callbackUri")
         self.authentication = ignore_case_get(self.data, "authentication", {})
         self.notification_types = ignore_case_get(self.filter, "notificationTypes", [])
@@ -57,13 +59,31 @@ class CreateSubscription(object):
         self.vnfd_id = ignore_case_get(self.filter, "vnfdId", [])
         self.vnf_pkg_id = ignore_case_get(self.filter, "vnfPkgId", [])
         self.vnf_products_from_provider = \
-            ignore_case_get(self.filter, "vnfProductsFromProviders", {})
+            ignore_case_get(self.filter, "vnfProductsFromProviders", [])
 
     def check_callbackuri_connection(self):
+        """
+        Check if the callback uri can access
+        :return:
+        """
         logger.debug("SubscribeNotification-post::> Sending GET request "
                      "to %s" % self.callback_uri)
         try:
-            response = requests.get(self.callback_uri, timeout=2)
+            if self.authentication:
+                if const.BASIC in self.authentication.get("authType", ''):
+                    params = self.authentication.get("paramsBasic", {})
+                    username = params.get("userName")
+                    password = params.get("password")
+                    response = requests.get(self.callback_uri, auth=HTTPBasicAuth(username, password), timeout=2,
+                                            verify=False)
+                elif const.OAUTH2_CLIENT_CREDENTIALS in self.authentication.get("authType", ''):
+                    # todo
+                    pass
+                else:
+                    # todo
+                    pass
+            else:
+                response = requests.get(self.callback_uri, timeout=2, verify=False)
             if response.status_code != status.HTTP_204_NO_CONTENT:
                 raise VnfPkgSubscriptionException(
                     "callbackUri %s returns %s status code." % (
@@ -77,9 +97,13 @@ class CreateSubscription(object):
             )
 
     def do_biz(self):
+        """
+        Do business
+        :return:
+        """
         self.subscription_id = str(uuid.uuid4())
-        self.check_callbackuri_connection()
         self.check_valid_auth_info()
+        self.check_callbackuri_connection()
         self.check_valid()
         self.save_db()
         subscription = VnfPkgSubscriptionModel.objects.get(
@@ -89,13 +113,16 @@ class CreateSubscription(object):
             return subscription.toDict()
 
     def check_valid_auth_info(self):
+        """
+        Check if the Auth info is valid
+        :return:
+        """
         logger.debug("SubscribeNotification--post::> Validating Auth "
                      "details if provided")
-        if self.authentication.get("paramsBasic", {}) and \
-                const.BASIC not in self.authentication.get("authType"):
+        if self.authentication.get("paramsBasic", {}) and const.BASIC not in self.authentication.get("authType"):
             raise VnfPkgSubscriptionException('Auth type should be ' + const.BASIC)
-        if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
-                const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
+        if self.authentication.get("paramsOauth2ClientCredentials", {}) \
+                and const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
             raise VnfPkgSubscriptionException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
 
     def check_filter_exists(self, sub):
@@ -104,7 +131,7 @@ class CreateSubscription(object):
             if not is_filter_type_equal(getattr(self, filter_type),
                                         ast.literal_eval(getattr(sub, filter_type))):
                 return False
-        # If all the above types are same then check id filters
+        # If all the above types are same then check id filter
         for id_filter in ["vnfd_id", "vnf_pkg_id"]:
             if not is_filter_type_equal(getattr(self, id_filter),
                                         ast.literal_eval(getattr(sub, id_filter))):
@@ -112,19 +139,24 @@ class CreateSubscription(object):
         return True
 
     def check_valid(self):
+        links = ""
         logger.debug("SubscribeNotification--post::> Checking DB if "
                      "callbackUri already exists")
         subscriptions = VnfPkgSubscriptionModel.objects.filter(callback_uri=self.callback_uri)
-        if not subscriptions.exists():
-            return True
         for subscription in subscriptions:
             if self.check_filter_exists(subscription):
+                links = json.loads(subscription.links)
+                logger.error("Subscriptions has already exists with the same callbackUri and filter:%s" % links)
                 raise VnfPkgDuplicateSubscriptionException(
-                    "Already Subscription (%s) exists with the "
-                    "same callbackUri and filter" % subscription.subscription_id)
+                    "/%s" % (links["self"]["href"]))
+
         return True
 
     def save_db(self):
+        """
+        Save the subscription info to DB
+        :return:
+        """
         logger.debug("SubscribeNotification--post::> Saving the subscription "
                      "%s to the database" % self.subscription_id)
         links = {
@@ -147,15 +179,22 @@ class CreateSubscription(object):
 
 
 class QuerySubscription(object):
-
+    """
+    The class for query subscription
+    """
     def query_multi_subscriptions(self, params):
+        """
+        Query subscriptions
+        :param params:
+        :return:
+        """
         query_data = {}
         logger.debug("QuerySubscription--get--multi--subscriptions--biz::> Check "
-                     "for filters in query params %s" % params)
+                     "for filter in query params %s" % params)
         for query, value in list(params.items()):
             if query in ROOT_FILTERS:
                 query_data[ROOT_FILTERS[query] + '__icontains'] = value
-        # Query the database with filters if the request has fields in request params, else fetch all records
+        # Query the database with filter if the request has fields in request params, else fetch all records
         if query_data:
             subscriptions = VnfPkgSubscriptionModel.objects.filter(**query_data)
         else:
@@ -165,6 +204,11 @@ class QuerySubscription(object):
         return [subscription.toDict() for subscription in subscriptions]
 
     def query_single_subscription(self, subscription_id):
+        """
+        Query subscription by id
+        :param subscription_id:
+        :return:
+        """
         logger.debug("QuerySingleSubscriptions--get--single--subscription--biz::> "
                      "ID: %s" % subscription_id)
 
@@ -172,12 +216,14 @@ class QuerySubscription(object):
             subscription_id=subscription_id)
         if not subscription.exists():
             raise SubscriptionDoesNotExistsException("Subscription with ID: %s "
-                                                     "does not exists" % subscription_id)
+                                                     "does not exist" % subscription_id)
         return subscription[0].toDict()
 
 
 class TerminateSubscription(object):
-
+    """
+    The class to terminate the subscription
+    """
     def terminate(self, subscription_id):
         logger.debug("TerminateSubscriptions--delete--biz::> "
                      "ID: %s" % subscription_id)
@@ -186,5 +232,5 @@ class TerminateSubscription(object):
             subscription_id=subscription_id)
         if not subscription.exists():
             raise SubscriptionDoesNotExistsException("Subscription with ID: %s "
-                                                     "does not exists" % subscription_id)
+                                                     "does not exist" % subscription_id)
         subscription[0].delete()