blob: 85de7193875839e40c2b9f7e6514d26b6811ee5a [file] [log] [blame]
Mark Beierl9e1379d2023-04-06 15:12:46 +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 temporalio import activity
19from osm_common.temporal_constants import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020020 ACTIVITY_CHANGE_VNF_STATE,
21 ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE,
Mark Beierl9e1379d2023-04-06 15:12:46 +000022 ACTIVITY_GET_TASK_QUEUE,
Gulsum Atici36365402023-04-07 00:07:07 +030023 ACTIVITY_GET_VNF_DETAILS,
24 ACTIVITY_SEND_NOTIFICATION_FOR_VNF,
Dario Faccin39301762023-04-17 16:56:37 +020025 ACTIVITY_SET_VNF_MODEL,
Gulsum Atici36365402023-04-07 00:07:07 +030026 VIM_TYPE_TASK_QUEUE_MAPPINGS,
Mark Beierl9e1379d2023-04-06 15:12:46 +000027)
28from osm_common.dataclasses.temporal_dataclasses import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020029 ChangeVnfInstantiationStateInput,
30 ChangeVnfStateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000031 GetTaskQueueInput,
32 GetTaskQueueOutput,
Gulsum Atici36365402023-04-07 00:07:07 +030033 GetVnfDetailsInput,
34 GetVnfDetailsOutput,
Dario Faccin39301762023-04-17 16:56:37 +020035 VnfInstantiateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000036)
Dario Faccin1eb24c92023-04-21 18:23:04 +020037from osm_lcm.data_utils.database.database import Database
Mark Beierl9e1379d2023-04-06 15:12:46 +000038
39
40class VnfOperations:
Dario Faccin1eb24c92023-04-21 18:23:04 +020041 def __init__(self, db: Database):
42 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +000043 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
44
45 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
46 async def get_task_queue(
47 self, get_task_queue_input: GetTaskQueueInput
48 ) -> GetTaskQueueOutput:
49 """Finds the appropriate task queue according to VIM type of VNF.
50
51 Collaborators:
52 DB Access Object
53
54 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +030055 DbException: If DB read operations fail, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +000056
57 Activity Lifecycle:
58 This activity should complete relatively quickly (less than a
59 second). However, it would be reasonable to wait up to 10
60 seconds.
61
62 This activity will not report a heartbeat due to its
63 short-running nature.
64
65 It is not necessary to implement a back-off strategy for this
66 activity, the operation is idempotent.
67
68 """
Gulsum Atici36365402023-04-07 00:07:07 +030069 vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
70 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
Gulsum Atici625a6542023-04-18 15:27:18 +030071 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000072 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
73 return GetTaskQueueOutput(task_queue)
74
Gulsum Atici36365402023-04-07 00:07:07 +030075 @activity.defn(name=ACTIVITY_GET_VNF_DETAILS)
76 async def get_vnf_details(
77 self, get_vnf_details_input: GetVnfDetailsInput
78 ) -> GetVnfDetailsOutput:
79 """Gets the VNF record and VNF descriptor from Database.
80
81 Collaborators:
82 DB Access Object
83
84 Raises (retryable):
85 DbException: If DB read operations fail, the collection or DB record ID does not exist.
86
87 Activity Lifecycle:
88 This activity should complete relatively quickly (less than 10
89 second).
90
91 This activity will not report a heartbeat due to its
92 short-running nature.
93
94 This is an idempotent activity.
95
96 """
97 vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid})
98 vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]})
99 self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.")
100 return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd)
101
Mark Beierl9e1379d2023-04-06 15:12:46 +0000102
103class VnfDbActivity:
104
105 """Perform Database operations for NS accounts.
106
107 Args:
Dario Faccin1eb24c92023-04-21 18:23:04 +0200108 db (Database): Data Access Object
Mark Beierl9e1379d2023-04-06 15:12:46 +0000109 """
110
Dario Faccin1eb24c92023-04-21 18:23:04 +0200111 def __init__(self, db: Database):
112 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +0000113 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
114
Dario Faccin0568c6c2023-04-14 10:19:07 +0200115 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
116 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000117 """Updates the VNF State in VNFR.
118
119 Collaborators:
120 DB Access Object
121
122 Raises (retryable):
123 DbException: If DB access/update fails, the collection or DB record ID does not exist.
124
125 Activity Lifecycle:
126 This activity should complete relatively quickly (less than a
127 second). However, it would be reasonable to wait up to 10
128 seconds.
129
130 This activity will not report a heartbeat due to its
131 short-running nature.
132
133 It is not necessary to implement a back-off strategy for this
134 activity, the operation is idempotent.
135
136 """
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300137 update_vnf_state = {"vnfState": vnf_state_input.state.name}
Dario Faccin0568c6c2023-04-14 10:19:07 +0200138 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000139 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300140 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000141 )
142
Dario Faccin0568c6c2023-04-14 10:19:07 +0200143 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
144 async def change_vnf_instantiation_state(
145 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000146 ) -> None:
147 """Updates the VNF Instantiation State in VNFR.
148
149 Collaborators:
150 DB Access Object
151
152 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +0300153 DbException: If DB access or update fails, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +0000154
155 Activity Lifecycle:
156 This activity should complete relatively quickly (less than a
157 second). However, it would be reasonable to wait up to 10
158 seconds.
159
160 This activity will not report a heartbeat due to its
161 short-running nature.
162
163 It is not necessary to implement a back-off strategy for this
164 activity, the operation is idempotent.
165
166 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200167 update_vnf_instantiation_state = {
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300168 "instantiationState": vnf_instantiation_state_input.state.name
Mark Beierl9e1379d2023-04-06 15:12:46 +0000169 }
170 self.db.set_one(
171 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200172 {"_id": vnf_instantiation_state_input.vnfr_uuid},
173 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000174 )
175 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300176 f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000177 )
178
Dario Faccin39301762023-04-17 16:56:37 +0200179 @activity.defn(name=ACTIVITY_SET_VNF_MODEL)
180 async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None:
181 """Updates the model name of VNF in VNFR.
182
183 Collaborators:
184 DB Access Object
185
186 Raises (retryable):
187 DbException: If DB access or update fails, the collection or DB record ID does not exist.
188
189 Activity Lifecycle:
190 This activity should complete relatively quickly (less than a
191 second). However, it would be reasonable to wait up to 10
192 seconds.
193
194 This activity will not report a heartbeat due to its
195 short-running nature.
196
197 It is not necessary to implement a back-off strategy for this
198 activity, the operation is idempotent.
199
200 """
201 update_namespace = {"namespace": set_vnf_model_input.model_name}
202 self.db.set_one(
203 "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace
204 )
205 self.logger.debug(
206 f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
207 )
208
Dario Faccin0568c6c2023-04-14 10:19:07 +0200209
210class VnfSendNotifications:
211 """Perform Notification operations."""
212
213 def __init__(self):
214 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
215
216 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
Gulsum Atici625a6542023-04-18 15:27:18 +0300217 async def send_notification_for_vnf(
218 self, input: ChangeVnfInstantiationStateInput
219 ) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000220 """If VNF LCM operation state changes, send notification updates.
221
222 This activity does nothing.
223
224 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200225 self.logger.debug("Send notification for VNF not implemented.")