Skip to content

Commit

Permalink
Refactor function to simplify the process
Browse files Browse the repository at this point in the history
Signed-off-by: EstherLerouzic <[email protected]>
Change-Id: I362d23a969c338ccd70caecc4e59e991d2a8d8a2
  • Loading branch information
EstherLerouzic committed Nov 17, 2023
1 parent db5e63d commit 79102e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
41 changes: 26 additions & 15 deletions gnpy/topology/spectrum_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,31 @@ def build_path_oms_id_list(pth):
return list(set(path_oms))


def spectrum_selection(path_oms, oms_list, requested_m, requested_n=None):
def aggregate_oms_bitmap(path_oms, oms_list):
spectrum = oms_list[path_oms[0]].spectrum_bitmap
bitmap = spectrum.bitmap
# assuming all oms have same freq indices
for oms in path_oms[1:]:
bitmap = bitmap_sum(oms_list[oms].spectrum_bitmap.bitmap, bitmap)
params = {
'oms_id': 0,
'el_id_list': 0,
'el_list': []
}
freq_min = nvalue_to_frequency(spectrum.freq_index_min)
freq_max = nvalue_to_frequency(spectrum.freq_index_max)
aggregate_oms = OMS(**params)
aggregate_oms.update_spectrum(freq_min, freq_max, grid=0.00625e12, existing_spectrum=bitmap)
return aggregate_oms


def spectrum_selection(test_oms, requested_m, requested_n=None):
"""Collects spectrum availability and call the select_candidate function"""
freq_index = test_oms.spectrum_bitmap.freq_index
freq_index_min = test_oms.spectrum_bitmap.freq_index_min
freq_index_max = test_oms.spectrum_bitmap.freq_index_max
freq_availability = test_oms.spectrum_bitmap.bitmap

# use indexes instead of ITU-T n values
# assuming all oms have same freq index
if not path_oms:
candidate = (None, None, None)
return candidate, path_oms
freq_index = oms_list[path_oms[0]].spectrum_bitmap.freq_index
freq_index_min = oms_list[path_oms[0]].spectrum_bitmap.freq_index_min
freq_index_max = oms_list[path_oms[0]].spectrum_bitmap.freq_index_max

freq_availability = oms_list[path_oms[0]].spectrum_bitmap.bitmap
for oms in path_oms[1:]:
freq_availability = bitmap_sum(oms_list[oms].spectrum_bitmap.bitmap, freq_availability)
if requested_n is None:
# avoid slots reserved on the edge 0.15e-12 on both sides -> 24
candidates = [(freq_index[i] + requested_m, freq_index[i], freq_index[i] + 2 * requested_m - 1)
Expand All @@ -346,7 +356,7 @@ def spectrum_selection(path_oms, oms_list, requested_m, requested_n=None):

candidate = select_candidate(candidates, policy='first_fit')
else:
i = oms_list[path_oms[0]].spectrum_bitmap.geti(requested_n)
i = test_oms.spectrum_bitmap.geti(requested_n)
# print(f'N {requested_n} i {i}')
# print(freq_availability[i-m:i+m] )
# print(freq_index[i-m:i+m])
Expand Down Expand Up @@ -399,7 +409,8 @@ def pth_assign_spectrum(pths, rqs, oms_list, rpths):
# use the req.M even if requested_m is smaller
requested_m = rq.M[0]
requested_n = getattr(rq, 'N', [None])[0]
center_n, _, _ = spectrum_selection(path_oms, oms_list, requested_m, requested_n)
test_oms = aggregate_oms_bitmap(path_oms, oms_list)
center_n, startn, stopn = spectrum_selection(test_oms, requested_m, requested_n)
# if requested n and m concern already occupied spectrum the previous function returns a None candidate
# if not None, center_n and start, stop frequencies are applicable to all oms of pth
# checks that spectrum is not None else indicate blocking reason
Expand Down
15 changes: 8 additions & 7 deletions tests/test_spectrum_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from gnpy.topology.request import compute_path_dsjctn, find_reversed_path, deduplicate_disjunctions, PathRequest
from gnpy.topology.spectrum_assignment import (build_oms_list, align_grids, nvalue_to_frequency,
bitmap_sum, Bitmap, spectrum_selection, pth_assign_spectrum,
build_path_oms_id_list)
build_path_oms_id_list, aggregate_oms_bitmap)
from gnpy.tools.json_io import (load_equipment, load_network, requests_from_json, disjunctions_from_json,
_check_one_request)

Expand Down Expand Up @@ -233,11 +233,11 @@ def test_spectrum_assignment_on_path(equipment, setup, requests):
req = [deepcopy(requests[1])]
paths = compute_path_dsjctn(network, equipment, req, [])
first_path_oms = build_path_oms_id_list(paths[0])

print(req)
for nval in range(100):
req = [deepcopy(requests[1])]
center_n, startn, stopn = spectrum_selection(first_path_oms, oms_list, 4)
test_oms = aggregate_oms_bitmap(first_path_oms, oms_list)
center_n, startn, stopn = spectrum_selection(test_oms, 4)
pth_assign_spectrum(paths, req, oms_list, [find_reversed_path(paths[0])])
print(f'testing on following oms {first_path_oms}')
# check that only 96 channels are feasible
Expand All @@ -253,13 +253,14 @@ def test_spectrum_assignment_on_path(equipment, setup, requests):
req = [requests[2]]
paths = compute_path_dsjctn(network, equipment, req, [])
second_path_oms = build_path_oms_id_list(paths[0])
center_n, startn, stopn = spectrum_selection(second_path_oms, oms_list, 4, 478)
test_oms = aggregate_oms_bitmap(second_path_oms, oms_list)
center_n, startn, stopn = spectrum_selection(test_oms, 4, 478)
print(oms_list[0].spectrum_bitmap.freq_index_max)
print(oms_list[0])
print(center_n, startn, stopn)
print('spectrum selection error: should be None')
assert center_n is None and startn is None and stopn is None
center_n, startn, stopn = spectrum_selection(second_path_oms, oms_list, 4, 477)
center_n, startn, stopn = spectrum_selection(test_oms, 4, 477)
print(center_n, startn, stopn)
print('spectrum selection error should not be None')
assert center_n is not None and startn is not None and stopn is not None
Expand Down Expand Up @@ -385,8 +386,8 @@ def test_reversed_direction(equipment, setup, requests, services):
number_wl = ceil(requests[i].path_bandwidth / requests[i].bit_rate)
requested_m = ceil(requests[i].spacing / slot) * number_wl
path_oms = build_path_oms_id_list(pth)
center_n, startn, stopn = spectrum_selection(path_oms, oms_list, requested_m,
requested_n=None)
test_oms = aggregate_oms_bitmap(path_oms, oms_list)
center_n, startn, stopn = spectrum_selection(test_oms, requested_m, requested_n=None)
spectrum_list.append([center_n, startn, stopn])
else:
spectrum_list.append([])
Expand Down

0 comments on commit 79102e2

Please sign in to comment.