blob: 2e8ab1c7920f56833511268b68606d9fd93c4599 [file] [log] [blame]
Patricia Reinoso38fb5da2023-04-27 13:48:50 +00001#########################################################################
Mark Beierl9e1379d2023-04-06 15:12:46 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import logging
Mark Beierl9e1379d2023-04-06 15:12:46 +000017from osm_common.temporal_constants import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020018 ACTIVITY_CHANGE_VNF_STATE,
19 ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE,
Mark Beierl9e1379d2023-04-06 15:12:46 +000020 ACTIVITY_GET_TASK_QUEUE,
Gulsum Aticia1898b42023-04-25 15:48:10 +030021 ACTIVITY_GET_VIM_CLOUD,
Gulsum Atici36365402023-04-07 00:07:07 +030022 ACTIVITY_GET_VNF_DETAILS,
23 ACTIVITY_SEND_NOTIFICATION_FOR_VNF,
Dario Faccin39301762023-04-17 16:56:37 +020024 ACTIVITY_SET_VNF_MODEL,
Gulsum Atici36365402023-04-07 00:07:07 +030025 VIM_TYPE_TASK_QUEUE_MAPPINGS,
Mark Beierl9e1379d2023-04-06 15:12:46 +000026)
27from osm_common.dataclasses.temporal_dataclasses import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020028 ChangeVnfInstantiationStateInput,
29 ChangeVnfStateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000030 GetTaskQueueInput,
31 GetTaskQueueOutput,
Gulsum Aticia1898b42023-04-25 15:48:10 +030032 GetVimCloudInput,
33 GetVimCloudOutput,
Gulsum Atici36365402023-04-07 00:07:07 +030034 GetVnfDetailsInput,
35 GetVnfDetailsOutput,
Dario Faccin39301762023-04-17 16:56:37 +020036 VnfInstantiateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000037)
Dario Faccin1eb24c92023-04-21 18:23:04 +020038from osm_lcm.data_utils.database.database import Database
Patricia Reinoso38fb5da2023-04-27 13:48:50 +000039from temporalio import activity
Mark Beierl9e1379d2023-04-06 15:12:46 +000040
41
42class VnfOperations:
Dario Faccin1eb24c92023-04-21 18:23:04 +020043 def __init__(self, db: Database):
44 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +000045 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
46
47 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
48 async def get_task_queue(
49 self, get_task_queue_input: GetTaskQueueInput
50 ) -> GetTaskQueueOutput:
51 """Finds the appropriate task queue according to VIM type of VNF.
52
53 Collaborators:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +000054 DB read: vim_accounts, vnfrs
Mark Beierl9e1379d2023-04-06 15:12:46 +000055
56 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +030057 DbException: If DB read operations fail, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +000058
59 Activity Lifecycle:
60 This activity should complete relatively quickly (less than a
61 second). However, it would be reasonable to wait up to 10
62 seconds.
63
64 This activity will not report a heartbeat due to its
65 short-running nature.
66
67 It is not necessary to implement a back-off strategy for this
68 activity, the operation is idempotent.
69
70 """
Gulsum Atici36365402023-04-07 00:07:07 +030071 vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
72 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
Gulsum Atici625a6542023-04-18 15:27:18 +030073 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000074 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
75 return GetTaskQueueOutput(task_queue)
76
Gulsum Aticia1898b42023-04-25 15:48:10 +030077 @activity.defn(name=ACTIVITY_GET_VIM_CLOUD)
78 async def get_vim_cloud(
79 self, get_vim_cloud_input: GetVimCloudInput
80 ) -> GetVimCloudOutput:
81 """Finds the cloud by checking the VIM account of VNF.
82
83 Collaborators:
84 DB Access Object
85
86 Raises (retryable):
87 DbException: If DB read operations fail, the collection or DB record ID does not exist.
88
89 Activity Lifecycle:
90 This activity should complete relatively quickly (less than a
91 second). However, it would be reasonable to wait up to 10
92 seconds.
93
94 This activity will not report a heartbeat due to its
95 short-running nature.
96
97 It is not necessary to implement a back-off strategy for this
98 activity, the operation is idempotent.
99
100 """
101 vnfr = self.db.get_one("vnfrs", {"_id": get_vim_cloud_input.vnfr_uuid})
102 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
103 cloud = vim_record["config"].get("cloud", "")
104 self.logger.debug(f"Got the cloud type {cloud} for VNF operations.")
105 return GetVimCloudOutput(cloud=cloud)
106
Gulsum Atici36365402023-04-07 00:07:07 +0300107 @activity.defn(name=ACTIVITY_GET_VNF_DETAILS)
108 async def get_vnf_details(
109 self, get_vnf_details_input: GetVnfDetailsInput
110 ) -> GetVnfDetailsOutput:
111 """Gets the VNF record and VNF descriptor from Database.
112
113 Collaborators:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +0000114 DB read: vnfrs, vnfds
Gulsum Atici36365402023-04-07 00:07:07 +0300115
116 Raises (retryable):
117 DbException: If DB read operations fail, the collection or DB record ID does not exist.
118
119 Activity Lifecycle:
120 This activity should complete relatively quickly (less than 10
121 second).
122
123 This activity will not report a heartbeat due to its
124 short-running nature.
125
126 This is an idempotent activity.
127
128 """
129 vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid})
130 vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]})
131 self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.")
132 return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd)
133
Mark Beierl9e1379d2023-04-06 15:12:46 +0000134
135class VnfDbActivity:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +0000136 """Perform Database operations for VNF accounts.
Mark Beierl9e1379d2023-04-06 15:12:46 +0000137
138 Args:
Dario Faccin1eb24c92023-04-21 18:23:04 +0200139 db (Database): Data Access Object
Mark Beierl9e1379d2023-04-06 15:12:46 +0000140 """
141
Dario Faccin1eb24c92023-04-21 18:23:04 +0200142 def __init__(self, db: Database):
143 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +0000144 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
145
Dario Faccin0568c6c2023-04-14 10:19:07 +0200146 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
147 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000148 """Updates the VNF State in VNFR.
149
150 Collaborators:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +0000151 DB Write: vnfrs
Mark Beierl9e1379d2023-04-06 15:12:46 +0000152
153 Raises (retryable):
154 DbException: If DB access/update fails, the collection or DB record ID does not exist.
155
156 Activity Lifecycle:
157 This activity should complete relatively quickly (less than a
158 second). However, it would be reasonable to wait up to 10
159 seconds.
160
161 This activity will not report a heartbeat due to its
162 short-running nature.
163
164 It is not necessary to implement a back-off strategy for this
165 activity, the operation is idempotent.
166
167 """
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300168 update_vnf_state = {"vnfState": vnf_state_input.state.name}
Dario Faccin0568c6c2023-04-14 10:19:07 +0200169 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000170 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300171 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000172 )
173
Dario Faccin0568c6c2023-04-14 10:19:07 +0200174 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
175 async def change_vnf_instantiation_state(
176 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000177 ) -> None:
178 """Updates the VNF Instantiation State in VNFR.
179
180 Collaborators:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +0000181 DB Write: vnfrs
Mark Beierl9e1379d2023-04-06 15:12:46 +0000182
183 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +0300184 DbException: If DB access or update fails, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +0000185
186 Activity Lifecycle:
187 This activity should complete relatively quickly (less than a
188 second). However, it would be reasonable to wait up to 10
189 seconds.
190
191 This activity will not report a heartbeat due to its
192 short-running nature.
193
194 It is not necessary to implement a back-off strategy for this
195 activity, the operation is idempotent.
196
197 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200198 update_vnf_instantiation_state = {
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300199 "instantiationState": vnf_instantiation_state_input.state.name
Mark Beierl9e1379d2023-04-06 15:12:46 +0000200 }
201 self.db.set_one(
202 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200203 {"_id": vnf_instantiation_state_input.vnfr_uuid},
204 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000205 )
206 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300207 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 +0000208 )
209
Dario Faccin39301762023-04-17 16:56:37 +0200210 @activity.defn(name=ACTIVITY_SET_VNF_MODEL)
211 async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None:
212 """Updates the model name of VNF in VNFR.
213
214 Collaborators:
Patricia Reinoso38fb5da2023-04-27 13:48:50 +0000215 DB Write: vnfrs
Dario Faccin39301762023-04-17 16:56:37 +0200216
217 Raises (retryable):
218 DbException: If DB access or update fails, the collection or DB record ID does not exist.
219
220 Activity Lifecycle:
221 This activity should complete relatively quickly (less than a
222 second). However, it would be reasonable to wait up to 10
223 seconds.
224
225 This activity will not report a heartbeat due to its
226 short-running nature.
227
228 It is not necessary to implement a back-off strategy for this
229 activity, the operation is idempotent.
230
231 """
232 update_namespace = {"namespace": set_vnf_model_input.model_name}
233 self.db.set_one(
234 "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace
235 )
236 self.logger.debug(
237 f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
238 )
239
Dario Faccin0568c6c2023-04-14 10:19:07 +0200240
241class VnfSendNotifications:
242 """Perform Notification operations."""
243
244 def __init__(self):
245 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
246
247 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
Gulsum Atici625a6542023-04-18 15:27:18 +0300248 async def send_notification_for_vnf(
249 self, input: ChangeVnfInstantiationStateInput
250 ) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000251 """If VNF LCM operation state changes, send notification updates.
252
253 This activity does nothing.
254
255 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200256 self.logger.debug("Send notification for VNF not implemented.")