1 # Copyright (C) 2018 Verizon. All Rights Reserved
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.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_SERVICE_IP, MSB_SERVICE_PORT
32 logger = logging.getLogger(__name__)
35 def is_filter_type_equal(new_filter, existing_filter):
36 return Counter(new_filter) == Counter(existing_filter)
39 class CreateSubscription:
40 def __init__(self, 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", [])
51 ignore_case_get(self.filter, "vnfInstanceSubscriptionFilter", {})
53 def check_callbackuri_connection(self):
54 logger.debug("SubscribeNotification-post::> Sending GET request "
55 "to %s" % self.callback_uri)
57 response = requests.get(self.callback_uri, timeout=2)
58 if response.status_code != status.HTTP_204_NO_CONTENT:
59 raise NFLCMException("callbackUri %s returns %s status "
60 "code." % (self.callback_uri, response.status_code))
62 raise NFLCMException("callbackUri %s didn't return 204 status"
63 "code." % self.callback_uri)
66 self.subscription_id = str(uuid.uuid4())
67 self.check_callbackuri_connection()
68 self.check_valid_auth_info()
69 self.check_filter_types()
72 subscription = SubscriptionModel.objects.get(subscription_id=self.subscription_id)
75 def check_filter_types(self):
76 logger.debug("SubscribeNotification--post::> Validating "
77 "operationTypes and operationStates if exists")
78 if self.operation_types and \
79 const.LCCNNOTIFICATION not in self.notification_types:
80 raise NFLCMException("If you are setting operationTypes,"
81 "then notificationTypes "
82 "must be " + const.LCCNNOTIFICATION)
83 if self.operation_states and \
84 const.LCCNNOTIFICATION not in self.notification_types:
85 raise NFLCMException("If you are setting operationStates,"
86 "then notificationTypes "
87 "must be " + const.LCCNNOTIFICATION)
89 def check_valid_auth_info(self):
90 logger.debug("SubscribeNotification--post::> Validating Auth "
91 "details if provided")
92 if self.authentication.get("paramsBasic", {}) and \
93 const.BASIC not in self.authentication.get("authType"):
94 raise NFLCMException('Auth type should be ' + const.BASIC)
95 if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
96 const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
97 raise NFLCMException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
99 def check_filter_exists(self, sub):
100 # Check the notificationTypes, operationTypes, operationStates
101 for filter_type in ["operation_types",
102 "notification_types", "operation_states"]:
103 if not is_filter_type_equal(getattr(self, filter_type),
104 ast.literal_eval(getattr(sub, filter_type))):
106 # If all the above types are same then check vnf instance filters
107 nf_filter = json.loads(sub.vnf_instance_filter)
108 for vnf_filter_type in ["vnfdIds", "vnfInstanceIds",
110 if not is_filter_type_equal(self.vnf_filter.get(vnf_filter_type, []),
111 nf_filter.get(vnf_filter_type, [])):
115 def check_valid(self):
116 logger.debug("SubscribeNotification--post::> Checking DB if "
117 "callbackUri already exists")
118 subscriptions = SubscriptionModel.objects.filter(callback_uri=self.callback_uri)
119 if not subscriptions.exists():
121 for subscription in subscriptions:
122 if self.check_filter_exists(subscription):
123 links = json.loads(subscription.links)
124 raise NFLCMExceptionSeeOther("http://%s:%s/%s" % (MSB_SERVICE_IP, MSB_SERVICE_PORT, links["self"]["href"]))
128 logger.debug("SubscribeNotification--post::> Saving the subscription "
129 "%s to the database" % self.subscription_id)
132 "href": const.ROOT_URI + self.subscription_id
135 SubscriptionModel.objects.create(subscription_id=self.subscription_id,
136 callback_uri=self.callback_uri,
137 auth_info=json.dumps(self.authentication),
138 notification_types=json.dumps(self.notification_types),
139 operation_types=json.dumps(self.operation_types),
140 operation_states=json.dumps(self.operation_states),
141 vnf_instance_filter=json.dumps(self.vnf_filter),
142 links=json.dumps(links))
143 logger.debug('Create Subscription[%s] success', self.subscription_id)