Merge "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 import json
19
20 from lcm.pub.database.models import NSLcmOpOccModel
21 from lcm.pub.utils.values import update_value
22
23
24 logger = logging.getLogger(__name__)
25
26
27 class NsLcmOpOcc(object):
28     @staticmethod
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(
33             id=str(uuid.uuid4()),
34             operation_state=operationState,
35             state_entered_time=cur_time,
36             start_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
42         )
43         logger.debug("lcm_op_occ(%s) create successfully." % lcm_op_occ.id)
44         return lcm_op_occ.id
45
46     @staticmethod
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)
49         if operationState:
50             lcm_op_occ.operation_state = operationState
51             lcm_op_occ.state_entered_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
52         if isCancelPending:
53             lcm_op_occ.is_cancel_pending = isCancelPending
54         if cancelMode:
55             lcm_op_occ.cancel_mode = cancelMode
56         if error:
57             lcm_op_occ.error = error
58         if resourceChanges:
59             lcm_op_occ.resource_changes = update_value(lcm_op_occ.resourceChanges if lcm_op_occ.resourceChanges else {}, resourceChanges)
60         lcm_op_occ.save()
61         logger.debug("lcm_op_occ(%s) update successfully." % lcm_op_occ.id)
62         return lcm_op_occ.id