Skip to content

Commit

Permalink
Refactor spectrum selection function
Browse files Browse the repository at this point in the history
Cut some functions into smaller pieces to be easily re-used afterwards.
This step to prepare multiple slots assignment.

Signed-off-by: EstherLerouzic <[email protected]>
Change-Id: If0fa2df7f6174e54405f92a57d60289d560c1166
  • Loading branch information
EstherLerouzic committed Nov 17, 2023
1 parent af42699 commit db5e63d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 11 additions & 8 deletions gnpy/topology/spectrum_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,17 +311,20 @@ def bitmap_sum(band1, band2):
return res


def spectrum_selection(pth, oms_list, requested_m, requested_n=None):
"""Collects spectrum availability and call the select_candidate function"""

# use indexes instead of ITU-T n values
def build_path_oms_id_list(pth):
path_oms = []
for elem in pth:
if not isinstance(elem, Roadm) and not isinstance(elem, Transceiver):
# only edfa, fused and fibers have oms_id attribute
path_oms.append(elem.oms_id)
# remove duplicate oms_id, order is not important
path_oms = list(set(path_oms))
return list(set(path_oms))


def spectrum_selection(path_oms, oms_list, requested_m, requested_n=None):
"""Collects spectrum availability and call the select_candidate function"""

# use indexes instead of ITU-T n values
# assuming all oms have same freq index
if not path_oms:
candidate = (None, None, None)
Expand Down Expand Up @@ -354,7 +357,7 @@ def spectrum_selection(pth, oms_list, requested_m, requested_n=None):
candidate = (requested_n, requested_n - requested_m, requested_n + requested_m - 1)
else:
candidate = (None, None, None)
return candidate, path_oms
return candidate


def select_candidate(candidates, policy):
Expand All @@ -381,6 +384,7 @@ def pth_assign_spectrum(pths, rqs, oms_list, rpths):
else:
nb_wl, requested_m = compute_spectrum_slot_vs_bandwidth(rq.path_bandwidth,
rq.spacing, rq.bit_rate)
path_oms = build_path_oms_id_list(pth + rpth)
if getattr(rq, 'M', [None])[0] is not None:
# Consistency check between the requested M and path_bandwidth
# M value should be bigger than the computed requested_m (simple estimate)
Expand All @@ -395,8 +399,7 @@ 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, startn, stopn), path_oms = spectrum_selection(pth + rpth, oms_list, requested_m,
requested_n)
center_n, _, _ = spectrum_selection(path_oms, oms_list, 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
18 changes: 11 additions & 7 deletions tests/test_spectrum_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from gnpy.core.exceptions import ServiceError, SpectrumError
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)
bitmap_sum, Bitmap, spectrum_selection, pth_assign_spectrum,
build_path_oms_id_list)
from gnpy.tools.json_io import (load_equipment, load_network, requests_from_json, disjunctions_from_json,
_check_one_request)

Expand Down Expand Up @@ -231,13 +232,14 @@ def test_spectrum_assignment_on_path(equipment, setup, requests):
network, oms_list = setup
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), path_oms = spectrum_selection(paths[0], oms_list, 4)
center_n, startn, stopn = spectrum_selection(first_path_oms, oms_list, 4)
pth_assign_spectrum(paths, req, oms_list, [find_reversed_path(paths[0])])
print(f'testing on following oms {path_oms}')
print(f'testing on following oms {first_path_oms}')
# check that only 96 channels are feasible
if nval >= 96:
print(center_n, startn, stopn)
Expand All @@ -250,13 +252,14 @@ def test_spectrum_assignment_on_path(equipment, setup, requests):

req = [requests[2]]
paths = compute_path_dsjctn(network, equipment, req, [])
(center_n, startn, stopn), path_oms = spectrum_selection(paths[0], oms_list, 4, 478)
second_path_oms = build_path_oms_id_list(paths[0])
center_n, startn, stopn = spectrum_selection(second_path_oms, oms_list, 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), path_oms = spectrum_selection(paths[0], oms_list, 4, 477)
center_n, startn, stopn = spectrum_selection(second_path_oms, oms_list, 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 @@ -381,8 +384,9 @@ def test_reversed_direction(equipment, setup, requests, services):
if pth:
number_wl = ceil(requests[i].path_bandwidth / requests[i].bit_rate)
requested_m = ceil(requests[i].spacing / slot) * number_wl
(center_n, startn, stopn), path_oms = spectrum_selection(pth, oms_list, requested_m,
requested_n=None)
path_oms = build_path_oms_id_list(pth)
center_n, startn, stopn = spectrum_selection(path_oms, oms_list, requested_m,
requested_n=None)
spectrum_list.append([center_n, startn, stopn])
else:
spectrum_list.append([])
Expand Down

0 comments on commit db5e63d

Please sign in to comment.