Search in sources :

Example 6 with ExplicitPrefixSet

use of org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet in project batfish by batfish.

the class TransferSSA method matchPrefixSet.

/*
   * Converts a prefix set to a boolean expression.
   */
private TransferResult<BoolExpr, BoolExpr> matchPrefixSet(Configuration conf, PrefixSetExpr e, SymbolicRoute other) {
    ArithExpr otherLen = other.getPrefixLength();
    TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
    if (e instanceof ExplicitPrefixSet) {
        ExplicitPrefixSet x = (ExplicitPrefixSet) e;
        Set<PrefixRange> ranges = x.getPrefixSpace().getPrefixRanges();
        if (ranges.isEmpty()) {
            return result.setReturnValue(_enc.mkTrue());
        }
        // by checking for static/connected/OSPF routes specifically.
        if (ranges.size() == 1) {
            for (PrefixRange r : ranges) {
                int start = r.getLengthRange().getStart();
                int end = r.getLengthRange().getEnd();
                Prefix pfx = r.getPrefix();
                if (start == end && start == pfx.getPrefixLength()) {
                    String router = _conf.getName();
                    Set<Prefix> origin = _enc.getOriginatedNetworks().get(router, Protocol.BGP);
                    if (origin != null && origin.contains(pfx)) {
                        // Compute static and connected routes
                        Set<Prefix> ostatic = _enc.getOriginatedNetworks().get(router, Protocol.STATIC);
                        Set<Prefix> oconn = _enc.getOriginatedNetworks().get(router, Protocol.CONNECTED);
                        boolean hasStatic = ostatic != null && ostatic.contains(pfx);
                        boolean hasConnected = oconn != null && oconn.contains(pfx);
                        ArithExpr originLength = _enc.mkInt(pfx.getPrefixLength());
                        if (hasStatic || hasConnected) {
                            BoolExpr directRoute = _enc.isRelevantFor(originLength, r);
                            ArithExpr newLength = _enc.mkIf(directRoute, originLength, otherLen);
                            result = result.addChangedVariable("PREFIX-LEN", newLength);
                            return result.setReturnValue(directRoute);
                        } else {
                            // Also use network statement if OSPF has a route with the correct length
                            SymbolicRoute rec = _enc.getBestNeighborPerProtocol(router, Protocol.OSPF);
                            if (rec != null) {
                                BoolExpr ospfRelevant = _enc.isRelevantFor(rec.getPrefixLength(), r);
                                ArithExpr newLength = _enc.mkIf(ospfRelevant, originLength, otherLen);
                                result = result.addChangedVariable("PREFIX-LEN", newLength);
                                return result.setReturnValue(ospfRelevant);
                            }
                        }
                    }
                }
            }
        }
        // Compute if the other best route is relevant for this match statement
        BoolExpr acc = _enc.mkFalse();
        for (PrefixRange range : ranges) {
            acc = _enc.mkOr(acc, _enc.isRelevantFor(otherLen, range));
        }
        return result.setReturnValue(acc);
    } else if (e instanceof NamedPrefixSet) {
        NamedPrefixSet x = (NamedPrefixSet) e;
        String name = x.getName();
        RouteFilterList fl = conf.getRouteFilterLists().get(name);
        return result.setReturnValue(matchFilterList(fl, other));
    } else {
        throw new BatfishException("TODO: match prefix set: " + e);
    }
}
Also used : ArithExpr(com.microsoft.z3.ArithExpr) BoolExpr(com.microsoft.z3.BoolExpr) BatfishException(org.batfish.common.BatfishException) PrefixRange(org.batfish.datamodel.PrefixRange) NamedPrefixSet(org.batfish.datamodel.routing_policy.expr.NamedPrefixSet) Prefix(org.batfish.datamodel.Prefix) TransferResult(org.batfish.symbolic.TransferResult) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) RouteFilterList(org.batfish.datamodel.RouteFilterList)

Example 7 with ExplicitPrefixSet

use of org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet 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)

Aggregations

ExplicitPrefixSet (org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet)7 PrefixRange (org.batfish.datamodel.PrefixRange)6 Prefix (org.batfish.datamodel.Prefix)5 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)5 PrefixSpace (org.batfish.datamodel.PrefixSpace)4 RouteFilterList (org.batfish.datamodel.RouteFilterList)4 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)4 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)4 DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)4 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)4 Not (org.batfish.datamodel.routing_policy.expr.Not)4 If (org.batfish.datamodel.routing_policy.statement.If)4 BatfishException (org.batfish.common.BatfishException)3 GeneratedRoute (org.batfish.datamodel.GeneratedRoute)3 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)3 SubRange (org.batfish.datamodel.SubRange)3 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)3 LiteralLong (org.batfish.datamodel.routing_policy.expr.LiteralLong)3 NamedPrefixSet (org.batfish.datamodel.routing_policy.expr.NamedPrefixSet)3 CallStatement (org.batfish.datamodel.routing_policy.statement.CallStatement)3