blob: dea055161534b25f31335245a62c75d6b6303ca1 [file] [log] [blame]
Patricia Reinoso38fb5da2023-04-27 13:48:50 +00001#########################################################################
Mark Beierl9e1379d2023-04-06 15:12:46 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Dario Faccinf5d65b52023-06-19 12:35:33 +020016from temporalio import activity
Gulsum Atici50d12e02023-04-27 16:41:43 +030017from typing import List, Any
18
Dario Faccinf5d65b52023-06-19 12:35:33 +020019from osm_common.temporal.activities.vnf import (
Patricia Reinoso14406d22023-07-11 15:17:49 +000020 DeleteVnfRecord,
Dario Faccinf5d65b52023-06-19 12:35:33 +020021 GetTaskQueue,
22 GetVimCloud,
23 GetVnfDescriptor,
24 GetVnfRecord,
Daniel Arndtc5982f52023-07-10 09:51:58 -030025 GetModelNames,
Dario Faccinf5d65b52023-06-19 12:35:33 +020026 ChangeVnfState,
27 ChangeVnfInstantiationState,
28 SetVnfModel,
29 SendNotificationForVnf,
30)
31from osm_common.temporal.dataclasses_common import VduComputeConstraints
32from osm_common.temporal_task_queues.task_queues_mappings import (
Gulsum Atici36365402023-04-07 00:07:07 +030033 VIM_TYPE_TASK_QUEUE_MAPPINGS,
Mark Beierl9e1379d2023-04-06 15:12:46 +000034)
Mark Beierl9e1379d2023-04-06 15:12:46 +000035
Gulsum Atici50d12e02023-04-27 16:41:43 +030036CONFIG_IDENTIFIER = "config::"
37
38
Mark Beierl82629422023-06-29 20:22:36 +000039@activity.defn(name=GetTaskQueue.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +020040class GetTaskQueueImpl(GetTaskQueue):
Dario Faccinf5d65b52023-06-19 12:35:33 +020041 async def __call__(self, activity_input: GetTaskQueue.Input) -> GetTaskQueue.Output:
42 vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid})
Gulsum Atici36365402023-04-07 00:07:07 +030043 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
Gulsum Atici625a6542023-04-18 15:27:18 +030044 task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]]
Mark Beierl9e1379d2023-04-06 15:12:46 +000045 self.logger.debug(f"Got the task queue {task_queue} for VNF operations.")
Dario Faccinf5d65b52023-06-19 12:35:33 +020046 return GetTaskQueue.Output(task_queue)
Mark Beierl9e1379d2023-04-06 15:12:46 +000047
Gulsum Aticia1898b42023-04-25 15:48:10 +030048
Mark Beierl82629422023-06-29 20:22:36 +000049@activity.defn(name=GetVimCloud.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +020050class GetVimCloudImpl(GetVimCloud):
Dario Faccinf5d65b52023-06-19 12:35:33 +020051 async def __call__(self, activity_input: GetVimCloud.Input) -> GetVimCloud.Output:
52 vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid})
Gulsum Aticia1898b42023-04-25 15:48:10 +030053 vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]})
54 cloud = vim_record["config"].get("cloud", "")
55 self.logger.debug(f"Got the cloud type {cloud} for VNF operations.")
Dario Faccinf5d65b52023-06-19 12:35:33 +020056 return GetVimCloud.Output(cloud=cloud)
Gulsum Aticia1898b42023-04-25 15:48:10 +030057
Gulsum Atici36365402023-04-07 00:07:07 +030058
Mark Beierl82629422023-06-29 20:22:36 +000059@activity.defn(name=GetVnfRecord.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +020060class GetVnfRecordImpl(GetVnfRecord):
Dario Faccinf5d65b52023-06-19 12:35:33 +020061 async def __call__(self, activity_input: GetVnfRecord.Input) -> GetVnfRecord.Output:
62 vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid})
Gulsum Atici50d12e02023-04-27 16:41:43 +030063 self.logger.debug("Got the vnfr from Database for VNF operations.")
Dario Faccinf5d65b52023-06-19 12:35:33 +020064 return GetVnfRecord.Output(vnfr=vnfr)
Gulsum Atici50d12e02023-04-27 16:41:43 +030065
Gulsum Atici50d12e02023-04-27 16:41:43 +030066
Mark Beierl82629422023-06-29 20:22:36 +000067@activity.defn(name=GetVnfDescriptor.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +020068class GetVnfDescriptorImpl(GetVnfDescriptor):
Dario Faccinf5d65b52023-06-19 12:35:33 +020069 async def __call__(
70 self, activity_input: GetVnfDescriptor.Input
71 ) -> GetVnfDescriptor.Output:
72 vnfd = self.db.get_one("vnfds", {"_id": activity_input.vnfd_uuid})
73 return GetVnfDescriptor.Output(vnfd=vnfd)
Gulsum Atici50d12e02023-04-27 16:41:43 +030074
Gulsum Atici50d12e02023-04-27 16:41:43 +030075
Dario Faccinf5d65b52023-06-19 12:35:33 +020076class VnfSpecifications:
Gulsum Atici50d12e02023-04-27 16:41:43 +030077 @staticmethod
78 def get_vdu_instantiation_params(
79 vdu_id: str, vnf_instantiation_config: dict
80 ) -> dict:
81 for vdu in vnf_instantiation_config.get("vdu", []):
82 if vdu.get("id") == vdu_id:
83 return vdu.get("configurable-properties", {})
84 return {}
85
86 @staticmethod
87 def get_compute_constraints(vdu: dict, vnfd: dict) -> VduComputeConstraints:
88 compute_desc_id = vdu.get("virtual-compute-desc")
89 if not compute_desc_id:
90 return VduComputeConstraints(cores=0, mem=0)
Dario Faccinf5d65b52023-06-19 12:35:33 +020091 flavor_details = VnfSpecifications._get_flavor_details(compute_desc_id, vnfd)
Gulsum Atici50d12e02023-04-27 16:41:43 +030092 if not flavor_details:
93 return VduComputeConstraints(cores=0, mem=0)
94
95 cpu_cores = flavor_details.get("virtual-cpu", {}).get("num-virtual-cpu", 0)
96 memory_gb = flavor_details.get("virtual-memory", {}).get("size", 0)
97 return VduComputeConstraints(cores=cpu_cores, mem=int(memory_gb))
98
99 @staticmethod
100 def _get_flavor_details(compute_desc_id: str, vnfd: dict) -> Any:
101 for flavor in vnfd.get("virtual-compute-desc", []):
102 if flavor.get("id") == compute_desc_id:
103 return flavor
104 return None
105
106 @staticmethod
107 def get_application_config(vdu: dict, vdu_instantiation_config: dict) -> dict:
108 configurable_properties = vdu.get("configurable-properties", [])
109
Dario Faccinf5d65b52023-06-19 12:35:33 +0200110 config_from_descriptor = VnfSpecifications._get_only_config_items(
111 VnfSpecifications._list_to_dict(configurable_properties)
Gulsum Atici50d12e02023-04-27 16:41:43 +0300112 )
113
Dario Faccinf5d65b52023-06-19 12:35:33 +0200114 config_from_instantiation = VnfSpecifications._get_only_config_items(
Gulsum Atici50d12e02023-04-27 16:41:43 +0300115 vdu_instantiation_config
116 )
117 return {**config_from_descriptor, **config_from_instantiation}
118
119 @staticmethod
120 def _get_only_config_items(config: dict) -> dict:
121 return {
122 key[len(CONFIG_IDENTIFIER) :]: value
123 for key, value in config.items()
124 if key.startswith(CONFIG_IDENTIFIER)
125 }
126
127 @staticmethod
128 def _list_to_dict(indata: List[dict]) -> dict:
129 return {
130 item["key"]: item["value"]
131 for item in indata
132 if item.get("key") and item.get("value")
133 }
Gulsum Atici36365402023-04-07 00:07:07 +0300134
Mark Beierl9e1379d2023-04-06 15:12:46 +0000135
Mark Beierl82629422023-06-29 20:22:36 +0000136@activity.defn(name=ChangeVnfState.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +0200137class ChangeVnfStateImpl(ChangeVnfState):
Dario Faccinf5d65b52023-06-19 12:35:33 +0200138 async def __call__(self, activity_input: ChangeVnfState.Input) -> None:
139 update_vnf_state = {"vnfState": activity_input.state.name}
140 self.db.set_one("vnfrs", {"_id": activity_input.vnfr_uuid}, update_vnf_state)
Mark Beierl9e1379d2023-04-06 15:12:46 +0000141 self.logger.debug(
Dario Faccinf5d65b52023-06-19 12:35:33 +0200142 f"VNF {activity_input.vnfr_uuid} state is updated to {activity_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000143 )
144
Mark Beierl9e1379d2023-04-06 15:12:46 +0000145
Mark Beierl82629422023-06-29 20:22:36 +0000146@activity.defn(name=ChangeVnfInstantiationState.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +0200147class ChangeVnfInstantiationStateImpl(ChangeVnfInstantiationState):
Dario Faccinf5d65b52023-06-19 12:35:33 +0200148 async def __call__(self, activity_input: ChangeVnfInstantiationState.Input) -> None:
Dario Faccin0568c6c2023-04-14 10:19:07 +0200149 update_vnf_instantiation_state = {
Dario Faccinf5d65b52023-06-19 12:35:33 +0200150 "instantiationState": activity_input.state.name
Mark Beierl9e1379d2023-04-06 15:12:46 +0000151 }
152 self.db.set_one(
153 "vnfrs",
Dario Faccinf5d65b52023-06-19 12:35:33 +0200154 {"_id": activity_input.vnfr_uuid},
Dario Faccin0568c6c2023-04-14 10:19:07 +0200155 update_vnf_instantiation_state,
Mark Beierl9e1379d2023-04-06 15:12:46 +0000156 )
157 self.logger.debug(
Dario Faccinf5d65b52023-06-19 12:35:33 +0200158 f"VNF {activity_input.vnfr_uuid} state is updated to {activity_input.state.name}."
Mark Beierl9e1379d2023-04-06 15:12:46 +0000159 )
160
Dario Faccin39301762023-04-17 16:56:37 +0200161
Mark Beierl82629422023-06-29 20:22:36 +0000162@activity.defn(name=SetVnfModel.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +0200163class SetVnfModelImpl(SetVnfModel):
Dario Faccinf5d65b52023-06-19 12:35:33 +0200164 async def __call__(self, activity_input: SetVnfModel.Input) -> None:
165 update_namespace = {"namespace": activity_input.model_name}
166 self.db.set_one("vnfrs", {"_id": activity_input.vnfr_uuid}, update_namespace)
Dario Faccin39301762023-04-17 16:56:37 +0200167 self.logger.debug(
Dario Faccinf5d65b52023-06-19 12:35:33 +0200168 f"VNF {activity_input.vnfr_uuid} model name is updated to {activity_input.model_name}."
Dario Faccin39301762023-04-17 16:56:37 +0200169 )
170
Dario Faccin0568c6c2023-04-14 10:19:07 +0200171
Mark Beierl82629422023-06-29 20:22:36 +0000172@activity.defn(name=SendNotificationForVnf.__name__)
Dario Faccinf5d65b52023-06-19 12:35:33 +0200173class SendNotificationForVnfImpl(SendNotificationForVnf):
Dario Faccinf5d65b52023-06-19 12:35:33 +0200174 async def __call__(self, activity_input: SendNotificationForVnf.Input) -> None:
Dario Faccin0568c6c2023-04-14 10:19:07 +0200175 self.logger.debug("Send notification for VNF not implemented.")
Daniel Arndtc5982f52023-07-10 09:51:58 -0300176
177
Patricia Reinoso14406d22023-07-11 15:17:49 +0000178@activity.defn(name=DeleteVnfRecord.__name__)
179class DeleteVnfRecordImpl(DeleteVnfRecord):
180 async def __call__(self, activity_input: DeleteVnfRecord.Input) -> None:
181 self.db.del_one("vnfrs", {"_id": activity_input.vnfr_uuid}, fail_on_empty=False)
182 self.logger.debug(f"VNF {activity_input.vnfr_uuid} record deleted from DB.")
183
184
Daniel Arndtc5982f52023-07-10 09:51:58 -0300185@activity.defn(name=GetModelNames.__name__)
186class GetModelNamesImpl(GetModelNames):
187 async def __call__(
188 self, activity_input: GetModelNames.Input
189 ) -> GetModelNames.Output:
190 ns_id = activity_input.ns_uuid
191
192 vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id})
193 self.logger.debug(
194 f"Retrieved {len(vnfrs)} vnf records matching {ns_id} from database."
195 )
196
197 return GetModelNames.Output(
198 model_names={vnfr["namespace"] for vnfr in vnfrs if "namespace" in vnfr}
199 )