1 # Copyright 2019 ZTE Corporation.
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.
20 from lcm.pub.database.models import NSLcmOpOccModel
21 from lcm.pub.utils.values import update_value
24 logger = logging.getLogger(__name__)
27 class NsLcmOpOcc(object):
29 def create(nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams):
30 logger.debug("lcm_op_occ(%s,%s,%s,%s,%s) create begin." % (nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams))
31 cur_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
32 lcm_op_occ = NSLcmOpOccModel.objects.create(
34 operation_state=operationState,
35 state_entered_time=cur_time,
37 ns_instance_id=nsInstanceId,
38 operation=lcmOperationType,
39 is_automatic_invocation=isAutomaticInvocation,
40 operation_params=json.dumps(operationParams),
41 is_cancel_pending=False
43 logger.debug("lcm_op_occ(%s) create successfully." % lcm_op_occ.id)
47 def update(occ_id, operationState=None, isCancelPending=None, cancelMode=None, error=None, resourceChanges=None):
48 lcm_op_occ = NSLcmOpOccModel.objects.get(id=occ_id)
50 lcm_op_occ.operation_state = operationState
51 lcm_op_occ.state_entered_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
53 lcm_op_occ.is_cancel_pending = isCancelPending
55 lcm_op_occ.cancel_mode = cancelMode
57 lcm_op_occ.error = error
59 lcm_op_occ.resource_changes = update_value(lcm_op_occ.resourceChanges if lcm_op_occ.resourceChanges else {}, resourceChanges)
61 logger.debug("lcm_op_occ(%s) update successfully." % lcm_op_occ.id)