Add nsi info in the nsi selection response
[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_id'] for key, values in recommendation.items())
47             if len(nsi_set) == 1:
48                 nsi_id = nsi_set.pop()
49                 candidate = list(recommendation.values())[0]['candidate']
50                 debug_log.debug("The NSSIs in the solution belongs to the same NSI {}"
51                                 .format(nsi_id))
52                 shared_nsi_solution = dict()
53                 shared_nsi_solution["NSIId"] = nsi_id
54                 shared_nsi_solution["NSIName"] = candidate.get('nsi_name')
55                 shared_nsi_solution["UUID"] = candidate.get('nsi_model_version_id')
56                 shared_nsi_solution["invariantUUID"] = candidate.get('nsi_model_invariant_id')
57                 shared_nsi_solutions.append(shared_nsi_solution)
58             else:
59                 nssi_solutions = get_nssi_solutions(recommendation)
60                 new_nsi_solution = dict()
61                 new_nsi_solution['matchLevel'] = ""
62                 new_nsi_solution['NSTInfo'] = nst_info_map.get(nst_name)
63                 new_nsi_solution['NSSISolutions'] = nssi_solutions
64                 new_nsi_solutions.append(new_nsi_solution)
65
66     solutions = dict()
67     solutions['sharedNSISolutions'] = shared_nsi_solutions
68     solutions['newNSISolutions'] = new_nsi_solutions
69     return get_nsi_selection_response(request_info, solutions)
70
71
72 def conductor_error_response_processor(request_info, error_message):
73     """Form response message from the error message
74         :param request_info: request info
75         :param error_message: error message while processing the request
76         :return: response json as dictionary
77     """
78     return {'requestId': request_info['requestId'],
79             'transactionId': request_info['transactionId'],
80             'requestStatus': 'error',
81             'statusMessage': error_message}
82
83
84 def get_nssi_solutions(recommendation):
85     """Get nssi solutions from recommendation
86         :param recommendation: recommendation from conductor
87         :return: new nssi solutions list
88     """
89     nssi_solutions = list()
90
91     for nsst_name, nsst_rec in recommendation.items():
92         candidate = nsst_rec['candidate']
93         nssi_info, slice_profile = get_solution_from_candidate(candidate)
94         nsst_info = {"NSSTName": nsst_name}
95         nssi_solution = {"sliceProfile": slice_profile,
96                          "NSSTInfo": nsst_info,
97                          "NSSISolution": nssi_info}
98         nssi_solutions.append(nssi_solution)
99     return nssi_solutions
100
101
102 def get_solution_from_candidate(candidate):
103     """Get nssi info from candidate
104         :param candidate: Candidate from the recommendation
105         :return: nssi_info and slice profile derived from candidate
106     """
107     slice_profile = dict()
108     nssi_info = {"NSSIName": candidate['instance_name'],
109                  "NSSIId": candidate['candidate_id']}
110
111     for field in SLICE_PROFILE_FIELDS:
112         if candidate[field]:
113             slice_profile[field] = candidate[field]
114
115     return nssi_info, slice_profile
116
117
118 def get_nsi_selection_response(request_info, solutions):
119     """Get NSI selection response from final solution
120         :param request_info: request info
121         :param solutions: final solutions
122         :return: NSI selection response to send back as dictionary
123     """
124     return {'requestId': request_info['requestId'],
125             'transactionId': request_info['transactionId'],
126             'requestStatus': 'completed',
127             'statusMessage': '',
128             'solutions': solutions}