1 # Copyright 2020 K Sai Kiran (Tata Elxsi)
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 __author__
= "K Sai Kiran <saikiran.k@tataelxsi.co.in>"
17 __date__
= "$28-Apr-2020 23:59:59$"
21 from http
import HTTPStatus
25 from uuid
import uuid4
28 class NotificationException(Exception):
30 Notification Exception
33 def __init__(self
, message
: str, http_code
: int = HTTPStatus
.BAD_REQUEST
) -> None:
35 Constructor of notification exception
36 :param message: String text containing exception details.
37 :param http_code: HTTP status code of exception.
39 self
.http_code
= http_code
40 Exception.__init
__(self
, message
)
43 class NotificationBase
:
44 response_models
= None
45 # Common HTTP payload header for all notifications.
46 payload_header
= {"Content-Type": "application/json", "Accept": "application/json"}
48 def __init__(self
, db
) -> None:
50 Constructor of NotificationBase class.
51 :param db: Database handler.
54 self
.logger
= logging
.getLogger("nbi.notifications")
55 self
.subscriber_collection
= None
57 def get_models(self
) -> dict:
59 Returns the SOL005 model of notification class
61 :return: dict of SOL005 data model
63 return NotificationBase
.response_models
65 def get_subscribers(self
, **kwargs
) -> NotificationException
:
67 Method should be implemented by all notification subclasses
68 :param kwargs: any keyword arguments needed for db query.
69 :return: List of subscribers
71 raise NotificationException(
72 "Method get_subscribers() is not implemented",
73 http_code
=HTTPStatus
.NOT_IMPLEMENTED
,
77 def _get_basic_auth(username
: str, password
: str) -> tuple:
78 return aiohttp
.BasicAuth(username
, password
)
80 def _decrypt_password(
81 self
, hashed
: str, salt
: str, schema_version
: str = "1.1"
83 return self
.db
.decrypt(hashed
, schema_version
, salt
=salt
)
85 def get_payload(self
, meta_notification
: dict) -> dict:
87 Generates SOL005 compliant payload structure and returns them in dictionary.
88 :param meta_notification: notification meta data which needs to be formatted as SOL005 compliant
89 :return: A dictionary which is SOL005 compliant.
91 model_name
= meta_notification
["notificationType"]
92 response_models
= self
.get_models()
93 if not response_models
or not response_models
.get(model_name
):
94 raise NotificationException(
95 "Response model {} is not defined.".format(model_name
),
96 HTTPStatus
.NOT_IMPLEMENTED
,
98 model_keys
= response_models
[model_name
]
99 payload
= dict.fromkeys(model_keys
, "N/A")
100 notification_keys
= set(meta_notification
.keys())
101 for model_key
in model_keys
.intersection(notification_keys
):
102 payload
[model_key
] = meta_notification
[model_key
]
104 "Payload generated for subscriber: {} for {}".format(
105 payload
["subscriptionId"], payload
["notificationType"]
110 async def send_notifications(
115 Generate tasks for all notification for an event.
116 :param subscribers: A list of subscribers who want to be notified for event.
119 for subscriber
in subscribers
:
120 # Notify without auth
121 if not subscriber
.get("authentication"):
122 notifications
.append(
124 "headers": self
.payload_header
,
125 "payload": self
.get_payload(subscriber
),
126 "CallbackUri": subscriber
["CallbackUri"],
129 elif subscriber
["authentication"]["authType"] == "basic":
130 salt
= subscriber
["subscriptionId"]
131 hashed_password
= subscriber
["authentication"]["paramsBasic"][
134 password
= self
._decrypt
_password
(hashed_password
, salt
)
135 auth_basic
= self
._get
_basic
_auth
(
136 subscriber
["authentication"]["paramsBasic"]["userName"], password
138 notifications
.append(
140 "headers": self
.payload_header
,
141 "payload": self
.get_payload(subscriber
),
142 "auth_basic": auth_basic
,
143 "CallbackUri": subscriber
["CallbackUri"],
146 # TODO add support for AuthType OAuth and TLS after support is added in subscription.
149 "Subscriber {} can not be notified. {} notification auth type is not implemented".format(
150 subscriber
["subscriptionId"],
151 subscriber
["authentication"]["authType"],
157 async with aiohttp
.ClientSession() as session
:
158 for notification
in notifications
:
160 asyncio
.ensure_future(
161 self
.send_notification(session
, notification
),
164 await asyncio
.gather(*tasks
)
166 async def send_notification(
168 session
: aiohttp
.ClientSession
,
170 retry_count
: int = 5,
171 timeout
: float = 5.0,
174 Performs HTTP Post request to notify subscriber. In case if for any reason notification is not sent successfully
175 after maximum number of reties, then notification is dropped.
176 :param session: An aiohttp client session object to maintain http session.
177 :param notification: A dictionary containing all necessary data to make POST request.
178 :param retry_count: An integer specifying the maximum number of reties for a notification.
179 :param timeout: A float representing client timeout of each HTTP request.
182 while retry_count
> 0:
184 async with session
.post(
185 url
=notification
["CallbackUri"],
186 headers
=notification
["headers"],
187 auth
=notification
.get("auth_basic", None),
188 data
=json
.dumps(notification
["payload"]),
191 # self.logger.debug("Notification response: {}".format(resp.status))
192 if resp
.status
== HTTPStatus
.NO_CONTENT
:
194 "Notification sent successfully to subscriber {}".format(
195 notification
["payload"]["subscriptionId"]
199 error_text
= "Erroneous response code: {}, ".format(resp
.status
)
200 error_text
+= await resp
.text()
201 raise NotificationException(error_text
)
203 except Exception as e
:
204 error_text
= type(e
).__name
__ + ": " + str(e
)
206 "Unable to send notification to subscriber {}. Details: {}".format(
207 notification
["payload"]["subscriptionId"], error_text
211 "error": type(e
).__name
__,
212 "error_text": str(e
),
213 "timestamp": time
.time(),
215 if "error_details" in notification
["payload"].keys():
216 notification
["payload"]["error_details"].append(error_detail
)
218 notification
["payload"]["error_details"] = [error_detail
]
222 "Retry Notification for subscriber: {} after backoff delay: {} seconds.".format(
223 notification
["payload"]["subscriptionId"], backoff_delay
226 await asyncio
.sleep(backoff_delay
)
227 # Dropping notification
229 "Notification {} sent failed to subscriber:{}.".format(
230 notification
["payload"]["notificationType"],
231 notification
["payload"]["subscriptionId"],
237 class NsLcmNotification(NotificationBase
):
238 # maps kafka commands of completed operations to the original operation type
239 completed_operation_map
= {
240 "INSTANTIATED": "INSTANTIATE",
242 "TERMINATED": "TERMINATE",
246 # SOL005 response model for nslcm notifications
248 "NsLcmOperationOccurrenceNotification": {
256 "notificationStatus",
258 "isAutomaticInvocation",
267 "NsIdentifierCreationNotification": {
274 "NsIdentifierDeletionNotification": {
281 "NsChangeNotification": {
285 "lcmOpOccIdImpactngNsComponent",
286 "lcmOpNameImpactingNsComponent",
287 "lcmOpOccStatusImpactingNsComponent",
296 def __init__(self
, db
) -> None:
298 Constructor of NsLcmNotification class.
299 :param db: Database handler.
302 self
.subscriber_collection
= "mapped_subscriptions"
304 def get_models(self
) -> dict:
306 Returns the SOL005 model of notification class
308 :return: dict of SOL005 data model
310 return NsLcmNotification
.response_models
313 def _format_nslcm_subscribers(subscribers
: list, event_details
: dict) -> list:
315 Formats the raw event details from kakfa message and subscriber details.
316 :param subscribers: A list of subscribers whom the event needs to be notified.
317 :param event_details: A dict containing all meta data of event.
320 notification_id
= str(uuid4())
321 event_timestamp
= event_details
["params"]["startTime"]
322 resource_links
= event_details
["params"]["links"]
323 event_operation
= event_details
["command"]
324 for key
in ["_admin", "_id", "id", "links"]:
325 event_details
["params"].pop(key
, None)
326 for subscriber
in subscribers
:
327 subscriber
["id"] = notification_id
328 subscriber
["timestamp"] = event_timestamp
329 subscriber
["_links"] = resource_links
330 subscriber
["subscriptionId"] = subscriber
["reference"]
331 subscriber
["operation"] = event_operation
332 del subscriber
["reference"]
333 del subscriber
["_id"]
334 subscriber
.update(event_details
["params"])
346 Queries database and returns list of subscribers.
347 :param nsd_id: NSD id of an NS whose lifecycle has changed. (scaled, terminated. etc)
348 :param ns_instance_id: NS instance id an NS whose lifecycle has changed.
349 :param command: the command for event.
350 :param op_state: the operation state of NS.
351 :param event_details: dict containing raw data of event occured.
352 :return: List of interested subscribers for occurred event.
354 notification_type
= [
355 "NsLcmOperationOccurrenceNotification",
356 "NsChangeNotification",
357 "NsIdentifierCreationNotification",
358 "NsIdentifierDeletionNotification",
361 "identifier": [nsd_id
, ns_instance_id
],
362 "operationStates": ["ANY"],
363 "operationTypes": ["ANY"],
364 "notificationType": notification_type
,
367 filter_q
["operationStates"].append(op_state
)
369 op_type
= self
.completed_operation_map
.get(command
, command
)
370 filter_q
["operationTypes"].append(op_type
)
371 # self.logger.debug("Db query is: {}".format(filter_q))
374 subscribers
= self
.db
.get_list(self
.subscriber_collection
, filter_q
)
375 subscribers
= self
._format
_nslcm
_subscribers
(subscribers
, event_details
)
376 except Exception as e
:
377 error_text
= type(e
).__name
__ + ": " + str(e
)
378 self
.logger
.debug("Error getting nslcm subscribers: {}".format(error_text
))
383 class VnfLcmNotification(NotificationBase
):
384 # SOL003 response model for vnflcm notifications
386 "VnfLcmOperationOccurrenceNotification": {
391 "notificationStatus",
395 "isAutomaticInvocation",
398 "affectedVirtualLinks",
399 "affectedExtLinkPorts",
400 "affectedVirtualStorages",
402 "changedExtConnectivity",
403 "modificationsTriggeredByVnfPkgChange",
407 "VnfIdentifierCreationNotification": {
415 "VnfIdentifierDeletionNotification": {
425 def __init__(self
, db
) -> None:
427 Constructor of VnfLcmNotification class.
428 :param db: Database handler.
431 self
.subscriber_collection
= "mapped_subscriptions"
433 def get_models(self
) -> dict:
435 Returns the SOL003 model of notification class
437 :return: dict of SOL003 data model
439 return self
.response_models
441 def _format_vnflcm_subscribers(
442 self
, subscribers
: list, event_details
: dict
445 Formats the raw event details from kafka message and subscriber details.
446 :param subscribers: A list of subscribers whom the event needs to be notified.
447 :param event_details: A dict containing all meta data of event.
450 notification_id
= str(uuid4())
451 event_timestamp
= time
.time()
452 event_operation
= event_details
["command"]
453 for subscriber
in subscribers
:
454 subscriber
["id"] = notification_id
455 subscriber
["timeStamp"] = event_timestamp
456 subscriber
["subscriptionId"] = subscriber
["reference"]
457 subscriber
["operation"] = event_operation
458 del subscriber
["reference"]
459 del subscriber
["_id"]
460 subscriber
.update(event_details
["params"])
466 vnf_instance_id
: str,
472 Queries database and returns list of subscribers.
473 :param vnfd_id: Vnfd id of a VNF whose lifecycle has changed. (instantiated, scaled, terminated. etc)
474 :param vnf_instance_id: Vnf instance id of a VNF whose lifecycle has changed.
475 :param command: the command for event.
476 :param op_state: the operation state of VNF.
477 :param event_details: dict containing raw data of event occurred.
478 :return: List of interested subscribers for occurred event.
480 notification_type
= [
481 "VnfIdentifierCreationNotification",
482 "VnfLcmOperationOccurrenceNotification",
483 "VnfIdentifierDeletionNotification",
486 "identifier": [vnfd_id
, vnf_instance_id
],
487 "operationStates": ["ANY"],
488 "operationTypes": ["ANY"],
489 "notificationType": notification_type
,
492 filter_q
["operationStates"].append(op_state
)
494 filter_q
["operationTypes"].append(command
)
497 subscribers
= self
.db
.get_list(self
.subscriber_collection
, filter_q
)
498 subscribers
= self
._format
_vnflcm
_subscribers
(subscribers
, event_details
)
499 except Exception as e
:
500 error_text
= type(e
).__name
__ + ": " + str(e
)
501 self
.logger
.debug("Error getting vnflcm subscribers: {}".format(error_text
))
506 class NsdNotification(NotificationBase
):
507 def __init__(self
, db
):
509 Constructor of the class
512 # TODO will update this once support is there from subscription
513 self
.response_models
= {}
514 self
.subscriber_collection
= None
517 class VnfdNotification(NotificationBase
):
518 def __init__(self
, db
):
520 Constructor of the class
523 # TODO will update this once support is there from subscription
524 self
.response_models
= {}
525 self
.subscriber_collection
= None