update link to upper-constraints.txt
[optf/osdf.git] / apps / slice_selection / optimizers / conductor / remote_opt_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 slice selection request
21 """
22
23 from requests import RequestException
24 from threading import Thread
25 import traceback
26
27 from apps.slice_selection.optimizers.conductor.response_processor import ResponseProcessor
28 from osdf.adapters.conductor import conductor
29 from osdf.adapters.policy.interface import get_policies
30 from osdf.logging.osdf_logging import debug_log
31 from osdf.logging.osdf_logging import error_log
32 from osdf.utils.interfaces import get_rest_client
33 from osdf.utils.mdc_utils import mdc_from_json
34
35
36 class SliceSelectionOptimizer(Thread):
37     def __init__(self, osdf_config, slice_config, request_json, model_type):
38         super().__init__()
39         self.osdf_config = osdf_config
40         self.slice_config = slice_config
41         self.request_json = request_json
42         self.model_type = model_type
43         self.response_processor = ResponseProcessor(request_json['requestInfo'], slice_config)
44
45     def run(self):
46         self.process_slice_selection_opt()
47
48     def process_slice_selection_opt(self):
49         """Process the slice selection request from the API layer"""
50         req_info = self.request_json['requestInfo']
51         rc = get_rest_client(self.request_json, service='so')
52
53         try:
54             if self.model_type == 'NSSI' \
55                     and self.request_json['sliceProfile'].get('resourceSharingLevel', "") \
56                     in ['not-shared', 'non-shared']:
57                 final_response = self.response_processor.get_slice_selection_response([])
58
59             else:
60                 final_response = self.do_slice_selection()
61
62         except Exception as ex:
63             error_log.error("Error for {} {}".format(req_info.get('requestId'),
64                                                      traceback.format_exc()))
65             error_message = str(ex)
66             final_response = self.response_processor.process_error_response(error_message)
67
68         try:
69             rc.request(json=final_response, noresponse=True)
70         except RequestException:
71             error_log.error("Error sending asynchronous notification for {} {}".format(req_info['request_id'],
72                                                                                        traceback.format_exc()))
73
74     def do_slice_selection(self):
75         req_info = self.request_json['requestInfo']
76         app_info = self.slice_config['app_info'][self.model_type]
77         mdc_from_json(self.request_json)
78         requirements = self.request_json.get(app_info['requirements_field'], {})
79         model_info = self.request_json.get(app_info['model_info'])
80         model_name = model_info['name']
81         policies = self.get_app_policies(model_name, app_info['app_name'])
82         request_parameters = self.get_request_parameters(requirements, model_info)
83
84         demands = [
85             {
86                 "resourceModuleName": model_name,
87                 "resourceModelInfo": {}
88             }
89         ]
90
91         try:
92             template_fields = {
93                 'location_enabled': False,
94                 'version': '2020-08-13'
95             }
96             resp = conductor.request(req_info, demands, request_parameters, {}, template_fields,
97                                      self.osdf_config, policies)
98         except RequestException as e:
99             resp = e.response.json()
100             error = resp['plans'][0]['message']
101             if isinstance(error, list) and "Unable to find any" in error[0]:
102                 return self.response_processor.get_slice_selection_response([])
103             error_log.error('Error from conductor {}'.format(error))
104             return self.response_processor.process_error_response(error)
105
106         debug_log.debug("Response from conductor {}".format(str(resp)))
107         recommendations = resp["plans"][0].get("recommendations")
108         subnets = [subnet['domainType'] for subnet in self.request_json['subnetCapabilities']] \
109             if self.request_json.get('subnetCapabilities') else []
110         return self.response_processor.process_response(recommendations, model_info, subnets, self.model_type)
111
112     def get_request_parameters(self, requirements, model_info):
113         camel_to_snake = self.slice_config['attribute_mapping']['camel_to_snake']
114         request_params = {camel_to_snake[key]: value for key, value in requirements.items()}
115         subnet_capabilities = self.request_json.get('subnetCapabilities')
116         if subnet_capabilities:
117             for subnet_capability in subnet_capabilities:
118                 domain_type = f"{subnet_capability['domainType']}_"
119                 capability_details = subnet_capability['capabilityDetails']
120                 for key, value in capability_details.items():
121                     request_params[f"{domain_type}{camel_to_snake[key]}"] = value
122         request_params.update(model_info)
123         return request_params
124
125     def get_app_policies(self, model_name, app_name):
126         policy_request_json = self.request_json.copy()
127         policy_request_json['serviceInfo'] = {'serviceName': model_name}
128         if 'serviceProfile' in self.request_json:
129             slice_scope = self.request_json['serviceProfile']['resourceSharingLevel']
130             if 'preferReuse' in self.request_json and slice_scope == "shared":
131                 slice_scope = slice_scope + "," + ("reuse" if self.request_json['preferReuse'] else "create_new")
132             policy_request_json['slice_scope'] = slice_scope
133         debug_log.debug("policy_request_json {}".format(str(policy_request_json)))
134         return get_policies(policy_request_json, app_name)