| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 1 | # Copyright 2021 Canonical Ltd. |
| 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 implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from typing import Any, Dict, NoReturn |
| 16 | |
| 17 | |
| 18 | def safe_get_ee_relation( |
| 19 | nsr_id: str, ee_relation: Dict[str, Any], vnf_profile_id: str = None |
| 20 | ) -> Dict[str, Any]: |
| 21 | return { |
| 22 | "nsr-id": nsr_id, |
| 23 | "vnf-profile-id": ee_relation.get("vnf-profile-id") or vnf_profile_id, |
| 24 | "vdu-profile-id": ee_relation.get("vdu-profile-id"), |
| 25 | "kdu-resource-profile-id": ee_relation.get("kdu-resource-profile-id"), |
| 26 | "execution-environment-ref": ee_relation.get("execution-environment-ref"), |
| 27 | "endpoint": ee_relation["endpoint"], |
| 28 | } |
| 29 | |
| 30 | |
| 31 | class EELevel: |
| 32 | VDU = "vdu" |
| 33 | VNF = "vnf" |
| 34 | KDU = "kdu" |
| 35 | NS = "ns" |
| 36 | |
| 37 | @staticmethod |
| 38 | def get_level(ee_relation: dict): |
| 39 | """Get the execution environment level""" |
| 40 | level = None |
| 41 | if ( |
| 42 | not ee_relation["vnf-profile-id"] |
| 43 | and not ee_relation["vdu-profile-id"] |
| 44 | and not ee_relation["kdu-resource-profile-id"] |
| 45 | ): |
| 46 | level = EELevel.NS |
| 47 | elif ( |
| 48 | ee_relation["vnf-profile-id"] |
| 49 | and not ee_relation["vdu-profile-id"] |
| 50 | and not ee_relation["kdu-resource-profile-id"] |
| 51 | ): |
| 52 | level = EELevel.VNF |
| 53 | elif ( |
| 54 | ee_relation["vnf-profile-id"] |
| 55 | and ee_relation["vdu-profile-id"] |
| 56 | and not ee_relation["kdu-resource-profile-id"] |
| 57 | ): |
| 58 | level = EELevel.VDU |
| 59 | elif ( |
| 60 | ee_relation["vnf-profile-id"] |
| 61 | and not ee_relation["vdu-profile-id"] |
| 62 | and ee_relation["kdu-resource-profile-id"] |
| 63 | ): |
| 64 | level = EELevel.KDU |
| 65 | else: |
| 66 | raise Exception("invalid relation endpoint") |
| 67 | return level |
| 68 | |
| 69 | |
| 70 | class EERelation(dict): |
| 71 | """Represents the execution environment of a relation""" |
| 72 | |
| 73 | def __init__( |
| 74 | self, |
| 75 | relation_ee: Dict[str, Any], |
| 76 | ) -> NoReturn: |
| 77 | """ |
| 78 | Args: |
| 79 | relation_ee: Relation Endpoint object in the VNFd or NSd. |
| 80 | Example: |
| 81 | { |
| 82 | "nsr-id": <>, |
| 83 | "vdu-profile-id": <>, |
| 84 | "kdu-resource-profile-id": <>, |
| 85 | "vnf-profile-id": <>, |
| 86 | "execution-environment-ref": <>, |
| 87 | "endpoint": <>, |
| 88 | } |
| 89 | """ |
| 90 | for key, value in relation_ee.items(): |
| 91 | self.__setitem__(key, value) |
| 92 | |
| 93 | @property |
| 94 | def vdu_profile_id(self): |
| 95 | """Returns the vdu-profile id""" |
| 96 | return self["vdu-profile-id"] |
| 97 | |
| 98 | @property |
| 99 | def kdu_resource_profile_id(self): |
| 100 | """Returns the kdu-resource-profile id""" |
| 101 | return self["kdu-resource-profile-id"] |
| 102 | |
| 103 | @property |
| 104 | def vnf_profile_id(self): |
| 105 | """Returns the vnf-profile id""" |
| 106 | return self["vnf-profile-id"] |
| 107 | |
| 108 | @property |
| 109 | def execution_environment_ref(self): |
| 110 | """Returns the reference to the execution environment (id)""" |
| 111 | return self["execution-environment-ref"] |
| 112 | |
| 113 | @property |
| 114 | def endpoint(self): |
| 115 | """Returns the endpoint of the execution environment""" |
| 116 | return self["endpoint"] |
| 117 | |
| 118 | @property |
| 119 | def nsr_id(self) -> str: |
| 120 | """Returns the nsr id""" |
| 121 | return self["nsr-id"] |
| 122 | |
| 123 | |
| 124 | class Relation(dict): |
| 125 | """Represents a relation""" |
| aticig | 15db614 | 2022-01-24 12:51:26 +0300 | [diff] [blame] | 126 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 127 | def __init__(self, name, provider: EERelation, requirer: EERelation) -> NoReturn: |
| 128 | """ |
| 129 | Args: |
| 130 | name: Name of the relation. |
| 131 | provider: Execution environment that provides the service for the relation. |
| 132 | requirer: Execution environment that requires the service from the provider. |
| 133 | """ |
| 134 | self.__setitem__("name", name) |
| 135 | self.__setitem__("provider", provider) |
| 136 | self.__setitem__("requirer", requirer) |
| 137 | |
| 138 | @property |
| 139 | def name(self) -> str: |
| 140 | """Returns the name of the relation""" |
| 141 | return self["name"] |
| 142 | |
| 143 | @property |
| 144 | def provider(self) -> EERelation: |
| 145 | """Returns the provider endpoint""" |
| 146 | return self["provider"] |
| 147 | |
| 148 | @property |
| 149 | def requirer(self) -> EERelation: |
| 150 | """Returns the requirer endpoint""" |
| 151 | return self["requirer"] |
| 152 | |
| 153 | |
| 154 | class DeployedComponent(dict): |
| Pedro Escaleira | 78adbaf | 2022-04-20 18:00:17 +0100 | [diff] [blame] | 155 | """Represents a deployed component (nsr["_admin"]["deployed"]["VCA" | "K8s"])""" |
| aticig | 15db614 | 2022-01-24 12:51:26 +0300 | [diff] [blame] | 156 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 157 | def __init__(self, data: Dict[str, Any]): |
| 158 | """ |
| 159 | Args: |
| 160 | data: dictionary with the data of the deployed component |
| 161 | """ |
| 162 | for key, value in data.items(): |
| 163 | self.__setitem__(key, value) |
| 164 | |
| 165 | @property |
| 166 | def vnf_profile_id(self): |
| 167 | """Returns the vnf-profile id""" |
| 168 | return self["member-vnf-index"] |
| 169 | |
| 170 | @property |
| 171 | def ee_id(self): |
| 172 | raise NotImplementedError() |
| 173 | |
| 174 | @property |
| 175 | def config_sw_installed(self) -> bool: |
| 176 | raise NotImplementedError() |
| 177 | |
| 178 | |
| 179 | class DeployedK8sResource(DeployedComponent): |
| 180 | """Represents a deployed component for a kdu resource""" |
| aticig | 15db614 | 2022-01-24 12:51:26 +0300 | [diff] [blame] | 181 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 182 | def __init__(self, data: Dict[str, Any]): |
| 183 | super().__init__(data) |
| 184 | |
| 185 | @property |
| 186 | def ee_id(self): |
| 187 | """Returns the execution environment id""" |
| Pedro Escaleira | 78adbaf | 2022-04-20 18:00:17 +0100 | [diff] [blame] | 188 | model = self["namespace"] |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 189 | application_name = self["resource-name"] |
| 190 | return f"{model}.{application_name}.k8s" |
| 191 | |
| 192 | @property |
| 193 | def config_sw_installed(self) -> bool: |
| 194 | return True |
| 195 | |
| 196 | |
| 197 | class DeployedVCA(DeployedComponent): |
| 198 | """Represents a VCA deployed component""" |
| aticig | 15db614 | 2022-01-24 12:51:26 +0300 | [diff] [blame] | 199 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 200 | def __init__(self, nsr_id: str, deployed_vca: Dict[str, Any]) -> NoReturn: |
| 201 | """ |
| 202 | Args: |
| 203 | db_nsr: NS record |
| 204 | vca_index: Vca index for the deployed VCA |
| 205 | """ |
| 206 | super().__init__(deployed_vca) |
| 207 | self.nsr_id = nsr_id |
| 208 | |
| 209 | @property |
| 210 | def ee_id(self) -> str: |
| 211 | """Returns the execution environment id""" |
| 212 | return self["ee_id"] |
| 213 | |
| 214 | @property |
| 215 | def vdu_profile_id(self) -> str: |
| 216 | """Returns the vdu-profile id""" |
| 217 | return self["vdu_id"] |
| 218 | |
| 219 | @property |
| 220 | def execution_environment_ref(self) -> str: |
| 221 | """Returns the execution environment id""" |
| 222 | return self["ee_descriptor_id"] |
| 223 | |
| 224 | @property |
| 225 | def config_sw_installed(self) -> bool: |
| 226 | return self.get("config_sw_installed", False) |
| Patricia Reinoso | ceb0386 | 2023-01-12 09:40:53 +0000 | [diff] [blame] | 227 | |
| 228 | @property |
| 229 | def target_element(self) -> str: |
| 230 | return self.get("target_element", "") |