Split the vim_id in LCM
[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 from lcm.ns.const import NS_OCC_BASE_URI, NS_INSTANCE_BASE_URI
21
22 logger = logging.getLogger(__name__)
23 FILTERS = {
24     'id': 'id',
25     'operationState': 'operation_state',
26     'stateEnteredTime': 'state_entered_time',
27     'startTime': 'start_time',
28     'nsInstanceId': 'ns_instance_id',
29     'operation': 'operation'
30 }
31
32
33 class QueryNsLcmOpOcc:
34     def __init__(self, data, lcm_op_occ_id=''):
35         self.ns_lcm_op_occ_id = lcm_op_occ_id
36         self.params = data
37
38     def query_multi_ns_lcm_op_occ(self):
39         query_data = {}
40         logger.debug("QueryMultiNsLcmOpOccs--get--biz::> Check for filters in query params" % self.params)
41         for query, value in self.params.iteritems():
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 = NSLcmOpOccModel.objects.filter(**query_data)
47         else:
48             lcm_ops = NSLcmOpOccModel.objects.all()
49         if not lcm_ops.exists():
50             return []
51             # raise NSLCMException('LCM Operation Occurances do not exist')
52         return [self.fill_resp_data(lcm_op) for lcm_op in lcm_ops]
53
54     def fill_resp_data(self, lcm_op):
55         NS_LCM_OP_OCC_URI = NS_OCC_BASE_URI % lcm_op.id
56         resp_data = {
57             'id': lcm_op.id,
58             'operationState': lcm_op.operation_state,
59             'stateEnteredTime': lcm_op.state_entered_time,
60             'startTime': lcm_op.start_time,
61             'nsInstanceId': lcm_op.ns_instance_id,
62             'operation': lcm_op.operation,
63             'isAutomaticInvocation': lcm_op.is_automatic_invocation,
64             'operationParams': json.loads(lcm_op.operation_params),
65             'isCancelPending': lcm_op.is_cancel_pending,
66             'cancelMode': lcm_op.cancel_mode,
67             'error': None if not lcm_op.error else json.loads(lcm_op.error),
68             'resourceChanges': None if not lcm_op.resource_changes else json.loads(lcm_op.resource_changes),
69             '_links': {
70                 'self': {'href': NS_LCM_OP_OCC_URI},
71                 'nsInstance': {'href': NS_INSTANCE_BASE_URI % lcm_op.ns_instance_id},
72                 'retry': {'href': NS_LCM_OP_OCC_URI + '/retry'},
73                 'rollback': {'href': NS_LCM_OP_OCC_URI + '/rollback'},
74                 'continue': {'href': NS_LCM_OP_OCC_URI + '/continue'},
75                 'fail': {'href': NS_LCM_OP_OCC_URI + '/fail'},
76                 'cancel': {'href': NS_LCM_OP_OCC_URI + '/cancel'}
77             }  # json.loads(lcm_op.links)
78         }
79         return resp_data
80
81     def query_single_ns_lcm_op_occ(self):
82         lcm_op = NSLcmOpOccModel.objects.filter(id=self.ns_lcm_op_occ_id)
83         if not lcm_op.exists():
84             raise NSLCMException('LCM Operation Occurance does not exist')
85         resp_data = self.fill_resp_data(lcm_op[0])
86         return resp_data