blob: 4259fa8e3a70e7b6deae3c82434be58560fd93b9 [file] [log] [blame]
Patricia Reinoso02a39fd2023-03-08 17:13:56 +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.
16
17import logging
18from temporalio import activity
19from time import time
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000020from osm_common.temporal_constants import (
21 ACTIVITY_DELETE_VIM,
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000022 ACTIVITY_UPDATE_VIM_OPERATION_STATE,
23 ACTIVITY_UPDATE_VIM_STATE,
24)
25from osm_common.dataclasses.temporal_dataclasses import (
26 DeleteVimInput,
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000027 UpdateVimOperationStateInput,
28 UpdateVimStateInput,
29)
30
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000031
32class VimDbActivity:
33 """Perform Database operations for VIM accounts.
34
35 Args:
36 db (object): Data Access Object
37 """
38
39 def __init__(self, db):
40 self.db = db
Mark Beierl0c202d22023-04-06 13:58:31 +000041 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000042
43 @activity.defn(name=ACTIVITY_UPDATE_VIM_STATE)
44 async def update_vim_state(self, data: UpdateVimStateInput) -> None:
45 """
46 Changes the state of the VIM itself. Should be either
47 ENABLED or ERROR, however this activity does not validate
48 the state as no validation was done in OSM previously.
49
50 Collaborators:
51 DB Write: vim_accounts
52
53 Raises (Retryable):
54 DbException If the target DB record does not exist or DB is not reachable.
55
56 Activity Lifecycle:
57 This activity will not report a heartbeat due to its
58 short-running nature.
59
60 As this is a direct DB update, it is not recommended to have
61 any specific retry policy
62 """
63 update_vim_state = {
64 "_admin.operationalState": data.operational_state.name,
65 "_admin.detailed-status": data.message,
66 "_admin.modified": time(),
67 }
68
69 self.db.set_one("vim_accounts", {"_id": data.vim_uuid}, update_vim_state)
Mark Beierl0c202d22023-04-06 13:58:31 +000070 self.logger.debug(
Patricia Reinoso02a39fd2023-03-08 17:13:56 +000071 f"Updated VIM {data.vim_uuid} to {data.operational_state.name}"
72 )
73
74 @activity.defn(name=ACTIVITY_UPDATE_VIM_OPERATION_STATE)
75 async def update_vim_operation_state(
76 self, data: UpdateVimOperationStateInput
77 ) -> None:
78 """
79 Changes the state of a VIM operation task. Should be done to
80 indicate progress, or completion of the task itself.
81
82 Collaborators:
83 DB Write: vim_accounts
84
85 Raises (Retryable):
86 DbException If the target DB record does not exist or DB is not reachable.
87
88 Activity Lifecycle:
89 This activity will not report a heartbeat due to its
90 short-running nature.
91
92 As this is a direct DB update, it is not recommended to have
93 any specific retry policy
94 """
95 update_operation_state = {
96 f"_admin.operations.{format(data.op_id)}.operationState": data.op_state.name,
97 f"_admin.operations.{format(data.op_id)}.detailed-status": data.message,
98 "_admin.current_operation": None,
99 }
100
101 self.db.set_one("vim_accounts", {"_id": data.vim_uuid}, update_operation_state)
Mark Beierl0c202d22023-04-06 13:58:31 +0000102 self.logger.debug(
Patricia Reinoso02a39fd2023-03-08 17:13:56 +0000103 f"Updated VIM {data.vim_uuid} OP ID {data.op_id} to {data.op_state.name}"
104 )
105
106 @activity.defn(name=ACTIVITY_DELETE_VIM)
107 async def delete_vim_record(self, data: DeleteVimInput) -> None:
108 """
109 Deletes the VIM record from the database.
110
111 Collaborators:
112 DB Delete: vim_accounts
113
Mark Beierl2bed6072023-04-05 20:01:41 +0000114 Raises (Retryable):
Patricia Reinoso02a39fd2023-03-08 17:13:56 +0000115 DbException If the target DB record does not exist or DB is not reachable.
116
117 Activity Lifecycle:
118 This activity will not report a heartbeat due to its
119 short-running nature.
120
121 As this is a direct DB update, it is not recommended to have
122 any specific retry policy
123 """
124
125 self.db.del_one("vim_accounts", {"_id": data.vim_uuid})
Mark Beierl0c202d22023-04-06 13:58:31 +0000126 self.logger.debug(f"Removed VIM {data.vim_uuid}")