Search in sources :

Example 1 with CallStatement

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));
}
Also used : CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) ImmutableMap(com.google.common.collect.ImmutableMap) CommonUtil(org.batfish.common.util.CommonUtil) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) FeatureMatcher(org.hamcrest.FeatureMatcher) ImmutableList(com.google.common.collect.ImmutableList) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Warnings(org.batfish.common.Warnings) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Map(java.util.Map) Matcher(org.hamcrest.Matcher) Collections(java.util.Collections) Before(org.junit.Before) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Warnings(org.batfish.common.Warnings) Test(org.junit.Test)

Example 2 with CallStatement

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());
}
Also used : CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) BufferedStatement(org.batfish.datamodel.routing_policy.statement.BufferedStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Test(org.junit.Test)

Example 3 with CallStatement

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));
}
Also used : BufferedStatement(org.batfish.datamodel.routing_policy.statement.BufferedStatement) Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) Not(org.batfish.datamodel.routing_policy.expr.Not) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) Matchers.containsString(org.hamcrest.Matchers.containsString) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) DisjunctionChain(org.batfish.datamodel.routing_policy.expr.DisjunctionChain) WithEnvironmentExpr(org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr) If(org.batfish.datamodel.routing_policy.statement.If) Test(org.junit.Test)

Example 4 with CallStatement

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());
}
Also used : CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) BufferedStatement(org.batfish.datamodel.routing_policy.statement.BufferedStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Test(org.junit.Test)

Example 5 with CallStatement

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));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Test(org.junit.Test)

Aggregations

CallStatement (org.batfish.datamodel.routing_policy.statement.CallStatement)6 Test (org.junit.Test)5 BufferedStatement (org.batfish.datamodel.routing_policy.statement.BufferedStatement)3 Statement (org.batfish.datamodel.routing_policy.statement.Statement)3 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)2 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)2 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)2 If (org.batfish.datamodel.routing_policy.statement.If)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigInteger (java.math.BigInteger)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BatfishException (org.batfish.common.BatfishException)1 Warnings (org.batfish.common.Warnings)1 CommonUtil (org.batfish.common.util.CommonUtil)1 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)1 ConjunctionChain (org.batfish.datamodel.routing_policy.expr.ConjunctionChain)1