blob: 6f64a822441a3c8bfe7670523f064e56a1c524dc [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 Aticia1898b42023-04-25 15:48:10 +030023 ACTIVITY_GET_VIM_CLOUD,
Gulsum Atici36365402023-04-07 00:07:07 +030024 ACTIVITY_GET_VNF_DETAILS,
25 ACTIVITY_SEND_NOTIFICATION_FOR_VNF,
Dario Faccin39301762023-04-17 16:56:37 +020026 ACTIVITY_SET_VNF_MODEL,
Gulsum Atici36365402023-04-07 00:07:07 +030027 VIM_TYPE_TASK_QUEUE_MAPPINGS,
Mark Beierl9e1379d2023-04-06 15:12:46 +000028)
29from osm_common.dataclasses.temporal_dataclasses import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020030 ChangeVnfInstantiationStateInput,
31 ChangeVnfStateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000032 GetTaskQueueInput,
33 GetTaskQueueOutput,
Gulsum Aticia1898b42023-04-25 15:48:10 +030034 GetVimCloudInput,
35 GetVimCloudOutput,
Gulsum Atici36365402023-04-07 00:07:07 +030036 GetVnfDetailsInput,
37 GetVnfDetailsOutput,
Dario Faccin39301762023-04-17 16:56:37 +020038 VnfInstantiateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000039)
Dario Faccin1eb24c92023-04-21 18:23:04 +020040from osm_lcm.data_utils.database.database import Database
Mark Beierl9e1379d2023-04-06 15:12:46 +000041
42
43class VnfOperations:
Dario Faccin1eb24c92023-04-21 18:23:04 +020044 def __init__(self, db: Database):
45 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +000046 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
47
48 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
49 async def get_task_queue(
50 self, get_task_queue_input: GetTaskQueueInput
51 ) -> GetTaskQueueOutput:
52 """Finds the appropriate task queue according to VIM type of VNF.
53
54 Collaborators:
55 DB Access Object
56
57 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +030058 DbException: If DB read operations fail, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +000059
60 Activity Lifecycle:
61 This activity should complete relatively quickly (less than a
62 second). However, it would be reasonable to wait up to 10
63 seconds.
64
65 This activity will not report a heartbeat due to its
66 short-running nature.
67
68 It is not necessary to implement a back-off strategy for this
69 activity, the operation is idempotent.
70
71 """
Gulsum Atici36365402023-04-07 00:07:07 +030072 vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
73 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
Gulsum Atici625a6542023-04-18 15:27:18 +030074 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000075 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
76 return GetTaskQueueOutput(task_queue)
77
Gulsum Aticia1898b42023-04-25 15:48:10 +030078 @activity.defn(name=ACTIVITY_GET_VIM_CLOUD)
79 async def get_vim_cloud(
80 self, get_vim_cloud_input: GetVimCloudInput
81 ) -> GetVimCloudOutput:
82 """Finds the cloud by checking the VIM account of VNF.
83
84 Collaborators:
85 DB Access Object
86
87 Raises (retryable):
88 DbException: If DB read operations fail, the collection or DB record ID does not exist.
89
90 Activity Lifecycle:
91 This activity should complete relatively quickly (less than a
92 second). However, it would be reasonable to wait up to 10
93 seconds.
94
95 This activity will not report a heartbeat due to its
96 short-running nature.
97
98 It is not necessary to implement a back-off strategy for this
99 activity, the operation is idempotent.
100
101 """
102 vnfr = self.db.get_one("vnfrs", {"_id": get_vim_cloud_input.vnfr_uuid})
103 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
104 cloud = vim_record["config"].get("cloud", "")
105 self.logger.debug(f"Got the cloud type {cloud} for VNF operations.")
106 return GetVimCloudOutput(cloud=cloud)
107
Gulsum Atici36365402023-04-07 00:07:07 +0300108 @activity.defn(name=ACTIVITY_GET_VNF_DETAILS)
109 async def get_vnf_details(
110 self, get_vnf_details_input: GetVnfDetailsInput
111 ) -> GetVnfDetailsOutput:
112 """Gets the VNF record and VNF descriptor from Database.
113
114 Collaborators:
115 DB Access Object
116
117 Raises (retryable):
118 DbException: If DB read operations fail, the collection or DB record ID does not exist.
119
120 Activity Lifecycle:
121 This activity should complete relatively quickly (less than 10
122 second).
123
124 This activity will not report a heartbeat due to its
125 short-running nature.
126
127 This is an idempotent activity.
128
129 """
130 vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid})
131 vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]})
132 self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.")
133 return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd)
134
Mark Beierl9e1379d2023-04-06 15:12:46 +0000135
136class VnfDbActivity:
137
138 """Perform Database operations for NS accounts.
139
140 Args:
Dario Faccin1eb24c92023-04-21 18:23:04 +0200141 db (Database): Data Access Object
Mark Beierl9e1379d2023-04-06 15:12:46 +0000142 """
143
Dario Faccin1eb24c92023-04-21 18:23:04 +0200144 def __init__(self, db: Database):
145 self.db: Database = db
Mark Beierl9e1379d2023-04-06 15:12:46 +0000146 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
147
Dario Faccin0568c6c2023-04-14 10:19:07 +0200148 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
149 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000150 """Updates the VNF State in VNFR.
151
152 Collaborators:
153 DB Access Object
154
155 Raises (retryable):
156 DbException: If DB access/update fails, the collection or DB record ID does not exist.
157
158 Activity Lifecycle:
159 This activity should complete relatively quickly (less than a
160 second). However, it would be reasonable to wait up to 10
161 seconds.
162
163 This activity will not report a heartbeat due to its
164 short-running nature.
165
166 It is not necessary to implement a back-off strategy for this
167 activity, the operation is idempotent.
168
169 """
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300170 update_vnf_state = {"vnfState": vnf_state_input.state.name}
Dario Faccin0568c6c2023-04-14 10:19:07 +0200171 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000172 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300173 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000174 )
175
Dario Faccin0568c6c2023-04-14 10:19:07 +0200176 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
177 async def change_vnf_instantiation_state(
178 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000179 ) -> None:
180 """Updates the VNF Instantiation State in VNFR.
181
182 Collaborators:
183 DB Access Object
184
185 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +0300186 DbException: If DB access or update fails, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +0000187
188 Activity Lifecycle:
189 This activity should complete relatively quickly (less than a
190 second). However, it would be reasonable to wait up to 10
191 seconds.
192
193 This activity will not report a heartbeat due to its
194 short-running nature.
195
196 It is not necessary to implement a back-off strategy for this
197 activity, the operation is idempotent.
198
199 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200200 update_vnf_instantiation_state = {
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300201 "instantiationState": vnf_instantiation_state_input.state.name
Mark Beierl9e1379d2023-04-06 15:12:46 +0000202 }
203 self.db.set_one(
204 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200205 {"_id": vnf_instantiation_state_input.vnfr_uuid},
206 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000207 )
208 self.logger.debug(
Gulsum Atici8ee9b1d2023-04-24 14:54:20 +0300209 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 +0000210 )
211
Dario Faccin39301762023-04-17 16:56:37 +0200212 @activity.defn(name=ACTIVITY_SET_VNF_MODEL)
213 async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None:
214 """Updates the model name of VNF in VNFR.
215
216 Collaborators:
217 DB Access Object
218
219 Raises (retryable):
220 DbException: If DB access or update fails, the collection or DB record ID does not exist.
221
222 Activity Lifecycle:
223 This activity should complete relatively quickly (less than a
224 second). However, it would be reasonable to wait up to 10
225 seconds.
226
227 This activity will not report a heartbeat due to its
228 short-running nature.
229
230 It is not necessary to implement a back-off strategy for this
231 activity, the operation is idempotent.
232
233 """
234 update_namespace = {"namespace": set_vnf_model_input.model_name}
235 self.db.set_one(
236 "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace
237 )
238 self.logger.debug(
239 f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
240 )
241
Dario Faccin0568c6c2023-04-14 10:19:07 +0200242
243class VnfSendNotifications:
244 """Perform Notification operations."""
245
246 def __init__(self):
247 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
248
249 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
Gulsum Atici625a6542023-04-18 15:27:18 +0300250 async def send_notification_for_vnf(
251 self, input: ChangeVnfInstantiationStateInput
252 ) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000253 """If VNF LCM operation state changes, send notification updates.
254
255 This activity does nothing.
256
257 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200258 self.logger.debug("Send notification for VNF not implemented.")