| 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, |
| 22 | ACTIVITY_SEND_NOTIFICATION_FOR_VNF, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 23 | ACTIVITY_GET_TASK_QUEUE, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 24 | VIM_TYPE_TASK_QUEUE_MAPPINGS, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame^] | 25 | ACTIVITY_SET_VNF_MODEL, |
| 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, |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame^] | 32 | VnfInstantiateInput, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | |
| 36 | class VnfOperations: |
| 37 | def __init__(self, db): |
| 38 | self.db = db |
| 39 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 40 | |
| 41 | @activity.defn(name=ACTIVITY_GET_TASK_QUEUE) |
| 42 | async def get_task_queue( |
| 43 | self, get_task_queue_input: GetTaskQueueInput |
| 44 | ) -> GetTaskQueueOutput: |
| 45 | """Finds the appropriate task queue according to VIM type of VNF. |
| 46 | |
| 47 | Collaborators: |
| 48 | DB Access Object |
| 49 | |
| 50 | Raises (retryable): |
| 51 | DbException: If DB read operations fail |
| 52 | |
| 53 | Activity Lifecycle: |
| 54 | This activity should complete relatively quickly (less than a |
| 55 | second). However, it would be reasonable to wait up to 10 |
| 56 | seconds. |
| 57 | |
| 58 | This activity will not report a heartbeat due to its |
| 59 | short-running nature. |
| 60 | |
| 61 | It is not necessary to implement a back-off strategy for this |
| 62 | activity, the operation is idempotent. |
| 63 | |
| 64 | """ |
| 65 | vnfrs = self.db.get_list("vnfrs", {"_id": get_task_queue_input.vnfr_uuid}) |
| 66 | vim_record = self.db.get_list("vim_accounts", {"_id": vnfrs["vim-account-id"]}) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 67 | task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim-type"]] |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 68 | self.logger.debug(f"Got the task queue {task_queue} for VNF operations.") |
| 69 | return GetTaskQueueOutput(task_queue) |
| 70 | |
| 71 | |
| 72 | class VnfDbActivity: |
| 73 | |
| 74 | """Perform Database operations for NS accounts. |
| 75 | |
| 76 | Args: |
| 77 | db (object): Data Access Object |
| 78 | """ |
| 79 | |
| 80 | def __init__(self, db): |
| 81 | self.db = db |
| 82 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 83 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 84 | @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE) |
| 85 | async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 86 | """Updates the VNF State in VNFR. |
| 87 | |
| 88 | Collaborators: |
| 89 | DB Access Object |
| 90 | |
| 91 | Raises (retryable): |
| 92 | DbException: If DB access/update fails, the collection or DB record ID does not exist. |
| 93 | |
| 94 | Activity Lifecycle: |
| 95 | This activity should complete relatively quickly (less than a |
| 96 | second). However, it would be reasonable to wait up to 10 |
| 97 | seconds. |
| 98 | |
| 99 | This activity will not report a heartbeat due to its |
| 100 | short-running nature. |
| 101 | |
| 102 | It is not necessary to implement a back-off strategy for this |
| 103 | activity, the operation is idempotent. |
| 104 | |
| 105 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 106 | update_vnf_state = {"vnfState": vnf_state_input.state} |
| 107 | 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] | 108 | self.logger.debug( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 109 | 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] | 110 | ) |
| 111 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 112 | @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE) |
| 113 | async def change_vnf_instantiation_state( |
| 114 | self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 115 | ) -> None: |
| 116 | """Updates the VNF Instantiation State in VNFR. |
| 117 | |
| 118 | Collaborators: |
| 119 | DB Access Object |
| 120 | |
| 121 | Raises (retryable): |
| 122 | DbException: If DB access or update fails |
| 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_instantiation_state = { |
| 137 | "vnfState": vnf_instantiation_state_input.state |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 138 | } |
| 139 | self.db.set_one( |
| 140 | "vnfrs", |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 141 | {"_id": vnf_instantiation_state_input.vnfr_uuid}, |
| 142 | update_vnf_instantiation_state, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 143 | ) |
| 144 | self.logger.debug( |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 145 | 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] | 146 | ) |
| 147 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame^] | 148 | @activity.defn(name=ACTIVITY_SET_VNF_MODEL) |
| 149 | async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None: |
| 150 | """Updates the model name of VNF in VNFR. |
| 151 | |
| 152 | Collaborators: |
| 153 | DB Access Object |
| 154 | |
| 155 | Raises (retryable): |
| 156 | DbException: If DB access or 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 | """ |
| 170 | update_namespace = {"namespace": set_vnf_model_input.model_name} |
| 171 | self.db.set_one( |
| 172 | "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace |
| 173 | ) |
| 174 | self.logger.debug( |
| 175 | f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}." |
| 176 | ) |
| 177 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 178 | |
| 179 | class VnfSendNotifications: |
| 180 | """Perform Notification operations.""" |
| 181 | |
| 182 | def __init__(self): |
| 183 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 184 | |
| 185 | @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF) |
| 186 | async def send_notification_for_vnf(self) -> None: |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 187 | """If VNF LCM operation state changes, send notification updates. |
| 188 | |
| 189 | This activity does nothing. |
| 190 | |
| 191 | """ |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 192 | self.logger.debug("Send notification for VNF not implemented.") |