summaryrefslogtreecommitdiffstats
path: root/lcm/lcm/pub/utils/notificationsutil.py
diff options
context:
space:
mode:
authorBharath Thiruveedula <bharath.thiruveedula@verizon.com>2018-09-06 12:48:42 +0530
committerBharath Thiruveedula <bharath.thiruveedula@verizon.com>2018-09-06 12:50:40 +0530
commitc78cda3f14d09c2d15ed1985be684a73f34282c1 (patch)
treed70a87344a305dfad5741b46fd157d2a647ada44 /lcm/lcm/pub/utils/notificationsutil.py
parent8f59e8d4426a11ea3f2a16d0c0479786fd02a027 (diff)
Add notification system util in GVNFM
Change-Id: I1489722e30121aebb69b524cdaafaa44729784a2 Signed-off-by: Bharath Thiruveedula<bharath.thiruveedula@verizon.com> Issue-ID: VFC-1096
Diffstat (limited to 'lcm/lcm/pub/utils/notificationsutil.py')
-rw-r--r--lcm/lcm/pub/utils/notificationsutil.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/lcm/lcm/pub/utils/notificationsutil.py b/lcm/lcm/pub/utils/notificationsutil.py
new file mode 100644
index 00000000..3af8f22a
--- /dev/null
+++ b/lcm/lcm/pub/utils/notificationsutil.py
@@ -0,0 +1,63 @@
+# Copyright (C) 2018 Verizon. All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+import logging
+import requests
+
+from rest_framework import status
+from requests.auth import HTTPBasicAuth
+
+from lcm.nf import const
+from lcm.pub.database.models import SubscriptionModel
+
+logger = logging.getLogger(__name__)
+
+
+class NotificationsUtil(object):
+ def __init__(self):
+ pass
+
+ def send_notification(self, notification):
+ logger.info("Send Notifications to the callbackUri")
+ filters = {
+ "vnfInstanceId": "vnf_instance_filter",
+ "operationState": "operation_states",
+ "operation": "operation_types"
+ }
+ subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
+
+ subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
+ if not subscriptions.exists():
+ logger.info("No subscriptions created for the filters %s" % notification)
+ return
+ logger.info("Start sending notifications")
+ for subscription in subscriptions:
+ # set subscription id
+ notification["subscriptionId"] = subscription.subscription_id
+ callbackUri = subscription.callback_uri
+ auth_info = json.loads(subscription.auth_info)
+ if auth_info["authType"] == const.OAUTH2_CLIENT_CREDENTIALS:
+ pass
+ self.post_notification(callbackUri, auth_info, notification)
+
+ def post_notification(self, callbackUri, auth_info, notification):
+ params = auth_info.get("paramsBasic", {})
+ username = params.get("userName")
+ password = params.get("password")
+ logger.info("Sending notification to %s", callbackUri)
+ resp = requests.post(callbackUri, data=notification, auth=HTTPBasicAuth(username, password))
+ if resp.status_code != status.HTTP_204_NO_CONTENT:
+ raise Exception("Unable to send the notification to %s, due to %s" % (callbackUri, resp.text))
+ return