763b1974a7b7148d3f78799625d8deffb929eedc
[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
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
23 )
24
25 logger = logging.getLogger(__name__)
26
27
28 def get_packageinfo_by_vnfdid(vnfdid):
29     ret = req_by_msb("api/gvnfmdriver/v1/vnfpackages", "GET")
30     if ret[0] != 0:
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])
34
35
36 def apply_grant_to_nfvo(data):
37     ret = req_by_msb("api/gvnfmdriver/v1/resource/grant", "PUT", data)
38     if ret[0] != 0:
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])
42
43
44 def notify_lcm_to_nfvo(data):
45     ret = req_by_msb("api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification", "POST", data)
46     if ret[0] != 0:
47         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
48         raise NFLCMException("Nf lcm notify exception")
49     return ret[1]
50
51
52 def prepare_notification_data(nfinstid, jobid, changetype):
53     logger.info('Send notify request to nfvo')
54     affected_vnfcs = []
55     vnfcs = VNFCInstModel.objects.filter(instid=nfinstid)
56     for vnfc in vnfcs:
57         vm_resource = {}
58         if vnfc.vmid:
59             vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
60             if vm:
61                 vm_resource = {
62                     'vimId': vm[0].vimid,
63                     'resourceId': vm[0].resourceid,
64                     'resourceProviderId': vm[0].vmname,  # TODO: is resourceName mapped to resourceProviderId?
65                     'vimLevelResourceType': 'vm'
66                 }
67         affected_vnfcs.append({
68             'id': vnfc.vnfcinstanceid,
69             'vduId': vnfc.vduid,
70             'changeType': changetype,
71             'computeResource': vm_resource
72         })
73     affected_vls = []
74     networks = NetworkInstModel.objects.filter(instid=nfinstid)
75     for network in networks:
76         network_resource = {
77             'vimConnectionId': network.vimid,
78             'resourceId': network.resourceid,
79             'resourceProviderId': network.name,  # TODO: is resourceName mapped to resourceProviderId?
80             'vimLevelResourceType': 'network'
81         }
82         affected_vls.append({
83             'id': network.networkid,
84             'virtualLinkDescId': network.nodeId,
85             'changeType': changetype,
86             'networkResource': network_resource
87         })
88     ext_link_ports = []
89     ports = PortInstModel.objects.filter(instid=nfinstid)
90     for port in ports:
91         ext_link_ports.append({
92             'id': port.portid,  # TODO: port.portid or port.nodeid?
93             'resourceHandle': {
94                 'vimConnectionId': port.vimid,
95                 'resourceId': port.resourceid,
96                 'resourceProviderId': port.name,  # TODO: is resourceName mapped to resourceProviderId?
97                 'vimLevelResourceType': 'port'
98             },
99             'cpInstanceId': port.cpinstanceid  # TODO: port.cpinstanceid is not initiated when create port resource.
100         }),
101     affected_vss = []
102     vss = StorageInstModel.objects.filter(instid=nfinstid)
103     for vs in vss:
104         affected_vss.append({
105             'id': vs.storageid,
106             'virtualStorageDescId': vs.nodeId,
107             'changeType': changetype,
108             'storageResource': {
109                 'vimConnectionId': vs.vimid,
110                 'resourceId': vs.resourceid,
111                 'resourceProviderId': vs.name,  # TODO: is resourceName mapped to resourceProviderId?
112                 'vimLevelResourceType': 'volume'
113             }
114         })
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': [{
125             'id': None,  # TODO
126             'resourceHandle': None,  # TODO
127             'extLinkPorts': ext_link_ports
128         }]
129     }
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