vfclcm upgrade from python2 to python3
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / query_vnf_lcm_op_occ.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
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 VNFLcmOpOccModel
19 from lcm.pub.exceptions import NFLCMException
20
21 logger = logging.getLogger(__name__)
22 FILTERS = {
23     'id': 'id',
24     'operationState': 'operation_state',
25     'stateEnteredTime': 'state_entered_time',
26     'startTime': 'start_time',
27     'vnfInstanceId': 'vnf_instance_id',
28     'grantId': 'grant_id',
29     'operation': 'operation'
30 }
31
32
33 class QueryVnfLcmOpOcc:
34     def __init__(self, data, lcm_op_occ_id=''):
35         self.vnf_lcm_op_occ_id = lcm_op_occ_id
36         self.params = data
37
38     def query_multi_vnf_lcm_op_occ(self):
39         query_data = {}
40         logger.debug("QueryMultiVnfLcmOpOccs--get--biz::> Check for filters in query params" % self.params)
41         for query, value in list(self.params.items()):
42             if query in FILTERS:
43                 query_data[FILTERS[query]] = value
44         # Query the database with filters if the request has fields in request params, else fetch all records
45         if query_data:
46             lcm_ops = VNFLcmOpOccModel.objects.filter(**query_data)
47         else:
48             lcm_ops = VNFLcmOpOccModel.objects.all()
49         if not lcm_ops.exists():
50             raise NFLCMException('LCM Operation Occurances do not exist')
51         return [self.fill_resp_data(lcm_op) for lcm_op in lcm_ops]
52
53     def fill_resp_data(self, lcm_op):
54         resp_data = {
55             'id': lcm_op.id,
56             'operationState': lcm_op.operation_state,
57             'stateEnteredTime': lcm_op.state_entered_time,
58             'startTime': lcm_op.start_time,
59             'vnfInstanceId': lcm_op.vnf_instance_id,
60             'grantId': None,
61             'operation': lcm_op.operation,
62             'isAutomaticInvocation': False if lcm_op.is_automatic_invocation == 'False' else True,
63             'operationParams': json.loads(lcm_op.operation_params),
64             'isCancelPending': False if lcm_op.is_cancel_pending == 'False' else True,
65             'cancelMode': lcm_op.cancel_mode,
66             'error': None if not lcm_op.error else json.loads(lcm_op.error),
67             'resourceChanges': None if not lcm_op.resource_changes else json.loads(lcm_op.resource_changes),
68             'changedInfo': None if not lcm_op.changed_info else json.loads(lcm_op.changed_info),
69             'changedExtConnectivity': None if not lcm_op.changed_ext_connectivity else json.loads(lcm_op.changed_ext_connectivity),
70             '_links': json.loads(lcm_op.links)
71         }
72         return resp_data
73
74     def query_single_vnf_lcm_op_occ(self):
75         lcm_op = VNFLcmOpOccModel.objects.filter(id=self.vnf_lcm_op_occ_id)
76         if not lcm_op.exists():
77             raise NFLCMException('LCM Operation Occurance does not exist')
78         resp_data = self.fill_resp_data(lcm_op[0])
79         return resp_data