1 # Copyright 2017 ZTE Corporation.
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.
19 from lcm.pub.exceptions import NFLCMException
20 from lcm.pub.utils.restcall import req_by_msb
21 from lcm.pub.utils.timeutil import now_time
22 from lcm.pub.database.models import (
23 NfInstModel, VmInstModel, NetworkInstModel,
24 PortInstModel, StorageInstModel, VNFCInstModel
27 logger = logging.getLogger(__name__)
30 def get_packageinfo_by_vnfdid(vnfdid):
31 ret = req_by_msb('api/gvnfmdriver/v1/vnfpackages', 'GET')
33 logger.error('Status code is %s, detail is %s.', ret[2], ret[1])
34 raise NFLCMException('Failed to query package_info of vnfdid(%s) from nslcm.' % vnfdid)
35 return json.JSONDecoder().decode(ret[1])
38 def apply_grant_to_nfvo(data):
39 ret = req_by_msb('api/gvnfmdriver/v1/resource/grant', 'PUT', data)
41 logger.error('Status code is %s, detail is %s.', ret[2], ret[1])
42 raise NFLCMException('Nf instancing apply grant exception')
43 return json.JSONDecoder().decode(ret[1])
46 def notify_lcm_to_nfvo(data):
47 ret = req_by_msb('api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification', 'POST', data)
49 logger.error('Status code is %s, detail is %s.', ret[2], ret[1])
50 raise NFLCMException('Nf lcm notify exception')
54 def prepare_notification_data(nfinstid, jobid, changetype, operation):
55 logger.info('Send notify request to nfvo')
57 vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
61 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
64 'vimConnectionId': vm[0].vimid,
65 'resourceId': vm[0].resourceid,
66 'resourceProviderId': vm[0].vmname, # TODO: is resourceName mapped to resourceProviderId?
67 'vimLevelResourceType': 'vm'
69 affected_vnfcs.append({
70 'id': vnfc.vnfcinstanceid,
72 'changeType': changetype,
73 'computeResource': vm_resource
76 networks = NetworkInstModel.objects.filter(instid=nfinstid)
77 for network in networks:
79 'vimConnectionId': network.vimid,
80 'resourceId': network.resourceid,
81 'resourceProviderId': network.name, # TODO: is resourceName mapped to resourceProviderId?
82 'vimLevelResourceType': 'network'
85 'id': network.networkid,
86 'virtualLinkDescId': network.nodeId,
87 'changeType': changetype,
88 'networkResource': network_resource
91 ext_connectivity_map = {}
92 ports = PortInstModel.objects.filter(instid=nfinstid)
94 if port.networkid not in ext_connectivity_map:
95 ext_connectivity_map[port.networkid] = []
96 ext_connectivity_map[port.networkid].append({
97 'id': port.portid, # TODO: port.portid or port.nodeid?
99 'vimConnectionId': port.vimid,
100 'resourceId': port.resourceid,
101 'resourceProviderId': port.name, # TODO: is resourceName mapped to resourceProviderId?
102 'vimLevelResourceType': 'port',
103 'tenant': port.tenant,
104 'ipAddress': port.ipaddress,
105 'macAddress': port.macaddress,
106 'instId': port.instid,
107 'portid': port.portid,
108 'networkid': port.networkid,
109 'subnetworkid': port.subnetworkid
111 'cpInstanceId': port.portid # TODO: port.cpinstanceid is not initiated when create port resource.
113 for network_id, ext_link_ports in list(ext_connectivity_map.items()):
114 networks = NetworkInstModel.objects.filter(networkid=network_id)
115 net_name = networks[0].name if networks else network_id
117 'vimConnectionId': ext_link_ports[0]['resourceHandle']['vimConnectionId'],
118 'resourceId': network_id,
119 'resourceProviderId': net_name, # TODO: is resourceName mapped to resourceProviderId?
120 'vimLevelResourceType': 'network'
122 ext_connectivity.append({
124 'resourceHandle': network_resource,
125 'changeType': changetype,
126 'extLinkPorts': ext_link_ports
128 logger.debug("ext_connectivity=%s", ext_connectivity)
130 vss = StorageInstModel.objects.filter(instid=nfinstid)
132 affected_vss.append({
134 'virtualStorageDescId': vs.nodeId,
135 'changeType': changetype,
137 'vimConnectionId': vs.vimid,
138 'resourceId': vs.resourceid,
139 'resourceProviderId': vs.name, # TODO: is resourceName mapped to resourceProviderId?
140 'vimLevelResourceType': 'volume'
143 notification_content = {
144 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
145 'notificationType': 'VnfLcmOperationOccurrenceNotification',
146 # set 'subscriptionId' after filtering for subscribers
147 'timeStamp': now_time(),
148 'notificationStatus': 'RESULT',
149 'operationState': 'COMPLETED',
150 'vnfInstanceId': nfinstid,
151 'operation': operation,
152 'isAutomaticInvocation': False,
153 'vnfLcmOpOccId': jobid,
154 'affectedVnfcs': affected_vnfcs,
155 'affectedVirtualLinks': affected_vls,
156 'affectedVirtualStorages': affected_vss,
157 'changedExtConnectivity': ext_connectivity,
159 'vnfInstance': {'href': '/api/vnflcm/v1/vnf_instances/%s' % nfinstid},
160 # set 'subscription' link after filtering for subscribers
161 'vnfLcmOpOcc': {'href': '/api/vnflcm/v1/vnf_lcm_op_occs/%s' % jobid}
164 nfInsts = NfInstModel.objects.filter(nfinstid=nfinstid)
165 notification_content['vnfmInstId'] = nfInsts[0].vnfminstid if nfInsts[0].vnfminstid else '1'
166 logger.info('Notify request data = %s' % notification_content)
167 return notification_content