blob: 3e2155b13322bd178a4cba0b3f781cea42671680 [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,
Mark Beierl9e1379d2023-04-06 15:12:46 +000025)
26from osm_common.dataclasses.temporal_dataclasses import (
Dario Faccin0568c6c2023-04-14 10:19:07 +020027 ChangeVnfInstantiationStateInput,
28 ChangeVnfStateInput,
Mark Beierl9e1379d2023-04-06 15:12:46 +000029 GetTaskQueueInput,
30 GetTaskQueueOutput,
31)
32
33
34class VnfOperations:
35 def __init__(self, db):
36 self.db = db
37 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
38
39 @activity.defn(name=ACTIVITY_GET_TASK_QUEUE)
40 async def get_task_queue(
41 self, get_task_queue_input: GetTaskQueueInput
42 ) -> GetTaskQueueOutput:
43 """Finds the appropriate task queue according to VIM type of VNF.
44
45 Collaborators:
46 DB Access Object
47
48 Raises (retryable):
49 DbException: If DB read operations fail
50
51 Activity Lifecycle:
52 This activity should complete relatively quickly (less than a
53 second). However, it would be reasonable to wait up to 10
54 seconds.
55
56 This activity will not report a heartbeat due to its
57 short-running nature.
58
59 It is not necessary to implement a back-off strategy for this
60 activity, the operation is idempotent.
61
62 """
63 vnfrs = self.db.get_list("vnfrs", {"_id": get_task_queue_input.vnfr_uuid})
64 vim_record = self.db.get_list("vim_accounts", {"_id": vnfrs["vim-account-id"]})
Patricia Reinoso52431352023-04-05 15:35:48 +000065 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim-type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000066 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
67 return GetTaskQueueOutput(task_queue)
68
69
70class VnfDbActivity:
71
72 """Perform Database operations for NS accounts.
73
74 Args:
75 db (object): Data Access Object
76 """
77
78 def __init__(self, db):
79 self.db = db
80 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
81
Dario Faccin0568c6c2023-04-14 10:19:07 +020082 @activity.defn(name=ACTIVITY_CHANGE_VNF_STATE)
83 async def change_vnf_state(self, vnf_state_input: ChangeVnfStateInput) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +000084 """Updates the VNF State in VNFR.
85
86 Collaborators:
87 DB Access Object
88
89 Raises (retryable):
90 DbException: If DB access/update fails, the collection or DB record ID does not exist.
91
92 Activity Lifecycle:
93 This activity should complete relatively quickly (less than a
94 second). However, it would be reasonable to wait up to 10
95 seconds.
96
97 This activity will not report a heartbeat due to its
98 short-running nature.
99
100 It is not necessary to implement a back-off strategy for this
101 activity, the operation is idempotent.
102
103 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200104 update_vnf_state = {"vnfState": vnf_state_input.state}
105 self.db.set_one("vnfrs", {"_id": vnf_state_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000106 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200107 f"VNF {vnf_state_input.vnfr_uuid} state is updated to {vnf_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000108 )
109
Dario Faccin0568c6c2023-04-14 10:19:07 +0200110 @activity.defn(name=ACTIVITY_CHANGE_VNF_INSTANTIATION_STATE)
111 async def change_vnf_instantiation_state(
112 self, vnf_instantiation_state_input: ChangeVnfInstantiationStateInput
Mark Beierl9e1379d2023-04-06 15:12:46 +0000113 ) -> None:
114 """Updates the VNF Instantiation State in VNFR.
115
116 Collaborators:
117 DB Access Object
118
119 Raises (retryable):
120 DbException: If DB access or update fails
121
122 Activity Lifecycle:
123 This activity should complete relatively quickly (less than a
124 second). However, it would be reasonable to wait up to 10
125 seconds.
126
127 This activity will not report a heartbeat due to its
128 short-running nature.
129
130 It is not necessary to implement a back-off strategy for this
131 activity, the operation is idempotent.
132
133 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200134 update_vnf_instantiation_state = {
135 "vnfState": vnf_instantiation_state_input.state
Mark Beierl9e1379d2023-04-06 15:12:46 +0000136 }
137 self.db.set_one(
138 "vnfrs",
Dario Faccin0568c6c2023-04-14 10:19:07 +0200139 {"_id": vnf_instantiation_state_input.vnfr_uuid},
140 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000141 )
142 self.logger.debug(
Dario Faccin0568c6c2023-04-14 10:19:07 +0200143 f"VNF {vnf_instantiation_state_input.vnfr_uuid} state is updated to {vnf_instantiation_state_input.state}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000144 )
145
Dario Faccin0568c6c2023-04-14 10:19:07 +0200146
147class VnfSendNotifications:
148 """Perform Notification operations."""
149
150 def __init__(self):
151 self.logger = logging.getLogger(f"lcm.act.{self.__class__.__name__}")
152
153 @activity.defn(name=ACTIVITY_SEND_NOTIFICATION_FOR_VNF)
154 async def send_notification_for_vnf(self) -> None:
Mark Beierl9e1379d2023-04-06 15:12:46 +0000155 """If VNF LCM operation state changes, send notification updates.
156
157 This activity does nothing.
158
159 """
Dario Faccin0568c6c2023-04-14 10:19:07 +0200160 self.logger.debug("Send notification for VNF not implemented.")