blob: c48628af4483bf9c75eeb8e14995cacb2ae64d0d [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:
35 def __init__(self, db):
36 self.db = db
37 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)
40 async def get_vnf_records(
41 self, get_vnf_records_input: GetVnfRecordIdsInput
42 ) -> GetVnfRecordIdsOutput:
Gulsum Atici625a6542023-04-18 15:27:18 +030043 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": get_vnf_records_input.ns_uuid})
44 return GetVnfRecordIdsOutput(vnfr_ids=[vnfr["id"] for vnfr in vnfrs])
Patricia Reinoso52431352023-04-05 15:35:48 +000045
46
47class NsDbActivity:
48
49 """Perform Database operations for NS accounts.
50
51 Args:
52 db (object): Data Access Object
53 """
54
Daniel Arndtfde85132023-04-21 09:32:04 -030055 def __init__(self, db: Database):
56 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +000057 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
58
Patricia Reinoso52431352023-04-05 15:35:48 +000059 @activity.defn(name=ACTIVITY_UPDATE_NS_STATE)
60 async def update_ns_state(self, data: UpdateNsStateInput) -> None:
61 """
62 Changes the state of the NS itself.
63
64 Collaborators:
65 DB Write: nsrs
66
67 Raises (Retryable):
68 DbException If the target DB record does not exist or DB is not reachable.
69
70 Activity Lifecycle:
71 This activity will not report a heartbeat due to its
72 short-running nature.
73
74 As this is a direct DB update, it is not recommended to have
75 any specific retry policy
76 """
77 update_ns_state = {
78 "nsState": data.state.name,
79 # "errorDescription" : data.message,
80 "_admin.nsState": data.state.name,
81 "_admin.detailed-status": data.message,
82 "_admin.modified": time(),
83 }
84 self.db.set_one("nsrs", {"_id": data.ns_uuid}, update_ns_state)
85 self.logger.debug(f"Updated NS {data.ns_uuid} to {data.state.name}")