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.
19 from rest_framework import status
20 from requests.auth import HTTPBasicAuth
22 from lcm.nf import const
23 from lcm.pub.database.models import SubscriptionModel
25 logger = logging.getLogger(__name__)
28 class NotificationsUtil(object):
32 def send_notification(self, notification):
33 logger.info("Send Notifications to the callbackUri")
35 "vnfInstanceId": "vnf_instance_filter",
36 "operationState": "operation_states",
37 "operation": "operation_types"
39 subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
41 subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
42 if not subscriptions.exists():
43 logger.info("No subscriptions created for the filters %s" % notification)
45 logger.info("Start sending notifications")
46 for subscription in subscriptions:
48 notification["subscriptionId"] = subscription.subscription_id
49 callbackUri = subscription.callback_uri
50 auth_info = json.loads(subscription.auth_info)
51 if auth_info["authType"] == const.OAUTH2_CLIENT_CREDENTIALS:
53 self.post_notification(callbackUri, auth_info, notification)
55 def post_notification(self, callbackUri, auth_info, notification):
56 params = auth_info.get("paramsBasic", {})
57 username = params.get("userName")
58 password = params.get("password")
59 logger.info("Sending notification to %s", callbackUri)
60 resp = requests.post(callbackUri, data=notification, auth=HTTPBasicAuth(username, password))
61 if resp.status_code != status.HTTP_204_NO_CONTENT:
62 raise Exception("Unable to send the notification to %s, due to %s" % (callbackUri, resp.text))