Implement of reporting port instance data from GVNFM to NFVO
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / msapi / gvnfmdriver.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 import logging
17 import uuid
18
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
25 )
26
27 logger = logging.getLogger(__name__)
28
29
30 def get_packageinfo_by_vnfdid(vnfdid):
31     ret = req_by_msb('api/gvnfmdriver/v1/vnfpackages', 'GET')
32     if ret[0] != 0:
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])
36
37
38 def apply_grant_to_nfvo(data):
39     ret = req_by_msb('api/gvnfmdriver/v1/resource/grant', 'PUT', data)
40     if ret[0] != 0:
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])
44
45
46 def notify_lcm_to_nfvo(data):
47     ret = req_by_msb('api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification', 'POST', data)
48     if ret[0] != 0:
49         logger.error('Status code is %s, detail is %s.', ret[2], ret[1])
50         raise NFLCMException('Nf lcm notify exception')
51     return ret[1]
52
53
54 def prepare_notification_data(nfinstid, jobid, changetype, operation):
55     logger.info('Send notify request to nfvo')
56     affected_vnfcs = []
57     vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
58     for vnfc in vnfcs:
59         vm_resource = {}
60         if vnfc.vmid:
61             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
62             if vm:
63                 vm_resource = {
64                     'vimConnectionId': vm[0].vimid,
65                     'resourceId': vm[0].resourceid,
66                     'resourceProviderId': vm[0].vmname,  # TODO: is resourceName mapped to resourceProviderId?
67                     'vimLevelResourceType': 'vm'
68                 }
69         affected_vnfcs.append({
70             'id': vnfc.vnfcinstanceid,
71             'vduId': vnfc.vduid,
72             'changeType': changetype,
73             'computeResource': vm_resource
74         })
75     affected_vls = []
76     networks = NetworkInstModel.objects.filter(instid=nfinstid)
77     for network in networks:
78         network_resource = {
79             'vimConnectionId': network.vimid,
80             'resourceId': network.resourceid,
81             'resourceProviderId': network.name,  # TODO: is resourceName mapped to resourceProviderId?
82             'vimLevelResourceType': 'network'
83         }
84         affected_vls.append({
85             'id': network.networkid,
86             'virtualLinkDescId': network.nodeId,
87             'changeType': changetype,
88             'networkResource': network_resource
89         })
90     ext_connectivity = []
91     ext_connectivity_map = {}
92     ports = PortInstModel.objects.filter(instid=nfinstid)
93     for port in ports:
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?
98             'resourceHandle': {
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
110             },
111             'cpInstanceId': port.portid  # TODO: port.cpinstanceid is not initiated when create port resource.
112         })
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
116         network_resource = {
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'
121         }
122         ext_connectivity.append({
123             'id': network_id,
124             'resourceHandle': network_resource,
125             'changeType': changetype,
126             'extLinkPorts': ext_link_ports
127         })
128     logger.debug("ext_connectivity=%s", ext_connectivity)
129     affected_vss = []
130     vss = StorageInstModel.objects.filter(instid=nfinstid)
131     for vs in vss:
132         affected_vss.append({
133             'id': vs.storageid,
134             'virtualStorageDescId': vs.nodeId,
135             'changeType': changetype,
136             'storageResource': {
137                 'vimConnectionId': vs.vimid,
138                 'resourceId': vs.resourceid,
139                 'resourceProviderId': vs.name,  # TODO: is resourceName mapped to resourceProviderId?
140                 'vimLevelResourceType': 'volume'
141             }
142         })
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,
158         '_links': {
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}
162         }
163     }
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