blob: 5f360f1765411ec0da70e4dd57876ecba003d6cc [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)
Dario Faccin1eb24c92023-04-21 18:23:04 +020027from osm_lcm.data_utils.database.database import Database
Mark Beierl2bed6072023-04-05 20:01:41 +000028
29
30class NsLcmActivity:
31 """
32 Handles NS Lifecycle Managment operations.
33 Args:
Dario Faccin1eb24c92023-04-21 18:23:04 +020034 db (Database): Data Access Object
Mark Beierl2bed6072023-04-05 20:01:41 +000035 """
36
Dario Faccin1eb24c92023-04-21 18:23:04 +020037 def __init__(self, db: Database):
38 self.db: Database = db
Mark Beierl2bed6072023-04-05 20:01:41 +000039 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
Mark Beierl2bed6072023-04-05 20:01:41 +000040
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 )