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