blob: 12ffc764aab0480af8e7f3824de6fd621e13910d [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 Arndt0d2142a2023-04-20 17:14:58 -030043 vnfrs = self.db.get_list(
44 "vnfrs", {"nsr-id-ref": get_vnf_record_ids_input.ns_uuid}
45 )
Gulsum Atici625a6542023-04-18 15:27:18 +030046 return GetVnfRecordIdsOutput(vnfr_ids=[vnfr["id"] for vnfr in vnfrs])
Patricia Reinoso52431352023-04-05 15:35:48 +000047
48
49class NsDbActivity:
50
51 """Perform Database operations for NS accounts.
52
53 Args:
54 db (object): Data Access Object
55 """
56
Daniel Arndtfde85132023-04-21 09:32:04 -030057 def __init__(self, db: Database):
58 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +000059 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
60
Patricia Reinoso52431352023-04-05 15:35:48 +000061 @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}")