Upgrade Optimizing SSL connection of calling lifecyclechangesnotification interface
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / create_subscription.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
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.nf import const
26 from lcm.pub.database.models import SubscriptionModel
27 from lcm.pub.exceptions import NFLCMException
28 from lcm.pub.exceptions import NFLCMExceptionSeeOther
29 from lcm.pub.utils.values import ignore_case_get
30 from lcm.pub.config.config import MSB_BASE_URL
31
32 logger = logging.getLogger(__name__)
33
34
35 def is_filter_type_equal(new_filter, existing_filter):
36     return Counter(new_filter) == Counter(existing_filter)
37
38
39 class CreateSubscription:
40     def __init__(self, data):
41         self.data = data
42         self.filter = ignore_case_get(self.data, "filter", {})
43         logger.debug("self.data:%s" % self.data)
44         logger.debug("self.filter:%s" % self.filter)
45         self.callback_uri = ignore_case_get(self.data, "callbackUri")
46         self.authentication = ignore_case_get(self.data, "authentication", {})
47         self.notification_types = ignore_case_get(self.filter, "notificationTypes", [])
48         self.operation_types = ignore_case_get(self.filter, "operationTypes", [])
49         self.operation_states = ignore_case_get(self.filter, "operationStates", [])
50         self.vnf_filter = \
51             ignore_case_get(self.filter, "vnfInstanceSubscriptionFilter", {})
52
53     def check_callbackuri_connection(self):
54         logger.debug("SubscribeNotification-post::> Sending GET request "
55                      "to %s" % self.callback_uri)
56         retry_count = 3
57         while retry_count > 0:
58             requests.packages.urllib3.disable_warnings()
59             response = requests.get(self.callback_uri, timeout=10, verify=False)
60             if response.status_code == status.HTTP_204_NO_CONTENT:
61                 return
62             logger.debug("callbackUri %s returns %s status code." % (self.callback_uri, response.status_code))
63             retry_count = - 1
64
65         raise NFLCMException("callbackUri %s didn't return 204 status." % self.callback_uri)
66
67     def do_biz(self):
68         self.subscription_id = str(uuid.uuid4())
69         self.check_callbackuri_connection()
70         self.check_valid_auth_info()
71         self.check_filter_types()
72         self.check_valid()
73         self.save_db()
74         subscription = SubscriptionModel.objects.get(subscription_id=self.subscription_id)
75         return subscription
76
77     def check_filter_types(self):
78         logger.debug("SubscribeNotification--post::> Validating "
79                      "operationTypes  and operationStates if exists")
80         if self.operation_types and \
81                 const.LCCNNOTIFICATION not in self.notification_types:
82             raise NFLCMException("If you are setting operationTypes,"
83                                  "then notificationTypes "
84                                  "must be " + const.LCCNNOTIFICATION)
85         if self.operation_states and \
86                 const.LCCNNOTIFICATION not in self.notification_types:
87             raise NFLCMException("If you are setting operationStates,"
88                                  "then notificationTypes "
89                                  "must be " + const.LCCNNOTIFICATION)
90
91     def check_valid_auth_info(self):
92         logger.debug("SubscribeNotification--post::> Validating Auth "
93                      "details if provided")
94         if self.authentication.get("paramsBasic", {}) and \
95                 const.BASIC not in self.authentication.get("authType"):
96             raise NFLCMException('Auth type should be ' + const.BASIC)
97         if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
98                 const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
99             raise NFLCMException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
100
101     def check_filter_exists(self, sub):
102         # Check the notificationTypes, operationTypes, operationStates
103         for filter_type in ["operation_types",
104                             "notification_types", "operation_states"]:
105             if not is_filter_type_equal(getattr(self, filter_type),
106                                         ast.literal_eval(getattr(sub, filter_type))):
107                 return False
108         # If all the above types are same then check vnf instance filters
109         nf_filter = json.loads(sub.vnf_instance_filter)
110         for vnf_filter_type in ["vnfdIds", "vnfInstanceIds",
111                                 "vnfInstanceNames"]:
112             if not is_filter_type_equal(self.vnf_filter.get(vnf_filter_type, []),
113                                         nf_filter.get(vnf_filter_type, [])):
114                 return False
115         return True
116
117     def check_valid(self):
118         logger.debug("SubscribeNotification--post::> Checking DB if "
119                      "callbackUri already exists")
120         subscriptions = SubscriptionModel.objects.filter(callback_uri=self.callback_uri)
121         if not subscriptions.exists():
122             return True
123         for subscription in subscriptions:
124             if self.check_filter_exists(subscription):
125                 links = json.loads(subscription.links)
126                 raise NFLCMExceptionSeeOther("%s/%s" % (MSB_BASE_URL, links["self"]["href"]))
127         return False
128
129     def save_db(self):
130         logger.debug("SubscribeNotification--post::> Saving the subscription "
131                      "%s to the database" % self.subscription_id)
132         links = {
133             "self": {
134                 "href": const.ROOT_URI + self.subscription_id
135             }
136         }
137         SubscriptionModel.objects.create(subscription_id=self.subscription_id,
138                                          callback_uri=self.callback_uri,
139                                          auth_info=json.dumps(self.authentication),
140                                          notification_types=json.dumps(self.notification_types),
141                                          operation_types=json.dumps(self.operation_types),
142                                          operation_states=json.dumps(self.operation_states),
143                                          vnf_instance_filter=json.dumps(self.vnf_filter),
144                                          links=json.dumps(links))
145         logger.debug('Create Subscription[%s] success', self.subscription_id)