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