| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +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 time import time |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 19 | |
| 20 | from osm_common.dataclasses.temporal_dataclasses import ( |
| 21 | GetVnfRecordIdsInput, |
| 22 | GetVnfRecordIdsOutput, |
| 23 | UpdateNsStateInput, |
| 24 | ) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 25 | from osm_common.temporal_constants import ( |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 26 | ACTIVITY_GET_VNF_RECORD_IDS, |
| Patricia Reinoso | 911946d | 2023-04-12 15:54:43 +0000 | [diff] [blame] | 27 | ACTIVITY_UPDATE_NS_STATE, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 28 | ) |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 29 | from temporalio import activity |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 30 | |
| Daniel Arndt | fde8513 | 2023-04-21 09:32:04 -0300 | [diff] [blame] | 31 | from osm_lcm.data_utils.database.database import Database |
| 32 | |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 33 | |
| 34 | class NsOperations: |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame^] | 35 | def __init__(self, db: Database): |
| 36 | self.db: Database = db |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 37 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 38 | |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 39 | @activity.defn(name=ACTIVITY_GET_VNF_RECORD_IDS) |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame^] | 40 | async def get_vnf_record_ids( |
| 41 | self, get_vnf_record_ids_input: GetVnfRecordIdsInput |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 42 | ) -> GetVnfRecordIdsOutput: |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame^] | 43 | vnfrs = self.db.get_list( |
| 44 | "vnfrs", {"nsr-id-ref": get_vnf_record_ids_input.ns_uuid} |
| 45 | ) |
| Gulsum Atici | 625a654 | 2023-04-18 15:27:18 +0300 | [diff] [blame] | 46 | return GetVnfRecordIdsOutput(vnfr_ids=[vnfr["id"] for vnfr in vnfrs]) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | class NsDbActivity: |
| 50 | |
| 51 | """Perform Database operations for NS accounts. |
| 52 | |
| 53 | Args: |
| 54 | db (object): Data Access Object |
| 55 | """ |
| 56 | |
| Daniel Arndt | fde8513 | 2023-04-21 09:32:04 -0300 | [diff] [blame] | 57 | def __init__(self, db: Database): |
| 58 | self.db: Database = db |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 59 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 60 | |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 61 | @activity.defn(name=ACTIVITY_UPDATE_NS_STATE) |
| 62 | async def update_ns_state(self, data: UpdateNsStateInput) -> None: |
| 63 | """ |
| 64 | Changes the state of the NS itself. |
| 65 | |
| 66 | Collaborators: |
| 67 | DB Write: nsrs |
| 68 | |
| 69 | Raises (Retryable): |
| 70 | DbException If the target DB record does not exist or DB is not reachable. |
| 71 | |
| 72 | Activity Lifecycle: |
| 73 | This activity will not report a heartbeat due to its |
| 74 | short-running nature. |
| 75 | |
| 76 | As this is a direct DB update, it is not recommended to have |
| 77 | any specific retry policy |
| 78 | """ |
| 79 | update_ns_state = { |
| 80 | "nsState": data.state.name, |
| 81 | # "errorDescription" : data.message, |
| 82 | "_admin.nsState": data.state.name, |
| 83 | "_admin.detailed-status": data.message, |
| 84 | "_admin.modified": time(), |
| 85 | } |
| 86 | self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state) |
| 87 | self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}") |