Add NSSI and slice profile in shared NSI 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
58                 nssi_info_list = get_nssi_solutions(recommendation)
59                 nssis = list()
60                 for nssi_info in nssi_info_list:
61                     nssi = dict()
62                     nssi["NSSIId"] = nssi_info.get("NSSISolution").get("NSSIId")
63                     nssi["NSSIName"] = nssi_info.get("NSSISolution").get("NSSIName")
64                     nssi["UUID"] = ""
65                     nssi["invariantUUID"] = ""
66                     nssi["sliceProfile"] = [nssi_info.get("sliceProfile")]
67                     nssis.append(nssi)
68
69                 shared_nsi_solution["NSSIs"] = nssis
70                 shared_nsi_solutions.append(shared_nsi_solution)
71             else:
72                 nssi_solutions = get_nssi_solutions(recommendation)
73                 new_nsi_solution = dict()
74                 new_nsi_solution['matchLevel'] = ""
75                 new_nsi_solution['NSTInfo'] = nst_info_map.get(nst_name)
76                 new_nsi_solution['NSSISolutions'] = nssi_solutions
77                 new_nsi_solutions.append(new_nsi_solution)
78
79     solutions = dict()
80     solutions['sharedNSISolutions'] = shared_nsi_solutions
81     solutions['newNSISolutions'] = new_nsi_solutions
82     return get_nsi_selection_response(request_info, solutions)
83
84
85 def conductor_error_response_processor(request_info, error_message):
86     """Form response message from the error message
87         :param request_info: request info
88         :param error_message: error message while processing the request
89         :return: response json as dictionary
90     """
91     return {'requestId': request_info['requestId'],
92             'transactionId': request_info['transactionId'],
93             'requestStatus': 'error',
94             'statusMessage': error_message}
95
96
97 def get_nssi_solutions(recommendation):
98     """Get nssi solutions from recommendation
99         :param recommendation: recommendation from conductor
100         :return: new nssi solutions list
101     """
102     nssi_solutions = list()
103
104     for nsst_name, nsst_rec in recommendation.items():
105         candidate = nsst_rec['candidate']
106         nssi_info, slice_profile = get_solution_from_candidate(candidate)
107         nsst_info = {"NSSTName": nsst_name}
108         nssi_solution = {"sliceProfile": slice_profile,
109                          "NSSTInfo": nsst_info,
110                          "NSSISolution": nssi_info}
111         nssi_solutions.append(nssi_solution)
112     return nssi_solutions
113
114
115 def get_solution_from_candidate(candidate):
116     """Get nssi info from candidate
117         :param candidate: Candidate from the recommendation
118         :return: nssi_info and slice profile derived from candidate
119     """
120     slice_profile = dict()
121     nssi_info = {"NSSIName": candidate['instance_name'],
122                  "NSSIId": candidate['candidate_id']}
123
124     for field in SLICE_PROFILE_FIELDS:
125         if candidate[field]:
126             slice_profile[field] = candidate[field]
127
128     return nssi_info, slice_profile
129
130
131 def get_nsi_selection_response(request_info, solutions):
132     """Get NSI selection response from final solution
133         :param request_info: request info
134         :param solutions: final solutions
135         :return: NSI selection response to send back as dictionary
136     """
137     return {'requestId': request_info['requestId'],
138             'transactionId': request_info['transactionId'],
139             'requestStatus': 'completed',
140             'statusMessage': '',
141             'solutions': solutions}