Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / biz / query_ns_lcm_op_occ.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
2 # Copyright (c) 2019, ZTE Corporation.
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import json
17 import logging
18
19 from lcm.pub.database.models import NSLcmOpOccModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.ns.const import NS_OCC_BASE_URI, NS_INSTANCE_BASE_URI
22
23 logger = logging.getLogger(__name__)
24 FILTERS = {
25     'id': 'id',
26     'operationState': 'operation_state',
27     'stateEnteredTime': 'state_entered_time',
28     'startTime': 'start_time',
29     'nsInstanceId': 'ns_instance_id',
30     'operation': 'operation'
31 }
32
33
34 class QueryNsLcmOpOcc:
35     def __init__(self, data, lcm_op_occ_id=''):
36         self.ns_lcm_op_occ_id = lcm_op_occ_id
37         self.params = data
38
39     def query_multi_ns_lcm_op_occ(self):
40         query_data = {}
41         logger.debug("QueryMultiNsLcmOpOccs--get--biz::> Check for filters in query params" % self.params)
42         for query, value in list(self.params.items()):
43             if query in FILTERS:
44                 query_data[FILTERS[query]] = value
45         # Query the database with filters if the request has fields in request params, else fetch all records
46         if query_data:
47             lcm_ops = NSLcmOpOccModel.objects.filter(**query_data)
48         else:
49             lcm_ops = NSLcmOpOccModel.objects.all()
50         if not lcm_ops.exists():
51             return []
52             # raise NSLCMException('LCM Operation Occurances do not exist')
53         return [self.fill_resp_data(lcm_op) for lcm_op in lcm_ops]
54
55     def fill_resp_data(self, lcm_op):
56         NS_LCM_OP_OCC_URI = NS_OCC_BASE_URI % lcm_op.id
57         resp_data = {
58             'id': lcm_op.id,
59             'operationState': lcm_op.operation_state,
60             'stateEnteredTime': lcm_op.state_entered_time,
61             'startTime': lcm_op.start_time,
62             'nsInstanceId': lcm_op.ns_instance_id,
63             'operation': lcm_op.operation,
64             'isAutomaticInvocation': lcm_op.is_automatic_invocation,
65             'operationParams': json.loads(lcm_op.operation_params),
66             'isCancelPending': lcm_op.is_cancel_pending,
67             'cancelMode': lcm_op.cancel_mode,
68             'error': None if not lcm_op.error else json.loads(lcm_op.error),
69             'resourceChanges': None if not lcm_op.resource_changes else json.loads(lcm_op.resource_changes),
70             '_links': {
71                 'self': {'href': NS_LCM_OP_OCC_URI},
72                 'nsInstance': {'href': NS_INSTANCE_BASE_URI % lcm_op.ns_instance_id},
73                 'retry': {'href': NS_LCM_OP_OCC_URI + '/retry'},
74                 'rollback': {'href': NS_LCM_OP_OCC_URI + '/rollback'},
75                 'continue': {'href': NS_LCM_OP_OCC_URI + '/continue'},
76                 'fail': {'href': NS_LCM_OP_OCC_URI + '/fail'},
77                 'cancel': {'href': NS_LCM_OP_OCC_URI + '/cancel'}
78             }  # json.loads(lcm_op.links)
79         }
80         return resp_data
81
82     def query_single_ns_lcm_op_occ(self):
83         lcm_op = NSLcmOpOccModel.objects.filter(id=self.ns_lcm_op_occ_id)
84         if not lcm_op.exists():
85             raise NSLCMException('LCM Operation Occurance does not exist')
86         resp_data = self.fill_resp_data(lcm_op[0])
87         return resp_data