import logging
import uuid
import requests
+import json
from rest_framework import status
+from requests.auth import HTTPBasicAuth
from catalog.packages import const
from catalog.pub.database.models import VnfPackageModel, VnfPkgSubscriptionModel, NsdmSubscriptionModel
import catalog.pub.utils.timeutil
from catalog.packages.serializers.vnf_pkg_notifications import PkgChangeNotificationSerializer, \
PkgOnboardingNotificationSerializer
-
logger = logging.getLogger(__name__)
def send_notification(self):
notification = self.prepare_notification()
- subscriptions_filter = {v + "__contains": notification[k] for k, v in self.filters.items()}
+ subscriptions_filter = {v + "__contains": notification[k] for k, v in self.filter.items()}
subscriptions_filter = remove_none_key(subscriptions_filter)
logger.debug('send_notification subscriptions_filter = %s' % subscriptions_filter)
q1 = Q()
subscriptions = self.SubscriptionModel.objects.filter(q1)
if not subscriptions.exists():
- logger.info("No subscriptions created for the filters %s" % notification)
+ logger.info("No subscriptions created for the filter %s" % notification)
return
logger.info("Start sending notifications")
for sub in subscriptions:
serialized_data = self.notifyserializer(data=notification)
if not serialized_data.is_valid():
logger.error('Notification Data is invalid:%s.' % serialized_data.errors)
- self.post_notification(callbackuri, notification)
-
- def post_notification(self, callbackuri, notification):
- """
- params = auth_info.get("paramsBasic", {})
- username, password = params.get("userName"), params.get("password")
- logger.info("Sending notification to %s, %s", callbackuri, params)
- resp = None
- if username:
- resp = requests.post(callbackuri,
- data=notification,
- auth=HTTPBasicAuth(username, password))
- else:
- """
+ if sub.auth_info:
+ self.post_notification(callbackuri, notification, auth_info=json.loads(sub.auth_info))
+ else:
+ self.post_notification(callbackuri, notification)
+
+ def post_notification(self, callbackuri, notification, auth_info=None):
try:
- resp = requests.post(callbackuri, data=notification, headers={'Connection': 'close'})
- if resp.status_code != status.HTTP_204_NO_CONTENT:
- logger.error("Sending notification to %s failed: %s" % (callbackuri, resp.text))
+ if auth_info:
+ if const.BASIC in auth_info.get("authType", ''):
+ params = auth_info.get("paramsBasic", {})
+ username = params.get("userName")
+ password = params.get("password")
+ resp = requests.post(callbackuri,
+ data=notification,
+ headers={'Connection': 'close',
+ 'content-type': 'application/json',
+ 'accept': 'application/json'},
+ auth=HTTPBasicAuth(username, password),
+ verify=False)
+ elif const.OAUTH2_CLIENT_CREDENTIALS in auth_info.get("authType", ''):
+ # todo
+ pass
+ else:
+ # todo
+ pass
+ else:
+ resp = requests.post(callbackuri,
+ data=notification,
+ headers={'Connection': 'close',
+ 'content-type': 'application/json',
+ 'accept': 'application/json'},
+ verify=False)
+
+ if resp.status_code == status.HTTP_204_NO_CONTENT:
+ logger.error("Sending notification to %s failed: %s" % (callbackuri, resp))
else:
logger.info("Sending notification to %s successfully.", callbackuri)
except:
class PkgNotifications(NotificationsUtil):
def __init__(self, notification_type, vnf_pkg_id, change_type=None, operational_state=None):
super(PkgNotifications, self).__init__(notification_type)
- self.filters = {
+ self.filter = {
'vnfdId': 'vnfd_id',
'vnfPkgId': 'vnf_pkg_id'
}
class NsdNotifications(NotificationsUtil):
def __init__(self, notification_type, nsd_info_id, nsd_id, failure_details=None, operational_state=None):
super(NsdNotifications, self).__init__(notification_type)
- self.filters = {
+ self.filter = {
'nsdInfoId': 'nsdInfoId',
'nsdId': 'nsdId',
}
class PnfNotifications(NotificationsUtil):
def __init__(self, notification_type, pnfd_info_id, pnfd_id, failure_details=None):
super(PnfNotifications, self).__init__(notification_type)
- self.filters = {
+ self.filter = {
'pnfdId': 'pnfdId',
'pnfdInfoIds': 'pnfdInfoIds',
}