Add support to process NSI selection request
[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         self.osdf_config = osdf_config
39         self.slice_config = slice_config
40         self.request_json = request_json
41         self.model_type = model_type
42         self.response_processor = ResponseProcessor(request_json['requestInfo'], slice_config)
43
44     def run(self):
45         self.process_slice_selection_opt()
46
47     def process_slice_selection_opt(self):
48         """Process the slice selection request from the API layer"""
49         req_info = self.request_json['requestInfo']
50         rc = get_rest_client(self.request_json, service='so')
51
52         try:
53             if self.model_type == 'NSSI' \
54                     and self.request_json['sliceProfile'].get('resourceSharingLevel', "") == 'not-shared':
55                 final_response = self.response_processor.get_slice_selection_response([])
56
57             else:
58                 final_response = self.do_slice_selection()
59
60         except Exception as ex:
61             error_log.error("Error for {} {}".format(req_info.get('requestId'),
62                                                      traceback.format_exc()))
63             error_message = str(ex)
64             final_response = self.response_processor.process_error_response(error_message)
65
66         try:
67             rc.request(json=final_response, noresponse=True)
68         except RequestException:
69             error_log.error("Error sending asynchronous notification for {} {}".format(req_info['request_id'],
70                                                                                        traceback.format_exc()))
71
72     def do_slice_selection(self):
73         req_info = self.request_json['requestInfo']
74         app_info = self.slice_config['app_info'][self.model_type]
75         mdc_from_json(self.request_json)
76         requirements = self.request_json.get(app_info['requirements_field'], {})
77         model_info = self.request_json.get(app_info['model_info'])
78         model_name = model_info['name']
79         policies = self.get_app_policies(model_name, app_info['app_name'])
80         request_parameters = self.get_request_parameters(requirements)
81
82         demands = [
83             {
84                 "resourceModuleName": model_name,
85                 "resourceModelInfo": {}
86             }
87         ]
88
89         try:
90             template_fields = {
91                 'location_enabled': False,
92                 'version': '2020-08-13'
93             }
94             resp = conductor.request(req_info, demands, request_parameters, {}, template_fields,
95                                      self.osdf_config, policies)
96         except RequestException as e:
97             resp = e.response.json()
98             error = resp['plans'][0]['message']
99             error_log.error('Error from conductor {}'.format(error))
100             return self.response_processor.process_error_response(error)
101
102         debug_log.debug("Response from conductor {}".format(str(resp)))
103         recommendations = resp["plans"][0].get("recommendations")
104         subnets = [subnet['domainType'] for subnet in self.request_json['subnetCapabilities']] \
105             if self.request_json.get('subnetCapabilities') else []
106         return self.response_processor.process_response(recommendations, model_info, subnets)
107
108     def get_request_parameters(self, requirements):
109         camel_to_snake = self.slice_config['attribute_mapping']['camel_to_snake']
110         request_params = {camel_to_snake[key]: value for key, value in requirements.items()}
111         subnet_capabilities = self.request_json.get('subnetCapabilities')
112         if subnet_capabilities:
113             for subnet_capability in subnet_capabilities:
114                 domain_type = f"{subnet_capability['domainType'].lower().replace('-', '_')}_"
115                 capability_details = subnet_capability['capabilityDetails']
116                 for key, value in capability_details.items():
117                     request_params[f"{domain_type}{camel_to_snake[key]}"] = value
118         return request_params
119
120     def get_app_policies(self, model_name, app_name):
121         policy_request_json = self.request_json.copy()
122         policy_request_json['serviceInfo'] = {'serviceName': model_name}
123         if 'preferReuse' in self.request_json:
124             policy_request_json['preferReuse'] = "reuse" if self.request_json['preferReuse'] else "create_new"
125         return get_policies(policy_request_json, app_name)