| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +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 | import logging |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 17 | import time |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 18 | |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 19 | from osm_common.dataclasses.temporal_dataclasses import ( |
| 20 | NsLcmOperationInput, |
| 21 | UpdateLcmOperationStateInput, |
| 22 | ) |
| 23 | from osm_common.temporal_constants import ( |
| 24 | ACTIVITY_UPDATE_LCM_OPERATION_STATE, |
| 25 | ACTIVITY_NSLCM_NO_OP, |
| 26 | ) |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 27 | from osm_lcm.data_utils.database.database import Database |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 28 | from temporalio import activity |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class NsLcmActivity: |
| 32 | """ |
| 33 | Handles NS Lifecycle Managment operations. |
| 34 | Args: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 35 | db (Database): Data Access Object |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 36 | """ |
| 37 | |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 38 | def __init__(self, db: Database): |
| 39 | self.db: Database = db |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 40 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| Mark Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 41 | |
| 42 | @activity.defn(name=ACTIVITY_NSLCM_NO_OP) |
| 43 | async def no_op(self, input: NsLcmOperationInput) -> None: |
| 44 | """ |
| 45 | This is a simple No Operation Activity that simply logs the data |
| 46 | with which it was called. It can be used as a placeholder when |
| 47 | developing workflows, or can be enhanced with logic to throw |
| 48 | exceptions on specific conditions to test exception handling in |
| 49 | a workflow. |
| 50 | """ |
| 51 | self.logger.debug(f"Called with: {input.nslcmop}") |
| 52 | |
| 53 | @activity.defn(name=ACTIVITY_UPDATE_LCM_OPERATION_STATE) |
| 54 | async def update_ns_lcm_operation_state( |
| 55 | self, data: UpdateLcmOperationStateInput |
| 56 | ) -> None: |
| 57 | """ |
| 58 | Changes the state of a LCM operation task. Should be done to |
| 59 | indicate progress, or completion of the task itself. |
| 60 | |
| 61 | Collaborators: |
| 62 | DB Write: nslcmops |
| 63 | |
| 64 | Raises (Retryable): |
| 65 | DbException If the target DB record does not exist or DB is not reachable. |
| 66 | |
| 67 | Activity Lifecycle: |
| 68 | This activity will not report a heartbeat due to its |
| 69 | short-running nature. |
| 70 | As this is a direct DB update, it is not recommended to have |
| 71 | any specific retry policy |
| 72 | """ |
| 73 | now = time.time() |
| 74 | |
| 75 | update_lcm_operation = { |
| 76 | "_admin.modified": now, |
| 77 | } |
| 78 | |
| 79 | if data.op_state is not None: |
| 80 | update_lcm_operation["operationState"] = data.op_state.name |
| 81 | update_lcm_operation["statusEnteredTime"] = now |
| 82 | |
| 83 | if data.stage is not None: |
| 84 | update_lcm_operation["stage"] = data.stage |
| 85 | |
| 86 | if data.error_message is not None: |
| 87 | update_lcm_operation["errorMessage"] = data.error_message |
| 88 | |
| 89 | if data.detailed_status is not None: |
| 90 | update_lcm_operation["detailedStatus"] = data.detailed_status |
| 91 | |
| 92 | self.db.set_one("nslcmops", {"_id": data.op_id}, update_lcm_operation) |
| 93 | self.logger.debug( |
| 94 | f"Updated LCM Operation {data.op_id} to {update_lcm_operation}" |
| 95 | ) |