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 slice selection request
25 from requests import RequestException
27 from apps.slice_selection.optimizers.conductor.response_processor \
28 import conductor_response_processor, conductor_error_response_processor, solution_with_only_slice_profile, get_nsi_selection_response
29 from osdf.adapters.conductor import conductor
30 from osdf.adapters.policy.interface import get_policies
31 from osdf.adapters.policy.utils import group_policies_gen
32 from osdf.logging.osdf_logging import error_log, debug_log
33 from osdf.utils.mdc_utils import mdc_from_json
36 def process_nsi_selection_opt(request_json, osdf_config):
37 """Process the nsi selection request from API layer
38 :param request_json: api request
39 :param policies: flattened policies corresponding to this request
40 :param osdf_config: configuration specific to OSDF app
41 :return: response as a dictionary
43 req_info = request_json['requestInfo']
45 mdc_from_json(request_json)
47 overall_recommendations = dict()
49 new_nsi_solutions = list()
50 for nst_info in request_json["NSTInfoList"]:
51 nst_name = nst_info["modelName"]
52 nst_info_map[nst_name] = {"NSTName": nst_name,
53 "UUID": nst_info["modelVersionId"],
54 "invariantUUID": nst_info["modelInvariantId"]}
56 if request_json["serviceProfile"]["resourceSharingLevel"] == "non-shared":
57 new_nsi_solution = solution_with_only_slice_profile(request_json['serviceProfile'], nst_info_map.get(nst_name))
58 new_nsi_solutions.append(new_nsi_solution)
60 policy_request_json = request_json.copy()
61 policy_request_json['serviceInfo']['serviceName'] = nst_name
62 policies = get_policies(policy_request_json, "slice_selection")
64 demands = get_slice_demands(nst_name, policies, osdf_config.core)
66 request_parameters = request_json.get('serviceProfile',{})
68 req_info['numSolutions'] = 'all'
70 resp = conductor.request(req_info, demands, request_parameters, service_info, False,
71 osdf_config, policies)
72 except RequestException as e:
73 resp = e.response.json()
74 error = resp['plans'][0]['message']
75 error_log.error('Error from conductor {}'.format(error))
76 debug_log.debug("Response from conductor {}".format(str(resp)))
77 overall_recommendations[nst_name] = resp["plans"][0].get("recommendations")
79 if request_json["serviceProfile"]["resourceSharingLevel"] == "non-shared":
81 solutions['newNSISolutions'] = new_nsi_solutions
82 solutions['sharedNSISolutions'] = []
83 return get_nsi_selection_response(req_info, solutions)
85 return conductor_response_processor(overall_recommendations, nst_info_map, req_info, request_json["serviceProfile"])
86 except Exception as ex:
87 error_log.error("Error for {} {}".format(req_info.get('requestId'),
88 traceback.format_exc()))
89 error_message = str(ex)
90 return conductor_error_response_processor(req_info, error_message)
93 def get_slice_demands(model_name, policies, config):
95 :param model_name: model name of the slice
96 :param policies: flattened polcies corresponding to the request
97 :param config: configuration specific to OSDF app
98 :return: list of demands for the request
100 group_policies = group_policies_gen(policies, config)
101 subscriber_policy_list = group_policies["onap.policies.optimization.service.SubscriberPolicy"]
102 slice_demands = list()
103 for subscriber_policy in subscriber_policy_list:
104 policy_properties = subscriber_policy[list(subscriber_policy.keys())[0]]['properties']
105 if model_name in policy_properties["services"]:
106 for subnet in policy_properties["properties"]["subscriberName"]:
107 slice_demand = dict()
108 slice_demand["resourceModuleName"] = subnet
109 slice_demand['resourceModelInfo'] = {}
110 slice_demands.append(slice_demand)