From b7d14200209f3a1679c6faeb55195bc5872a67a3 Mon Sep 17 00:00:00 2001 From: cjsoftuk Date: Mon, 21 Jun 2021 08:51:49 +0100 Subject: [PATCH] Firebrick: Fix an issue which led to duplication of BGP peers. (#12932) * Firebrick: Fix an issue which led to duplication of BGP peers. * Code style. * Attempt to get some sanity with BGP Table 0. * Attempt to make it so peers are not immediately added then deleted. * Force vrfId to Null if empty. * Another attempt at keeping the DB clean. * Code style. * Stop the dicovery deleting peers it just created. --- .../discovery/bgp-peers/firebrick.inc.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/discovery/bgp-peers/firebrick.inc.php b/includes/discovery/bgp-peers/firebrick.inc.php index e74d649008..89043c04f0 100644 --- a/includes/discovery/bgp-peers/firebrick.inc.php +++ b/includes/discovery/bgp-peers/firebrick.inc.php @@ -33,8 +33,7 @@ foreach ($bgpPeersCache as $key => $value) { if (strlen($address) > 15) { $address = IP::fromHexString($address)->compressed(); } - - if (isset($value['fbBgpPeerTableId'])) { + if (isset($value['fbBgpPeerTableId']) && $value['fbBgpPeerTableId'] !== '') { $bgpPeers[$value['fbBgpPeerTableId']][$address] = $value; } else { $bgpPeers[0][$address] = $value; @@ -45,6 +44,8 @@ unset($bgpPeersCache); foreach ($bgpPeers as $vrfId => $vrf) { if (empty($vrfId)) { $checkVrf = ' AND `vrf_id` IS NULL '; + // Force to null to avoid 0s going to the DB instead of Nulls + $vrfId = null; } else { $checkVrf = ' AND vrf_id = ? '; $vrfs = [ @@ -93,19 +94,25 @@ foreach ($bgpPeers as $vrfId => $vrf) { $peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]); foreach ($peers as $value) { $vrfId = $value['vrf_id']; - if ($vrfId === null) { - $vrfId = 0; - } $address = $value['bgpPeerIdentifier']; - if (empty($bgpPeers[$vrfId][$address])) { + // Cleanup code to deal with 0 vs Null in the DB + if ($vrfId === 0) { + // Database says it's table 0 - which is wrong. It should be "null" for global table + $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]); + echo str_repeat('-', $deleted); + continue; + } else { + $testVrfId = empty($vrfId) ? 0 : $vrfId; + } + + if (empty($bgpPeers[$testVrfId][$address])) { if ($vrfId === null) { $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id IS NULL', [$device['device_id'], $address]); } else { $deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]); } echo str_repeat('-', $deleted); - echo PHP_EOL; } }