1 # -------------------------------------------------------------------------
2 # Copyright (C) 2020 Wipro Limited.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # -------------------------------------------------------------------------
20 Module for processing response from conductor for slice selection
26 class ResponseProcessor(object):
27 def __init__(self, request_info, slice_config):
28 self.request_info = request_info
29 self.slice_config = slice_config
31 def process_response(self, recommendations, model_info, subnets):
32 """Process conductor response to form the response for the API request
34 :param recommendations: recommendations from conductor
35 :param model_info: model info from the request
36 :param subnets: list of subnets
37 :return: response json as a dictionary
39 if not recommendations:
40 return self.get_slice_selection_response([])
41 model_name = model_info['name']
42 solutions = [self.get_solution_from_candidate(rec[model_name]['candidate'], model_info, subnets)
43 for rec in recommendations]
44 return self.get_slice_selection_response(solutions)
46 def get_solution_from_candidate(self, candidate, model_info, subnets):
47 if candidate['inventory_type'] == 'nssi':
49 'UUID': model_info['UUID'],
50 'invariantUUID': model_info['invariantUUID'],
51 'NSSIName': candidate['instance_name'],
52 'NSSIId': candidate['instance_id']
55 elif candidate['inventory_type'] == 'nsi':
58 'sharedNSISolution': {
59 'UUID': model_info['UUID'],
60 'invariantUUID': model_info['invariantUUID'],
61 'NSIName': candidate['instance_name'],
62 'NSIId': candidate['instance_id']
66 elif candidate['inventory_type'] == 'slice_profiles':
70 'slice_profiles': self.get_slice_profiles_from_candidate(candidate, subnets)
74 def get_slice_profiles_from_candidate(self, candidate, subnets):
76 for subnet in subnets:
77 slice_profile = {self.get_profile_attribute(k, subnet): v for k, v in candidate.items()
78 if k.startswith(subnet)}
79 slice_profile['domainType'] = subnet
80 slice_profiles.append(slice_profile)
83 def get_profile_attribute(self, attribute, subnet):
84 snake_to_camel = self.slice_config['attribute_mapping']['snake_to_camel']
85 return snake_to_camel[re.sub(f'^{subnet}_', '', attribute)]
87 def process_error_response(self, error_message):
88 """Form response message from the error message
90 :param error_message: error message while processing the request
91 :return: response json as dictionary
93 return {'requestId': self.request_info['requestId'],
94 'transactionId': self.request_info['transactionId'],
95 'requestStatus': 'error',
96 'statusMessage': error_message}
98 def get_slice_selection_response(self, solutions):
99 """Get NSI selection response from final solution
101 :param solutions: final solutions
102 :return: NSI selection response to send back as dictionary
104 return {'requestId': self.request_info['requestId'],
105 'transactionId': self.request_info['transactionId'],
106 'requestStatus': 'completed',
108 'solutions': solutions}