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