| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import logging |
| 18 | from temporalio import activity |
| 19 | from osm_common.temporal_constants import ( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 20 | ACTIVITY_CHANGE_VNF_STATE, |
| 21 | ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 22 | ACTIVITY_GET_TASK_QUEUE, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 23 | ACTIVITY_GET_VNF_DETAILS, |
| 24 | ACTIVITY_SEND_NOTIFICATION_FOR_VNF, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 25 | ACTIVITY_SET_VNF_MODEL, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 26 | VIM_TYPE_TASK_QUEUE_MAPPINGS, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 27 | ) |
| 28 | from osm_common.dataclasses.temporal_dataclasses import ( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 29 | ChangeVnfInstantiationStateInput, |
| 30 | ChangeVnfStateInput, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 31 | GetTaskQueueInput, |
| 32 | GetTaskQueueOutput, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 33 | GetVnfDetailsInput, |
| 34 | GetVnfDetailsOutput, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 35 | VnfInstantiateInput, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 36 | ) |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 37 | from osm_lcm.data_utils.database.database import Database |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | class VnfOperations: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 41 | def __init__(self, db: Database): |
| 42 | self.db: Database = db |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 43 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 44 | |
| 45 | @activity.defn(name=ACTIVITY_GET_TASK_QUEUE) |
| 46 | async def get_task_queue( |
| 47 | self, get_task_queue_input: GetTaskQueueInput |
| 48 | ) -> GetTaskQueueOutput: |
| 49 | """Finds the appropriate task queue according to VIM type of VNF. |
| 50 | |
| 51 | Collaborators: |
| 52 | DB Access Object |
| 53 | |
| 54 | Raises (retryable): |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 55 | DbException: If DB read operations fail, the collection or DB record ID does not exist. |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 56 | |
| 57 | Activity Lifecycle: |
| 58 | This activity should complete relatively quickly (less than a |
| 59 | second). However, it would be reasonable to wait up to 10 |
| 60 | seconds. |
| 61 | |
| 62 | This activity will not report a heartbeat due to its |
| 63 | short-running nature. |
| 64 | |
| 65 | It is not necessary to implement a back-off strategy for this |
| 66 | activity, the operation is idempotent. |
| 67 | |
| 68 | """ |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 69 | vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid}) |
| 70 | vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]}) |
| Gulsum Atici | 625a654 | 2023-04-18 15:27:18 +0300 | [diff] [blame] | 71 | task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]] |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 72 | self.logger.debug(f"Got the task queue {task_queue} for VNF operations.") |
| 73 | return GetTaskQueueOutput(task_queue) |
| 74 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 75 | @activity.defn(name=ACTIVITY_GET_VNF_DETAILS) |
| 76 | async def get_vnf_details( |
| 77 | self, get_vnf_details_input: GetVnfDetailsInput |
| 78 | ) -> GetVnfDetailsOutput: |
| 79 | """Gets the VNF record and VNF descriptor from Database. |
| 80 | |
| 81 | Collaborators: |
| 82 | DB Access Object |
| 83 | |
| 84 | Raises (retryable): |
| 85 | DbException: If DB read operations fail, the collection or DB record ID does not exist. |
| 86 | |
| 87 | Activity Lifecycle: |
| 88 | This activity should complete relatively quickly (less than 10 |
| 89 | second). |
| 90 | |
| 91 | This activity will not report a heartbeat due to its |
| 92 | short-running nature. |
| 93 | |
| 94 | This is an idempotent activity. |
| 95 | |
| 96 | """ |
| 97 | vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid}) |
| 98 | vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]}) |
| 99 | self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.") |
| 100 | return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd) |
| 101 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 102 | |
| 103 | class VnfDbActivity: |
| 104 | |
| 105 | """Perform Database operations for NS accounts. |
| 106 | |
| 107 | Args: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 108 | db (Database): Data Access Object |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 109 | """ |
| 110 | |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 111 | def __init__(self, db: Database): |
| 112 | self.db: Database = db |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 113 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 114 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 115 | @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE) |
| 116 | async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 117 | """Updates the VNF State in VNFR. |
| 118 | |
| 119 | Collaborators: |
| 120 | DB Access Object |
| 121 | |
| 122 | Raises (retryable): |
| 123 | DbException: If DB access/update fails, the collection or DB record ID does not exist. |
| 124 | |
| 125 | Activity Lifecycle: |
| 126 | This activity should complete relatively quickly (less than a |
| 127 | second). However, it would be reasonable to wait up to 10 |
| 128 | seconds. |
| 129 | |
| 130 | This activity will not report a heartbeat due to its |
| 131 | short-running nature. |
| 132 | |
| 133 | It is not necessary to implement a back-off strategy for this |
| 134 | activity, the operation is idempotent. |
| 135 | |
| 136 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 137 | update_vnf_state = {"vnfState": vnf_state_input.state} |
| 138 | self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 139 | self.logger.debug( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 140 | f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 141 | ) |
| 142 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 143 | @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE) |
| 144 | async def change_vnf_instantiation_state( |
| 145 | self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 146 | ) -> None: |
| 147 | """Updates the VNF Instantiation State in VNFR. |
| 148 | |
| 149 | Collaborators: |
| 150 | DB Access Object |
| 151 | |
| 152 | Raises (retryable): |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 153 | DbException: If DB access or update fails, the collection or DB record ID does not exist. |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 154 | |
| 155 | Activity Lifecycle: |
| 156 | This activity should complete relatively quickly (less than a |
| 157 | second). However, it would be reasonable to wait up to 10 |
| 158 | seconds. |
| 159 | |
| 160 | This activity will not report a heartbeat due to its |
| 161 | short-running nature. |
| 162 | |
| 163 | It is not necessary to implement a back-off strategy for this |
| 164 | activity, the operation is idempotent. |
| 165 | |
| 166 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 167 | update_vnf_instantiation_state = { |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 168 | "instantiationState": vnf_instantiation_state_input.state |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 169 | } |
| 170 | self.db.set_one( |
| 171 | "vnfrs", |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 172 | {"_id": vnf_instantiation_state_input.vnfr_uuid}, |
| 173 | update_vnf_instantiation_state, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 174 | ) |
| 175 | self.logger.debug( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 176 | f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 177 | ) |
| 178 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 179 | @activity.defn(name=ACTIVITY_SET_VNF_MODEL) |
| 180 | async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None: |
| 181 | """Updates the model name of VNF in VNFR. |
| 182 | |
| 183 | Collaborators: |
| 184 | DB Access Object |
| 185 | |
| 186 | Raises (retryable): |
| 187 | DbException: If DB access or update fails, the collection or DB record ID does not exist. |
| 188 | |
| 189 | Activity Lifecycle: |
| 190 | This activity should complete relatively quickly (less than a |
| 191 | second). However, it would be reasonable to wait up to 10 |
| 192 | seconds. |
| 193 | |
| 194 | This activity will not report a heartbeat due to its |
| 195 | short-running nature. |
| 196 | |
| 197 | It is not necessary to implement a back-off strategy for this |
| 198 | activity, the operation is idempotent. |
| 199 | |
| 200 | """ |
| 201 | update_namespace = {"namespace": set_vnf_model_input.model_name} |
| 202 | self.db.set_one( |
| 203 | "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace |
| 204 | ) |
| 205 | self.logger.debug( |
| 206 | f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}." |
| 207 | ) |
| 208 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 209 | |
| 210 | class VnfSendNotifications: |
| 211 | """Perform Notification operations.""" |
| 212 | |
| 213 | def __init__(self): |
| 214 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 215 | |
| 216 | @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF) |
| Gulsum Atici | 625a654 | 2023-04-18 15:27:18 +0300 | [diff] [blame] | 217 | async def send_notification_for_vnf( |
| 218 | self, input: ChangeVnfInstantiationStateInput |
| 219 | ) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 220 | """If VNF LCM operation state changes, send notification updates. |
| 221 | |
| 222 | This activity does nothing. |
| 223 | |
| 224 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 225 | self.logger.debug("Send notification for VNF not implemented.") |