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'
104 'cpInstanceId': port.portid # TODO: port.cpinstanceid is not initiated when create port resource.
106 for network_id, ext_link_ports in ext_connectivity_map.items():
107 networks = NetworkInstModel.objects.filter(networkid=network_id)
108 net_name = networks[0].name if networks else network_id
110 'vimConnectionId': ext_link_ports[0]['resourceHandle']['vimConnectionId'],
111 'resourceId': network_id,
112 'resourceProviderId': net_name, # TODO: is resourceName mapped to resourceProviderId?
113 'vimLevelResourceType': 'network'
115 ext_connectivity.append({
117 'resourceHandle': network_resource,
118 'extLinkPorts': ext_link_ports
120 logger.debug("ext_connectivity=%s", ext_connectivity)
122 vss = StorageInstModel.objects.filter(instid=nfinstid)
124 affected_vss.append({
126 'virtualStorageDescId': vs.nodeId,
127 'changeType': changetype,
129 'vimConnectionId': vs.vimid,
130 'resourceId': vs.resourceid,
131 'resourceProviderId': vs.name, # TODO: is resourceName mapped to resourceProviderId?
132 'vimLevelResourceType': 'volume'
135 notification_content = {
136 'id': str(uuid.uuid4()), # shall be the same if sent multiple times due to multiple subscriptions.
137 'notificationType': 'VnfLcmOperationOccurrenceNotification',
138 # set 'subscriptionId' after filtering for subscribers
139 'timeStamp': now_time(),
140 'notificationStatus': 'RESULT',
141 'operationState': 'COMPLETED',
142 'vnfInstanceId': nfinstid,
143 'operation': operation,
144 'isAutomaticInvocation': False,
145 'vnfLcmOpOccId': jobid,
146 'affectedVnfcs': affected_vnfcs,
147 'affectedVirtualLinks': affected_vls,
148 'affectedVirtualStorages': affected_vss,
149 'changedExtConnectivity': [], # TODO: will add in R4
151 'vnfInstance': {'href': '/api/vnflcm/v1/vnf_instances/%s' % nfinstid},
152 # set 'subscription' link after filtering for subscribers
153 'vnfLcmOpOcc': {'href': '/api/vnflcm/v1/vnf_lcm_op_occs/%s' % jobid}
156 nfInsts = NfInstModel.objects.filter(nfinstid=nfinstid)
157 notification_content['vnfmInstId'] = nfInsts[0].vnfminstid if nfInsts[0].vnfminstid else '1'
158 logger.info('Notify request data = %s' % notification_content)
159 return notification_content