1 # Copyright (C) 2019 ZTE. All Rights Reserved
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.
17 from multiprocessing import Lock
19 from lcm.nf import const
20 from lcm.pub.database.models import VNFLcmOpOccModel
21 from lcm.pub.exceptions import NFLCMExceptionNotFound
22 from lcm.pub.utils.notificationsutil import NotificationsUtil
23 from lcm.pub.utils.notificationsutil import prepare_notification
24 from lcm.pub.utils.timeutil import now_time
26 logger = logging.getLogger(__name__)
28 MUTEX_UPD_OCC = Lock()
32 operation: INSTANTIATE, SCALE, SCALE_TO_LEVEL, CHANGE_FLAVOUR,
33 TERMINATE, HEAL, OPERATE, CHANGE_EXT_CONN, MODIFY_INFO
34 task: instantiate, scale, scale_to_level, change_flavour
35 operate, heal, change_ext_conn, terminate
36 operation_state: STARTING, PROCESSING, COMPLETED, FAILED_TEMP,
37 FAILED, ROLLING_BACK, ROLLED_BACK
42 def __init__(self, vnf_inst_id, lcm_op_id, operation, task):
43 self.vnf_inst_id = vnf_inst_id
44 self.lcm_op_id = lcm_op_id
45 self.operation = operation
50 "id": self.vnf_inst_id,
52 "prefix": const.URL_PREFIX
54 href = "%(prefix)s/vnf_instances/%(id)s/%(task)s" % href_params
55 if href.endswith("/"):
57 VNFLcmOpOccModel(id=self.lcm_op_id,
58 operation_state=const.OPERATION_STATE_TYPE.STARTING,
59 state_entered_time=now_time(),
60 start_time=now_time(),
61 vnf_instance_id=self.vnf_inst_id,
63 operation=self.operation,
64 is_automatic_invocation=False,
65 operation_params='{}',
66 is_cancel_pending=False,
69 resource_changes=None,
70 changed_ext_connectivity=None,
76 "href": self.vnf_inst_id
80 def upd(self, operation_state=None, sub_operation=None, error=None):
81 occ = VNFLcmOpOccModel.objects.filter(id=self.lcm_op_id)
84 occ.update(operation_state=operation_state)
86 occ.update(sub_operation=sub_operation)
88 occ.update(error=json.dumps(error))
91 lcm_op_occ_obj = VNFLcmOpOccModel.objects.filter(id=self.lcm_op_id).first()
92 if not lcm_op_occ_obj:
93 raise NFLCMExceptionNotFound('Occurrence(%s) does not exist.' % self.lcm_op_id)
96 def is_in_processing(self):
97 lcm_op_occ = VNFLcmOpOccModel.objects.filter(vnf_instance_id=self.vnf_inst_id)
99 for occ in lcm_op_occ:
100 if occ.operation_state not in const.FINAL_STATE_RANGE:
101 return True, occ.operation
104 def notify_lcm(self, operation_state, error=''):
105 data = prepare_notification(nfinstid=self.vnf_inst_id,
106 jobid=self.lcm_op_id,
107 operation=self.operation,
108 operation_state=operation_state)
110 data['error'] = error
112 logger.debug('Notification data: %s' % data)
113 return NotificationsUtil().send_notification(data)