@Override public void applyTo( List<Statement> statements, JuniperConfiguration juniperVendorConfiguration, Configuration c, Warnings warnings) { If ifStatement = new If( BooleanExprs.CALL_EXPR_CONTEXT, ImmutableList.of(Statements.ReturnFalse.toStaticStatement()), ImmutableList.of(Statements.ExitReject.toStaticStatement())); statements.add(ifStatement); } }
@Override public void applyTo( List<Statement> statements, JuniperConfiguration juniperVendorConfiguration, Configuration c, Warnings w) { If ifStatement = new If( BooleanExprs.CALL_EXPR_CONTEXT, ImmutableList.of(Statements.ReturnTrue.toStaticStatement()), ImmutableList.of(Statements.ExitAccept.toStaticStatement())); statements.add(ifStatement); } }
@Override public final void applyTo( List<Statement> statements, CiscoConfiguration cc, Configuration c, Warnings w) { Statement setStatement = toSetStatement(cc, c, w); Statement bufferedStatement = new BufferedStatement(setStatement); statements.add(bufferedStatement); If ifStatement = new If( BooleanExprs.CALL_EXPR_CONTEXT, ImmutableList.of(Statements.SetLocalDefaultActionAccept.toStaticStatement()), ImmutableList.of(Statements.SetDefaultActionAccept.toStaticStatement())); statements.add(ifStatement); }
@Override public List<Statement> simplify() { if (_simplified != null) { return _simplified; } ImmutableList.Builder<Statement> simpleTrueStatementsBuilder = ImmutableList.builder(); ImmutableList.Builder<Statement> simpleFalseStatementsBuilder = ImmutableList.builder(); BooleanExpr simpleGuard = _guard.simplify(); for (Statement trueStatement : _trueStatements) { simpleTrueStatementsBuilder.addAll(trueStatement.simplify()); } List<Statement> simpleTrueStatements = simpleTrueStatementsBuilder.build(); for (Statement falseStatement : _falseStatements) { simpleFalseStatementsBuilder.addAll(falseStatement.simplify()); } List<Statement> simpleFalseStatements = simpleFalseStatementsBuilder.build(); if (simpleGuard.equals(BooleanExprs.TRUE)) { _simplified = simpleTrueStatements; } else if (simpleGuard.equals(BooleanExprs.FALSE)) { _simplified = simpleFalseStatements; } else if (simpleTrueStatements.isEmpty() && simpleFalseStatements.isEmpty()) { _simplified = Collections.emptyList(); } else { If simple = new If(getComment(), simpleGuard, simpleTrueStatements, simpleFalseStatements); _simplified = ImmutableList.of(simple); simple._simplified = _simplified; } return _simplified; } }
private RoutingPolicy toRoutingPolicy(Configuration c, RoutePolicy routePolicy) { String name = routePolicy.getName(); RoutingPolicy rp = new RoutingPolicy(name, c); List<Statement> statements = rp.getStatements(); for (RoutePolicyStatement routePolicyStatement : routePolicy.getStatements()) { routePolicyStatement.applyTo(statements, this, c, _w); } If nonBoolean = new If( BooleanExprs.CALL_STATEMENT_CONTEXT, Collections.singletonList(Statements.Return.toStaticStatement()), Collections.singletonList(Statements.DefaultAction.toStaticStatement())); @SuppressWarnings("unused") // TODO(https://github.com/batfish/batfish/issues/1306) If endPolicy = new If( BooleanExprs.CALL_EXPR_CONTEXT, Collections.singletonList(Statements.ReturnLocalDefaultAction.toStaticStatement()), Collections.singletonList(nonBoolean)); return rp; }
new If(guard, statements, ImmutableList.of(Statements.ExitReject.toStaticStatement())));
/** * For each static route in the given {@link RoutingInstance} that has at least one community set, * creates an {@link If} that matches that route (specifically, matches static routes with that * route's destination network), and sets communities for matching exported routes. */ @Nonnull private static List<If> getStaticRouteCommunitySetters(@Nonnull RoutingInstance ri) { MatchProtocol matchStatic = new MatchProtocol(RoutingProtocol.STATIC); return ri.getRibs().get(RoutingInformationBase.RIB_IPV4_UNICAST).getStaticRoutes().values() .stream() .filter(route -> !route.getCommunities().isEmpty()) .map( route -> { // Create matcher that matches routes that share this route's destination network PrefixExpr destNetworkMatcher = DestinationNetwork.instance(); PrefixSetExpr destNetwork = new ExplicitPrefixSet(new PrefixSpace(PrefixRange.fromPrefix(route.getPrefix()))); MatchPrefixSet networkMatcher = new MatchPrefixSet(destNetworkMatcher, destNetwork); // When a matching static route is exported, set its communities return new If( new Conjunction(ImmutableList.of(matchStatic, networkMatcher)), ImmutableList.of( new SetCommunity(new LiteralCommunitySet(route.getCommunities())))); }) .collect(ImmutableList.toImmutableList()); }
@Override public void applyTo( List<Statement> statements, CiscoConfiguration cc, Configuration c, Warnings w) { If mainIf = new If(); mainIf.setGuard(_guard.toBooleanExpr(cc, c, w)); If currentIf = mainIf; If elseIf = new If(); elseIf.setGuard(elseIfBlock.getGuard().toBooleanExpr(cc, c, w)); List<Statement> elseIfStatements = new ArrayList<>();
private RoutingPolicy toRoutingPolicy(RouteMap routeMap) { String name = routeMap.getName(); RoutingPolicy routingPolicy = new RoutingPolicy(name, _c); List<Statement> statements = routingPolicy.getStatements(); for (Entry<Integer, RouteMapRule> e : routeMap.getRules().entrySet()) { String ruleName = Integer.toString(e.getKey()); RouteMapRule rule = e.getValue(); If ifStatement = new If(); List<Statement> trueStatements = ifStatement.getTrueStatements(); ifStatement.setComment(ruleName); Conjunction conj = new Conjunction(); for (RouteMapMatch match : rule.getMatches()) { conj.getConjuncts().add(match.toBooleanExpr(this, _c, _w)); } ifStatement.setGuard(conj.simplify()); switch (rule.getAction()) { case PERMIT: trueStatements.add(Statements.ExitAccept.toStaticStatement()); break; case DENY: trueStatements.add(Statements.ExitReject.toStaticStatement()); break; default: throw new BatfishException("Invalid action"); } statements.add(ifStatement); } statements.add(Statements.ExitReject.toStaticStatement()); return routingPolicy; }
return new If( "Suppress more specific networks for summary-only aggregate-address networks", new MatchPrefixSet(
new If( new DisjunctionChain(policyCalls), ImmutableList.of(Statements.ReturnTrue.toStaticStatement()),
private void applyLocalRoutePolicy(RoutingInstance routingInstance, RoutingPolicy targetPolicy) { boolean lan = routingInstance.getExportLocalRoutesLan(); boolean ptp = routingInstance.getExportLocalRoutesPointToPoint(); if (lan && ptp) { // All local routes are allowed, so no need for filter return; } BooleanExpr matchProtocol = new MatchProtocol(RoutingProtocol.LOCAL); BooleanExpr match; if (!lan && !ptp) { // No need to check length, since all local routes will be rejected match = matchProtocol; } else { SubRange rejectedLength = !lan ? new SubRange(0, Prefix.MAX_PREFIX_LENGTH - 2) : new SubRange(Prefix.MAX_PREFIX_LENGTH - 1, Prefix.MAX_PREFIX_LENGTH - 1); match = new Conjunction( ImmutableList.of( matchProtocol, new MatchLocalRouteSourcePrefixLength(rejectedLength))); } targetPolicy .getStatements() .add(new If(match, ImmutableList.of(Statements.ExitReject.toStaticStatement()))); }
private void initDefaultBgpExportPolicy() { if (_c.getRoutingPolicies().containsKey(DEFAULT_BGP_EXPORT_POLICY_NAME)) { return; } // set up default export policy (accept bgp routes) RoutingPolicy defaultBgpExportPolicy = new RoutingPolicy(DEFAULT_BGP_EXPORT_POLICY_NAME, _c); _c.getRoutingPolicies().put(DEFAULT_BGP_EXPORT_POLICY_NAME, defaultBgpExportPolicy); If defaultBgpExportPolicyConditional = new If(); defaultBgpExportPolicy.getStatements().add(defaultBgpExportPolicyConditional); // guard Disjunction isBgp = new Disjunction(); isBgp.getDisjuncts().add(new MatchProtocol(RoutingProtocol.BGP)); isBgp.getDisjuncts().add(new MatchProtocol(RoutingProtocol.IBGP)); defaultBgpExportPolicyConditional.setGuard(isBgp); PsThenAccept.INSTANCE.applyTo( defaultBgpExportPolicyConditional.getTrueStatements(), this, _c, _w); PsThenReject.INSTANCE.applyTo( defaultBgpExportPolicyConditional.getFalseStatements(), this, _c, _w); }
/** * Generates and returns a {@link RoutingPolicy} that matches routes that should be aggregated for * aggregate network indicated by the given {@link Prefix}. * * <p>Does the bookkeeping in the provided {@link Configuration} to ensure the generated policy is * available and tracked. */ static RoutingPolicy generateAggregateRoutePolicy( Configuration c, String vrfName, Prefix prefix) { BooleanExpr matchLongerNetworks = new MatchPrefixSet( DestinationNetwork.instance(), new ExplicitPrefixSet(new PrefixSpace(PrefixRange.moreSpecificThan(prefix)))); If currentGeneratedRouteConditional = new If(matchLongerNetworks, singletonList(Statements.ReturnTrue.toStaticStatement())); RoutingPolicy policy = new RoutingPolicy("~AGGREGATE_ROUTE_GEN:" + vrfName + ":" + prefix + "~", c); policy.setStatements(ImmutableList.of(currentGeneratedRouteConditional)); c.getRoutingPolicies().put(policy.getName(), policy); return policy; }
return new If( "OSPF export routes for " + protocol.protocolName(), ospfExportConditions,
/** * Generate a {@link RoutingPolicy} for use when importing routes from pseudo-protocols (direct, * static, aggregate, generated) */ @VisibleForTesting static RoutingPolicy generateDefaultPseudoProtocolImportPolicy(@Nonnull Configuration c) { return RoutingPolicy.builder() .setOwner(c) .setName(DEFAULT_PSEUDO_PROTOCOL_IMPORT_POLICY_NAME) .setStatements( ImmutableList.of( new If( new Disjunction( new MatchProtocol(RoutingProtocol.CONNECTED), new MatchProtocol(RoutingProtocol.LOCAL), new MatchProtocol(RoutingProtocol.STATIC), new MatchProtocol(RoutingProtocol.AGGREGATE)), ImmutableList.of(Statements.ReturnTrue.toStaticStatement()), ImmutableList.of(Statements.ReturnFalse.toStaticStatement())))) .build(); }
/** Policy with actual circular reference as expr */ @Test public void testRoutingPolicyCircularReferenceExpr() { String parentPolicyName = "parent"; CallExpr callExpr = new CallExpr(parentPolicyName); If ifStatement = new If(); ifStatement.setGuard(callExpr); _rpb.setName(parentPolicyName).setStatements(ImmutableList.of(ifStatement)).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)); }
String policyName = "~SUMMARY" + policyNameSuffix + "~"; RoutingPolicy routingPolicy = new RoutingPolicy(policyName, _c); If routingPolicyConditional = new If(); routingPolicy.getStatements().add(routingPolicyConditional); routingPolicyConditional.getTrueStatements().add(Statements.ExitAccept.toStaticStatement());
we1.setPostStatements(ImmutableList.of(bs)); If if1 = new If(); if1.setGuard(we1); 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)); If if5 = new If(); if5.setGuard(not);