Fix DES interface in PCI opt
[optf/osdf.git] / apps / pci / optimizers / solver / ml_model.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 import json
20
21 from osdf.adapters.dcae import des
22 from osdf.adapters.dcae.des import DESException
23 from osdf.config.base import osdf_config
24 from osdf.logging.osdf_logging import error_log
25
26
27 class MlModel(object):
28     def __init__(self):
29         self.config = osdf_config.core['PCI']
30
31     def get_additional_inputs(self, dzn_data, network_cell_info):
32         """Add/update additional info to the existing models.
33
34         The method returns nothing. Instead, it modifies the dzn_data
35         :params: dzn_data: map with data for the optimization
36         """
37         self.compute_ml_model(dzn_data, network_cell_info)
38
39     def compute_ml_model(self, dzn_data, network_cell_info):
40         average_ho_threshold = self.config['ML']['average_ho_threshold']
41         latest_ho_threshold = self.config['ML']['latest_ho_threshold']
42
43         fixed_cells = set()
44         for cell in network_cell_info['cell_list']:
45             cell_id = cell['cell_id']
46             average_ho, latest_ho = self.get_ho_details(cell['cell_id'])
47             if average_ho > average_ho_threshold or latest_ho > latest_ho_threshold:
48                 fixed_cells.add(cell_id)
49
50         fixed_cells.update(dzn_data.get('PCI_UNCHANGEABLE_CELLS', []))
51         dzn_data['PCI_UNCHANGEABLE_CELLS'] = list(fixed_cells)
52
53     def get_ho_details(self, cell_id):
54         service_id = self.config['DES']['service_id']
55         request_data = self.config['DES']['filter']
56         request_data['cell_id'] = cell_id
57         try:
58             result = des.extract_data(service_id, json.dumps(request_data))
59         except DESException as e:
60             error_log.error("Error while calling DES {}".format(e))
61             return 0, 0
62
63         if not result:
64             return 0, 0
65
66         ho_list = []
67         for pm_data in result:
68             ho = pm_data['overallHoAtt']
69             ho_list.append(ho)
70
71         return sum(ho_list) / len(ho_list), ho_list[0]