blob: b937717ede755fd9129395a9aeb8dea1546a88d7 [file] [log] [blame]
Patricia Reinoso52431352023-04-05 15:35:48 +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 time import time
Daniel Arndtc56d3362023-04-18 11:26:16 -030019
20from osm_common.dataclasses.temporal_dataclasses import (
21 GetVnfRecordIdsInput,
22 GetVnfRecordIdsOutput,
23 UpdateNsStateInput,
24)
Patricia Reinoso52431352023-04-05 15:35:48 +000025from osm_common.temporal_constants import (
Daniel Arndtc56d3362023-04-18 11:26:16 -030026 ACTIVITY_GET_VNF_RECORD_IDS,
Patricia Reinoso911946d2023-04-12 15:54:43 +000027 ACTIVITY_UPDATE_NS_STATE,
Patricia Reinoso52431352023-04-05 15:35:48 +000028)
Daniel Arndtc56d3362023-04-18 11:26:16 -030029from temporalio import activity
Patricia Reinoso52431352023-04-05 15:35:48 +000030
Daniel Arndtfde85132023-04-21 09:32:04 -030031from osm_lcm.data_utils.database.database import Database
32
Patricia Reinoso52431352023-04-05 15:35:48 +000033
34class NsOperations:
Daniel Arndt0d2142a2023-04-20 17:14:58 -030035 def __init__(self, db: Database):
36 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +000037 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
38
Daniel Arndtc56d3362023-04-18 11:26:16 -030039 @activity.defn(name=ACTIVITY_GET_VNF_RECORD_IDS)
Daniel Arndt0d2142a2023-04-20 17:14:58 -030040 async def get_vnf_record_ids(
41 self, get_vnf_record_ids_input: GetVnfRecordIdsInput
Daniel Arndtc56d3362023-04-18 11:26:16 -030042 ) -> GetVnfRecordIdsOutput:
Daniel Arndt1963ad12023-05-03 16:32:07 +020043 """
44 Gets the list of VNF record IDs for a given NS record ID.
45
46 Collaborators:
47 DB Read: vnfrs
48
49 Raises (Retryable):
50 DbException If the target DB record does not exist or DB is not reachable.
51
52 Activity Lifecycle:
53 This activity will not report a heartbeat due to its short-running nature.
54
55 Since this activity only reads from the DB, it is safe to retry, although
56 you may wish to have some back-off policy.
57 """
Daniel Arndt0d2142a2023-04-20 17:14:58 -030058 vnfrs = self.db.get_list(
59 "vnfrs", {"nsr-id-ref": get_vnf_record_ids_input.ns_uuid}
60 )
Gulsum Atici625a6542023-04-18 15:27:18 +030061 return GetVnfRecordIdsOutput(vnfr_ids=[vnfr["id"] for vnfr in vnfrs])
Patricia Reinoso52431352023-04-05 15:35:48 +000062
63
64class NsDbActivity:
65
66 """Perform Database operations for NS accounts.
67
68 Args:
Daniel Arndt1963ad12023-05-03 16:32:07 +020069 db (database): Data Access Object
Patricia Reinoso52431352023-04-05 15:35:48 +000070 """
71
Daniel Arndtfde85132023-04-21 09:32:04 -030072 def __init__(self, db: Database):
73 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +000074 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
75
Patricia Reinoso52431352023-04-05 15:35:48 +000076 @activity.defn(name=ACTIVITY_UPDATE_NS_STATE)
77 async def update_ns_state(self, data: UpdateNsStateInput) -> None:
78 """
79 Changes the state of the NS itself.
80
81 Collaborators:
82 DB Write: nsrs
83
84 Raises (Retryable):
85 DbException If the target DB record does not exist or DB is not reachable.
86
87 Activity Lifecycle:
88 This activity will not report a heartbeat due to its
89 short-running nature.
90
91 As this is a direct DB update, it is not recommended to have
92 any specific retry policy
93 """
94 update_ns_state = {
95 "nsState": data.state.name,
96 # "errorDescription" : data.message,
97 "_admin.nsState": data.state.name,
98 "_admin.detailed-status": data.message,
99 "_admin.modified": time(),
100 }
101 self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state)
102 self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}")