diff --git a/src/cnaas_nms/devicehandler/underlay.py b/src/cnaas_nms/devicehandler/underlay.py index f3a116f5..da9145e4 100644 --- a/src/cnaas_nms/devicehandler/underlay.py +++ b/src/cnaas_nms/devicehandler/underlay.py @@ -52,9 +52,10 @@ def find_free_mgmt_lo_ip(session) -> Optional[IPv4Address]: return None -def find_free_infra_linknet(session) -> Optional[IPv4Network]: +def find_free_infra_linknet(session, offset: int = 0) -> Optional[IPv4Network]: """Returns first free IPv4 infra linknet (/31).""" used_linknets = [] + current_offset = offset linknet_query = session.query(Linknet).filter(Linknet.device_a_ip.isnot(None)) ln: Linknet for ln in linknet_query: @@ -65,6 +66,9 @@ def find_free_infra_linknet(session) -> Optional[IPv4Network]: for num, net in enumerate(infra_ip_net.subnets(new_prefix=31)): if net in used_linknets: continue + elif current_offset > 0: + current_offset -= 1 + continue else: return net return None diff --git a/src/cnaas_nms/devicehandler/update.py b/src/cnaas_nms/devicehandler/update.py index 109e4b03..f227d83d 100644 --- a/src/cnaas_nms/devicehandler/update.py +++ b/src/cnaas_nms/devicehandler/update.py @@ -251,7 +251,7 @@ def update_linknets( ) ) - for local_if, data in neighbors.items(): + for idx, (local_if, data) in enumerate(neighbors.items()): logger.debug(f"Local: {local_if}, remote: {data[0]['hostname']} {data[0]['port']}") remote_device_inst: Device = session.query(Device).filter(Device.hostname == data[0]["hostname"]).one_or_none() if not remote_device_inst: @@ -339,7 +339,11 @@ def update_linknets( DeviceType.CORE, DeviceType.DIST, ]: - ipv4_network = find_free_infra_linknet(session) + if dry_run: + # dry_run requires linknet offset since linknet IPs are not added to database + ipv4_network = find_free_infra_linknet(session, idx) + else: + ipv4_network = find_free_infra_linknet(session) else: ipv4_network = None new_link = Linknet.create_linknet(