Comment changedExtConnectivity param in vnf notify
[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             },
104             'cpInstanceId': port.portid  # TODO: port.cpinstanceid is not initiated when create port resource.
105         })
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
109         network_resource = {
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'
114         }
115         ext_connectivity.append({
116             'id': network_id,
117             'resourceHandle': network_resource,
118             'extLinkPorts': ext_link_ports
119         })
120     logger.debug("ext_connectivity=%s", ext_connectivity)
121     affected_vss = []
122     vss = StorageInstModel.objects.filter(instid=nfinstid)
123     for vs in vss:
124         affected_vss.append({
125             'id': vs.storageid,
126             'virtualStorageDescId': vs.nodeId,
127             'changeType': changetype,
128             'storageResource': {
129                 'vimConnectionId': vs.vimid,
130                 'resourceId': vs.resourceid,
131                 'resourceProviderId': vs.name,  # TODO: is resourceName mapped to resourceProviderId?
132                 'vimLevelResourceType': 'volume'
133             }
134         })
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
150         '_links': {
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}
154         }
155     }
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