blob: 8c7c1820b8c50e380fb40d146747374ad14bb181 [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 (
Gulsum Atici50d12e02023-04-27 16:41:43 +030021 GetNsRecordInput,
22 GetNsRecordOutput,
23 GetVnfDetailsInput,
24 GetVnfDetailsOutput,
Daniel Arndtc56d3362023-04-18 11:26:16 -030025 UpdateNsStateInput,
26)
Patricia Reinoso52431352023-04-05 15:35:48 +000027from osm_common.temporal_constants import (
Gulsum Atici50d12e02023-04-27 16:41:43 +030028 ACTIVITY_GET_NS_RECORD,
29 ACTIVITY_GET_VNF_DETAILS,
Patricia Reinoso911946d2023-04-12 15:54:43 +000030 ACTIVITY_UPDATE_NS_STATE,
Patricia Reinoso52431352023-04-05 15:35:48 +000031)
Daniel Arndtfde85132023-04-21 09:32:04 -030032from osm_lcm.data_utils.database.database import Database
Gulsum Atici50d12e02023-04-27 16:41:43 +030033from temporalio import activity
Daniel Arndtfde85132023-04-21 09:32:04 -030034
Patricia Reinoso52431352023-04-05 15:35:48 +000035
36class NsOperations:
Daniel Arndt0d2142a2023-04-20 17:14:58 -030037 def __init__(self, db: Database):
38 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +000039 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
40
Gulsum Atici50d12e02023-04-27 16:41:43 +030041 @activity.defn(name=ACTIVITY_GET_VNF_DETAILS)
42 async def get_vnf_details(
43 self, get_vnf_details_input: GetVnfDetailsInput
44 ) -> GetVnfDetailsOutput:
Daniel Arndt1963ad12023-05-03 16:32:07 +020045 """
Gulsum Atici50d12e02023-04-27 16:41:43 +030046 Gets the list of VNF record IDs, VNF member-index-refs for a given NS record ID.
Daniel Arndt1963ad12023-05-03 16:32:07 +020047
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 Atici50d12e02023-04-27 16:41:43 +030060 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 Arndt0d2142a2023-04-20 17:14:58 -030063 )
Gulsum Atici50d12e02023-04-27 16:41:43 +030064
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 Reinoso52431352023-04-05 15:35:48 +000090
91
92class NsDbActivity:
93
94 """Perform Database operations for NS accounts.
95
96 Args:
Daniel Arndt1963ad12023-05-03 16:32:07 +020097 db (database): Data Access Object
Patricia Reinoso52431352023-04-05 15:35:48 +000098 """
99
Daniel Arndtfde85132023-04-21 09:32:04 -0300100 def __init__(self, db: Database):
101 self.db: Database = db
Patricia Reinoso52431352023-04-05 15:35:48 +0000102 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
103
Patricia Reinoso52431352023-04-05 15:35:48 +0000104 @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}")