1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
22 logger = logging.getLogger(__name__)
25 'operationState': 'operation_state',
26 'stateEnteredTime': 'state_entered_time',
27 'startTime': 'start_time',
28 'nsInstanceId': 'ns_instance_id',
29 'operation': 'operation'
33 class QueryNsLcmOpOcc:
34 def __init__(self, data, lcm_op_occ_id=''):
35 self.ns_lcm_op_occ_id = lcm_op_occ_id
38 def query_multi_ns_lcm_op_occ(self):
40 logger.debug("QueryMultiNsLcmOpOccs--get--biz::> Check for filters in query params" % self.params)
41 for query, value in self.params.iteritems():
43 query_data[FILTERS[query]] = value
44 # Query the database with filters if the request has fields in request params, else fetch all records
46 lcm_ops = NSLcmOpOccModel.objects.filter(**query_data)
48 lcm_ops = NSLcmOpOccModel.objects.all()
49 if not lcm_ops.exists():
51 # raise NSLCMException('LCM Operation Occurances do not exist')
52 return [self.fill_resp_data(lcm_op) for lcm_op in lcm_ops]
54 def fill_resp_data(self, lcm_op):
55 NS_LCM_OP_OCC_URI = NS_OCC_BASE_URI % 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),
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)
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])