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