1 # Copyright (C) 2018 Verizon. All Rights Reserved
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from requests.auth import HTTPBasicAuth
21 from rest_framework import status
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
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.utils.enumutil import enum
32 logger = logging.getLogger(__name__)
34 NOTIFY_TYPE = enum(lCM_OP_OCC="VnfLcmOperationOccurrenceNotification",
35 CREATION="VnfIdentifierCreationNotification",
36 DELETION="VnfIdentifierDeletionNotification")
39 class NotificationsUtil(object):
43 def send_notification(self, notification):
44 logger.info("Send Notifications to the callbackUri")
46 "operationState": "operation_states",
47 "operation": "operation_types"
49 subscriptions_filter = {v + "__contains": notification[k] for k, v in filters.iteritems()}
51 subscriptions = SubscriptionModel.objects.filter(**subscriptions_filter)
52 if not subscriptions.exists():
53 logger.info("No subscriptions created for the filters %s" % notification)
55 logger.info("Start sending notifications")
56 for subscription in subscriptions:
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:
65 self.post_notification(callbackUri, auth_info, notification)
66 except Exception as e:
67 logger.error("Failed to post notification: %s", e.message)
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))
80 def set_affected_vnfcs(affected_vnfcs, nfinstid, changetype):
81 vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
85 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
88 'vimConnectionId': vm[0].vimid,
89 'resourceId': vm[0].resourceid,
90 'resourceProviderId': vm[0].vmname, # TODO: is resourceName mapped to resourceProviderId?
91 'vimLevelResourceType': 'vm'
93 affected_vnfcs.append({
94 'id': vnfc.vnfcinstanceid,
96 'changeType': changetype,
97 'computeResource': vm_resource
99 logger.debug("affected_vnfcs=%s", affected_vnfcs)
100 return affected_vnfcs
103 def set_affected_vls(affected_vls, nfinstid, changetype):
104 networks = NetworkInstModel.objects.filter(instid=nfinstid)
105 for network in networks:
107 'vimConnectionId': network.vimid,
108 'resourceId': network.resourceid,
109 'resourceProviderId': network.name, # TODO: is resourceName mapped to resourceProviderId?
110 'vimLevelResourceType': 'network'
112 affected_vls.append({
113 'id': network.networkid,
114 'virtualLinkDescId': network.nodeId,
115 'changeType': changetype,
116 'networkResource': network_resource
118 logger.debug("affected_vls=%s", affected_vls)
121 def set_ext_connectivity(ext_connectivity, nfinstid):
122 ext_connectivity_map = {}
123 ports = PortInstModel.objects.filter(instid=nfinstid)
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?
130 'vimConnectionId': port.vimid,
131 'resourceId': port.resourceid,
132 'resourceProviderId': port.name, # TODO: is resourceName mapped to resourceProviderId?
133 'vimLevelResourceType': 'port'
135 'cpInstanceId': port.portid # TODO: port.cpinstanceid is not initiated when create port resource.
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
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'
146 ext_connectivity.append({
148 'resourceHandle': network_resource,
149 'extLinkPorts': ext_link_ports
151 logger.debug("ext_connectivity=%s", ext_connectivity)
154 def set_affected_vss(affected_vss, nfinstid, changetype):
155 vss = StorageInstModel.objects.filter(instid=nfinstid)
157 affected_vss.append({
159 'virtualStorageDescId': vs.nodeId,
160 'changeType': changetype,
162 'vimConnectionId': vs.vimid,
163 'resourceId': vs.resourceid,
164 'resourceProviderId': vs.name, # TODO: is resourceName mapped to resourceProviderId?
165 'vimLevelResourceType': 'volume'
168 logger.debug("affected_vss=%s", affected_vss)
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
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,
192 'affectedVirtualLinks': [],
193 'affectedVirtualStorages': [],
194 'changedExtConnectivity': [],
198 'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
201 'href': '%s/vnf_lcm_op_occs/%s' % (const.URL_PREFIX, jobid)
205 return notification_content
208 def prepare_notification_data(nfinstid, jobid, changetype, operation):
209 data = prepare_notification(nfinstid=nfinstid,
212 operation_state=const.OPERATION_STATE_TYPE.COMPLETED)
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)
219 logger.debug('Notification content: %s' % data)
223 def prepare_vnf_identifier_notification(notify_type, nfinstid):
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,
231 'href': '%s/vnf_instances/%s' % (const.URL_PREFIX, nfinstid)
236 logger.debug('Vnf Identifier Notification: %s' % data)