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):
55 logger.info('Send notify request to nfvo')
57 vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
61 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
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 ports = PortInstModel.objects.filter(instid=nfinstid)
93 ext_link_ports.append({
94 'id': port.portid, # TODO: port.portid or port.nodeid?
96 'vimConnectionId': port.vimid,
97 'resourceId': port.resourceid,
98 'resourceProviderId': port.name, # TODO: is resourceName mapped to resourceProviderId?
99 'vimLevelResourceType': 'port'
101 'cpInstanceId': port.cpinstanceid # TODO: port.cpinstanceid is not initiated when create port resource.
104 vss = StorageInstModel.objects.filter(instid=nfinstid)
106 affected_vss.append({
108 'virtualStorageDescId': vs.nodeId,
109 'changeType': changetype,
111 'vimConnectionId': vs.vimid,
112 'resourceId': vs.resourceid,
113 'resourceProviderId': vs.name, # TODO: is resourceName mapped to resourceProviderId?
114 'vimLevelResourceType': 'volume'
117 notification_content = {
118 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
119 'notificationType': 'VnfLcmOperationOccurrenceNotification',
120 # set 'subscriptionId' after filtering for subscribers
121 'timeStamp': now_time(),
122 'notificationStatus': 'RESULT',
123 'operationState': 'COMPLETED',
124 'vnfInstanceId': nfinstid,
125 'operation': 'INSTANTIATE',
126 'isAutomaticInvocation': False,
127 'vnfLcmOpOccId': jobid,
128 'affectedVnfcs': affected_vnfcs,
129 'affectedVirtualLinks': affected_vls,
130 'affectedVirtualStorages': affected_vss,
131 'chengedExtConnectivity': [{
133 'resourceHandle': None, # TODO
134 'extLinkPorts': ext_link_ports
137 'vnfInstance': {'href': '/api/vnflcm/v1/vnf_instances/%s' % nfinstid},
138 # set 'subscription' link after filtering for subscribers
139 'vnfLcmOpOcc': {'href': '/api/vnflcm/v1/vnf_lcm_op_occs/%s' % jobid}
142 nfInsts = NfInstModel.objects.filter(nfinstid=nfinstid)
143 notification_content['vnfmInstId'] = nfInsts[0].vnfminstid
144 logger.info('Notify request data = %s' % notification_content)
145 return notification_content