X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog%2Fpackages%2Fbiz%2Fvnf_pkg_subscription.py;h=fef90b4581435774baea8794eb4cfd05984259df;hb=ac2f763bcf8ed41d9f2867a33b29860318854502;hp=c457bfeb1fe9f1cb3546cc7d0416ace15b328e3b;hpb=36fa4d4ccf968236872e6d86c0a279ff0d9eb552;p=modeling%2Fetsicatalog.git diff --git a/catalog/packages/biz/vnf_pkg_subscription.py b/catalog/packages/biz/vnf_pkg_subscription.py index c457bfe..fef90b4 100644 --- a/catalog/packages/biz/vnf_pkg_subscription.py +++ b/catalog/packages/biz/vnf_pkg_subscription.py @@ -21,12 +21,13 @@ import uuid from collections import Counter from rest_framework import status - +from requests.auth import HTTPBasicAuth from catalog.packages import const from catalog.pub.database.models import VnfPkgSubscriptionModel from catalog.pub.exceptions import VnfPkgSubscriptionException, \ VnfPkgDuplicateSubscriptionException, SubscriptionDoesNotExistsException from catalog.pub.utils.values import ignore_case_get +from catalog.pub.config.config import MSB_SERVICE_IP, MSB_SERVICE_PORT logger = logging.getLogger(__name__) @@ -44,7 +45,6 @@ def is_filter_type_equal(new_filter, existing_filter): class CreateSubscription(object): - def __init__(self, data): self.data = data self.filter = ignore_case_get(self.data, "filter", {}) @@ -62,7 +62,21 @@ class CreateSubscription(object): 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,8 +91,8 @@ class CreateSubscription(object): def do_biz(self): 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( @@ -90,11 +104,10 @@ class CreateSubscription(object): def check_valid_auth_info(self): 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): @@ -111,16 +124,17 @@ 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) + "https://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, links["self"]["href"])) + return True def save_db(self): @@ -146,7 +160,6 @@ class CreateSubscription(object): class QuerySubscription(object): - def query_multi_subscriptions(self, params): query_data = {} logger.debug("QuerySubscription--get--multi--subscriptions--biz::> Check " @@ -176,7 +189,6 @@ class QuerySubscription(object): class TerminateSubscription(object): - def terminate(self, subscription_id): logger.debug("TerminateSubscriptions--delete--biz::> " "ID: %s" % subscription_id)