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.
18 from lcm.pub.exceptions import NFLCMException
19 from lcm.pub.utils.restcall import req_by_msb
20 from lcm.pub.database.models import (
21 NfInstModel, VmInstModel, NetworkInstModel,
22 PortInstModel, StorageInstModel, VNFCInstModel
25 logger = logging.getLogger(__name__)
28 def get_packageinfo_by_vnfdid(vnfdid):
29 ret = req_by_msb("api/gvnfmdriver/v1/vnfpackages", "GET")
31 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
32 raise NFLCMException("Failed to query package_info of vnfdid(%s) from nslcm." % vnfdid)
33 return json.JSONDecoder().decode(ret[1])
36 def apply_grant_to_nfvo(data):
37 ret = req_by_msb("api/gvnfmdriver/v1/resource/grant", "PUT", data)
39 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
40 raise NFLCMException("Nf instancing apply grant exception")
41 return json.JSONDecoder().decode(ret[1])
44 def notify_lcm_to_nfvo(data):
45 ret = req_by_msb("api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification", "POST", data)
47 logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
48 raise NFLCMException("Nf lcm notify exception")
52 def prepare_notification_data(nfinstid, jobid, changetype):
53 logger.info('Send notify request to nfvo')
55 vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
59 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
63 'resourceId': vm[0].resourceid,
64 'resourceProviderId': vm[0].vmname, # TODO: is resourceName mapped to resourceProviderId?
65 'vimLevelResourceType': 'vm'
67 affected_vnfcs.append({
68 'id': vnfc.vnfcinstanceid,
70 'changeType': changetype,
71 'computeResource': vm_resource
74 networks = NetworkInstModel.objects.filter(instid=nfinstid)
75 for network in networks:
77 'vimConnectionId': network.vimid,
78 'resourceId': network.resourceid,
79 'resourceProviderId': network.name, # TODO: is resourceName mapped to resourceProviderId?
80 'vimLevelResourceType': 'network'
83 'id': network.networkid,
84 'virtualLinkDescId': network.nodeId,
85 'changeType': changetype,
86 'networkResource': network_resource
89 ports = PortInstModel.objects.filter(instid=nfinstid)
91 ext_link_ports.append({
92 'id': port.portid, # TODO: port.portid or port.nodeid?
94 'vimConnectionId': port.vimid,
95 'resourceId': port.resourceid,
96 'resourceProviderId': port.name, # TODO: is resourceName mapped to resourceProviderId?
97 'vimLevelResourceType': 'port'
99 'cpInstanceId': port.cpinstanceid # TODO: port.cpinstanceid is not initiated when create port resource.
102 vss = StorageInstModel.objects.filter(instid=nfinstid)
104 affected_vss.append({
106 'virtualStorageDescId': vs.nodeId,
107 'changeType': changetype,
109 'vimConnectionId': vs.vimid,
110 'resourceId': vs.resourceid,
111 'resourceProviderId': vs.name, # TODO: is resourceName mapped to resourceProviderId?
112 'vimLevelResourceType': 'volume'
115 notification_content = {
116 "notificationType": 'VnfLcmOperationOccurrenceNotification',
117 "notificationStatus": 'RESULT',
118 "vnfInstanceId": nfinstid,
119 "operation": 'INSTANTIATE',
120 "vnfLcmOpOccId": jobid,
121 'affectedVnfcs': affected_vnfcs,
122 'affectedVirtualLinks': affected_vls,
123 'affectedVirtualStorages': affected_vss,
124 'chengedExtConnectivity': [{
126 'resourceHandle': None, # TODO
127 'extLinkPorts': ext_link_ports
130 nfInsts = NfInstModel.objects.filter(nfinstid=nfinstid)
131 notification_content['vnfmInstId'] = nfInsts[0].vnfminstid
132 logger.info('Notify request data = %s' % notification_content)
133 return notification_content