db10ee5a8830cee326a274a5f1609d47d2d0e6af
[optf/has.git] / conductor / conductor / data / plugins / inventory_provider / candidates / slice_profiles_candidate.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (C) 2020 Wipro Limited.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 import copy
21
22 from conductor.data.plugins.inventory_provider.candidates.candidate import Candidate
23
24
25 def copy_first(x):
26     return list(filter(None, x))[0]
27
28
29 ATTRIBUTE_AGGREGATION = {
30     "max_bandwidth": copy_first,
31     "jitter": sum,
32     "sst": copy_first,
33     "latency": sum,
34     "resource_sharing_level": copy_first,
35     "s_nssai": copy_first,
36     "plmn_id_list": copy_first,
37     "availability": copy_first,
38     "throughput": min,
39     "reliability": copy_first,
40     "max_number_of_ues": copy_first,
41     "exp_data_rate_ul": min,
42     "exp_data_rate_dl": min,
43     "ue_mobility_level": copy_first,
44     "coverage_area_ta_list": copy_first,
45     "activity_factor": copy_first,
46     "survival_time": copy_first,
47     "max_number_of_conns": copy_first
48 }
49
50
51 class SliceProfilesCandidate(Candidate):
52
53     def __init__(self, **kwargs):
54         super().__init__(kwargs["info"])
55         self.subnet_requirements = kwargs["subnet_requirements"]
56         if "slice_requirements" in kwargs:
57             self.slice_requirements = kwargs["slice_requirements"]
58         else:
59             self.slice_requirements = None
60         self.other = kwargs["default_fields"]
61
62     def convert_nested_dict_to_dict(self):
63         nested_dict = self.__dict__
64
65         if not self.slice_requirements:
66             self.slice_requirements = get_slice_requirements(self.subnet_requirements)
67
68         slice_profile_candidate = copy.deepcopy(nested_dict)
69         slice_profile_candidate.pop("slice_requirements")
70         slice_profile_candidate.pop("subnet_requirements")
71         slice_profile_candidate.pop("other")
72         slice_profile_candidate.update(self.slice_requirements)
73         for subnet, slice_profile in self.subnet_requirements.items():
74             subnet_req = {f'{subnet}_{key}': value for key, value in slice_profile.items()}
75             slice_profile_candidate.update(subnet_req)
76
77         slice_profile_candidate.update(self.other)
78
79         return slice_profile_candidate
80
81
82 def get_slice_requirements(subnet_requirements):
83     slice_requirements_keys = set()
84     for slice_profile in subnet_requirements.values():
85         slice_requirements_keys.update(slice_profile.keys())
86
87     slice_profile_tuples = {}
88     for key in slice_requirements_keys:
89         attributes = []
90         for slice_profile in subnet_requirements.values():
91             attributes.append(slice_profile.get(key))
92         slice_profile_tuples[key] = attributes
93
94     return {attr: ATTRIBUTE_AGGREGATION[attr](values) for attr, values in slice_profile_tuples.items()}