From 1de62619c62fd229c0150929d3763e8bb32e14a1 Mon Sep 17 00:00:00 2001 From: Quantum Date: Tue, 29 Oct 2024 01:26:26 -0400 Subject: [PATCH] Add version of ASPA validation restricted to bird DSL powers --- aspa/test.py | 14 +++++++++++++- aspa/validate.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/aspa/test.py b/aspa/test.py index 253be75..4b2fc98 100644 --- a/aspa/test.py +++ b/aspa/test.py @@ -2,7 +2,7 @@ import unittest from pathlib import Path from aspa.data import ASPA, parse_json -from aspa.validate import Validator +from aspa.validate import BirdValidator, Validator class ParserTest(unittest.TestCase): @@ -158,5 +158,17 @@ class UpstreamTest(unittest.TestCase): self.assertTrue(self.validator.is_aspa_invalid_upstream(64531, [64521, 64521, 64514, 64520, 64530])) +class BirdCustomerTest(CustomerTest): + validator_class = BirdValidator + + +class BirdPeerTest(PeerTest): + validator_class = BirdValidator + + +class BirdUpstreamTest(UpstreamTest): + validator_class = BirdValidator + + if __name__ == '__main__': unittest.main() diff --git a/aspa/validate.py b/aspa/validate.py index a6cc115..956b82b 100644 --- a/aspa/validate.py +++ b/aspa/validate.py @@ -51,3 +51,46 @@ class Validator: min_down_ramp = max(min_down_ramp, i) return min_down_ramp > max_up_ramp + + +class BirdValidator(Validator): + """Validator coded with only language features available in bird DSL.""" + + def is_aspa_invalid_customer(self, my_asn: int, bgp_path: list[int]) -> bool: + prev_asn = my_asn + + for asn in bgp_path: + if prev_asn != asn and self.is_invalid_pair(prev_asn, asn): + return True + prev_asn = asn + + return False + + def is_aspa_invalid_peer(self, peer_asn: int, bgp_path: list[int]) -> bool: + prev_asn = peer_asn + + for asn in bgp_path: + if asn != peer_asn and prev_asn != asn and self.is_invalid_pair(prev_asn, asn): + return True + prev_asn = asn + + return False + + def is_aspa_invalid_upstream(self, my_asn: int, bgp_path: list[int]) -> bool: + prev_asn = my_asn + max_up_ramp = len(bgp_path) + min_down_ramp = 0 + i = 0 + + for asn in bgp_path: + if prev_asn != asn: + if self.is_invalid_pair(asn, prev_asn) and max_up_ramp > i: + max_up_ramp = i + + if self.is_invalid_pair(prev_asn, asn) and min_down_ramp < i: + min_down_ramp = i + + prev_asn = asn + i += 1 + + return min_down_ramp > max_up_ramp