Merge "fixing a bug for aaf authentication"
[optf/osdf.git] / osdf / optimizers / pciopt / solver / optimizer.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2018 AT&T Intellectual Property
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 itertools
20
21 import os
22 import pymzn
23
24 from osdf.logging.osdf_logging import debug_log
25 from .pci_utils import get_id
26
27 BASE_DIR = os.path.dirname(__file__)
28 MZN_FILE_NAME = os.path.join(BASE_DIR, 'no_conflicts_no_confusion.mzn')
29
30
31 def pci_optimize(cell_id, network_cell_info, cell_info_list):
32     debug_log.debug("Cell ID {} ".format(cell_id))
33     dzn_data = {}
34     dzn_data['NUM_NODES'] = len(cell_info_list)
35     dzn_data['NUM_PCIS'] = len(cell_info_list)
36
37     conflict_edges = get_conflict_edges(cell_id, network_cell_info)
38
39     dzn_data['NUM_CONFLICT_EDGES'] = len(conflict_edges)
40     dzn_data['CONFLICT_EDGES'] = conflict_edges
41
42     confusion_edges = get_confusion_edges(cell_id, network_cell_info)
43
44     dzn_data['NUM_CONFUSION_EDGES'] = len(confusion_edges)
45     dzn_data['CONFUSION_EDGES'] = confusion_edges
46
47     return solve(dzn_data)
48
49 def solve(dzn_data):
50     return pymzn.minizinc(MZN_FILE_NAME, data=dzn_data)
51
52
53 def get_conflict_edges(cell_id, network_cell_info):
54     conflict_edges = []
55     for cell in network_cell_info['cell_list']:
56
57         if cell_id == cell['cell_id']:
58             add_to_conflict_edges(network_cell_info, cell, conflict_edges)
59     return conflict_edges
60
61
62 def add_to_conflict_edges(network_cell_info, cell, conflict_edges):
63     cell_id = cell['cell_id']
64     for nbr in cell.get('nbr_list', []):
65         conflict_edges.append([get_id(network_cell_info, cell_id), get_id(network_cell_info, nbr['cellId'])])
66
67
68
69 def get_confusion_edges(cell_id, network_cell_info):
70     confusion_edges = []
71     for cell in network_cell_info['cell_list']:
72         if cell_id == cell['cell_id']:
73             return add_to_confusion_edges(network_cell_info, cell)
74     return confusion_edges
75
76
77 def add_to_confusion_edges(network_cell_info, cell):
78     cell_id = cell['cell_id']
79     nbr_list = []
80     for nbr in cell.get('nbr_list', []):
81         nbr_list.append(get_id(network_cell_info, nbr['cellId']))
82     return [list(elem) for elem in list(itertools.combinations(nbr_list, 2))]