Add operate api to GVNFM
[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     lifecycleOperration = grant_type
29
30     content_args = {
31         'vnfInstanceId': nf_inst_id,
32         'vnfDescriptorId': '',
33         'lifecycleOperation': lifecycleOperration,
34         'vnfLcmOpOccId': job_id,
35         'addResources': [],
36         'updateResources': [],
37         'removeResources': [],
38         'placementConstraints': [],
39         'additionalParams': {}
40     }
41
42     if grant_type == "Terminate":
43         res_index = 1
44         for vdu in vdus:
45             res_def = {
46                 'type': 'VDU',
47                 'resDefId': str(res_index),
48                 'resDesId': vdu.resouceid}
49             content_args['removeResources'].append(res_def)
50             res_index += 1
51         content_args['additionalParams']['vimid'] = vdus[0].vimid
52     elif grant_type == "Instantiate":
53         vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
54         res_index = 1
55         for vdu in vdus:
56             res_def = {
57                 'type': 'VDU',
58                 'resDefId': str(res_index),
59                 'resDesId': ignore_case_get(vdu, "vdu_id")
60             }
61             content_args['addResources'].append(res_def)
62             res_index += 1
63         content_args['additionalParams']['vimid'] = vim_id
64     elif grant_type == GRANT_TYPE.OPERATE:
65         res_index = 1
66         for vdu in vdus:
67             res_def = {
68                 'type': 'VDU',
69                 'resDefId': str(res_index),
70                 'resDesId': vdu.resouceid}
71             content_args['updateResources'].append(res_def)
72             res_index += 1
73         content_args['additionalParams']['vimid'] = vdus[0].vimid
74
75     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
76     content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
77     logger.info('Grant request data=%s' % content_args)
78     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
79     return apply_result