1 # Copyright (c) 2019, ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 from collections import Counter
23 from rest_framework import status
25 from lcm.ns import const
26 from lcm.pub.database.models import SubscriptionModel
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.exceptions import SeeOtherException
29 from lcm.pub.utils.values import ignore_case_get
31 logger = logging.getLogger(__name__)
34 def is_filter_type_equal(new_filter, existing_filter):
35 return Counter(new_filter) == Counter(existing_filter)
38 class CreateSubscription:
40 def __init__(self, data):
42 self.filter = ignore_case_get(self.data, "filter", {})
43 self.callback_uri = ignore_case_get(self.data, "callbackUri")
44 self.authentication = ignore_case_get(self.data, "authentication", {})
45 self.notification_types = ignore_case_get(
46 self.filter, "notificationTypes", [])
47 self.operation_types = ignore_case_get(
48 self.filter, "operationTypes", [])
49 self.operation_states = ignore_case_get(
50 self.filter, "notificationStates", [])
51 self.ns_component_types = ignore_case_get(
52 self.filter, "nsComponentTypes", [])
53 self.lcm_opname_impacting_nscomponent = ignore_case_get(
54 self.filter, "lcmOpNameImpactingNsComponent", [])
55 self.lcm_opoccstatus_impacting_nscomponent = ignore_case_get(
56 self.filter, "lcmOpOccStatusImpactingNsComponent", [])
57 self.ns_filter = ignore_case_get(
58 self.filter, "nsInstanceSubscriptionFilter", {})
60 def check_callbackuri_connection(self):
61 logger.debug("SubscribeNotification-post::> Sending GET request "
62 "to %s" % self.callback_uri)
64 response = requests.get(self.callback_uri, timeout=2)
65 if response.status_code != status.HTTP_204_NO_CONTENT:
66 raise NSLCMException("callbackUri %s returns %s status "
67 "code." % (self.callback_uri, response.status_code))
69 raise NSLCMException("callbackUri %s didn't return 204 status"
70 "code." % self.callback_uri)
73 self.subscription_id = str(uuid.uuid4())
74 # self.check_callbackuri_connection()
75 self.check_valid_auth_info()
76 self.check_filter_types()
79 subscription = SubscriptionModel.objects.get(
80 subscription_id=self.subscription_id)
83 def check_filter_types(self):
84 logger.debug("SubscribeNotification--post::> Validating "
85 "operationTypes and operationStates if exists")
86 if self.operation_types and \
87 const.LCCNNOTIFICATION not in self.notification_types:
88 raise NSLCMException("If you are setting operationTypes,"
89 "then notificationTypes "
90 "must be " + const.LCCNNOTIFICATION)
91 if self.operation_states and \
92 const.LCCNNOTIFICATION not in self.notification_types:
93 raise NSLCMException("If you are setting operationStates,"
94 "then notificationTypes "
95 "must be " + const.LCCNNOTIFICATION)
97 def check_valid_auth_info(self):
98 logger.debug("SubscribeNotification--post::> Validating Auth "
99 "details if provided")
100 if self.authentication.get("paramsBasic", {}) and \
101 const.BASIC not in self.authentication.get("authType"):
102 raise NSLCMException('Auth type should be ' + const.BASIC)
103 if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
104 const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
105 raise NSLCMException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
107 def check_filter_exists(self, sub):
108 # Check the notificationTypes, operationTypes, operationStates
109 for filter_type in ["operation_types", "ns_component_types", "lcm_opname_impacting_nscomponent",
110 "lcm_opoccstatus_impacting_nscomponent", "notification_types",
112 if not is_filter_type_equal(getattr(self, filter_type),
113 ast.literal_eval(getattr(sub, filter_type))):
115 # If all the above types are same then check ns instance filters
116 ns_filter = json.loads(sub.ns_instance_filter)
117 for ns_filter_type in ["nsdIds", "nsInstanceIds", "vnfdIds", "pnfdIds", "nsInstanceNames"]:
118 if not is_filter_type_equal(self.ns_filter.get(ns_filter_type, []),
119 ns_filter.get(ns_filter_type, [])):
123 def check_valid(self):
124 logger.debug("SubscribeNotification--post::> Checking DB if callbackUri already exists")
125 subscriptions = SubscriptionModel.objects.filter(
126 callback_uri=self.callback_uri)
127 if not subscriptions.exists():
129 for subscription in subscriptions:
130 if self.check_filter_exists(subscription):
131 raise SeeOtherException("Already Subscription exists with the same callbackUri and filter")
135 logger.debug("SubscribeNotification--post::> Saving the subscription "
136 "%s to the database" % self.subscription_id)
139 "href": const.SUBSCRIPTION_ROOT_URI % self.subscription_id
142 SubscriptionModel.objects.create(subscription_id=self.subscription_id,
143 callback_uri=self.callback_uri,
144 auth_info=self.authentication,
145 notification_types=json.dumps(
146 self.notification_types),
147 operation_types=json.dumps(
148 self.operation_types),
149 operation_states=json.dumps(
150 self.operation_states),
151 ns_instance_filter=json.dumps(
153 ns_component_types=json.dumps(
154 self.ns_component_types),
155 lcm_opname_impacting_nscomponent=json.dumps(
156 self.lcm_opname_impacting_nscomponent),
157 lcm_opoccstatus_impacting_nscomponent=json.dumps(
158 self.lcm_opoccstatus_impacting_nscomponent),
159 links=json.dumps(links))
160 logger.debug('Create Subscription[%s] success', self.subscription_id)