| 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 ( |
| 20 | GetTaskQueue, |
| 21 | GetVimCloud, |
| 22 | GetVnfDescriptor, |
| 23 | GetVnfRecord, |
| Daniel Arndt | c5982f5 | 2023-07-10 09:51:58 -0300 | [diff] [blame] | 24 | GetModelNames, |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 25 | ChangeVnfState, |
| 26 | ChangeVnfInstantiationState, |
| 27 | SetVnfModel, |
| 28 | SendNotificationForVnf, |
| 29 | ) |
| 30 | from osm_common.temporal.dataclasses_common import VduComputeConstraints |
| 31 | from osm_common.temporal_task_queues.task_queues_mappings import ( |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 32 | VIM_TYPE_TASK_QUEUE_MAPPINGS, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 33 | ) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 34 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 35 | CONFIG_IDENTIFIER = "config::" |
| 36 | |
| 37 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 38 | @activity.defn(name=GetTaskQueue.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 39 | class GetTaskQueueImpl(GetTaskQueue): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 40 | async def __call__(self, activity_input: GetTaskQueue.Input) -> GetTaskQueue.Output: |
| 41 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 42 | 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] | 43 | task_queue = VIM_TYPE_TASK_QUEUE_MAPPINGS[vim_record["vim_type"]] |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 44 | 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] | 45 | return GetTaskQueue.Output(task_queue) |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 46 | |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 47 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 48 | @activity.defn(name=GetVimCloud.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 49 | class GetVimCloudImpl(GetVimCloud): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 50 | async def __call__(self, activity_input: GetVimCloud.Input) -> GetVimCloud.Output: |
| 51 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 52 | vim_record = self.db.get_one("vim_accounts", {"_id": vnfr["vim-account-id"]}) |
| 53 | cloud = vim_record["config"].get("cloud", "") |
| 54 | self.logger.debug(f"Got the cloud type {cloud} for VNF operations.") |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 55 | return GetVimCloud.Output(cloud=cloud) |
| Gulsum Atici | a1898b4 | 2023-04-25 15:48:10 +0300 | [diff] [blame] | 56 | |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 57 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 58 | @activity.defn(name=GetVnfRecord.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 59 | class GetVnfRecordImpl(GetVnfRecord): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 60 | async def __call__(self, activity_input: GetVnfRecord.Input) -> GetVnfRecord.Output: |
| 61 | vnfr = self.db.get_one("vnfrs", {"_id": activity_input.vnfr_uuid}) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 62 | self.logger.debug("Got the vnfr from Database for VNF operations.") |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 63 | return GetVnfRecord.Output(vnfr=vnfr) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 64 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 65 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 66 | @activity.defn(name=GetVnfDescriptor.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 67 | class GetVnfDescriptorImpl(GetVnfDescriptor): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 68 | async def __call__( |
| 69 | self, activity_input: GetVnfDescriptor.Input |
| 70 | ) -> GetVnfDescriptor.Output: |
| 71 | vnfd = self.db.get_one("vnfds", {"_id": activity_input.vnfd_uuid}) |
| 72 | return GetVnfDescriptor.Output(vnfd=vnfd) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 73 | |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 74 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 75 | class VnfSpecifications: |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 76 | @staticmethod |
| 77 | def get_vdu_instantiation_params( |
| 78 | vdu_id: str, vnf_instantiation_config: dict |
| 79 | ) -> dict: |
| 80 | for vdu in vnf_instantiation_config.get("vdu", []): |
| 81 | if vdu.get("id") == vdu_id: |
| 82 | return vdu.get("configurable-properties", {}) |
| 83 | return {} |
| 84 | |
| 85 | @staticmethod |
| 86 | def get_compute_constraints(vdu: dict, vnfd: dict) -> VduComputeConstraints: |
| 87 | compute_desc_id = vdu.get("virtual-compute-desc") |
| 88 | if not compute_desc_id: |
| 89 | return VduComputeConstraints(cores=0, mem=0) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 90 | flavor_details = VnfSpecifications._get_flavor_details(compute_desc_id, vnfd) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 91 | if not flavor_details: |
| 92 | return VduComputeConstraints(cores=0, mem=0) |
| 93 | |
| 94 | cpu_cores = flavor_details.get("virtual-cpu", {}).get("num-virtual-cpu", 0) |
| 95 | memory_gb = flavor_details.get("virtual-memory", {}).get("size", 0) |
| 96 | return VduComputeConstraints(cores=cpu_cores, mem=int(memory_gb)) |
| 97 | |
| 98 | @staticmethod |
| 99 | def _get_flavor_details(compute_desc_id: str, vnfd: dict) -> Any: |
| 100 | for flavor in vnfd.get("virtual-compute-desc", []): |
| 101 | if flavor.get("id") == compute_desc_id: |
| 102 | return flavor |
| 103 | return None |
| 104 | |
| 105 | @staticmethod |
| 106 | def get_application_config(vdu: dict, vdu_instantiation_config: dict) -> dict: |
| 107 | configurable_properties = vdu.get("configurable-properties", []) |
| 108 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 109 | config_from_descriptor = VnfSpecifications._get_only_config_items( |
| 110 | VnfSpecifications._list_to_dict(configurable_properties) |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 111 | ) |
| 112 | |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 113 | config_from_instantiation = VnfSpecifications._get_only_config_items( |
| Gulsum Atici | 50d12e0 | 2023-04-27 16:41:43 +0300 | [diff] [blame] | 114 | vdu_instantiation_config |
| 115 | ) |
| 116 | return {**config_from_descriptor, **config_from_instantiation} |
| 117 | |
| 118 | @staticmethod |
| 119 | def _get_only_config_items(config: dict) -> dict: |
| 120 | return { |
| 121 | key[len(CONFIG_IDENTIFIER) :]: value |
| 122 | for key, value in config.items() |
| 123 | if key.startswith(CONFIG_IDENTIFIER) |
| 124 | } |
| 125 | |
| 126 | @staticmethod |
| 127 | def _list_to_dict(indata: List[dict]) -> dict: |
| 128 | return { |
| 129 | item["key"]: item["value"] |
| 130 | for item in indata |
| 131 | if item.get("key") and item.get("value") |
| 132 | } |
| Gulsum Atici | 3636540 | 2023-04-07 00:07:07 +0300 | [diff] [blame] | 133 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 134 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 135 | @activity.defn(name=ChangeVnfState.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 136 | class ChangeVnfStateImpl(ChangeVnfState): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 137 | async def __call__(self, activity_input: ChangeVnfState.Input) -> None: |
| 138 | update_vnf_state = {"vnfState": activity_input.state.name} |
| 139 | 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] | 140 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 141 | 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] | 142 | ) |
| 143 | |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 144 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 145 | @activity.defn(name=ChangeVnfInstantiationState.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 146 | class ChangeVnfInstantiationStateImpl(ChangeVnfInstantiationState): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 147 | async def __call__(self, activity_input: ChangeVnfInstantiationState.Input) -> None: |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 148 | update_vnf_instantiation_state = { |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 149 | "instantiationState": activity_input.state.name |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 150 | } |
| 151 | self.db.set_one( |
| 152 | "vnfrs", |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 153 | {"_id": activity_input.vnfr_uuid}, |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 154 | update_vnf_instantiation_state, |
| Mark Beierl | 9e1379d | 2023-04-06 15:12:46 +0000 | [diff] [blame] | 155 | ) |
| 156 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 157 | 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] | 158 | ) |
| 159 | |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 160 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 161 | @activity.defn(name=SetVnfModel.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 162 | class SetVnfModelImpl(SetVnfModel): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 163 | async def __call__(self, activity_input: SetVnfModel.Input) -> None: |
| 164 | update_namespace = {"namespace": activity_input.model_name} |
| 165 | self.db.set_one("vnfrs", {"_id": activity_input.vnfr_uuid}, update_namespace) |
| Dario Faccin | 3930176 | 2023-04-17 16:56:37 +0200 | [diff] [blame] | 166 | self.logger.debug( |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 167 | 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] | 168 | ) |
| 169 | |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 170 | |
| Mark Beierl | 8262942 | 2023-06-29 20:22:36 +0000 | [diff] [blame] | 171 | @activity.defn(name=SendNotificationForVnf.__name__) |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 172 | class SendNotificationForVnfImpl(SendNotificationForVnf): |
| Dario Faccin | f5d65b5 | 2023-06-19 12:35:33 +0200 | [diff] [blame] | 173 | async def __call__(self, activity_input: SendNotificationForVnf.Input) -> None: |
| Dario Faccin | 0568c6c | 2023-04-14 10:19:07 +0200 | [diff] [blame] | 174 | self.logger.debug("Send notification for VNF not implemented.") |
| Daniel Arndt | c5982f5 | 2023-07-10 09:51:58 -0300 | [diff] [blame] | 175 | |
| 176 | |
| 177 | @activity.defn(name=GetModelNames.__name__) |
| 178 | class GetModelNamesImpl(GetModelNames): |
| 179 | async def __call__( |
| 180 | self, activity_input: GetModelNames.Input |
| 181 | ) -> GetModelNames.Output: |
| 182 | ns_id = activity_input.ns_uuid |
| 183 | |
| 184 | vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id}) |
| 185 | self.logger.debug( |
| 186 | f"Retrieved {len(vnfrs)} vnf records matching {ns_id} from database." |
| 187 | ) |
| 188 | |
| 189 | return GetModelNames.Output( |
| 190 | model_names={vnfr["namespace"] for vnfr in vnfrs if "namespace" in vnfr} |
| 191 | ) |