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