| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 17 | import logging |
| 18 | from temporalio import activity |
| 19 | from time import time |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 20 | from osm_common.temporal_constants import ( |
| 21 | ACTIVITY_DELETE_VIM, |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 22 | ACTIVITY_UPDATE_VIM_OPERATION_STATE, |
| 23 | ACTIVITY_UPDATE_VIM_STATE, |
| 24 | ) |
| 25 | from osm_common.dataclasses.temporal_dataclasses import ( |
| 26 | DeleteVimInput, |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 27 | UpdateVimOperationStateInput, |
| 28 | UpdateVimStateInput, |
| 29 | ) |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 30 | from osm_lcm.data_utils.database.database import Database |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 31 | |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 32 | |
| 33 | class VimDbActivity: |
| 34 | """Perform Database operations for VIM accounts. |
| 35 | |
| 36 | Args: |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 37 | db (Database): Data Access Object |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 38 | """ |
| 39 | |
| Dario Faccin | 1eb24c9 | 2023-04-21 18:23:04 +0200 | [diff] [blame] | 40 | def __init__(self, db: Database): |
| 41 | self.db: Database = db |
| Mark Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 42 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 43 | |
| 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 71 | self.logger.debug( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 72 | 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 103 | self.logger.debug( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 104 | 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 Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 115 | Raises (Retryable): |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 116 | 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame] | 127 | self.logger.debug(f"Removed VIM {data.vim_uuid}") |