| Patricia Reinoso | 38fb5da | 2023-04-27 13:48:50 +0000 | [diff] [blame] | 1 | ######################################################################### |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 2 | # |
| 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 Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 16 | from temporalio import activity |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 17 | from typing import List, Any |
| 18 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 19 | from osm_common.temporal.activities.vnf import ( |
| Patricia Reinoso | 14406d2 | 2023-07-11 15:17:49 +0000 | [diff] [blame] | 20 | DeleteVnfRecord, |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 21 | GetTaskQueue, |
| 22 | GetVimCloud, |
| 23 | GetVnfDescriptor, |
| 24 | GetVnfRecord, |
| Daniel Arndt | c5982f5 | 2023-07-10 09:51:58 -0300 | [diff] [blame] | 25 | GetModelNames, |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 26 | ChangeVnfState, |
| 27 | ChangeVnfInstantiationState, |
| 28 | SetVnfModel, |
| 29 | SendNotificationForVnf, |
| 30 | ) |
| 31 | from osm_common.temporal.dataclasses_common import VduComputeConstraints |
| 32 | from osm_common.temporal_task_queues.task_queues_mappings import ( |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 33 | VIM_TYPE_TASK_QUEUE_MAPPINGS, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 34 | ) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 35 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 36 | CONFIG_IDENTIFIER = "config::" |
| 37 | |
| 38 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 39 | @activity.defn(name=GetTaskQueue.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 40 | class GetTaskQueueImpl(GetTaskQueue): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 41 | async def __call__(self, activity_input: GetTaskQueue.Input) -> GetTaskQueue.Output: |
| 42 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 43 | vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]}) |
| Gulsum Atici | 625a654 | 2023-04-18 15:27:18 +0300 | [diff] [blame] | 44 | task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]] |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 45 | self.logger.debug(f"Got the task queue {task_queue} for VNF operations.") |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 46 | return GetTaskQueue.Output(task_queue) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 47 | |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 48 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 49 | @activity.defn(name=GetVimCloud.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 50 | class GetVimCloudImpl(GetVimCloud): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 51 | async def __call__(self, activity_input: GetVimCloud.Input) -> GetVimCloud.Output: |
| 52 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 53 | 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 Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 56 | return GetVimCloud.Output(cloud=cloud) |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 57 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 58 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 59 | @activity.defn(name=GetVnfRecord.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 60 | class GetVnfRecordImpl(GetVnfRecord): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 61 | async def __call__(self, activity_input: GetVnfRecord.Input) -> GetVnfRecord.Output: |
| 62 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 63 | self.logger.debug("Got the vnfr from Database for VNF operations.") |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 64 | return GetVnfRecord.Output(vnfr=vnfr) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 65 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 66 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 67 | @activity.defn(name=GetVnfDescriptor.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 68 | class GetVnfDescriptorImpl(GetVnfDescriptor): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 69 | 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 Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 74 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 75 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 76 | class VnfSpecifications: |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 77 | @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 Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 91 | flavor_details = VnfSpecifications._get_flavor_details(compute_desc_id, vnfd) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 92 | 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 Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 110 | config_from_descriptor = VnfSpecifications._get_only_config_items( |
| 111 | VnfSpecifications._list_to_dict(configurable_properties) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 112 | ) |
| 113 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 114 | config_from_instantiation = VnfSpecifications._get_only_config_items( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 115 | 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 Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 134 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 135 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 136 | @activity.defn(name=ChangeVnfState.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 137 | class ChangeVnfStateImpl(ChangeVnfState): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 138 | 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 Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 141 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 142 | f"VNF {activity_input.vnfr_uuid} state is updated to {activity_input.state.name}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 143 | ) |
| 144 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 145 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 146 | @activity.defn(name=ChangeVnfInstantiationState.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 147 | class ChangeVnfInstantiationStateImpl(ChangeVnfInstantiationState): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 148 | async def __call__(self, activity_input: ChangeVnfInstantiationState.Input) -> None: |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 149 | update_vnf_instantiation_state = { |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 150 | "instantiationState": activity_input.state.name |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 151 | } |
| 152 | self.db.set_one( |
| 153 | "vnfrs", |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 154 | {"_id": activity_input.vnfr_uuid}, |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 155 | update_vnf_instantiation_state, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 156 | ) |
| 157 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 158 | f"VNF {activity_input.vnfr_uuid} state is updated to {activity_input.state.name}." |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 159 | ) |
| 160 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 161 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 162 | @activity.defn(name=SetVnfModel.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 163 | class SetVnfModelImpl(SetVnfModel): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 164 | 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 Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 167 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 168 | f"VNF {activity_input.vnfr_uuid} model name is updated to {activity_input.model_name}." |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 169 | ) |
| 170 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 171 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 172 | @activity.defn(name=SendNotificationForVnf.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 173 | class SendNotificationForVnfImpl(SendNotificationForVnf): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 174 | async def __call__(self, activity_input: SendNotificationForVnf.Input) -> None: |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 175 | self.logger.debug("Send notification for VNF not implemented.") |
| Daniel Arndt | c5982f5 | 2023-07-10 09:51:58 -0300 | [diff] [blame] | 176 | |
| 177 | |
| Patricia Reinoso | 14406d2 | 2023-07-11 15:17:49 +0000 | [diff] [blame] | 178 | @activity.defn(name=DeleteVnfRecord.__name__) |
| 179 | class 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 Arndt | c5982f5 | 2023-07-10 09:51:58 -0300 | [diff] [blame] | 185 | @activity.defn(name=GetModelNames.__name__) |
| 186 | class 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 | ) |