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