Refactor codes for subscription
[vfc/nfvo/lcm.git] / lcm / ns / biz / create_subscription.py
1 # Copyright (c) 2019, ZTE Corporation.
2
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
6
7 # http://www.apache.org/licenses/LICENSE-2.0
8
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.
14
15 import ast
16 import json
17 import logging
18 import requests
19 import uuid
20
21 from collections import Counter
22
23 from rest_framework import status
24
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
30
31 logger = logging.getLogger(__name__)
32
33
34 def is_filter_type_equal(new_filter, existing_filter):
35     return Counter(new_filter) == Counter(existing_filter)
36
37
38 class CreateSubscription:
39
40     def __init__(self, data):
41         self.data = 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", {})
59
60     def check_callbackuri_connection(self):
61         logger.debug("SubscribeNotification-post::> Sending GET request "
62                      "to %s" % self.callback_uri)
63         try:
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))
68         except Exception:
69             raise NSLCMException("callbackUri %s didn't return 204 status"
70                                  "code." % self.callback_uri)
71
72     def do_biz(self):
73         self.subscription_id = str(uuid.uuid4())
74         # self.check_callbackuri_connection()
75         self.check_valid_auth_info()
76         self.check_filter_types()
77         self.check_valid()
78         self.save_db()
79         subscription = SubscriptionModel.objects.get(
80             subscription_id=self.subscription_id)
81         return subscription
82
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)
96
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)
106
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",
111                             "operation_states"]:
112             if not is_filter_type_equal(getattr(self, filter_type),
113                                         ast.literal_eval(getattr(sub, filter_type))):
114                 return False
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, [])):
120                 return False
121         return True
122
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():
128             return True
129         for subscription in subscriptions:
130             if self.check_filter_exists(subscription):
131                 raise SeeOtherException("Already Subscription exists with the same callbackUri and filter")
132         return False
133
134     def save_db(self):
135         logger.debug("SubscribeNotification--post::> Saving the subscription "
136                      "%s to the database" % self.subscription_id)
137         links = {
138             "self": {
139                 "href": const.SUBSCRIPTION_ROOT_URI % self.subscription_id
140             }
141         }
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(
152                                              self.ns_filter),
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)