| 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 ( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 21 | GetNsRecordInput, |
| 22 | GetNsRecordOutput, |
| 23 | GetVnfDetailsInput, |
| 24 | GetVnfDetailsOutput, |
| Daniel Arndt | c56d336 | 2023-04-18 11:26:16 -0300 | [diff] [blame] | 25 | UpdateNsStateInput, |
| 26 | ) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 27 | from osm_common.temporal_constants import ( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 28 | ACTIVITY_GET_NS_RECORD, |
| 29 | ACTIVITY_GET_VNF_DETAILS, |
| Patricia Reinoso | 911946d | 2023-04-12 15:54:43 +0000 | [diff] [blame] | 30 | ACTIVITY_UPDATE_NS_STATE, |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 31 | ) |
| Daniel Arndt | fde8513 | 2023-04-21 09:32:04 -0300 | [diff] [blame] | 32 | from osm_lcm.data_utils.database.database import Database |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 33 | from temporalio import activity |
| Daniel Arndt | fde8513 | 2023-04-21 09:32:04 -0300 | [diff] [blame] | 34 | |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 35 | |
| 36 | class NsOperations: |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 37 | def __init__(self, db: Database): |
| 38 | self.db: Database = db |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 39 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 40 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 41 | @activity.defn(name=ACTIVITY_GET_VNF_DETAILS) |
| 42 | async def get_vnf_details( |
| 43 | self, get_vnf_details_input: GetVnfDetailsInput |
| 44 | ) -> GetVnfDetailsOutput: |
| Daniel Arndt | 1963ad1 | 2023-05-03 16:32:07 +0200 | [diff] [blame] | 45 | """ |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 46 | Gets the list of VNF record IDs, VNF member-index-refs for a given NS record ID. |
| Daniel Arndt | 1963ad1 | 2023-05-03 16:32:07 +0200 | [diff] [blame] | 47 | |
| 48 | Collaborators: |
| 49 | DB Read: vnfrs |
| 50 | |
| 51 | Raises (Retryable): |
| 52 | DbException If the target DB record does not exist or DB is not reachable. |
| 53 | |
| 54 | Activity Lifecycle: |
| 55 | This activity will not report a heartbeat due to its short-running nature. |
| 56 | |
| 57 | Since this activity only reads from the DB, it is safe to retry, although |
| 58 | you may wish to have some back-off policy. |
| 59 | """ |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 60 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": get_vnf_details_input.ns_uuid}) |
| 61 | return GetVnfDetailsOutput( |
| 62 | vnf_details=[(vnfr["id"], vnfr["member-vnf-index-ref"]) for vnfr in vnfrs] |
| Daniel Arndt | 0d2142a | 2023-04-20 17:14:58 -0300 | [diff] [blame] | 63 | ) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame^] | 64 | |
| 65 | @activity.defn(name=ACTIVITY_GET_NS_RECORD) |
| 66 | async def get_ns_record( |
| 67 | self, get_ns_record_input: GetNsRecordInput |
| 68 | ) -> GetNsRecordOutput: |
| 69 | """Gets the NS record from Database. |
| 70 | |
| 71 | Collaborators: |
| 72 | DB Read: nsrs |
| 73 | |
| 74 | Raises (retryable): |
| 75 | DbException: If DB read operations fail, the collection or DB record ID does not exist. |
| 76 | |
| 77 | Activity Lifecycle: |
| 78 | This activity should complete relatively quickly (less than 10 |
| 79 | second). |
| 80 | |
| 81 | This activity will not report a heartbeat due to its |
| 82 | short-running nature. |
| 83 | |
| 84 | This is an idempotent activity. |
| 85 | |
| 86 | """ |
| 87 | nsr = self.db.get_one("nsrs", {"_id": get_ns_record_input.nsr_uuid}) |
| 88 | self.logger.debug("Got the nsr from Database for VNF operations.") |
| 89 | return GetNsRecordOutput(nsr=nsr) |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | class NsDbActivity: |
| 93 | |
| 94 | """Perform Database operations for NS accounts. |
| 95 | |
| 96 | Args: |
| Daniel Arndt | 1963ad1 | 2023-05-03 16:32:07 +0200 | [diff] [blame] | 97 | db (database): Data Access Object |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 98 | """ |
| 99 | |
| Daniel Arndt | fde8513 | 2023-04-21 09:32:04 -0300 | [diff] [blame] | 100 | def __init__(self, db: Database): |
| 101 | self.db: Database = db |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 102 | self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}") |
| 103 | |
| Patricia Reinoso | 5243135 | 2023-04-05 15:35:48 +0000 | [diff] [blame] | 104 | @activity.defn(name=ACTIVITY_UPDATE_NS_STATE) |
| 105 | async def update_ns_state(self, data: UpdateNsStateInput) -> None: |
| 106 | """ |
| 107 | Changes the state of the NS itself. |
| 108 | |
| 109 | Collaborators: |
| 110 | DB Write: nsrs |
| 111 | |
| 112 | Raises (Retryable): |
| 113 | DbException If the target DB record does not exist or DB is not reachable. |
| 114 | |
| 115 | Activity Lifecycle: |
| 116 | This activity will not report a heartbeat due to its |
| 117 | short-running nature. |
| 118 | |
| 119 | As this is a direct DB update, it is not recommended to have |
| 120 | any specific retry policy |
| 121 | """ |
| 122 | update_ns_state = { |
| 123 | "nsState": data.state.name, |
| 124 | # "errorDescription" : data.message, |
| 125 | "_admin.nsState": data.state.name, |
| 126 | "_admin.detailed-status": data.message, |
| 127 | "_admin.modified": time(), |
| 128 | } |
| 129 | self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state) |
| 130 | self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}") |