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