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, model_type):
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 :param model_type: NSI or NSSI
38 :return: response json as a dictionary
40 if not recommendations:
41 return self.get_slice_selection_response([])
42 model_name = model_info['name']
43 solutions = [self.get_solution_from_candidate(rec[model_name]['candidate'], model_info, subnets, model_type)
44 for rec in recommendations]
45 return self.get_slice_selection_response(solutions)
47 def get_solution_from_candidate(self, candidate, model_info, subnets, model_type):
48 if candidate['inventory_type'] == 'slice_profiles':
52 'sliceProfiles': self.get_slice_profiles_from_candidate(candidate, subnets)
55 elif model_type == 'NSSI':
57 'UUID': model_info['UUID'],
58 'invariantUUID': model_info['invariantUUID'],
59 'NSSIName': candidate['instance_name'],
60 'NSSIId': candidate['instance_id']
63 elif model_type == 'NSI':
66 'sharedNSISolution': {
67 'UUID': model_info['UUID'],
68 'invariantUUID': model_info['invariantUUID'],
69 'NSIName': candidate['instance_name'],
70 'NSIId': candidate['instance_id']
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}