blob: 2602eebe65bae3262e15b7231175286b582ba6d3 [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 (
20 ACTIVITY_CHANGE_NF_STATE,
21 ACTIVITY_CHANGE_NF_INSTANTIATION_STATE,
22 ACTIVITY_CHANGE_NF_NOTIFICATION_STATE,
23 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 (
27 ChangeNFInstantiationStateInput,
28 ChangeNFStateInput,
29 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
82 @activity.defn(name=ACTIVITY_CHANGE_NF_STATE)
83 async def change_nf_state(self, nf_state_input: ChangeNFStateInput) -> None:
84 """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 """
104 update_nf_state = {"vnfState": nf_state_input.nf_state}
105 self.db.set_one("vnfrs", {"_id": nf_state_input.vnfr_uuid}, update_nf_state)
106 self.logger.debug(
107 f"VNF {nf_state_input.vnfr_uuid} state is updated to {nf_state_input.nf_state}."
108 )
109
110 @activity.defn(name=ACTIVITY_CHANGE_NF_INSTANTIATION_STATE)
111 async def change_nf_instantiation_state(
112 self, nf_instantiation_state_input: ChangeNFInstantiationStateInput
113 ) -> 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 """
134 update_nf_instantiation_state = {
135 "vnfState": nf_instantiation_state_input.nf_instantiation_state
136 }
137 self.db.set_one(
138 "vnfrs",
139 {"_id": nf_instantiation_state_input.vnfr_uuid},
140 update_nf_instantiation_state,
141 )
142 self.logger.debug(
143 f"VNF {nf_instantiation_state_input.vnfr_uuid} state is updated to {nf_instantiation_state_input.nf_instantiation_state}."
144 )
145
146 @activity.defn(name=ACTIVITY_CHANGE_NF_NOTIFICATION_STATE)
147 async def change_nf_notification_state(self) -> None:
148 """If VNF LCM operation state changes, send notification updates.
149
150 This activity does nothing.
151
152 """
153 pass