Add lcm op occ for chg ext conn
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / operate_vnf_lcm_op_occ.py
1 # Copyright (C) 2019 ZTE. All Rights Reserved
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 json
16 import logging
17 from multiprocessing import Lock
18
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
25
26 logger = logging.getLogger(__name__)
27
28 MUTEX_UPD_OCC = Lock()
29
30
31 """
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
38 """
39
40
41 class VnfLcmOpOcc:
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
46         self.task = task
47
48     def add(self):
49         href_params = {
50             "id": self.vnf_inst_id,
51             "task": self.task,
52             "prefix": const.URL_PREFIX
53         }
54         href = "%(prefix)s/vnf_instances/%(id)s/%(task)s" % href_params
55         if href.endswith("/"):
56             href = href[:-1]
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,
62                          grant_id=None,
63                          operation=self.operation,
64                          is_automatic_invocation=False,
65                          operation_params='{}',
66                          is_cancel_pending=False,
67                          cancel_mode=None,
68                          error=None,
69                          resource_changes=None,
70                          changed_ext_connectivity=None,
71                          links=json.dumps({
72                              "self": {
73                                  "href": href
74                              },
75                              "vnfInstance": {
76                                  "href": self.vnf_inst_id
77                              }
78                          })).save()
79
80     def upd(self, operation_state=None, sub_operation=None, error=None):
81         occ = VNFLcmOpOccModel.objects.filter(id=self.lcm_op_id)
82         with MUTEX_UPD_OCC:
83             if operation_state:
84                 occ.update(operation_state=operation_state)
85             if sub_operation:
86                 occ.update(sub_operation=sub_operation)
87             if error:
88                 occ.update(error=json.dumps(error))
89
90     def get(self):
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)
94         return lcm_op_occ_obj
95
96     def is_in_processing(self):
97         lcm_op_occ = VNFLcmOpOccModel.objects.filter(vnf_instance_id=self.vnf_inst_id)
98
99         for occ in lcm_op_occ:
100             if occ.operation_state not in const.FINAL_STATE_RANGE:
101                 return True, occ.operation
102         return False, None
103
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)
109         if error:
110             data['error'] = error
111
112         logger.debug('Notification data: %s' % data)
113         return NotificationsUtil().send_notification(data)