Fix lcm notify bug
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / utils / notificationsutil.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 json
16 import logging
17 import requests
18
19 from rest_framework import status
20 from requests.auth import HTTPBasicAuth
21
22 from lcm.nf import const
23 from lcm.pub.database.models import SubscriptionModel
24
25 logger = logging.getLogger(__name__)
26
27
28 class NotificationsUtil(object):
29     def __init__(self):
30         pass
31
32     def send_notification(self, notification):
33         logger.info("Send Notifications to the callbackUri")
34         filters = {
35             "operationState": "operation_states",
36             "operation": "operation_types"
37         }
38         subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
39
40         subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
41         if not subscriptions.exists():
42             logger.info("No subscriptions created for the filters %s" % notification)
43             return
44         logger.info("Start sending notifications")
45         for subscription in subscriptions:
46             # set subscription id
47             notification["subscriptionId"] = subscription.subscription_id
48             notification['_links']['subscription'] = {'href': '/api/vnflcm/v1/subscriptions/%s' % 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:
52                 pass
53             try:
54                 self.post_notification(callbackUri, auth_info, notification)
55             except Exception as e:
56                 logger.error("Failed to post notification: %s", e.message)
57
58     def post_notification(self, callbackUri, auth_info, notification):
59         params = auth_info.get("paramsBasic", {})
60         username = params.get("userName")
61         password = params.get("password")
62         logger.info("Sending notification to %s", callbackUri)
63         resp = requests.post(callbackUri, data=notification, auth=HTTPBasicAuth(username, password))
64         if resp.status_code != status.HTTP_204_NO_CONTENT:
65             raise Exception("Unable to send the notification to %s, due to %s" % (callbackUri, resp.text))
66         return