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
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:
39 def __init__(self, data):
41 self.filter = ignore_case_get(self.data, "filter", {})
42 self.callback_uri = ignore_case_get(self.data, "callbackUri")
43 self.authentication = ignore_case_get(self.data, "authentication", {})
44 self.notification_types = ignore_case_get(self.filter, "notificationTypes", [])
45 self.operation_types = ignore_case_get(self.filter, "operationTypes", [])
46 self.operation_states = ignore_case_get(self.filter, "operationStates", [])
48 ignore_case_get(self.filter, "vnfInstanceSubscriptionFilter", {})
50 def check_callbackuri_connection(self):
51 logger.debug("SubscribeNotification-post::> Sending GET request "
52 "to %s" % self.callback_uri)
54 response = requests.get(self.callback_uri, timeout=2)
55 if response.status_code != status.HTTP_204_NO_CONTENT:
56 raise NFLCMException("callbackUri %s returns %s status "
57 "code." % (self.callback_uri, response.status_code))
59 raise NFLCMException("callbackUri %s didn't return 204 status"
60 "code." % self.callback_uri)
63 self.subscription_id = str(uuid.uuid4())
64 # self.check_callbackuri_connection()
65 self.check_valid_auth_info()
66 self.check_filter_types()
69 subscription = SubscriptionModel.objects.get(subscription_id=self.subscription_id)
72 def check_filter_types(self):
73 logger.debug("SubscribeNotification--post::> Validating "
74 "operationTypes and operationStates if exists")
75 if self.operation_types and \
76 const.LCCNNOTIFICATION not in self.notification_types:
77 raise NFLCMException("If you are setting operationTypes,"
78 "then notificationTypes "
79 "must be " + const.LCCNNOTIFICATION)
80 if self.operation_states and \
81 const.LCCNNOTIFICATION not in self.notification_types:
82 raise NFLCMException("If you are setting operationStates,"
83 "then notificationTypes "
84 "must be " + const.LCCNNOTIFICATION)
86 def check_valid_auth_info(self):
87 logger.debug("SubscribeNotification--post::> Validating Auth "
88 "details if provided")
89 if self.authentication.get("paramsBasic", {}) and \
90 const.BASIC not in self.authentication.get("authType"):
91 raise NFLCMException('Auth type should be ' + const.BASIC)
92 if self.authentication.get("paramsOauth2ClientCredentials", {}) and \
93 const.OAUTH2_CLIENT_CREDENTIALS not in self.authentication.get("authType"):
94 raise NFLCMException('Auth type should be ' + const.OAUTH2_CLIENT_CREDENTIALS)
96 def check_filter_exists(self, sub):
97 # Check the notificationTypes, operationTypes, operationStates
98 for filter_type in ["operation_types",
99 "notification_types", "operation_states"]:
100 if not is_filter_type_equal(getattr(self, filter_type),
101 ast.literal_eval(getattr(sub, filter_type))):
103 # If all the above types are same then check vnf instance filters
104 nf_filter = json.loads(sub.vnf_instance_filter)
105 for vnf_filter_type in ["vnfdIds", "vnfInstanceIds",
107 if not is_filter_type_equal(self.vnf_filter.get(vnf_filter_type, []),
108 nf_filter.get(vnf_filter_type, [])):
112 def check_valid(self):
113 logger.debug("SubscribeNotification--post::> Checking DB if "
114 "callbackUri already exists")
115 subscriptions = SubscriptionModel.objects.filter(callback_uri=self.callback_uri)
116 if not subscriptions.exists():
118 for subscription in subscriptions:
119 if self.check_filter_exists(subscription):
120 raise NFLCMExceptionSeeOther("Already Subscription exists with the "
121 "same callbackUri and filter")
125 logger.debug("SubscribeNotification--post::> Saving the subscription "
126 "%s to the database" % self.subscription_id)
129 "href": const.ROOT_URI + self.subscription_id
132 SubscriptionModel.objects.create(subscription_id=self.subscription_id,
133 callback_uri=self.callback_uri,
134 auth_info=self.authentication,
135 notification_types=json.dumps(self.notification_types),
136 operation_types=json.dumps(self.operation_types),
137 operation_states=json.dumps(self.operation_states),
138 vnf_instance_filter=json.dumps(self.vnf_filter),
139 links=json.dumps(links))
140 logger.debug('Create Subscription[%s] success', self.subscription_id)