fix nslcm middleware
[vfc/nfvo/lcm.git] / lcm / ns / biz / query_ns_lcm_op_occ.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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 NSLcmOpOccModel
19 from lcm.pub.exceptions import NSLCMException
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     'nsInstanceId': 'ns_instance_id',
28     'operation': 'operation'
29 }
30
31
32 class QueryNsLcmOpOcc:
33     def __init__(self, data, lcm_op_occ_id=''):
34         self.ns_lcm_op_occ_id = lcm_op_occ_id
35         self.params = data
36
37     def query_multi_ns_lcm_op_occ(self):
38         query_data = {}
39         logger.debug("QueryMultiNsLcmOpOccs--get--biz::> Check for filters in query params" % self.params)
40         for query, value in self.params.iteritems():
41             if query in FILTERS:
42                 query_data[FILTERS[query]] = value
43         # Query the database with filters if the request has fields in request params, else fetch all records
44         if query_data:
45             lcm_ops = NSLcmOpOccModel.objects.filter(**query_data)
46         else:
47             lcm_ops = NSLcmOpOccModel.objects.all()
48         if not lcm_ops.exists():
49             return []
50             # raise NSLCMException('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             'nsInstanceId': lcm_op.ns_instance_id,
60             'operation': lcm_op.operation,
61             'isAutomaticInvocation': lcm_op.is_automatic_invocation,
62             'operationParams': json.loads(lcm_op.operation_params),
63             'isCancelPending': lcm_op.is_cancel_pending,
64             'cancelMode': lcm_op.cancel_mode,
65             'error': None if not lcm_op.error else json.loads(lcm_op.error),
66             'resourceChanges': None if not lcm_op.resource_changes else json.loads(lcm_op.resource_changes),
67             '_links': json.loads(lcm_op.links)
68         }
69         return resp_data
70
71     def query_single_ns_lcm_op_occ(self):
72         lcm_op = NSLcmOpOccModel.objects.filter(id=self.ns_lcm_op_occ_id)
73         if not lcm_op.exists():
74             raise NSLCMException('LCM Operation Occurance does not exist')
75         resp_data = self.fill_resp_data(lcm_op[0])
76         return resp_data