summaryrefslogtreecommitdiffstats
path: root/lcm/lcm/pub/utils/notificationsutil.py
blob: 32667fa0a23bfc663156f8bc338c5f2eaa5ffd4a (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 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 base64
import sys
import traceback
import logging
import urllib.request
import urllib.error
import urllib.parse
import uuid
import httplib2

from lcm.nf import const
from lcm.pub.database.models import SubscriptionModel
from lcm.pub.database.models import VmInstModel
from lcm.pub.database.models import NetworkInstModel
from lcm.pub.database.models import PortInstModel
from lcm.pub.database.models import StorageInstModel
from lcm.pub.database.models import VNFCInstModel
from lcm.pub.database.models import NfInstModel
from lcm.pub.utils.timeutil import now_time
from lcm.pub.utils.enumutil import enum

logger = logging.getLogger(__name__)

rest_no_auth, rest_oneway_auth, rest_bothway_auth = 0, 1, 2
HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED = '200', '201', '204', '202'
status_ok_list = [HTTP_200_OK, HTTP_201_CREATED, HTTP_204_NO_CONTENT, HTTP_202_ACCEPTED]
HTTP_404_NOTFOUND, HTTP_403_FORBIDDEN, HTTP_401_UNAUTHORIZED, HTTP_400_BADREQUEST = '404', '403', '401', '400'
NOTIFY_TYPE = enum(
    lCM_OP_OCC="VnfLcmOperationOccurrenceNotification",
    CREATION="VnfIdentifierCreationNotification",
    DELETION="VnfIdentifierDeletionNotification"
)


class NotificationsUtil(object):
    def send_notification(self, notification):
        logger.info("Send Notifications to the callbackUri")
        filters = {
            "operationState": "operation_states",
            "operation": "operation_types",
            "vnfInstanceId": "vnf_instance_filter"
        }
        subscriptions_filter = {v + "__contains": notification[k] for k, v in list(filters.items())}

        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
            notification['_links']['subscription'] = {
                'href': '/api/vnflcm/v1/subscriptions/%s' % subscription.subscription_id
            }
            callbackUri = subscription.callback_uri
            auth_info = json.loads(subscription.auth_info)
            if const.BASIC in auth_info["authType"]:
                try:
                    self.post_notification(callbackUri, notification)
                except Exception as e:
                    logger.error("Failed to post notification: %s", e.args[0])

    def post_notification(self, callbackUri, notification):
        logger.info("Sending notification to %s", callbackUri)
        resp = self.call_req(callbackUri, "", "", "POST", json.dumps(notification))
        if resp[0] != 0:
            logger.error('Status code is %s, detail is %s.', resp[2], resp[1])

    def call_req(self, full_url, user, passwd, method, content=''):
        callid = str(uuid.uuid1())
        logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s')" % (
            callid, full_url, user, passwd, rest_no_auth, method, content))
        ret = None
        resp_Location = ''
        resp_status = ''
        try:
            headers = {'content-type': 'application/json', 'accept': 'application/json'}
            if user:
                headers['Authorization'] = 'Basic %s' % base64.b64encode(
                    bytes('%s:%s' % (user, passwd), "utf-8")).decode()
            ca_certs = None
            for retry_times in range(3):
                http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=True)
                http.follow_all_redirects = True
                try:
                    resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers)
                    logger.info("resp=%s,resp_content=%s" % (resp, resp_content))
                    resp_status, resp_body = resp['status'], resp_content.decode('UTF-8')
                    resp_Location = resp.get('Location', "")
                    logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body))
                    if resp_status in status_ok_list:
                        ret = [0, resp_body, resp_status, resp_Location]
                    else:
                        ret = [1, resp_body, resp_status, resp_Location]
                    break
                except Exception as ex:
                    if 'httplib.ResponseNotReady' in str(sys.exc_info()):
                        logger.debug("retry_times=%d", retry_times)
                        logger.error(traceback.format_exc())
                        ret = [1, "Unable to connect to %s" % full_url, resp_status, resp_Location]
                        continue
                    raise ex
        except urllib.error.URLError as err:
            ret = [2, str(err), resp_status, resp_Location]
        except Exception as ex:
            logger.error(traceback.format_exc())
            logger.error("[%s]ret=%s" % (callid, str(sys.exc_info())))
            res_info = str(sys.exc_info())
            if 'httplib.ResponseNotReady' in res_info:
                res_info = "The URL[%s] request failed or is not responding." % full_url
            ret = [3, res_info, resp_status, resp_Location]
        except:
            logger.error(traceback.format_exc())
            ret = [4, str(sys.exc_info()), resp_status, resp_Location]

        logger.debug("[%s]ret=%s" % (callid, str(ret)))
        return ret


def set_affected_vnfcs(affected_vnfcs, nfinstid, changetype):
    vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
    for vnfc in vnfcs:
        vm_resource = {}
        if vnfc.vmid:
            vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
            if vm:
                vm_resource = {
                    'vimConnectionId': vm[0].vimid,
                    'resourceId': vm[0].resourceid,
                    'resourceProviderId': vm[0].vmname,  # TODO: is resourceName mapped to resourceProviderId?
                    'vimLevelResourceType': 'vm'
                }
        affected_vnfcs.append({
            'id': vnfc.vnfcinstanceid,
            'vduId': vnfc.vduid,
            'changeType': changetype,
            'computeResource': vm_resource
        })
    logger.debug("affected_vnfcs=%s", affected_vnfcs)
    return affected_vnfcs


def set_affected_vls(affected_vls, nfinstid, changetype):
    networks = NetworkInstModel.objects.filter(instid=nfinstid)
    for network in networks:
        network_resource = {
            'vimConnectionId': network.vimid,
            'resourceId': network.resourceid,
            'resourceProviderId': network.name,  # TODO: is resourceName mapped to resourceProviderId?
            'vimLevelResourceType': 'network'
        }
        affected_vls.append({
            'id': network.networkid,
            'virtualLinkDescId': network.nodeId,
            'changeType': changetype,
            'networkResource': network_resource
        })
    logger.debug("affected_vls=%s", affected_vls)


def set_ext_connectivity(ext_connectivity, nfinstid):
    ext_connectivity_map = {}
    ports = PortInstModel.objects.filter(instid=nfinstid)
    for port in ports:
        if port.networkid not in ext_connectivity_map:
            ext_connectivity_map[port.networkid] = []
        ext_connectivity_map[port.networkid].append({
            'id': port.portid,  # TODO: port.portid or port.nodeid?
            'resourceHandle': {
                'vimConnectionId': port.vimid,
                'resourceId': port.resourceid,
                'resourceProviderId': port.name,  # TODO: is resourceName mapped to resourceProviderId?
                'vimLevelResourceType': 'port'
            },
            'cpInstanceId': port.portid  # TODO: port.cpinstanceid is not initiated when create port resource.
        })
    for network_id, ext_link_ports in list(ext_connectivity_map.items()):
        networks = NetworkInstModel.objects.filter(networkid=network_id)
        net_name = networks[0].name if networks else network_id
        network_resource = {
            'vimConnectionId': ext_link_ports[0]['resourceHandle']['vimConnectionId'],
            'resourceId': network_id,
            'resourceProviderId': net_name,  # TODO: is resourceName mapped to resourceProviderId?
            'vimLevelResourceType': 'network'
        }
        ext_connectivity.append({
            'id': network_id,
            'resourceHandle': network_resource,
            'extLinkPorts': ext_link_ports
        })
    logger.debug("ext_connectivity=%s", ext_connectivity)


def set_affected_vss(affected_vss, nfinstid, changetype):
    vss = StorageInstModel.objects.filter(instid=nfinstid)
    for vs in vss:
        affected_vss.append({
            'id': vs.storageid,
            'virtualStorageDescId': vs.nodeId,
            'changeType': changetype,
            'storageResource': {
                'vimConnectionId': vs.vimid,
                'resourceId': vs.resourceid,
                'resourceProviderId': vs.name,  # TODO: is resourceName mapped to resourceProviderId?
                'vimLevelResourceType': 'volume'
            }
        })
    logger.debug("affected_vss=%s", affected_vss)


def get_notification_status(operation_state):
    if operation_state == const.OPERATION_STATE_TYPE.STARTING:
        return const.LCM_NOTIFICATION_STATUS.START
    return const.LCM_NOTIFICATION_STATUS.RESULT


def prepare_notification(nfinstid, jobid, operation, operation_state):
    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': NOTIFY_TYPE.lCM_OP_OCC,
        # set 'subscriptionId' after filtering for subscribers
        'timeStamp': now_time(),
        'notificationStatus': get_notification_status(operation_state),
        'operationState': operation_state,
        'vnfInstanceId': nfinstid,
        'operation': operation,
        'isAutomaticInvocation': False,
        'vnfLcmOpOccId': jobid,
        'affectedVnfcs': [],
        'affectedVirtualLinks': [],
        'affectedVirtualStorages': [],
        'changedExtConnectivity': [],
        'error': None,
        '_links': {
            'vnfInstance': {
                'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
            },
            'vnfLcmOpOcc': {
                'href': '%s/vnf_lcm_op_occs/%s' % (const.URL_PREFIX, jobid)
            }
        }
    }
    nfInsts = NfInstModel.objects.filter(nfinstid=nfinstid)
    if nfInsts.exists():
        notification_content['vnfmInstId'] = nfInsts[0].vnfminstid
    else:
        notification_content['vnfmInstId'] = "1"
    return notification_content


def prepare_notification_data(nfinstid, jobid, changetype, operation):
    data = prepare_notification(
        nfinstid=nfinstid,
        jobid=jobid,
        operation=operation,
        operation_state=const.OPERATION_STATE_TYPE.COMPLETED
    )

    set_affected_vnfcs(data['affectedVnfcs'], nfinstid, changetype)
    set_affected_vls(data['affectedVirtualLinks'], nfinstid, changetype)
    set_affected_vss(data['affectedVirtualStorages'], nfinstid, changetype)
    set_ext_connectivity(data['changedExtConnectivity'], nfinstid)

    logger.debug('Notification content: %s' % data)
    return data


def prepare_vnf_identifier_notification(notify_type, nfinstid):
    data = {
        "id": str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
        "notificationType": notify_type,
        "timeStamp": now_time(),
        "vnfInstanceId": nfinstid,
        "_links": {
            "vnfInstance": {
                'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
            }
        }
    }

    logger.debug('Vnf Identifier Notification: %s' % data)
    return data