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