use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class ConfigurationTest method testComputeRoutingPolicySources.
@Test
public void testComputeRoutingPolicySources() {
String bgpExportPolicyName = "bgpExportPolicy";
String bgpImportPolicyName = "bgpImportPolicy";
String bgpMissingExportPolicyName = "bgpMissingExportPolicy";
String bgpMissingImportPolicyName = null;
String generatedRouteAttributePolicyName = "generatedRouteAttributePolicy";
String generatedRouteGenerationPolicyName = "generatedRouteGenerationPolicy";
String ospfExportPolicyName = "ospfExportPolicy";
String ospfExportSubPolicyName = "ospfExportSubPolicy";
Prefix neighborPrefix = new Prefix(Ip.ZERO, Prefix.MAX_PREFIX_LENGTH);
Prefix generatedRoutePrefix = neighborPrefix;
Prefix neigborWithMissingPoliciesPrefix = new Prefix(Ip.MAX, Prefix.MAX_PREFIX_LENGTH);
Configuration c = new Configuration("test", ConfigurationFormat.CISCO_IOS);
Vrf vrf = c.getVrfs().computeIfAbsent(Configuration.DEFAULT_VRF_NAME, Vrf::new);
// BGP
BgpProcess bgpProcess = new BgpProcess();
vrf.setBgpProcess(bgpProcess);
BgpNeighbor neighbor = bgpProcess.getNeighbors().computeIfAbsent(neighborPrefix, BgpNeighbor::new);
neighbor.setExportPolicy(c.getRoutingPolicies().computeIfAbsent(bgpExportPolicyName, n -> new RoutingPolicy(n, c)).getName());
neighbor.setImportPolicy(c.getRoutingPolicies().computeIfAbsent(bgpImportPolicyName, n -> new RoutingPolicy(n, c)).getName());
BgpNeighbor neighborWithMissingPolicies = bgpProcess.getNeighbors().computeIfAbsent(neigborWithMissingPoliciesPrefix, BgpNeighbor::new);
neighborWithMissingPolicies.setExportPolicy(bgpMissingExportPolicyName);
neighborWithMissingPolicies.setImportPolicy(bgpMissingImportPolicyName);
// Generated route
GeneratedRoute gr = new GeneratedRoute.Builder().setNetwork(generatedRoutePrefix).setAttributePolicy(c.getRoutingPolicies().computeIfAbsent(generatedRouteAttributePolicyName, n -> new RoutingPolicy(n, c)).getName()).setGenerationPolicy(c.getRoutingPolicies().computeIfAbsent(generatedRouteGenerationPolicyName, n -> new RoutingPolicy(n, c)).getName()).build();
vrf.getGeneratedRoutes().add(gr);
// OSPF
OspfProcess ospfProcess = new OspfProcess();
vrf.setOspfProcess(ospfProcess);
RoutingPolicy ospfExportPolicy = c.getRoutingPolicies().computeIfAbsent(ospfExportPolicyName, n -> new RoutingPolicy(n, c));
ospfProcess.setExportPolicy(ospfExportPolicyName);
ospfExportPolicy.getStatements().add(new CallStatement(c.getRoutingPolicies().computeIfAbsent(ospfExportSubPolicyName, n -> new RoutingPolicy(n, c)).getName()));
// Compute policy sources
Warnings w = new Warnings();
c.computeRoutingPolicySources(w);
// BGP tests
assertThat(neighbor.getExportPolicySources(), equalTo(Collections.singleton(bgpExportPolicyName)));
assertThat(neighbor.getImportPolicySources(), equalTo(Collections.singleton(bgpImportPolicyName)));
assertThat(neighborWithMissingPolicies.getExportPolicySources(), equalTo(Collections.emptySet()));
assertThat(neighborWithMissingPolicies.getImportPolicySources(), equalTo(Collections.emptySet()));
// Generated route tests
assertThat(gr.getAttributePolicySources(), equalTo(Collections.singleton(generatedRouteAttributePolicyName)));
assertThat(gr.getGenerationPolicySources(), equalTo(Collections.singleton(generatedRouteGenerationPolicyName)));
// OSPF tests
assertThat(ospfProcess.getExportPolicySources(), containsInAnyOrder(ospfExportPolicyName, ospfExportSubPolicyName));
}
use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class RoutingPolicyTests method testRoutingPolicyTwoCopiesCallStatement.
/**
* Policy with two copies of same call statement - should not contain circular reference
*/
@Test
public void testRoutingPolicyTwoCopiesCallStatement() {
RoutingPolicy calledPolicy = _rpb.build();
Statement callStatement = new CallStatement(calledPolicy.getName());
_rpb.setStatements(ImmutableList.of(callStatement, callStatement)).build();
_c.computeRoutingPolicySources(_w);
// No circular reference warnings should be emitted
assertThat(_w.getRedFlagWarnings(), empty());
}
use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class RoutingPolicyTests method testRoutingPolicyDeepCircularReference.
/**
* Policy with actual circular reference deep in the policy
*/
@Test
public void testRoutingPolicyDeepCircularReference() {
String parentPolicyName = "parent";
CallStatement callStatement = new CallStatement(parentPolicyName);
BufferedStatement bs = new BufferedStatement(callStatement);
WithEnvironmentExpr we1 = new WithEnvironmentExpr();
we1.setPostStatements(ImmutableList.of(bs));
If if1 = new If();
if1.setGuard(we1);
WithEnvironmentExpr we2 = new WithEnvironmentExpr();
we2.setPostTrueStatements(ImmutableList.of(if1));
If if2 = new If();
if2.setGuard(we2);
If if3 = new If();
if3.setTrueStatements(ImmutableList.of(if2));
If if4 = new If();
if4.setFalseStatements(ImmutableList.of(if3));
WithEnvironmentExpr we3 = new WithEnvironmentExpr();
we3.setPreStatements(ImmutableList.of(if4));
WithEnvironmentExpr we4 = new WithEnvironmentExpr();
we4.setExpr(we3);
Conjunction conj = new Conjunction();
conj.setConjuncts(ImmutableList.of(we4));
ConjunctionChain conjunctionChain = new ConjunctionChain(ImmutableList.of(conj));
Disjunction disjunction = new Disjunction();
disjunction.setDisjuncts(ImmutableList.of(conjunctionChain));
DisjunctionChain disjunctionChain = new DisjunctionChain(ImmutableList.of(disjunction));
Not not = new Not(disjunctionChain);
If if5 = new If();
if5.setGuard(not);
_rpb.setName(parentPolicyName).setStatements(ImmutableList.of(if5)).build();
_c.computeRoutingPolicySources(_w);
/*
* A circular reference warning should be emitted containing the name of the circularly
* referenced policy.
*/
assertThat(_w.getRedFlagWarnings(), not(empty()));
assertThat(_w.getRedFlagWarnings().iterator().next().getText(), containsString(parentPolicyName));
}
use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class RoutingPolicyTests method testRoutingPolicyTwoDifferentCallStatementsSamePolicy.
/**
* Policy with two different call statements for same policy - should not contain circular
* reference
*/
@Test
public void testRoutingPolicyTwoDifferentCallStatementsSamePolicy() {
RoutingPolicy calledPolicy = _rpb.build();
Statement callStatement1 = new CallStatement(calledPolicy.getName());
Statement callStatement2 = new CallStatement(calledPolicy.getName());
_rpb.setStatements(ImmutableList.of(callStatement1, callStatement2)).build();
_c.computeRoutingPolicySources(_w);
// No circular reference warnings should be emitted
assertThat(_w.getRedFlagWarnings(), empty());
}
use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class RoutingPolicyTests method testRoutingPolicyCircularReference.
/**
* Policy with actual circular reference as statement
*/
@Test
public void testRoutingPolicyCircularReference() {
String parentPolicyName = "parent";
_rpb.setName(parentPolicyName).setStatements(ImmutableList.of(new CallStatement(parentPolicyName))).build();
_c.computeRoutingPolicySources(_w);
/*
* A circular reference warning should be emitted containing the name of the circularly
* referenced policy.
*/
assertThat(_w.getRedFlagWarnings(), not(empty()));
assertThat(_w.getRedFlagWarnings().iterator().next().getText(), containsString(parentPolicyName));
}
Aggregations