blob: 16cc93c3c70877d4795b28d2799504c6ab28ece5 [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)
37
38
39class VnfOperations:
40 def __init__(self, db):
41 self.db = db
42 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
43
44 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
45 async def get_task_queue(
46 self, get_task_queue_input: GetTaskQueueInput
47 ) -> GetTaskQueueOutput:
48 """Finds the appropriate task queue according to VIM type of VNF.
49
50 Collaborators:
51 DB Access Object
52
53 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +030054 DbException: If DB read operations fail, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +000055
56 Activity Lifecycle:
57 This activity should complete relatively quickly (less than a
58 second). However, it would be reasonable to wait up to 10
59 seconds.
60
61 This activity will not report a heartbeat due to its
62 short-running nature.
63
64 It is not necessary to implement a back-off strategy for this
65 activity, the operation is idempotent.
66
67 """
Gulsum Atici36365402023-04-07 00:07:07 +030068 vnfr = self.db.get_one("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
69 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
Patricia Reinoso52431352023-04-05 15:35:48 +000070 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim-type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000071 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
72 return GetTaskQueueOutput(task_queue)
73
Gulsum Atici36365402023-04-07 00:07:07 +030074 @activity.defn(name=ACTIVITY_GET_VNF_DETAILS)
75 async def get_vnf_details(
76 self, get_vnf_details_input: GetVnfDetailsInput
77 ) -> GetVnfDetailsOutput:
78 """Gets the VNF record and VNF descriptor from Database.
79
80 Collaborators:
81 DB Access Object
82
83 Raises (retryable):
84 DbException: If DB read operations fail, the collection or DB record ID does not exist.
85
86 Activity Lifecycle:
87 This activity should complete relatively quickly (less than 10
88 second).
89
90 This activity will not report a heartbeat due to its
91 short-running nature.
92
93 This is an idempotent activity.
94
95 """
96 vnfr = self.db.get_one("vnfrs", {"_id": get_vnf_details_input.vnfr_uuid})
97 vnfd = self.db.get_one("vnfds", {"_id": vnfr["vnfd-id"]})
98 self.logger.debug("Got the vnfr and vnfd from Database for VNF operations.")
99 return GetVnfDetailsOutput(vnfr=vnfr, vnfd=vnfd)
100
Mark Beierl9e1379d2023-04-06 15:12:46 +0000101
102class VnfDbActivity:
103
104 """Perform Database operations for NS accounts.
105
106 Args:
107 db (object): Data Access Object
108 """
109
110 def __init__(self, db):
111 self.db = db
112 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
113
Dario Faccin0568c6c2023-04-14 10:19:07 +0200114 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
115 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000116 """Updates the VNF State in VNFR.
117
118 Collaborators:
119 DB Access Object
120
121 Raises (retryable):
122 DbException: If DB access/update fails, the collection or DB record ID does not exist.
123
124 Activity Lifecycle:
125 This activity should complete relatively quickly (less than a
126 second). However, it would be reasonable to wait up to 10
127 seconds.
128
129 This activity will not report a heartbeat due to its
130 short-running nature.
131
132 It is not necessary to implement a back-off strategy for this
133 activity, the operation is idempotent.
134
135 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200136 update_vnf_state = {"vnfState": vnf_state_input.state}
137 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000138 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200139 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000140 )
141
Dario Faccin0568c6c2023-04-14 10:19:07 +0200142 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
143 async def change_vnf_instantiation_state(
144 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000145 ) -> None:
146 """Updates the VNF Instantiation State in VNFR.
147
148 Collaborators:
149 DB Access Object
150
151 Raises (retryable):
Gulsum Atici36365402023-04-07 00:07:07 +0300152 DbException: If DB access or update fails, the collection or DB record ID does not exist.
Mark Beierl9e1379d2023-04-06 15:12:46 +0000153
154 Activity Lifecycle:
155 This activity should complete relatively quickly (less than a
156 second). However, it would be reasonable to wait up to 10
157 seconds.
158
159 This activity will not report a heartbeat due to its
160 short-running nature.
161
162 It is not necessary to implement a back-off strategy for this
163 activity, the operation is idempotent.
164
165 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200166 update_vnf_instantiation_state = {
Gulsum Atici36365402023-04-07 00:07:07 +0300167 "instantiationState": vnf_instantiation_state_input.state
Mark Beierl9e1379d2023-04-06 15:12:46 +0000168 }
169 self.db.set_one(
170 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200171 {"_id": vnf_instantiation_state_input.vnfr_uuid},
172 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000173 )
174 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200175 f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000176 )
177
Dario Faccin39301762023-04-17 16:56:37 +0200178 @activity.defn(name=ACTIVITY_SET_VNF_MODEL)
179 async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None:
180 """Updates the model name of VNF in VNFR.
181
182 Collaborators:
183 DB Access Object
184
185 Raises (retryable):
186 DbException: If DB access or update fails, the collection or DB record ID does not exist.
187
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 """
200 update_namespace = {"namespace": set_vnf_model_input.model_name}
201 self.db.set_one(
202 "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace
203 )
204 self.logger.debug(
205 f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
206 )
207
Dario Faccin0568c6c2023-04-14 10:19:07 +0200208
209class VnfSendNotifications:
210 """Perform Notification operations."""
211
212 def __init__(self):
213 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
214
215 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
216 async def send_notification_for_vnf(self) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000217 """If VNF LCM operation state changes, send notification updates.
218
219 This activity does nothing.
220
221 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200222 self.logger.debug("Send notification for VNF not implemented.")