5b7be01e2f87a8684db3d3d04c20fddd2cdff116
[optf/osdf.git] / apps / slice_selection / optimizers / conductor / response_processor.py
1 # -------------------------------------------------------------------------
2 #   Copyright (C) 2020 Wipro Limited.
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 """
20 Module for processing response from conductor for slice selection
21 """
22
23 from osdf.logging.osdf_logging import debug_log
24
25
26 SLICE_PROFILE_FIELDS = ["latency", "max_number_of_ues", "coverage_area_ta_list",
27                         "ue_mobility_level", "resource_sharing_level", "exp_data_rate_ul",
28                         "exp_data_rate_dl", "area_traffic_cap_ul", "area_traffic_cap_dl",
29                         "activity_factor", "e2e_latency", "jitter", "survival_time",
30                         "exp_data_rate", "payload_size", "traffic_density", "conn_density",
31                         "reliability", "service_area_dimension", "cs_availability"]
32
33
34 def conductor_response_processor(overall_recommendations, nst_info_map, request_info):
35     """Process conductor response to form the response for the API request
36         :param overall_recommendations: recommendations from conductor
37         :param nst_info_map: NST info from the request
38         :param request_info: request info
39         :return: response json as a dictionary
40     """
41     shared_nsi_solutions = list()
42     new_nsi_solutions = list()
43
44     for nst_name, recommendations in overall_recommendations.items():
45         for recommendation in recommendations:
46             nsi_set = set(values['candidate']['nsi_name'] for key, values in recommendation.items())
47             if len(nsi_set) == 1:
48                 nsi = nsi_set.pop()
49                 debug_log.debug("The NSSIs in the solution belongs to the same NSI {}".format(nsi))
50                 shared_nsi_solution = dict()
51                 shared_nsi_solution["NSIName"] = nsi
52                 shared_nsi_solutions.append(shared_nsi_solution)
53             else:
54                 nssi_solutions = get_nssi_solutions(recommendation)
55                 new_nsi_solution = dict()
56                 new_nsi_solution['matchLevel'] = ""
57                 new_nsi_solution['NSTInfo'] = nst_info_map.get(nst_name)
58                 new_nsi_solution['NSSISolutions'] = nssi_solutions
59                 new_nsi_solutions.append(new_nsi_solution)
60
61     solutions = dict()
62     solutions['sharedNSISolutions'] = shared_nsi_solutions
63     solutions['newNSISolutions'] = new_nsi_solutions
64     return get_nsi_selection_response(request_info, solutions)
65
66
67 def conductor_error_response_processor(request_info, error_message):
68     """Form response message from the error message
69         :param request_info: request info
70         :param error_message: error message while processing the request
71         :return: response json as dictionary
72     """
73     return {'requestId': request_info['requestId'],
74             'transactionId': request_info['transactionId'],
75             'requestStatus': 'error',
76             'statusMessage': error_message}
77
78
79 def get_nssi_solutions(recommendation):
80     """Get nssi solutions from recommendation
81         :param recommendation: recommendation from conductor
82         :return: new nssi solutions list
83     """
84     nssi_solutions = list()
85
86     for nsst_name, nsst_rec in recommendation.items():
87         candidate = nsst_rec['candidate']
88         nssi_info, slice_profile = get_solution_from_candidate(candidate)
89         nsst_info = {"NSSTName": nsst_name}
90         nssi_solution = {"sliceProfile": slice_profile,
91                          "NSSTInfo": nsst_info,
92                          "NSSISolution": nssi_info}
93         nssi_solutions.append(nssi_solution)
94     return nssi_solutions
95
96
97 def get_solution_from_candidate(candidate):
98     """Get nssi info from candidate
99         :param candidate: Candidate from the recommendation
100         :return: nssi_info and slice profile derived from candidate
101     """
102     slice_profile = dict()
103     nssi_info = {"NSSIName": candidate['instance_name'],
104                  "NSSIId": candidate['candidate_id']}
105
106     for field in SLICE_PROFILE_FIELDS:
107         if candidate[field]:
108             slice_profile[field] = candidate[field]
109
110     return nssi_info, slice_profile
111
112
113 def get_nsi_selection_response(request_info, solutions):
114     """Get NSI selection response from final solution
115         :param request_info: request info
116         :param solutions: final solutions
117         :return: NSI selection response to send back as dictionary
118     """
119     return {'requestId': request_info['requestId'],
120             'transactionId': request_info['transactionId'],
121             'requestStatus': 'completed',
122             'statusMessage': '',
123             'solutions': solutions}