aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfujinhua <fu.jinhua@zte.com.cn>2019-06-27 09:26:59 +0800
committerfujinhua <fu.jinhua@zte.com.cn>2019-06-27 09:26:59 +0800
commit52538e341f82ffea2c077440200888f747a8b185 (patch)
treeec56c09c0f6bb25ab62527270cfdbd2e5f019f39
parentf6ab15b9e6959612bffb731161fc684536b43ed8 (diff)
Refactor codes for create subscription
Change-Id: I7e2242a13d201ce9170b7f8511c2b15b372d2853 Issue-ID: VFC-1431 Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
-rw-r--r--lcm/ns/biz/create_subscription.py70
1 files changed, 49 insertions, 21 deletions
diff --git a/lcm/ns/biz/create_subscription.py b/lcm/ns/biz/create_subscription.py
index b9ab6a3e..e1a44eee 100644
--- a/lcm/ns/biz/create_subscription.py
+++ b/lcm/ns/biz/create_subscription.py
@@ -13,18 +13,19 @@
# limitations under the License.
import ast
-from collections import Counter
import json
-from lcm.pub.database.models import SubscriptionModel
-from lcm.pub.exceptions import NSLCMException, SeeOtherException
-from lcm.pub.utils.values import ignore_case_get
-from lcm.ns import const
-from lcm.ns.enum import NOTIFICATION_TYPE, AUTH_TYPE
import logging
import requests
-from rest_framework import status
import uuid
+from collections import Counter
+from rest_framework import status
+
+from lcm.ns.enum import NOTIFICATION_TYPE, AUTH_TYPE
+from lcm.pub.database.models import SubscriptionModel
+from lcm.pub.exceptions import NSLCMException, SeeOtherException
+from lcm.pub.utils.values import ignore_case_get
+from lcm.ns import const
logger = logging.getLogger(__name__)
@@ -39,14 +40,16 @@ FILTER_TYPE = [
"lcm_opname_impacting_nscomponent",
"lcm_opoccstatus_impacting_nscomponent",
"notification_types",
- "operation_states"]
+ "operation_states"
+]
NS_FILTER_TYPE = [
"nsdIds",
"nsInstanceIds",
"vnfdIds",
"pnfdIds",
- "nsInstanceNames"]
+ "nsInstanceNames"
+]
class CreateSubscription:
@@ -60,16 +63,31 @@ class CreateSubscription:
self.operation_types = ignore_case_get(self.filter, "operationTypes", [])
self.operation_states = ignore_case_get(self.filter, "notificationStates", [])
self.ns_component_types = ignore_case_get(self.filter, "nsComponentTypes", [])
- self.lcm_opname_impacting_nscomponent = ignore_case_get(self.filter, "lcmOpNameImpactingNsComponent", [])
- self.lcm_opoccstatus_impacting_nscomponent = ignore_case_get(self.filter, "lcmOpOccStatusImpactingNsComponent", [])
- self.ns_filter = ignore_case_get(self.filter, "nsInstanceSubscriptionFilter", {})
+ self.lcm_opname_impacting_nscomponent = ignore_case_get(
+ self.filter,
+ "lcmOpNameImpactingNsComponent",
+ []
+ )
+ self.lcm_opoccstatus_impacting_nscomponent = ignore_case_get(
+ self.filter,
+ "lcmOpOccStatusImpactingNsComponent",
+ []
+ )
+ self.ns_filter = ignore_case_get(
+ self.filter,
+ "nsInstanceSubscriptionFilter",
+ {}
+ )
def check_callback_uri(self):
logger.debug("SubscribeNotification-post::> Sending GET request to %s" % self.callback_uri)
try:
response = requests.get(self.callback_uri, timeout=2)
if response.status_code != status.HTTP_204_NO_CONTENT:
- raise NSLCMException("callbackUri %s returns %s status code." % (self.callback_uri, response.status_code))
+ raise NSLCMException("callbackUri %s returns %s status code." % (
+ self.callback_uri,
+ response.status_code
+ ))
except Exception:
raise NSLCMException("callbackUri %s didn't return 204 status code." % self.callback_uri)
@@ -85,29 +103,39 @@ class CreateSubscription:
def check_filter_types(self):
logger.debug("SubscribeNotification--post::> Validating operationTypes and operationStates if exists")
- if self.operation_types and NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION not in self.notification_types:
+ occ_notification = NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION
+ if self.operation_types and occ_notification not in self.notification_types:
except_message = "If you are setting operationTypes, notificationTypes must be %s"
- raise NSLCMException(except_message % NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION)
- if self.operation_states and NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION not in self.notification_types:
+ raise NSLCMException(except_message % occ_notification)
+ if self.operation_states and occ_notification not in self.notification_types:
except_message = "If you are setting operationStates, notificationTypes must be %s"
- raise NSLCMException(except_message % NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION)
+ raise NSLCMException(except_message % occ_notification)
def check_valid_auth_info(self):
logger.debug("SubscribeNotification--post::> Validating Auth details if provided")
- if self.authentication.get("paramsBasic", {}) and AUTH_TYPE.BASIC not in self.authentication.get("authType"):
+ auth_type = self.authentication.get("authType")
+ params_basic = self.authentication.get("paramsBasic")
+ params_oauth2 = self.authentication.get("paramsOauth2ClientCredentials")
+ if params_basic and AUTH_TYPE.BASIC not in auth_type:
raise NSLCMException('Auth type should be ' + AUTH_TYPE.BASIC)
- if self.authentication.get("paramsOauth2ClientCredentials", {}) and AUTH_TYPE.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
+ if params_oauth2 and AUTH_TYPE.OAUTH2_CLIENT_CREDENTIALS not in auth_type:
raise NSLCMException('Auth type should be ' + AUTH_TYPE.OAUTH2_CLIENT_CREDENTIALS)
def check_filter_exists(self, sub):
# Check the notificationTypes, operationTypes, operationStates
for filter_type in FILTER_TYPE:
- if not is_filter_type_equal(getattr(self, filter_type), ast.literal_eval(getattr(sub, filter_type))):
+ 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 ns instance filters
ns_filter = json.loads(sub.ns_instance_filter)
for ns_filter_type in NS_FILTER_TYPE:
- if not is_filter_type_equal(self.ns_filter.get(ns_filter_type, []), ns_filter.get(ns_filter_type, [])):
+ if not is_filter_type_equal(
+ self.ns_filter.get(ns_filter_type, []),
+ ns_filter.get(ns_filter_type, [])
+ ):
return False
return True