Search in sources :

Example 31 with If

use of org.batfish.datamodel.routing_policy.statement.If in project batfish by batfish.

the class BatfishCompressor method applyFilters.

/**
 * Update a list of RoutingPolicy statements by filtering traffic according to the input filter.
 *
 * @param statements The list of RoutingPolicy statements.
 * @param filter The filter used to restrict traffic
 * @return A new list of RoutingPolicy statements
 */
// PrefixTrie: capture the prefixes you are installing to allow traffic through. Restrict
// to those prefixes
// Boolean: are the prefixes for the default equivalence class?
private List<Statement> applyFilters(List<Statement> statements, @Nullable EquivalenceClassFilter filter) {
    If i = new If();
    List<Statement> newStatements = new ArrayList<>();
    List<Statement> falseStatements = new ArrayList<>();
    Statement reject = new StaticStatement(Statements.ExitReject);
    falseStatements.add(reject);
    if (filter == null) {
        StaticBooleanExpr sbe = new StaticBooleanExpr(BooleanExprs.False);
        i.setGuard(sbe);
    } else {
        AbstractionPrefixSet eps = new AbstractionPrefixSet(filter._prefixTrie);
        MatchPrefixSet match = new MatchPrefixSet(new DestinationNetwork(), eps);
        if (filter._isForDefaultSlice) {
            // Let traffic through if it passes the filter or was advertised from outside the network.
            Disjunction pfxOrExternal = new Disjunction();
            pfxOrExternal.setDisjuncts(ImmutableList.of(match, matchExternalTraffic()));
            i.setGuard(pfxOrExternal);
        } else {
            // Not default equivalence class, so just let traffic through if dest matches the filter
            i.setGuard(match);
        }
    }
    i.setFalseStatements(falseStatements);
    i.setTrueStatements(statements);
    newStatements.add(i);
    return newStatements;
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) AbstractionPrefixSet(org.batfish.datamodel.routing_policy.expr.AbstractionPrefixSet) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) ArrayList(java.util.ArrayList) If(org.batfish.datamodel.routing_policy.statement.If) StaticBooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExprs.StaticBooleanExpr)

Example 32 with If

use of org.batfish.datamodel.routing_policy.statement.If in project batfish by batfish.

the class JuniperConfiguration method toRoutingPolicy.

private RoutingPolicy toRoutingPolicy(PolicyStatement ps) {
    String name = ps.getName();
    RoutingPolicy routingPolicy = new RoutingPolicy(name, _c);
    List<Statement> statements = routingPolicy.getStatements();
    boolean hasDefaultTerm = ps.getDefaultTerm().getFroms().size() > 0 || ps.getDefaultTerm().getThens().size() > 0;
    List<PsTerm> terms = new ArrayList<>();
    terms.addAll(ps.getTerms().values());
    if (hasDefaultTerm) {
        terms.add(ps.getDefaultTerm());
    }
    for (PsTerm term : terms) {
        List<Statement> thens = toStatements(term.getThens());
        if (!term.getFroms().isEmpty()) {
            If ifStatement = new If();
            ifStatement.setComment(term.getName());
            Conjunction conj = new Conjunction();
            List<BooleanExpr> subroutines = new ArrayList<>();
            for (PsFrom from : term.getFroms()) {
                if (from instanceof PsFromRouteFilter) {
                    int actionLineCounter = 0;
                    PsFromRouteFilter fromRouteFilter = (PsFromRouteFilter) from;
                    String routeFilterName = fromRouteFilter.getRouteFilterName();
                    RouteFilter rf = _routeFilters.get(routeFilterName);
                    for (RouteFilterLine line : rf.getLines()) {
                        if (line.getThens().size() > 0) {
                            String lineListName = name + "_ACTION_LINE_" + actionLineCounter;
                            RouteFilterList lineSpecificList = new RouteFilterList(lineListName);
                            line.applyTo(lineSpecificList);
                            actionLineCounter++;
                            _c.getRouteFilterLists().put(lineListName, lineSpecificList);
                            If lineSpecificIfStatement = new If();
                            String lineSpecificClauseName = routeFilterName + "_ACTION_LINE_" + actionLineCounter;
                            lineSpecificIfStatement.setComment(lineSpecificClauseName);
                            MatchPrefixSet mrf = new MatchPrefixSet(new DestinationNetwork(), new NamedPrefixSet(lineListName));
                            lineSpecificIfStatement.setGuard(mrf);
                            lineSpecificIfStatement.getTrueStatements().addAll(toStatements(line.getThens()));
                            statements.add(lineSpecificIfStatement);
                        }
                    }
                }
                BooleanExpr booleanExpr = from.toBooleanExpr(this, _c, _w);
                if (from instanceof PsFromPolicyStatement || from instanceof PsFromPolicyStatementConjunction) {
                    subroutines.add(booleanExpr);
                } else {
                    conj.getConjuncts().add(booleanExpr);
                }
            }
            if (!subroutines.isEmpty()) {
                ConjunctionChain chain = new ConjunctionChain(subroutines);
                conj.getConjuncts().add(chain);
            }
            BooleanExpr guard = conj.simplify();
            ifStatement.setGuard(guard);
            ifStatement.getTrueStatements().addAll(thens);
            statements.add(ifStatement);
        } else {
            statements.addAll(thens);
        }
    }
    If endOfPolicy = new If();
    endOfPolicy.setGuard(BooleanExprs.CallExprContext.toStaticBooleanExpr());
    endOfPolicy.setFalseStatements(Collections.singletonList(Statements.Return.toStaticStatement()));
    statements.add(endOfPolicy);
    return routingPolicy;
}
Also used : NamedPrefixSet(org.batfish.datamodel.routing_policy.expr.NamedPrefixSet) Statement(org.batfish.datamodel.routing_policy.statement.Statement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) ArrayList(java.util.ArrayList) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) RouteFilterList(org.batfish.datamodel.RouteFilterList) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) If(org.batfish.datamodel.routing_policy.statement.If) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr)

Example 33 with If

use of org.batfish.datamodel.routing_policy.statement.If in project batfish by batfish.

the class OspfTest method getExportPolicyStatements.

private static List<Statement> getExportPolicyStatements(InterfaceAddress address) {
    long externalOspfMetric = 20L;
    If exportIfMatchL2Prefix = new If();
    exportIfMatchL2Prefix.setGuard(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(ImmutableSet.of(PrefixRange.fromPrefix(address.getPrefix()))))));
    exportIfMatchL2Prefix.setTrueStatements(ImmutableList.of(new SetOspfMetricType(OspfMetricType.E1), new SetMetric(new LiteralLong(externalOspfMetric)), Statements.ExitAccept.toStaticStatement()));
    exportIfMatchL2Prefix.setFalseStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
    return ImmutableList.of(exportIfMatchL2Prefix);
}
Also used : DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) SetMetric(org.batfish.datamodel.routing_policy.statement.SetMetric) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) PrefixSpace(org.batfish.datamodel.PrefixSpace) SetOspfMetricType(org.batfish.datamodel.routing_policy.statement.SetOspfMetricType) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) If(org.batfish.datamodel.routing_policy.statement.If)

Example 34 with If

use of org.batfish.datamodel.routing_policy.statement.If in project batfish by batfish.

the class RouteReflectionTest method setup.

/**
 * Initialize builders with values common to all tests
 */
@Before
public void setup() {
    _ab = new BgpAdvertisement.Builder().setClusterList(ImmutableSortedSet.of()).setCommunities(ImmutableSortedSet.of()).setDstVrf(Configuration.DEFAULT_VRF_NAME).setOriginType(OriginType.INCOMPLETE).setSrcProtocol(RoutingProtocol.AGGREGATE).setSrcVrf(Configuration.DEFAULT_VRF_NAME).setType(BgpAdvertisementType.EBGP_SENT);
    _nf = new NetworkFactory();
    _cb = _nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    _ib = _nf.interfaceBuilder().setActive(true);
    _nb = _nf.bgpNeighborBuilder().setLocalAs(2);
    _pb = _nf.bgpProcessBuilder();
    _vb = _nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
    If acceptIffBgp = new If();
    Disjunction guard = new Disjunction();
    guard.setDisjuncts(ImmutableList.of(new MatchProtocol(RoutingProtocol.BGP), new MatchProtocol(RoutingProtocol.IBGP)));
    acceptIffBgp.setGuard(guard);
    acceptIffBgp.setTrueStatements(ImmutableList.of(Statements.ExitAccept.toStaticStatement()));
    acceptIffBgp.setFalseStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
    /* Builder that creates default BGP export policy */
    _defaultExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(acceptIffBgp));
    /* Builder that creates BGP export policy that rejects everything */
    _nullExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) NetworkFactory(org.batfish.datamodel.NetworkFactory) If(org.batfish.datamodel.routing_policy.statement.If) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Before(org.junit.Before)

Example 35 with If

use of org.batfish.datamodel.routing_policy.statement.If in project batfish by batfish.

the class BatfishCompressionTest method testCompressionFibs_compressibleNetwork.

/**
 * Test the following invariant: if a FIB appears on concrete router “r”, then a corresponding
 * abstract FIB appears on one of these representatives. For example, if there is a concrete FIB
 * from C to D, then there should be an abstract FIB from A to B, where A is in representatives(C)
 * and B is in representatives(D).
 */
@Test
public void testCompressionFibs_compressibleNetwork() throws IOException {
    DataPlane origDataPlane = getDataPlane(compressibleNetwork());
    SortedMap<String, Configuration> compressedConfigs = compressNetwork(compressibleNetwork(), new HeaderSpace());
    DataPlane compressedDataPlane = getDataPlane(compressedConfigs);
    SortedMap<String, SortedMap<String, GenericRib<AbstractRoute>>> origRibs = origDataPlane.getRibs();
    SortedMap<String, SortedMap<String, GenericRib<AbstractRoute>>> compressedRibs = compressedDataPlane.getRibs();
    /* Compression removed a node */
    assertThat(compressedConfigs.entrySet(), hasSize(2));
    compressedConfigs.values().forEach(BatfishCompressionTest::assertIsCompressedConfig);
    compressedRibs.forEach((hostname, compressedRibsByVrf) -> compressedRibsByVrf.forEach((vrf, compressedRib) -> {
        GenericRib<AbstractRoute> origRib = origRibs.get(hostname).get(vrf);
        Set<AbstractRoute> origRoutes = origRib.getRoutes();
        Set<AbstractRoute> compressedRoutes = compressedRib.getRoutes();
        for (AbstractRoute route : compressedRoutes) {
            /* Every compressed route should appear in original RIB */
            assertThat(origRoutes, hasItem(route));
        }
    }));
}
Also used : DataPlane(org.batfish.datamodel.DataPlane) AbstractRoute(org.batfish.datamodel.AbstractRoute) BatfishTestUtils(org.batfish.main.BatfishTestUtils) FibMatchers.hasNextHopInterfaces(org.batfish.datamodel.matchers.FibMatchers.hasNextHopInterfaces) BdpDataPlanePlugin(org.batfish.bdp.BdpDataPlanePlugin) HeaderSpace(org.batfish.datamodel.HeaderSpace) Matchers.either(org.hamcrest.Matchers.either) If(org.batfish.datamodel.routing_policy.statement.If) TopologyMatchers.isNeighborOfNode(org.batfish.datamodel.matchers.TopologyMatchers.isNeighborOfNode) Matchers.not(org.hamcrest.Matchers.not) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Matchers.hasValue(org.hamcrest.Matchers.hasValue) Matchers.hasKey(org.hamcrest.Matchers.hasKey) Interface(org.batfish.datamodel.Interface) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) ImmutableList(com.google.common.collect.ImmutableList) AbstractRoute(org.batfish.datamodel.AbstractRoute) Topology(org.batfish.datamodel.Topology) TopologyMatchers.withNode(org.batfish.datamodel.matchers.TopologyMatchers.withNode) Map(java.util.Map) Configuration(org.batfish.datamodel.Configuration) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Batfish(org.batfish.main.Batfish) Vrf(org.batfish.datamodel.Vrf) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) DataPlane(org.batfish.datamodel.DataPlane) NetworkFactory(org.batfish.datamodel.NetworkFactory) StaticRoute(org.batfish.datamodel.StaticRoute) Fib(org.batfish.datamodel.Fib) Set(java.util.Set) GenericRib(org.batfish.datamodel.GenericRib) IOException(java.io.IOException) Test(org.junit.Test) IBatfish(org.batfish.common.plugin.IBatfish) Matchers.hasItem(org.hamcrest.Matchers.hasItem) TreeMap(java.util.TreeMap) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assert.assertEquals(org.junit.Assert.assertEquals) SortedMap(java.util.SortedMap) IpWildcard(org.batfish.datamodel.IpWildcard) TemporaryFolder(org.junit.rules.TemporaryFolder) Prefix(org.batfish.datamodel.Prefix) Set(java.util.Set) Configuration(org.batfish.datamodel.Configuration) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap) GenericRib(org.batfish.datamodel.GenericRib) HeaderSpace(org.batfish.datamodel.HeaderSpace) Test(org.junit.Test)

Aggregations

If (org.batfish.datamodel.routing_policy.statement.If)35 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)22 Statement (org.batfish.datamodel.routing_policy.statement.Statement)20 Prefix (org.batfish.datamodel.Prefix)14 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)13 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)12 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)12 ArrayList (java.util.ArrayList)11 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)11 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)11 BatfishException (org.batfish.common.BatfishException)10 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)9 SetOspfMetricType (org.batfish.datamodel.routing_policy.statement.SetOspfMetricType)9 Set (java.util.Set)8 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)8 Configuration (org.batfish.datamodel.Configuration)8 GeneratedRoute (org.batfish.datamodel.GeneratedRoute)8