blob: 17a98a99b0d369568dfaf16bf3aec26ae35f95a7 [file] [log] [blame]
bravof922c4172020-11-24 21:21:43 -03001# -*- 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
25from osm_lcm.data_utils import list_utils
26
27
28def get_lcm_operations_configuration(vnfd):
29 return vnfd.get("df", ())[0].get("lcm-operations-configuration", ())
30
31
32def get_vdu_list(vnfd):
33 return vnfd.get("vdu", ())
34
35
36def get_kdu_list(vnfd):
37 return vnfd.get("kdu", ())
38
39
garciadeblas5697b8b2021-03-24 09:17:02 +010040def get_ee_sorted_initial_config_primitive_list(
41 primitive_list, vca_deployed, ee_descriptor_id
42):
bravof922c4172020-11-24 21:21:43 -030043 """
44 Generates a list of initial-config-primitive based on the list provided by the descriptor. It includes internal
45 primitives as verify-ssh-credentials, or config when needed
46 :param primitive_list: information of the descriptor
47 :param vca_deployed: information of the deployed, needed for known if it is related to an NS, VNF, VDU and if
48 this element contains a ssh public key
49 :param ee_descriptor_id: execution environment descriptor id. It is the value of
50 XXX_configuration.execution-environment-list.INDEX.id; it can be None
51 :return: The modified list. Can ba an empty list, but always a list
52 """
53 primitive_list = primitive_list or []
54 primitive_list = [
garciadeblas5697b8b2021-03-24 09:17:02 +010055 p
56 for p in primitive_list
57 if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id
bravof922c4172020-11-24 21:21:43 -030058 ]
59 if primitive_list:
garciadeblas5697b8b2021-03-24 09:17:02 +010060 primitive_list.sort(key=lambda val: int(val["seq"]))
bravof922c4172020-11-24 21:21:43 -030061
62 # look for primitive config, and get the position. None if not present
63 config_position = None
64 for index, primitive in enumerate(primitive_list):
65 if primitive["name"] == "config":
66 config_position = index
67 break
68
69 # for NS, add always a config primitive if not present (bug 874)
70 if not vca_deployed["member-vnf-index"] and config_position is None:
71 primitive_list.insert(0, {"name": "config", "parameter": []})
72 config_position = 0
73 # TODO revise if needed: for VNF/VDU add verify-ssh-credentials after config
garciadeblas5697b8b2021-03-24 09:17:02 +010074 if (
75 vca_deployed["member-vnf-index"]
76 and config_position is not None
77 and vca_deployed.get("ssh-public-key")
78 ):
79 primitive_list.insert(
80 config_position + 1, {"name": "verify-ssh-credentials", "parameter": []}
81 )
bravof922c4172020-11-24 21:21:43 -030082 return primitive_list
83
84
85def get_ee_sorted_terminate_config_primitive_list(primitive_list, ee_descriptor_id):
86 primitive_list = primitive_list or []
87 primitive_list = [
garciadeblas5697b8b2021-03-24 09:17:02 +010088 p
89 for p in primitive_list
90 if p.get("execution-environment-ref", ee_descriptor_id) == ee_descriptor_id
bravof922c4172020-11-24 21:21:43 -030091 ]
92 if primitive_list:
garciadeblas5697b8b2021-03-24 09:17:02 +010093 primitive_list.sort(key=lambda val: int(val["seq"]))
bravof922c4172020-11-24 21:21:43 -030094 return primitive_list
95
96
97def get_vdu_profile(vnfd, vdu_profile_id):
98 return list_utils.find_in_list(
99 vnfd.get("df", ())[0]["vdu-profile"],
garciadeblas5697b8b2021-03-24 09:17:02 +0100100 lambda vdu_profile: vdu_profile["id"] == vdu_profile_id,
101 )
bravof922c4172020-11-24 21:21:43 -0300102
103
aktas5f75f102021-03-15 11:26:10 +0300104def get_kdu_profile(vnfd, kdu_profile_id):
105 return list_utils.find_in_list(
106 vnfd.get("df", ())[0]["kdu-resource-profile"],
107 lambda kdu_profile: kdu_profile["id"] == kdu_profile_id,
108 )
109
110
bravofe5a31bc2021-02-17 19:09:12 -0300111def get_configuration(vnfd, entity_id):
112 lcm_ops_config = vnfd.get("df")[0].get("lcm-operations-configuration")
113 if not lcm_ops_config:
bravof922c4172020-11-24 21:21:43 -0300114 return None
bravofe5a31bc2021-02-17 19:09:12 -0300115 ops_vnf = lcm_ops_config.get("operate-vnf-op-config")
116 if not ops_vnf:
117 return None
118 day12ops = ops_vnf.get("day1-2", [])
aktas13251562021-02-12 22:19:10 +0300119 return list_utils.find_in_list(
garciadeblas5697b8b2021-03-24 09:17:02 +0100120 day12ops, lambda configuration: configuration["id"] == entity_id
121 )
bravof922c4172020-11-24 21:21:43 -0300122
123
124def get_virtual_link_profiles(vnfd):
125 return vnfd.get("df")[0].get("virtual-link-profile", ())
126
127
128def get_vdu(vnfd, vdu_id):
garciadeblas5697b8b2021-03-24 09:17:02 +0100129 return list_utils.find_in_list(vnfd.get("vdu", ()), lambda vdu: vdu["id"] == vdu_id)
bravof922c4172020-11-24 21:21:43 -0300130
131
132def get_vdu_index(vnfd, vdu_id):
133 target_vdu = list_utils.find_in_list(
garciadeblas5697b8b2021-03-24 09:17:02 +0100134 vnfd.get("vdu", ()), lambda vdu: vdu["id"] == vdu_id
135 )
bravof922c4172020-11-24 21:21:43 -0300136 if target_vdu:
137 return vnfd.get("vdu", ()).index(target_vdu)
138 else:
139 return -1
bravof832f8992020-12-07 12:57:31 -0300140
141
142def get_scaling_aspect(vnfd):
143 return vnfd.get("df", ())[0].get("scaling-aspect", ())
144
145
146def get_number_of_instances(vnfd, vdu_id):
147 return list_utils.find_in_list(
garciadeblas5697b8b2021-03-24 09:17:02 +0100148 vnfd.get("df", ())[0].get("instantiation-level", ())[0].get("vdu-level", ()),
149 lambda a_vdu: a_vdu["vdu-id"] == vdu_id,
aktas13251562021-02-12 22:19:10 +0300150 ).get("number-of-instances", 1)
bravof9a256db2021-02-22 18:02:07 -0300151
152
153def get_juju_ee_ref(vnfd, entity_id):
154 return list_utils.find_in_list(
garciadeblas5697b8b2021-03-24 09:17:02 +0100155 get_configuration(vnfd, entity_id).get("execution-environment-list", []),
156 lambda ee: "juju" in ee,
bravof9a256db2021-02-22 18:02:07 -0300157 )