444e8071217c69624f3fd6d3d8d4eafaed80c412
[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 uuid
18
19 import requests
20 from requests.auth import HTTPBasicAuth
21 from rest_framework import status
22
23 from lcm.nf import const
24 from lcm.pub.database.models import SubscriptionModel
25 from lcm.pub.database.models import (
26     VmInstModel, NetworkInstModel,
27     PortInstModel, StorageInstModel, VNFCInstModel
28 )
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.utils.enumutil import enum
31
32 logger = logging.getLogger(__name__)
33
34 NOTIFY_TYPE = enum(lCM_OP_OCC="VnfLcmOperationOccurrenceNotification",
35                    CREATION="VnfIdentifierCreationNotification",
36                    DELETION="VnfIdentifierDeletionNotification")
37
38
39 class NotificationsUtil(object):
40     def __init__(self):
41         pass
42
43     def send_notification(self, notification):
44         logger.info("Send Notifications to the callbackUri")
45         filters = {
46             "operationState": "operation_states",
47             "operation": "operation_types"
48         }
49         subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
50
51         subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
52         if not subscriptions.exists():
53             logger.info("No subscriptions created for the filters %s" % notification)
54             return
55         logger.info("Start sending notifications")
56         for subscription in subscriptions:
57             # set subscription id
58             notification["subscriptionId"] = subscription.subscription_id
59             notification['_links']['subscription'] = {'href': '/api/vnflcm/v1/subscriptions/%s' % subscription.subscription_id}
60             callbackUri = subscription.callback_uri
61             auth_info = json.loads(subscription.auth_info)
62             if auth_info["authType"] == const.OAUTH2_CLIENT_CREDENTIALS:
63                 pass
64             try:
65                 self.post_notification(callbackUri, auth_info, notification)
66             except Exception as e:
67                 logger.error("Failed to post notification: %s", e.message)
68
69     def post_notification(self, callbackUri, auth_info, notification):
70         params = auth_info.get("paramsBasic", {})
71         username = params.get("userName")
72         password = params.get("password")
73         logger.info("Sending notification to %s", callbackUri)
74         resp = requests.post(callbackUri, data=notification, auth=HTTPBasicAuth(username, password))
75         if resp.status_code != status.HTTP_204_NO_CONTENT:
76             raise Exception("Unable to send the notification to %s, due to %s" % (callbackUri, resp.text))
77         return
78
79
80 def set_affected_vnfcs(affected_vnfcs, nfinstid, changetype):
81     vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
82     for vnfc in vnfcs:
83         vm_resource = {}
84         if vnfc.vmid:
85             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
86             if vm:
87                 vm_resource = {
88                     'vimConnectionId': vm[0].vimid,
89                     'resourceId': vm[0].resourceid,
90                     'resourceProviderId': vm[0].vmname,  # TODO: is resourceName mapped to resourceProviderId?
91                     'vimLevelResourceType': 'vm'
92                 }
93         affected_vnfcs.append({
94             'id': vnfc.vnfcinstanceid,
95             'vduId': vnfc.vduid,
96             'changeType': changetype,
97             'computeResource': vm_resource
98         })
99     logger.debug("affected_vnfcs=%s", affected_vnfcs)
100     return affected_vnfcs
101
102
103 def set_affected_vls(affected_vls, nfinstid, changetype):
104     networks = NetworkInstModel.objects.filter(instid=nfinstid)
105     for network in networks:
106         network_resource = {
107             'vimConnectionId': network.vimid,
108             'resourceId': network.resourceid,
109             'resourceProviderId': network.name,  # TODO: is resourceName mapped to resourceProviderId?
110             'vimLevelResourceType': 'network'
111         }
112         affected_vls.append({
113             'id': network.networkid,
114             'virtualLinkDescId': network.nodeId,
115             'changeType': changetype,
116             'networkResource': network_resource
117         })
118     logger.debug("affected_vls=%s", affected_vls)
119
120
121 def set_ext_connectivity(ext_connectivity, nfinstid):
122     ext_connectivity_map = {}
123     ports = PortInstModel.objects.filter(instid=nfinstid)
124     for port in ports:
125         if port.networkid not in ext_connectivity_map:
126             ext_connectivity_map[port.networkid] = []
127         ext_connectivity_map[port.networkid].append({
128             'id': port.portid,  # TODO: port.portid or port.nodeid?
129             'resourceHandle': {
130                 'vimConnectionId': port.vimid,
131                 'resourceId': port.resourceid,
132                 'resourceProviderId': port.name,  # TODO: is resourceName mapped to resourceProviderId?
133                 'vimLevelResourceType': 'port'
134             },
135             'cpInstanceId': port.portid  # TODO: port.cpinstanceid is not initiated when create port resource.
136         })
137     for network_id, ext_link_ports in ext_connectivity_map.items():
138         networks = NetworkInstModel.objects.filter(networkid=network_id)
139         net_name = networks[0].name if networks else network_id
140         network_resource = {
141             'vimConnectionId': ext_link_ports[0]['resourceHandle']['vimConnectionId'],
142             'resourceId': network_id,
143             'resourceProviderId': net_name,  # TODO: is resourceName mapped to resourceProviderId?
144             'vimLevelResourceType': 'network'
145         }
146         ext_connectivity.append({
147             'id': network_id,
148             'resourceHandle': network_resource,
149             'extLinkPorts': ext_link_ports
150         })
151     logger.debug("ext_connectivity=%s", ext_connectivity)
152
153
154 def set_affected_vss(affected_vss, nfinstid, changetype):
155     vss = StorageInstModel.objects.filter(instid=nfinstid)
156     for vs in vss:
157         affected_vss.append({
158             'id': vs.storageid,
159             'virtualStorageDescId': vs.nodeId,
160             'changeType': changetype,
161             'storageResource': {
162                 'vimConnectionId': vs.vimid,
163                 'resourceId': vs.resourceid,
164                 'resourceProviderId': vs.name,  # TODO: is resourceName mapped to resourceProviderId?
165                 'vimLevelResourceType': 'volume'
166             }
167         })
168     logger.debug("affected_vss=%s", affected_vss)
169
170
171 def get_notification_status(operation_state):
172     notification_status = const.LCM_NOTIFICATION_STATUS.START
173     if operation_state in const.RESULT_RANGE:
174         notification_status = const.LCM_NOTIFICATION_STATUS.RESULT
175     return notification_status
176
177
178 def prepare_notification(nfinstid, jobid, operation, operation_state):
179     logger.info('Start to prepare notification')
180     notification_content = {
181         'id': str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
182         'notificationType': NOTIFY_TYPE.lCM_OP_OCC,
183         # set 'subscriptionId' after filtering for subscribers
184         'timeStamp': now_time(),
185         'notificationStatus': get_notification_status(operation_state),
186         'operationState': operation_state,
187         'vnfInstanceId': nfinstid,
188         'operation': operation,
189         'isAutomaticInvocation': False,
190         'vnfLcmOpOccId': jobid,
191         'affectedVnfcs': [],
192         'affectedVirtualLinks': [],
193         'affectedVirtualStorages': [],
194         'changedExtConnectivity': [],
195         'error': '',
196         '_links': {
197             'vnfInstance': {
198                 'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
199             },
200             'vnfLcmOpOcc': {
201                 'href': '%s/vnf_lcm_op_occs/%s' % (const.URL_PREFIX, jobid)
202             }
203         }
204     }
205     return notification_content
206
207
208 def prepare_notification_data(nfinstid, jobid, changetype, operation):
209     data = prepare_notification(nfinstid=nfinstid,
210                                 jobid=jobid,
211                                 operation=operation,
212                                 operation_state=const.OPERATION_STATE_TYPE.COMPLETED)
213
214     set_affected_vnfcs(data['affectedVnfcs'], nfinstid, changetype)
215     set_affected_vls(data['affectedVirtualLinks'], nfinstid, changetype)
216     set_affected_vss(data['affectedVirtualStorages'], nfinstid, changetype)
217     set_ext_connectivity(data['changedExtConnectivity'], nfinstid)
218
219     logger.debug('Notification content: %s' % data)
220     return data
221
222
223 def prepare_vnf_identifier_notification(notify_type, nfinstid):
224     data = {
225         "id": str(uuid.uuid4()),  # shall be the same if sent multiple times due to multiple subscriptions.
226         "notificationType": notify_type,
227         "timeStamp": now_time(),
228         "vnfInstanceId": nfinstid,
229         "_links": {
230             "vnfInstance": {
231                 'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
232             }
233         }
234     }
235
236     logger.debug('Vnf Identifier Notification: %s' % data)
237     return data