fix nslcm middleware
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_lcm_op_occ.py
1 # Copyright 2019 ZTE Corporation.
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 logging
16 import datetime
17 import uuid
18
19 from lcm.pub.database.models import NSLcmOpOccModel
20 from lcm.pub.utils.values import update_value
21
22
23 logger = logging.getLogger(__name__)
24
25
26 class NsLcmOpOcc(object):
27     @staticmethod
28     def create(nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams):
29         logger.debug("lcm_op_occ(%s,%s,%s,%s,%s) create begin." % (nsInstanceId, lcmOperationType, operationState, isAutomaticInvocation, operationParams))
30         cur_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
31         lcm_op_occ = NSLcmOpOccModel.objects.create(
32             id=str(uuid.uuid4()),
33             operation_state=operationState,
34             state_entered_time=cur_time,
35             start_time=cur_time,
36             ns_instance_id=nsInstanceId,
37             operation=lcmOperationType,
38             is_automatic_invocation=isAutomaticInvocation,
39             operation_params=operationParams,
40             is_cancel_pending=False
41         )
42         logger.debug("lcm_op_occ(%s) create successfully." % lcm_op_occ.id)
43         return lcm_op_occ.id
44
45     @staticmethod
46     def update(occ_id, operationState=None, isCancelPending=None, cancelMode=None, error=None, resourceChanges=None):
47         lcm_op_occ = NSLcmOpOccModel.objects.get(id=occ_id)
48         if operationState:
49             lcm_op_occ.operation_state = operationState
50             lcm_op_occ.state_entered_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
51         if isCancelPending:
52             lcm_op_occ.is_cancel_pending = isCancelPending
53         if cancelMode:
54             lcm_op_occ.cancel_mode = cancelMode
55         if error:
56             lcm_op_occ.error = error
57         if resourceChanges:
58             lcm_op_occ.resource_changes = update_value(lcm_op_occ.resourceChanges if lcm_op_occ.resourceChanges else {}, resourceChanges)
59         lcm_op_occ.save()
60         logger.debug("lcm_op_occ(%s) update successfully." % lcm_op_occ.id)
61         return lcm_op_occ.id