dd22c06ec873be395d7bec229fdd364c4e45a768
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / grant_vnf.py
1 # Copyright 2018 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.database.models import NfInstModel
19 from lcm.pub.msapi.gvnfmdriver import apply_grant_to_nfvo
20 from lcm.pub.utils.values import ignore_case_get
21 from lcm.nf.const import GRANT_TYPE
22
23 logger = logging.getLogger(__name__)
24
25
26 def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
27     logger.info("Grant resource begin")
28
29     content_args = {
30         'vnfInstanceId': nf_inst_id,
31         'vnfLcmOpOccId': job_id,
32         # 'vnfdId': None,  # TODO
33         # 'flavourId': "default",  # TODO
34         'operation': grant_type,
35         'isAutomaticInvocation': True,  # TODO
36         # 'instantiationLevelId': None,  # TODO
37         # 'addResources': [],
38         # 'tmpResources': [],
39         # 'updateResources': [],
40         # 'removeResources': [],
41         # 'placementConstraints': [],
42         # 'vimConstraints': [],
43         # 'additionalParams': {}
44         # '_links': None  # TODO
45     }
46
47     if grant_type == GRANT_TYPE.TERMINATE:
48         res_index = 1
49         content_args['removeResources'] = []
50         for vdu in vdus:
51             res_def = {
52                 'id': str(res_index),
53                 'type': 'COMPUTE',
54                 'vduId': None,
55                 'resourceTemplateId': None,
56                 'resource': {
57                     'vimConnectionId': None,
58                     'resourceProviderId': None,
59                     'resourceid': vdu.resourceid,
60                     'vimLevelResourceType': None
61                 }
62             }
63             content_args['removeResources'].append(res_def)
64             res_index += 1
65         if vdus[0].vimid:
66             content_args['additionalParams'] = {}
67             content_args['additionalParams']['vimid'] = vdus[0].vimid
68     elif grant_type == GRANT_TYPE.INSTANTIATE:
69         vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
70         res_index = 1
71         content_args['addResources'] = []
72         for vdu in vdus:
73             res_def = {
74                 'id': str(res_index),
75                 'type': 'COMPUTE',
76                 # 'vduId': vdu["vdu_id"],
77                 'resourceTemplateId': vdu["vdu_id"]  # , None,
78                 # 'resource': None
79             }
80             content_args['addResources'].append(res_def)
81             res_index += 1
82         if vim_id:
83             content_args['additionalParams'] = {}
84             content_args['additionalParams']['vimid'] = vim_id
85     elif grant_type == GRANT_TYPE.OPERATE:
86         res_index = 1
87         content_args['updateResources'] = []
88         for vdu in vdus:
89             res_def = {
90                 'id': str(res_index),
91                 'type': 'COMPUTE',
92                 'vduId': None,
93                 'resourceTemplateId': None,
94                 'resource': {
95                     'vimConnectionId': None,
96                     'resourceProviderId': None,
97                     'resourceid': vdu.resourceid,
98                     'vimLevelResourceType': None
99                 }
100             }
101             content_args['updateResources'].append(res_def)
102             res_index += 1
103         if vdus[0].vimid:
104             content_args['additionalParams'] = {}
105             content_args['additionalParams']['vimid'] = vdus[0].vimid
106
107     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
108     if vnfInsts[0].vnfminstid:
109         content_args['additionalParams'] = {}
110         content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
111     logger.info('Grant request data=%s' % content_args)
112     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
113     logger.info("apply_result: %s" % apply_result)
114     return apply_result