| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2020 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | # |
| 6 | # This file is part of OSM Monitoring module |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | # For those usages not covered by the Apache License, Version 2.0 please |
| 22 | # contact: fbravo@whitestack.com |
| 23 | ## |
| 24 | |
| 25 | from osm_lcm.data_utils import list_utils |
| 26 | |
| 27 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 28 | def get_vdu_list(vnfd): |
| 29 | return vnfd.get("vdu", ()) |
| 30 | |
| 31 | |
| 32 | def get_kdu_list(vnfd): |
| 33 | return vnfd.get("kdu", ()) |
| 34 | |
| 35 | |
| David Garcia | 78b6e6d | 2022-04-29 05:50:46 +0200 | [diff] [blame] | 36 | def get_kdu(vnfd, kdu_name): |
| 37 | return list_utils.find_in_list( |
| 38 | get_kdu_list(vnfd), lambda kdu: kdu["name"] == kdu_name |
| 39 | ) |
| 40 | |
| 41 | |
| 42 | def get_kdu_services(kdu): |
| 43 | return kdu.get("service", []) |
| 44 | |
| 45 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 46 | def get_ee_sorted_initial_config_primitive_list( |
| 47 | primitive_list, vca_deployed, ee_descriptor_id |
| 48 | ): |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 49 | """ |
| 50 | Generates a list of initial-config-primitive based on the list provided by the descriptor. It includes internal |
| 51 | primitives as verify-ssh-credentials, or config when needed |
| 52 | :param primitive_list: information of the descriptor |
| 53 | :param vca_deployed: information of the deployed, needed for known if it is related to an NS, VNF, VDU and if |
| 54 | this element contains a ssh public key |
| 55 | :param ee_descriptor_id: execution environment descriptor id. It is the value of |
| 56 | XXX_configuration.execution-environment-list.INDEX.id; it can be None |
| 57 | :return: The modified list. Can ba an empty list, but always a list |
| 58 | """ |
| 59 | primitive_list = primitive_list or [] |
| 60 | primitive_list = [ |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 61 | p |
| 62 | for p in primitive_list |
| 63 | if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 64 | ] |
| 65 | if primitive_list: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 66 | primitive_list.sort(key=lambda val: int(val["seq"])) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 67 | |
| 68 | # look for primitive config, and get the position. None if not present |
| 69 | config_position = None |
| 70 | for index, primitive in enumerate(primitive_list): |
| 71 | if primitive["name"] == "config": |
| 72 | config_position = index |
| 73 | break |
| 74 | |
| 75 | # for NS, add always a config primitive if not present (bug 874) |
| 76 | if not vca_deployed["member-vnf-index"] and config_position is None: |
| 77 | primitive_list.insert(0, {"name": "config", "parameter": []}) |
| 78 | config_position = 0 |
| 79 | # TODO revise if needed: for VNF/VDU add verify-ssh-credentials after config |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 80 | if ( |
| 81 | vca_deployed["member-vnf-index"] |
| 82 | and config_position is not None |
| 83 | and vca_deployed.get("ssh-public-key") |
| 84 | ): |
| 85 | primitive_list.insert( |
| 86 | config_position + 1, {"name": "verify-ssh-credentials", "parameter": []} |
| 87 | ) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 88 | return primitive_list |
| 89 | |
| 90 | |
| 91 | def get_ee_sorted_terminate_config_primitive_list(primitive_list, ee_descriptor_id): |
| 92 | primitive_list = primitive_list or [] |
| 93 | primitive_list = [ |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 94 | p |
| 95 | for p in primitive_list |
| 96 | if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 97 | ] |
| 98 | if primitive_list: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 99 | primitive_list.sort(key=lambda val: int(val["seq"])) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 100 | return primitive_list |
| 101 | |
| 102 | |
| 103 | def get_vdu_profile(vnfd, vdu_profile_id): |
| 104 | return list_utils.find_in_list( |
| 105 | vnfd.get("df", ())[0]["vdu-profile"], |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 106 | lambda vdu_profile: vdu_profile["id"] == vdu_profile_id, |
| 107 | ) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 108 | |
| 109 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 110 | def get_kdu_resource_profile(vnfd, kdu_profile_id): |
| aktas | 5f75f10 | 2021-03-15 11:26:10 +0300 | [diff] [blame] | 111 | return list_utils.find_in_list( |
| 112 | vnfd.get("df", ())[0]["kdu-resource-profile"], |
| 113 | lambda kdu_profile: kdu_profile["id"] == kdu_profile_id, |
| 114 | ) |
| 115 | |
| 116 | |
| bravof | e5a31bc | 2021-02-17 19:09:12 -0300 | [diff] [blame] | 117 | def get_configuration(vnfd, entity_id): |
| 118 | lcm_ops_config = vnfd.get("df")[0].get("lcm-operations-configuration") |
| 119 | if not lcm_ops_config: |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 120 | return None |
| bravof | e5a31bc | 2021-02-17 19:09:12 -0300 | [diff] [blame] | 121 | ops_vnf = lcm_ops_config.get("operate-vnf-op-config") |
| 122 | if not ops_vnf: |
| 123 | return None |
| 124 | day12ops = ops_vnf.get("day1-2", []) |
| aktas | 1325156 | 2021-02-12 22:19:10 +0300 | [diff] [blame] | 125 | return list_utils.find_in_list( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 126 | day12ops, lambda configuration: configuration["id"] == entity_id |
| 127 | ) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 128 | |
| 129 | |
| David Garcia | b4ebcd0 | 2021-10-28 02:00:43 +0200 | [diff] [blame] | 130 | def get_relation_list(vnfd, entity_id): |
| 131 | return (get_configuration(vnfd, entity_id) or {}).get("relation", []) |
| 132 | |
| 133 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 134 | def get_virtual_link_profiles(vnfd): |
| 135 | return vnfd.get("df")[0].get("virtual-link-profile", ()) |
| 136 | |
| 137 | |
| 138 | def get_vdu(vnfd, vdu_id): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 139 | return list_utils.find_in_list(vnfd.get("vdu", ()), lambda vdu: vdu["id"] == vdu_id) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def get_vdu_index(vnfd, vdu_id): |
| 143 | target_vdu = list_utils.find_in_list( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 144 | vnfd.get("vdu", ()), lambda vdu: vdu["id"] == vdu_id |
| 145 | ) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 146 | if target_vdu: |
| 147 | return vnfd.get("vdu", ()).index(target_vdu) |
| 148 | else: |
| 149 | return -1 |
| bravof | 832f899 | 2020-12-07 12:57:31 -0300 | [diff] [blame] | 150 | |
| 151 | |
| 152 | def get_scaling_aspect(vnfd): |
| 153 | return vnfd.get("df", ())[0].get("scaling-aspect", ()) |
| 154 | |
| 155 | |
| 156 | def get_number_of_instances(vnfd, vdu_id): |
| 157 | return list_utils.find_in_list( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 158 | vnfd.get("df", ())[0].get("instantiation-level", ())[0].get("vdu-level", ()), |
| 159 | lambda a_vdu: a_vdu["vdu-id"] == vdu_id, |
| aktas | 1325156 | 2021-02-12 22:19:10 +0300 | [diff] [blame] | 160 | ).get("number-of-instances", 1) |
| bravof | 9a256db | 2021-02-22 18:02:07 -0300 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def get_juju_ee_ref(vnfd, entity_id): |
| 164 | return list_utils.find_in_list( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 165 | get_configuration(vnfd, entity_id).get("execution-environment-list", []), |
| 166 | lambda ee: "juju" in ee, |
| bravof | 9a256db | 2021-02-22 18:02:07 -0300 | [diff] [blame] | 167 | ) |
| aticig | dffa621 | 2022-04-12 15:27:53 +0300 | [diff] [blame] | 168 | |
| 169 | |
| jegan | 9944890 | 2024-12-06 07:19:34 +0000 | [diff] [blame] | 170 | def get_helm_ee_ref(vnfd, entity_id): |
| 171 | return list_utils.find_in_list( |
| 172 | get_configuration(vnfd, entity_id).get("execution-environment-list", []), |
| 173 | lambda ee: "helm-chart" in ee, |
| 174 | ) |
| 175 | |
| 176 | |
| aticig | dffa621 | 2022-04-12 15:27:53 +0300 | [diff] [blame] | 177 | def find_software_version(vnfd: dict) -> str: |
| 178 | """Find the sotware version in the VNFD descriptors |
| 179 | |
| 180 | Args: |
| 181 | vnfd (dict): Descriptor as a dictionary |
| 182 | |
| 183 | Returns: |
| 184 | software-version (str) |
| 185 | """ |
| 186 | |
| 187 | default_sw_version = "1.0" |
| 188 | |
| 189 | if vnfd.get("vnfd"): |
| 190 | vnfd = vnfd["vnfd"] |
| 191 | |
| 192 | if vnfd.get("software-version"): |
| 193 | return vnfd["software-version"] |
| 194 | |
| 195 | else: |
| 196 | return default_sw_version |
| Gabriel Cuba | 1411a00 | 2022-10-07 11:38:23 -0500 | [diff] [blame] | 197 | |
| 198 | |
| 199 | def check_helm_ee_in_ns(db_vnfds: list) -> bool: |
| 200 | for vnfd in db_vnfds: |
| 201 | descriptor_config = get_configuration(vnfd, vnfd["id"]) |
| 202 | if not ( |
| 203 | descriptor_config and "execution-environment-list" in descriptor_config |
| 204 | ): |
| 205 | continue |
| 206 | ee_list = descriptor_config.get("execution-environment-list", []) |
| 207 | if list_utils.find_in_list(ee_list, lambda ee_item: "helm-chart" in ee_item): |
| 208 | return True |