| 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 | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame^] | 23 | ACTIVITY_GET_VIM_CLOUD, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 24 | ACTIVITY_GET_VNF_DETAILS, |
| 25 | ACTIVITY_SEND_NOTIFICATION_FOR_VNF, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 26 | ACTIVITY_SET_VNF_MODEL, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 27 | VIM_TYPE_TASK_QUEUE_MAPPINGS, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 28 | ) |
| 29 | from osm_common.dataclasses.temporal_dataclasses import ( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 30 | ChangeVnfInstantiationStateInput, |
| 31 | ChangeVnfStateInput, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 32 | GetTaskQueueInput, |
| 33 | GetTaskQueueOutput, |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame^] | 34 | GetVimCloudInput, |
| 35 | GetVimCloudOutput, |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 36 | GetVnfDetailsInput, |
| 37 | GetVnfDetailsOutput, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 38 | VnfInstantiateInput, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 39 | ) |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 40 | from osm_lcm.data_utils.database.database import Database |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | class VnfOperations: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 44 | def __init__(self, db: Database): |
| 45 | self.db: Database = db |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 46 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 47 | |
| 48 | @activity.defn(name=ACTIVITY_GET_TASK_QUEUE) |
| 49 | async def get_task_queue( |
| 50 | self, get_task_queue_input: GetTaskQueueInput |
| 51 | ) -> GetTaskQueueOutput: |
| 52 | """Finds the appropriate task queue according to VIM type of VNF. |
| 53 | |
| 54 | Collaborators: |
| 55 | DB Access Object |
| 56 | |
| 57 | Raises (retryable): |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 58 | 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] | 59 | |
| 60 | Activity Lifecycle: |
| 61 | This activity should complete relatively quickly (less than a |
| 62 | second). However, it would be reasonable to wait up to 10 |
| 63 | seconds. |
| 64 | |
| 65 | This activity will not report a heartbeat due to its |
| 66 | short-running nature. |
| 67 | |
| 68 | It is not necessary to implement a back-off strategy for this |
| 69 | activity, the operation is idempotent. |
| 70 | |
| 71 | """ |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 72 | vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid}) |
| 73 | 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] | 74 | task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]] |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 75 | self.logger.debug(f"Got the task queue {task_queue} for VNF operations.") |
| 76 | return GetTaskQueueOutput(task_queue) |
| 77 | |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame^] | 78 | @activity.defn(name=ACTIVITY_GET_VIM_CLOUD) |
| 79 | async def get_vim_cloud( |
| 80 | self, get_vim_cloud_input: GetVimCloudInput |
| 81 | ) -> GetVimCloudOutput: |
| 82 | """Finds the cloud by checking the VIM account of VNF. |
| 83 | |
| 84 | Collaborators: |
| 85 | DB Access Object |
| 86 | |
| 87 | Raises (retryable): |
| 88 | DbException: If DB read operations fail, the collection or DB record ID does not exist. |
| 89 | |
| 90 | Activity Lifecycle: |
| 91 | This activity should complete relatively quickly (less than a |
| 92 | second). However, it would be reasonable to wait up to 10 |
| 93 | seconds. |
| 94 | |
| 95 | This activity will not report a heartbeat due to its |
| 96 | short-running nature. |
| 97 | |
| 98 | It is not necessary to implement a back-off strategy for this |
| 99 | activity, the operation is idempotent. |
| 100 | |
| 101 | """ |
| 102 | vnfr = self.db.get_one("vnfrs", {"_id": get_vim_cloud_input.vnfr_uuid}) |
| 103 | vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]}) |
| 104 | cloud = vim_record["config"].get("cloud", "") |
| 105 | self.logger.debug(f"Got the cloud type {cloud} for VNF operations.") |
| 106 | return GetVimCloudOutput(cloud=cloud) |
| 107 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 108 | @activity.defn(name=ACTIVITY_GET_VNF_DETAILS) |
| 109 | async def get_vnf_details( |
| 110 | self, get_vnf_details_input: GetVnfDetailsInput |
| 111 | ) -> GetVnfDetailsOutput: |
| 112 | """Gets the VNF record and VNF descriptor from Database. |
| 113 | |
| 114 | Collaborators: |
| 115 | DB Access Object |
| 116 | |
| 117 | Raises (retryable): |
| 118 | DbException: If DB read operations fail, the collection or DB record ID does not exist. |
| 119 | |
| 120 | Activity Lifecycle: |
| 121 | This activity should complete relatively quickly (less than 10 |
| 122 | second). |
| 123 | |
| 124 | This activity will not report a heartbeat due to its |
| 125 | short-running nature. |
| 126 | |
| 127 | This is an idempotent activity. |
| 128 | |
| 129 | """ |
| 130 | vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid}) |
| 131 | vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]}) |
| 132 | self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.") |
| 133 | return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd) |
| 134 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 135 | |
| 136 | class VnfDbActivity: |
| 137 | |
| 138 | """Perform Database operations for NS accounts. |
| 139 | |
| 140 | Args: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 141 | db (Database): Data Access Object |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 142 | """ |
| 143 | |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 144 | def __init__(self, db: Database): |
| 145 | self.db: Database = db |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 146 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 147 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 148 | @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE) |
| 149 | async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 150 | """Updates the VNF State in VNFR. |
| 151 | |
| 152 | Collaborators: |
| 153 | DB Access Object |
| 154 | |
| 155 | Raises (retryable): |
| 156 | DbException: If DB access/update fails, the collection or DB record ID does not exist. |
| 157 | |
| 158 | Activity Lifecycle: |
| 159 | This activity should complete relatively quickly (less than a |
| 160 | second). However, it would be reasonable to wait up to 10 |
| 161 | seconds. |
| 162 | |
| 163 | This activity will not report a heartbeat due to its |
| 164 | short-running nature. |
| 165 | |
| 166 | It is not necessary to implement a back-off strategy for this |
| 167 | activity, the operation is idempotent. |
| 168 | |
| 169 | """ |
| Gulsum Atici | 8ee9b1d | 2023-04-24 14:54:20 +0300 | [diff] [blame] | 170 | update_vnf_state = {"vnfState": vnf_state_input.state.name} |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 171 | 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] | 172 | self.logger.debug( |
| Gulsum Atici | 8ee9b1d | 2023-04-24 14:54:20 +0300 | [diff] [blame] | 173 | f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state.name}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 174 | ) |
| 175 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 176 | @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE) |
| 177 | async def change_vnf_instantiation_state( |
| 178 | self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 179 | ) -> None: |
| 180 | """Updates the VNF Instantiation State in VNFR. |
| 181 | |
| 182 | Collaborators: |
| 183 | DB Access Object |
| 184 | |
| 185 | Raises (retryable): |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 186 | 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] | 187 | |
| 188 | Activity Lifecycle: |
| 189 | This activity should complete relatively quickly (less than a |
| 190 | second). However, it would be reasonable to wait up to 10 |
| 191 | seconds. |
| 192 | |
| 193 | This activity will not report a heartbeat due to its |
| 194 | short-running nature. |
| 195 | |
| 196 | It is not necessary to implement a back-off strategy for this |
| 197 | activity, the operation is idempotent. |
| 198 | |
| 199 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 200 | update_vnf_instantiation_state = { |
| Gulsum Atici | 8ee9b1d | 2023-04-24 14:54:20 +0300 | [diff] [blame] | 201 | "instantiationState": vnf_instantiation_state_input.state.name |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 202 | } |
| 203 | self.db.set_one( |
| 204 | "vnfrs", |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 205 | {"_id": vnf_instantiation_state_input.vnfr_uuid}, |
| 206 | update_vnf_instantiation_state, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 207 | ) |
| 208 | self.logger.debug( |
| Gulsum Atici | 8ee9b1d | 2023-04-24 14:54:20 +0300 | [diff] [blame] | 209 | f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state.name}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 210 | ) |
| 211 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 212 | @activity.defn(name=ACTIVITY_SET_VNF_MODEL) |
| 213 | async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None: |
| 214 | """Updates the model name of VNF in VNFR. |
| 215 | |
| 216 | Collaborators: |
| 217 | DB Access Object |
| 218 | |
| 219 | Raises (retryable): |
| 220 | DbException: If DB access or update fails, the collection or DB record ID does not exist. |
| 221 | |
| 222 | Activity Lifecycle: |
| 223 | This activity should complete relatively quickly (less than a |
| 224 | second). However, it would be reasonable to wait up to 10 |
| 225 | seconds. |
| 226 | |
| 227 | This activity will not report a heartbeat due to its |
| 228 | short-running nature. |
| 229 | |
| 230 | It is not necessary to implement a back-off strategy for this |
| 231 | activity, the operation is idempotent. |
| 232 | |
| 233 | """ |
| 234 | update_namespace = {"namespace": set_vnf_model_input.model_name} |
| 235 | self.db.set_one( |
| 236 | "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace |
| 237 | ) |
| 238 | self.logger.debug( |
| 239 | f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}." |
| 240 | ) |
| 241 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 242 | |
| 243 | class VnfSendNotifications: |
| 244 | """Perform Notification operations.""" |
| 245 | |
| 246 | def __init__(self): |
| 247 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 248 | |
| 249 | @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF) |
| Gulsum Atici | 625a654 | 2023-04-18 15:27:18 +0300 | [diff] [blame] | 250 | async def send_notification_for_vnf( |
| 251 | self, input: ChangeVnfInstantiationStateInput |
| 252 | ) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 253 | """If VNF LCM operation state changes, send notification updates. |
| 254 | |
| 255 | This activity does nothing. |
| 256 | |
| 257 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 258 | self.logger.debug("Send notification for VNF not implemented.") |