| 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 | ) |
| 30 | |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 31 | |
| 32 | class 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame^] | 41 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 42 | |
| 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame^] | 70 | self.logger.debug( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 71 | 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame^] | 102 | self.logger.debug( |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 103 | 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 Beierl | 2bed607 | 2023-04-05 20:01:41 +0000 | [diff] [blame] | 114 | Raises (Retryable): |
| Patricia Reinoso | 02a39fd | 2023-03-08 17:13:56 +0000 | [diff] [blame] | 115 | 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 Beierl | 0c202d2 | 2023-04-06 13:58:31 +0000 | [diff] [blame^] | 126 | self.logger.debug(f"Removed VIM {data.vim_uuid}") |