Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E test for external BGP announcements #7231

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public boolean isTerminated() {
public static final String RELPATH_CONFIGURATIONS_DIR = "configs";
public static final String RELPATH_EDGE_BLACKLIST_FILE = "edge_blacklist";
public static final String RELPATH_ENVIRONMENT_BGP_TABLES = "bgp";
public static final String RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS = "external_bgp_announcements.json";
public static final String RELPATH_HOST_CONFIGS_DIR = "hosts";
public static final String RELPATH_INPUT = "input";
public static final String RELPATH_INTERFACE_BLACKLIST_FILE = "interface_blacklist";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ public class FileBasedStorage implements StorageProvider {
private static final String RELPATH_DATA_PLANE = "dp";
private static final String RELPATH_SERIALIZED_ENVIRONMENT_BGP_TABLES = "bgp_processed";
private static final String RELPATH_ENVIRONMENT_BGP_TABLES_ANSWER = "bgp_answer";
private static final String RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS =
"external_bgp_announcements.json";
private static final String RELPATH_PARSE_ANSWER_PATH = "parse_answer";
private static final String RELPATH_VENDOR_SPECIFIC_CONFIG_DIR = "vendor";
private static final String RELPATH_AWS_ACCOUNTS_DIR = "accounts";
Expand Down Expand Up @@ -1583,7 +1581,9 @@ public Optional<String> loadExternalBgpAnnouncementsFile(NetworkSnapshot snapsho
throws IOException {
Path path =
getSnapshotInputObjectPath(
snapshot.getNetwork(), snapshot.getSnapshot(), RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS);
snapshot.getNetwork(),
snapshot.getSnapshot(),
BfConsts.RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS);
if (!Files.exists(path)) {
return Optional.empty();
}
Expand Down
64 changes: 64 additions & 0 deletions projects/batfish/src/test/java/org/batfish/main/BatfishTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
import org.batfish.common.topology.Layer1Topology;
import org.batfish.common.util.isp.IspModelingUtils.ModeledNodes;
import org.batfish.datamodel.AsPath;
import org.batfish.datamodel.AsSet;
import org.batfish.datamodel.BgpAdvertisement;
import org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType;
import org.batfish.datamodel.Bgpv4Route;
import org.batfish.datamodel.Configuration;
import org.batfish.datamodel.ConfigurationFormat;
import org.batfish.datamodel.Edge;
Expand All @@ -75,6 +77,7 @@
import org.batfish.datamodel.answers.Answer;
import org.batfish.datamodel.answers.AnswerElement;
import org.batfish.datamodel.answers.AnswerStatus;
import org.batfish.datamodel.bgp.community.StandardCommunity;
import org.batfish.datamodel.collections.BgpAdvertisementsByVrf;
import org.batfish.datamodel.questions.Question;
import org.batfish.datamodel.questions.TestQuestion;
Expand Down Expand Up @@ -294,6 +297,67 @@ public void testInitSnapshotWithEnvironmentBgpTables() throws IOException {
.build())))));
}

@Test
public void testInitSnapshotWithExternalBgpAnnouncements() throws IOException {
String snapshotResourcePrefix = "org/batfish/main/snapshots/external_bgp_announcements";
Batfish batfish =
BatfishTestUtils.getBatfishFromTestrigText(
TestrigText.builder()
.setExternalBgpAnnouncements(snapshotResourcePrefix)
.setConfigurationFiles(snapshotResourcePrefix, "as1border2.cfg")
.build(),
_folder);
batfish.computeDataPlane(batfish.getSnapshot());

// is the data read as expected?
Set<BgpAdvertisement> externalBgpAnnouncements =
batfish.loadExternalBgpAnnouncements(
batfish.getSnapshot(), batfish.loadConfigurations(batfish.getSnapshot()));
assertThat(
externalBgpAnnouncements,
equalTo(
ImmutableSet.of(
BgpAdvertisement.builder()
.setType(BgpAdvertisementType.EBGP_SENT)
.setNetwork(Prefix.strict("4.0.0.0/8"))
.setNextHopIp(Ip.parse("1.1.1.1"))
.setSrcIp(Ip.parse("10.14.22.4"))
.setDstNode("as1border2")
.setDstIp(Ip.parse("10.14.22.1"))
.setSrcProtocol(RoutingProtocol.AGGREGATE)
.setOriginType(OriginType.EGP)
.setLocalPreference(0L)
.setMed(20L)
.setOriginatorIp(Ip.ZERO)
.setAsPath(AsPath.of(AsSet.of(1239)))
.setCommunities(ImmutableSortedSet.of(StandardCommunity.parse("262145")))
.setSrcVrf("default")
.setDstVrf("default")
.setClusterList(ImmutableSortedSet.of())
.build())));

// is the data processed (including import policies) as expected?
assertThat(
batfish.loadDataPlane(batfish.getSnapshot()).getBgpRoutes().get("as1border2", "default"),
equalTo(
ImmutableSet.of(
Bgpv4Route.builder()
.setProtocol(RoutingProtocol.BGP)
.setNetwork(Prefix.strict("4.0.0.0/8"))
.setNextHopIp(Ip.parse("10.14.22.4")) // policy has next-hop peer-address
.setReceivedFromIp(Ip.parse("10.14.22.4"))
.setSrcProtocol(RoutingProtocol.BGP)
.setOriginType(OriginType.EGP)
.setLocalPreference(350L) // the value specified in the import policy
.setMetric(20L)
.setOriginatorIp(Ip.ZERO)
.setAsPath(AsPath.of(AsSet.of(1239)))
.setCommunities(ImmutableSortedSet.of(StandardCommunity.parse("262145")))
.setClusterList(ImmutableSortedSet.of())
.setAdmin(20)
.build())));
}

@Test
public void testLoadLayer1Topology() throws IOException {
TestrigText.Builder testrigTextBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static Batfish getBatfishFromTestrigText(
Map<String, byte[]> awsBytes = testrigText.getAwsBytes();
Map<String, byte[]> bgpTablesBytes = testrigText.getBgpTablesBytes();
Map<String, byte[]> configurationBytes = testrigText.getConfigurationBytes();
byte[] externalBgpAnnouncementsBytes = testrigText.getExternalBgpAnnouncementBytes();
Map<String, byte[]> hostsBytes = testrigText.getHostsBytes();
Map<String, byte[]> iptablesFilesBytes = testrigText.getIptablesFilesBytes();
byte[] layer1TopologyBytes = testrigText.getLayer1TopologyBytes();
Expand All @@ -196,6 +197,14 @@ public static Batfish getBatfishFromTestrigText(
writeTemporarySnapshotInputFiles(awsBytes, RELPATH_AWS_CONFIGS_DIR, storage, TEST_SNAPSHOT);
writeTemporarySnapshotInputFiles(
bgpTablesBytes, RELPATH_ENVIRONMENT_BGP_TABLES, storage, TEST_SNAPSHOT);
if (externalBgpAnnouncementsBytes != null) {
writeTemporarySnapshotInputFiles(
ImmutableMap.of(
BfConsts.RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS, externalBgpAnnouncementsBytes),
"",
storage,
TEST_SNAPSHOT);
}
writeTemporarySnapshotInputFiles(hostsBytes, RELPATH_HOST_CONFIGS_DIR, storage, TEST_SNAPSHOT);
writeTemporarySnapshotInputFiles(iptablesFilesBytes, "iptables", storage, TEST_SNAPSHOT);
if (layer1TopologyBytes != null) {
Expand Down
23 changes: 23 additions & 0 deletions projects/batfish/src/test/java/org/batfish/main/TestrigText.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private static Map<String, byte[]> readTestrigResources(
private Map<String, byte[]> _awsBytes;
private Map<String, byte[]> _bgpTablesBytes;
private Map<String, byte[]> _configurationBytes;
private byte[] _externalBgpAnnouncementsBytes;
private Map<String, byte[]> _hostsBytes;
private Map<String, byte[]> _iptablesFilesBytes;
private byte[] _layer1TopologyBytes;
Expand All @@ -58,6 +59,7 @@ public TestrigText build() {
testrigText.setAwsBytes(_awsBytes);
testrigText.setBgpTablesBytes(_bgpTablesBytes);
testrigText.setConfigurationBytes(_configurationBytes);
testrigText.setExternalBgpAnnouncements(_externalBgpAnnouncementsBytes);
testrigText.setHostsBytes(_hostsBytes);
testrigText.setIptablesFilesBytes(_iptablesFilesBytes);
testrigText.setLayer1TopologyBytes(_layer1TopologyBytes);
Expand Down Expand Up @@ -161,6 +163,18 @@ public Builder setIptablesFiles(String testrigResourcePrefix, Iterable<String> f
.next();
return this;
}

public @Nonnull Builder setExternalBgpAnnouncements(String testrigResourcePrefix) {
_externalBgpAnnouncementsBytes =
readTestrigResources(
testrigResourcePrefix,
null,
ImmutableList.of(BfConsts.RELPATH_EXTERNAL_BGP_ANNOUNCEMENTS))
.values()
.iterator()
.next();
return this;
}
}

public static Builder builder() {
Expand All @@ -170,6 +184,7 @@ public static Builder builder() {
private Map<String, byte[]> _awsBytes;
private Map<String, byte[]> _bgpTablesBytes;
private Map<String, byte[]> _configurationBytes;
private byte[] _externalBgpAnnouncementBytes;
private Map<String, byte[]> _hostsBytes;
private Map<String, byte[]> _iptablesFilesBytes;
private byte[] _layer1TopologyBytes;
Expand All @@ -188,6 +203,10 @@ public Map<String, byte[]> getConfigurationBytes() {
return _configurationBytes;
}

public @Nullable byte[] getExternalBgpAnnouncementBytes() {
return _externalBgpAnnouncementBytes;
}

public Map<String, byte[]> getHostsBytes() {
return _hostsBytes;
}
Expand Down Expand Up @@ -220,6 +239,10 @@ public void setConfigurationBytes(Map<String, byte[]> configurationText) {
_configurationBytes = configurationText;
}

public void setExternalBgpAnnouncements(byte[] externalBgpAnnouncementsBytes) {
_externalBgpAnnouncementBytes = externalBgpAnnouncementsBytes;
}

public void setHostsBytes(Map<String, byte[]> hostsBytes) {
_hostsBytes = hostsBytes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
!
hostname as1border2
!
interface Loopback0
ip address 1.2.2.2 255.255.255.255
!
interface GigabitEthernet2/0
ip address 10.14.22.1 255.255.255.0
!
router bgp 1
bgp router-id 1.2.2.2
neighbor as4 peer-group
neighbor as4 remote-as 4
neighbor 10.14.22.4 peer-group as4
!
address-family ipv4
neighbor as4 route-map as4_to_as1 in
neighbor 10.14.22.4 activate
exit-address-family
!
ip community-list expanded as4_community permit _4:
!
!
ip prefix-list as4-prefixes seq 1 permit 4.0.0.0/8 le 32
!
route-map as4_to_as1 permit 100
match ip address prefix-list as4-prefixes
match community as4_community
set local-preference 350
set ip next-hop peer-address
!
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Announcements": [
{
"type" : "ebgp_sent",
"network" : "4.0.0.0/8",
"nextHopIp" : "1.1.1.1",
"srcIp" : "10.14.22.4",
"dstNode" : "as1border2",
"dstIp" : "10.14.22.1",
"srcProtocol" : "AGGREGATE",
"originType" : "egp",
"localPreference" : 0,
"med" : 20,
"originatorIp" : "0.0.0.0",
"asPath" : [ [ 1239 ]],
"communities" : [ 262145 ],
"dstVrf":"default",
"clusterList":[]
}
]
}