summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormaopengzhang <zhang.maopeng1@zte.com.cn>2019-04-20 14:09:39 +0800
committermaopengzhang <zhang.maopeng1@zte.com.cn>2019-04-20 14:10:24 +0800
commita3f8eb7bcb0c1ba23774c722d6ae27219f3ef6fb (patch)
tree5e6f0050d8cfc07cbd8e34a41e7bd8969c5f729e
parent9ff63a7a44cba66bc059694eaa573243fd3e9b43 (diff)
refactor subscribtion biz code
refactor subscribtion biz code Change-Id: I1f8ab80cfcdc49ce3051eae55dbc3130dd5be81a Issue-ID: VFC-1241 Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
-rw-r--r--lcm/ns/biz/create_subscription.py151
-rw-r--r--lcm/ns/tests/test_subscribe_notification.py2
2 files changed, 68 insertions, 85 deletions
diff --git a/lcm/ns/biz/create_subscription.py b/lcm/ns/biz/create_subscription.py
index 06ec4b00..b9ab6a3e 100644
--- a/lcm/ns/biz/create_subscription.py
+++ b/lcm/ns/biz/create_subscription.py
@@ -13,20 +13,18 @@
# 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
-import uuid
-
-from collections import Counter
-
from rest_framework import status
+import uuid
-from lcm.ns import const
-from lcm.pub.database.models import SubscriptionModel
-from lcm.pub.exceptions import NSLCMException
-from lcm.pub.exceptions import SeeOtherException
-from lcm.pub.utils.values import ignore_case_get
logger = logging.getLogger(__name__)
@@ -35,6 +33,22 @@ def is_filter_type_equal(new_filter, existing_filter):
return Counter(new_filter) == Counter(existing_filter)
+FILTER_TYPE = [
+ "operation_types",
+ "ns_component_types",
+ "lcm_opname_impacting_nscomponent",
+ "lcm_opoccstatus_impacting_nscomponent",
+ "notification_types",
+ "operation_states"]
+
+NS_FILTER_TYPE = [
+ "nsdIds",
+ "nsInstanceIds",
+ "vnfdIds",
+ "pnfdIds",
+ "nsInstanceNames"]
+
+
class CreateSubscription:
def __init__(self, data):
@@ -42,88 +56,64 @@ class CreateSubscription:
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", [])
- 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", {})
-
- def check_callbackuri_connection(self):
- logger.debug("SubscribeNotification-post::> Sending GET request "
- "to %s" % self.callback_uri)
+ self.notification_types = ignore_case_get(self.filter, "notificationTypes", [])
+ 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", {})
+
+ 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)
+ raise NSLCMException("callbackUri %s didn't return 204 status code." % self.callback_uri)
def do_biz(self):
self.subscription_id = str(uuid.uuid4())
- # self.check_callbackuri_connection()
+ # self.check_callback_uri()
self.check_valid_auth_info()
self.check_filter_types()
self.check_valid()
self.save_db()
- subscription = SubscriptionModel.objects.get(
- subscription_id=self.subscription_id)
+ subscription = SubscriptionModel.objects.get(subscription_id=self.subscription_id)
return subscription
def check_filter_types(self):
- logger.debug("SubscribeNotification--post::> Validating "
- "operationTypes and operationStates if exists")
- if self.operation_types and \
- const.LCCNNOTIFICATION not in self.notification_types:
- raise NSLCMException("If you are setting operationTypes,"
- "then notificationTypes "
- "must be " + const.LCCNNOTIFICATION)
- if self.operation_states and \
- const.LCCNNOTIFICATION not in self.notification_types:
- raise NSLCMException("If you are setting operationStates,"
- "then notificationTypes "
- "must be " + const.LCCNNOTIFICATION)
+ 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:
+ 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:
+ except_message = "If you are setting operationStates, notificationTypes must be %s"
+ raise NSLCMException(except_message % NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION)
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"):
- raise NSLCMException('Auth type should be ' + const.BASIC)
- if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
- const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
- raise NSLCMException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
+ logger.debug("SubscribeNotification--post::> Validating Auth details if provided")
+ if self.authentication.get("paramsBasic", {}) and AUTH_TYPE.BASIC not in self.authentication.get("authType"):
+ 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"):
+ 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 ["operation_types", "ns_component_types", "lcm_opname_impacting_nscomponent",
- "lcm_opoccstatus_impacting_nscomponent", "notification_types",
- "operation_states"]:
- if not is_filter_type_equal(getattr(self, filter_type),
- ast.literal_eval(getattr(sub, filter_type))):
+ for filter_type in 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 ["nsdIds", "nsInstanceIds", "vnfdIds", "pnfdIds", "nsInstanceNames"]:
- if not is_filter_type_equal(self.ns_filter.get(ns_filter_type, []),
- ns_filter.get(ns_filter_type, [])):
+ 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, [])):
return False
return True
def check_valid(self):
logger.debug("SubscribeNotification--post::> Checking DB if callbackUri already exists")
- subscriptions = SubscriptionModel.objects.filter(
- callback_uri=self.callback_uri)
+ subscriptions = SubscriptionModel.objects.filter(callback_uri=self.callback_uri)
if not subscriptions.exists():
return True
for subscription in subscriptions:
@@ -132,29 +122,22 @@ class CreateSubscription:
return False
def save_db(self):
- logger.debug("SubscribeNotification--post::> Saving the subscription "
- "%s to the database" % self.subscription_id)
+ logger.debug("SubscribeNotification--post::> Saving the subscription(%s) to the database" % self.subscription_id)
links = {
"self": {
"href": const.SUBSCRIPTION_ROOT_URI % self.subscription_id
}
}
- SubscriptionModel.objects.create(subscription_id=self.subscription_id,
- callback_uri=self.callback_uri,
- auth_info=self.authentication,
- notification_types=json.dumps(
- self.notification_types),
- operation_types=json.dumps(
- self.operation_types),
- operation_states=json.dumps(
- self.operation_states),
- ns_instance_filter=json.dumps(
- self.ns_filter),
- ns_component_types=json.dumps(
- self.ns_component_types),
- lcm_opname_impacting_nscomponent=json.dumps(
- self.lcm_opname_impacting_nscomponent),
- lcm_opoccstatus_impacting_nscomponent=json.dumps(
- self.lcm_opoccstatus_impacting_nscomponent),
- links=json.dumps(links))
+ SubscriptionModel.objects.create(
+ subscription_id=self.subscription_id,
+ callback_uri=self.callback_uri,
+ auth_info=self.authentication,
+ notification_types=json.dumps(self.notification_types),
+ operation_types=json.dumps(self.operation_types),
+ operation_states=json.dumps(self.operation_states),
+ ns_instance_filter=json.dumps(self.ns_filter),
+ ns_component_types=json.dumps(self.ns_component_types),
+ lcm_opname_impacting_nscomponent=json.dumps(self.lcm_opname_impacting_nscomponent),
+ lcm_opoccstatus_impacting_nscomponent=json.dumps(self.lcm_opoccstatus_impacting_nscomponent),
+ links=json.dumps(links))
logger.debug('Create Subscription[%s] success', self.subscription_id)
diff --git a/lcm/ns/tests/test_subscribe_notification.py b/lcm/ns/tests/test_subscribe_notification.py
index 38354a25..9738bee8 100644
--- a/lcm/ns/tests/test_subscribe_notification.py
+++ b/lcm/ns/tests/test_subscribe_notification.py
@@ -121,7 +121,7 @@ class TestSubscription(TestCase):
mock_requests.get.return_value.status_code = 204
expected_data = {
'status': 500,
- 'detail': 'If you are setting operationTypes,then notificationTypes must be '
+ 'detail': 'If you are setting operationTypes, notificationTypes must be '
'NsLcmOperationOccurrenceNotification'
}
response = self.client.post("/api/nslcm/v1/subscriptions", data=dummy_subscription, format='json')