blob: 6ddb0f3c5755fe9b2125093a3a52e83d1d4a0d85 [file] [log] [blame]
Mark Beierl2bed6072023-04-05 20:01:41 +00001#######################################################################################
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.
16import logging
Mark Beierl2bed6072023-04-05 20:01:41 +000017import time
Gulsum Atici50d12e02023-04-27 16:41:43 +030018
Mark Beierl2bed6072023-04-05 20:01:41 +000019from osm_common.dataclasses.temporal_dataclasses import (
20 NsLcmOperationInput,
21 UpdateLcmOperationStateInput,
22)
23from osm_common.temporal_constants import (
24 ACTIVITY_UPDATE_LCM_OPERATION_STATE,
25 ACTIVITY_NSLCM_NO_OP,
26)
Dario Faccin1eb24c92023-04-21 18:23:04 +020027from osm_lcm.data_utils.database.database import Database
Gulsum Atici50d12e02023-04-27 16:41:43 +030028from temporalio import activity
Mark Beierl2bed6072023-04-05 20:01:41 +000029
30
31class NsLcmActivity:
32 """
33 Handles NS Lifecycle Managment operations.
34 Args:
Dario Faccin1eb24c92023-04-21 18:23:04 +020035 db (Database): Data Access Object
Mark Beierl2bed6072023-04-05 20:01:41 +000036 """
37
Dario Faccin1eb24c92023-04-21 18:23:04 +020038 def __init__(self, db: Database):
39 self.db: Database = db
Mark Beierl2bed6072023-04-05 20:01:41 +000040 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
Mark Beierl2bed6072023-04-05 20:01:41 +000041
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 )