aboutsummaryrefslogtreecommitdiffstats
path: root/catalog/packages/biz/notificationsutil.py
blob: 4fa8e79c3ea9a81bc75fe29b9a83c54224f46d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright 2019 ZTE Corporation.
#
# 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 logging
import uuid
import requests
from rest_framework import status
from catalog.packages import const
from catalog.pub.database.models import VnfPkgSubscriptionModel, NsdmSubscriptionModel
from catalog.pub.database.models import VnfPackageModel
import catalog.pub.utils.timeutil
from catalog.pub.utils.values import remove_none_key
from catalog.pub.config import config as pub_config
import traceback
from django.db.models import Q

logger = logging.getLogger(__name__)


class NotificationsUtil(object):
    def __init__(self):
        pass

    def send_notification(self, notification, filters, isvnfpkg):
        subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.items()}
        subscriptions_filter = remove_none_key(subscriptions_filter)
        logger.debug('send_notification subscriptions_filter = %s' % subscriptions_filter)
        q1 = Q()
        q1.connector = 'OR'
        for k, v in subscriptions_filter.items():
            q1.children.append((k, v))
        if isvnfpkg:
            subscriptions = VnfPkgSubscriptionModel.objects.filter(q1)
            subscription_root_uri = const.VNFPKG_SUBSCRIPTION_ROOT_URI
        else:
            subscriptions = NsdmSubscriptionModel.objects.filter(q1)
            subscription_root_uri = const.NSDM_SUBSCRIPTION_ROOT_URI

        if not subscriptions.exists():
            logger.info("No subscriptions created for the filters %s" % notification)
            return
        logger.info("Start sending notifications")
        for sub in subscriptions:
            # set subscription id
            if isvnfpkg:
                notification["subscriptionId"] = sub.subscription_id
            else:
                notification["subscriptionId"] = sub.subscriptionid
            notification['_links']['subscription'] = {
                'href': 'http://%s:%s/%s%s' % (pub_config.MSB_SERVICE_IP,
                                               pub_config.MSB_SERVICE_PORT,
                                               subscription_root_uri,
                                               notification["subscriptionId"])
            }
            callbackuri = sub.callback_uri
            """
            auth_info = json.loads(sub.auth_info)
            if auth_info["authType"] == const.OAUTH2_CLIENT_CREDENTIALS:
                pass
            """
            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:
        """

        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))
            else:
                logger.info("Sending notification to %s successfully.", callbackuri)
        except:
            logger.error("Post notification failed.")
            logger.error(traceback.format_exc())


def prepare_vnfpkg_notification(vnf_pkg_id, notification_type, pkg_change_type, operational_state):
    logger.info('Start to prepare notification')
    vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnf_pkg_id)
    vnfd_id = None
    if vnf_pkg:
        vnfd_id = vnf_pkg[0].vnfdId
    notification_content = {
        'id': str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
        'notificationType': notification_type,
        # set 'subscriptionId' after filtering for subscribers
        'timeStamp': catalog.pub.utils.timeutil.now_time(),
        'vnfPkgId': vnf_pkg_id,
        'vnfdId': vnfd_id,
        'changeType': pkg_change_type,
        'operationalState': operational_state,
        '_links': {
            'vnfPackage': {
                'href': 'http://%s:%s/%s/vnf_packages/%s' % (pub_config.MSB_SERVICE_IP,
                                                             pub_config.MSB_SERVICE_PORT,
                                                             const.PKG_URL_PREFIX,
                                                             vnf_pkg_id)
            }
        }
    }
    return notification_content


def prepare_nsd_notification(nsd_info_id, nsd_id, notification_type, failure_details=None, operational_state=None):
    logger.info('Start to prepare notification')
    notification_content = {
        'id': str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
        'notificationType': notification_type,
        # set 'subscriptionId' after filtering for subscribers
        'timeStamp': catalog.pub.utils.timeutil.now_time(),
        'nsdInfoId': nsd_info_id,
        'nsdId': nsd_id,
        'onboardingFailureDetails': failure_details,
        'nsdOperationalState': operational_state,
        '_links': {
            'nsdInfo': {
                'href': 'http://%s:%s/%s/ns_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
                                                               pub_config.MSB_SERVICE_PORT,
                                                               const.NSD_URL_PREFIX,
                                                               nsd_info_id)
            }
        }
    }
    return notification_content


def prepare_pnfd_notification(pnfd_info_id, pnfd_id, notification_type, failure_details=None):
    logger.info('Start to prepare notification')
    notification_content = {
        'id': str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
        'notificationType': notification_type,
        # set 'subscriptionId' after filtering for subscribers
        'timeStamp': catalog.pub.utils.timeutil.now_time(),
        'pnfdInfoIds': pnfd_info_id,
        'pnfdId': pnfd_id,
        'onboardingFailureDetails': failure_details,
        '_links': {
            'pnfdInfo': {
                'href': 'http://%s:%s/%s/pnf_descriptors/%s' % (pub_config.MSB_SERVICE_IP,
                                                                pub_config.MSB_SERVICE_PORT,
                                                                const.NSD_URL_PREFIX,
                                                                pnfd_info_id)
            }
        }
    }
    return notification_content