blob: 6e5abcb02bad58c6cd0089ad91a7155e6157e01a [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,
22 ACTIVITY_SEND_NOTIFICATION_FOR_VNF,
Mark Beierl9e1379d2023-04-06 15:12:46 +000023 ACTIVITY_GET_TASK_QUEUE,
Patricia Reinoso52431352023-04-05 15:35:48 +000024 VIM_TYPE_TASK_QUEUE_MAPPINGS,
Dario Faccin39301762023-04-17 16:56:37 +020025 ACTIVITY_SET_VNF_MODEL,
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,
Dario Faccin39301762023-04-17 16:56:37 +020032 VnfInstantiateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000033)
34
35
36class VnfOperations:
37 def __init__(self, db):
38 self.db = db
39 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
40
41 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
42 async def get_task_queue(
43 self, get_task_queue_input: GetTaskQueueInput
44 ) -> GetTaskQueueOutput:
45 """Finds the appropriate task queue according to VIM type of VNF.
46
47 Collaborators:
48 DB Access Object
49
50 Raises (retryable):
51 DbException: If DB read operations fail
52
53 Activity Lifecycle:
54 This activity should complete relatively quickly (less than a
55 second). However, it would be reasonable to wait up to 10
56 seconds.
57
58 This activity will not report a heartbeat due to its
59 short-running nature.
60
61 It is not necessary to implement a back-off strategy for this
62 activity, the operation is idempotent.
63
64 """
65 vnfrs = self.db.get_list("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
66 vim_record = self.db.get_list("vim_accounts", {"_id": vnfrs["vim-account-id"]})
Patricia Reinoso52431352023-04-05 15:35:48 +000067 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim-type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000068 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
69 return GetTaskQueueOutput(task_queue)
70
71
72class VnfDbActivity:
73
74 """Perform Database operations for NS accounts.
75
76 Args:
77 db (object): Data Access Object
78 """
79
80 def __init__(self, db):
81 self.db = db
82 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
83
Dario Faccin0568c6c2023-04-14 10:19:07 +020084 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
85 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +000086 """Updates the VNF State in VNFR.
87
88 Collaborators:
89 DB Access Object
90
91 Raises (retryable):
92 DbException: If DB access/update fails, the collection or DB record ID does not exist.
93
94 Activity Lifecycle:
95 This activity should complete relatively quickly (less than a
96 second). However, it would be reasonable to wait up to 10
97 seconds.
98
99 This activity will not report a heartbeat due to its
100 short-running nature.
101
102 It is not necessary to implement a back-off strategy for this
103 activity, the operation is idempotent.
104
105 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200106 update_vnf_state = {"vnfState": vnf_state_input.state}
107 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000108 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200109 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000110 )
111
Dario Faccin0568c6c2023-04-14 10:19:07 +0200112 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
113 async def change_vnf_instantiation_state(
114 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000115 ) -> None:
116 """Updates the VNF Instantiation State in VNFR.
117
118 Collaborators:
119 DB Access Object
120
121 Raises (retryable):
122 DbException: If DB access or update fails
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_instantiation_state = {
137 "vnfState": vnf_instantiation_state_input.state
Mark Beierl9e1379d2023-04-06 15:12:46 +0000138 }
139 self.db.set_one(
140 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200141 {"_id": vnf_instantiation_state_input.vnfr_uuid},
142 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000143 )
144 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200145 f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000146 )
147
Dario Faccin39301762023-04-17 16:56:37 +0200148 @activity.defn(name=ACTIVITY_SET_VNF_MODEL)
149 async def set_vnf_model(self, set_vnf_model_input: VnfInstantiateInput) -> None:
150 """Updates the model name of VNF in VNFR.
151
152 Collaborators:
153 DB Access Object
154
155 Raises (retryable):
156 DbException: If DB access or 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 """
170 update_namespace = {"namespace": set_vnf_model_input.model_name}
171 self.db.set_one(
172 "vnfrs", {"_id": set_vnf_model_input.vnfr_uuid}, update_namespace
173 )
174 self.logger.debug(
175 f"VNF {set_vnf_model_input.vnfr_uuid} model name is updated to {set_vnf_model_input.model_name}."
176 )
177
Dario Faccin0568c6c2023-04-14 10:19:07 +0200178
179class VnfSendNotifications:
180 """Perform Notification operations."""
181
182 def __init__(self):
183 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
184
185 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
186 async def send_notification_for_vnf(self) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000187 """If VNF LCM operation state changes, send notification updates.
188
189 This activity does nothing.
190
191 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200192 self.logger.debug("Send notification for VNF not implemented.")