private void convertRouteMaps() { for (Entry<String, RouteMap> e : _routeMaps.entrySet()) { String name = e.getKey(); RouteMap routeMap = e.getValue(); RoutingPolicy rp = toRoutingPolicy(routeMap); _c.getRoutingPolicies().put(name, rp); } }
public void visit( Collection<Configuration> configs, Consumer<Statement> fs, Consumer<BooleanExpr> fe) { for (Configuration conf : configs) { for (RoutingPolicy pol : conf.getRoutingPolicies().values()) { visit(conf, pol.getStatements(), fs, fe); } } } }
private void initDefaultBgpImportPolicy() { if (_c.getRoutingPolicies().containsKey(DEFAULT_BGP_IMPORT_POLICY_NAME)) { return; } // set up default import policy (accept all routes) RoutingPolicy defaultBgpImportPolicy = new RoutingPolicy(DEFAULT_BGP_IMPORT_POLICY_NAME, _c); _c.getRoutingPolicies().put(DEFAULT_BGP_IMPORT_POLICY_NAME, defaultBgpImportPolicy); PsThenAccept.INSTANCE.applyTo(defaultBgpImportPolicy.getStatements(), this, _c, _w); }
@Override public RoutingPolicy build() { String name = _name != null ? _name : generateName(); RoutingPolicy routingPolicy = new RoutingPolicy(name, _owner); if (_owner != null) { _owner.getRoutingPolicies().put(name, routingPolicy); } routingPolicy.setStatements(_statements.build()); return routingPolicy; }
/** * Initialize default pseudo-protocol import policy (if it does not exist) in the * vendor-independent {@link Configuration}. * * @return the name of the initialized policy */ private void initDefaultPseudoProtocolImportPolicy() { if (_c.getRoutingPolicies().containsKey(DEFAULT_PSEUDO_PROTOCOL_IMPORT_POLICY_NAME)) { return; } generateDefaultPseudoProtocolImportPolicy(_c); }
/** Apply a rib group to a given source rib */ private void applyRibGroup( @Nonnull RibGroup ribGroup, @Nonnull GenericRib<? extends AbstractRoute> sourceRib) { RoutingPolicy policy = _c.getRoutingPolicies().get(ribGroup.getImportPolicy()); checkState(policy != null, "RIB group %s is missing import policy", ribGroup.getName()); sourceRib.getRoutes().stream() .map( route -> { AbstractRouteBuilder<?, ?> builder = route.toBuilder(); boolean accept = policy.process(route, builder, null, _name, IN); return accept ? builder.build() : null; }) .filter(Objects::nonNull) .forEach( r -> ribGroup .getImportRibs() .forEach(ribId -> _node.getRib(ribId).ifPresent(rib -> rib.mergeRoute(r)))); }
private void enqueueCrossVrfRoutes( @Nonnull CrossVrfEdgeId remoteVrfToOurRib, @Nonnull Collection<RouteAdvertisement<AbstractRoute>> routes, @Nullable String policyName) { if (!_crossVrfIncomingRoutes.containsKey(remoteVrfToOurRib)) { // We either messed up royally or https://github.com/batfish/batfish/issues/3050 return; } Collection<RouteAdvertisement<AbstractRoute>> filteredRoutes = routes; if (policyName != null) { RoutingPolicy policy = _c.getRoutingPolicies().get(policyName); filteredRoutes = routes.stream() .map( ra -> { AbstractRouteBuilder<?, ?> routeBuilder = ra.getRoute().toBuilder(); if (policy.process(ra.getRoute(), routeBuilder, null, _name, IN)) { return ra.toBuilder().setRoute(routeBuilder.build()).build(); } else { return null; } }) .filter(Objects::nonNull) .collect(ImmutableList.toImmutableList()); } _crossVrfIncomingRoutes.get(remoteVrfToOurRib).addAll(filteredRoutes); }
@Nullable public static RoutingPolicy findCommonRoutingPolicy(Configuration conf, Protocol proto) { if (proto.isOspf()) { String exp = conf.getDefaultVrf().getOspfProcess().getExportPolicy(); return conf.getRoutingPolicies().get(exp); } if (proto.isBgp()) { for (Map.Entry<String, RoutingPolicy> entry : conf.getRoutingPolicies().entrySet()) { String name = entry.getKey(); if (name.contains(BGP_COMMON_FILTER_LIST_NAME)) { return entry.getValue(); } } return null; } if (proto.isStatic()) { return null; } if (proto.isConnected()) { return null; } throw new BatfishException("TODO: findCommonRoutingPolicy for " + proto.name()); }
@Nullable public RoutingPolicy findExportRoutingPolicy(String router, Protocol proto, GraphEdge ge) { Configuration conf = _configurations.get(router); if (proto.isConnected()) { return null; } if (proto.isStatic()) { return null; } if (proto.isOspf()) { OspfProcess p = conf.getDefaultVrf().getOspfProcess(); if (p == null) { return null; } String exp = p.getExportPolicy(); return conf.getRoutingPolicies().get(exp); } if (proto.isBgp()) { BgpPeerConfig n = findBgpNeighbor(ge); // if no neighbor (e.g., loopback), or no export policy if (n == null || n.getExportPolicy() == null) { return null; } return conf.getRoutingPolicies().get(n.getExportPolicy()); } throw new BatfishException("TODO: findExportRoutingPolicy for " + proto.name()); }
/** * Check whether given {@link GeneratedRoute} should be sent to a BGP neighbor. This checks * activation conditions for the generated route, and converts it to a {@link BgpRoute}. No export * policy computation is performed. * * @param generatedRoute route to process * @return a new {@link BgpRoute} if the {@code generatedRoute} was activated. */ @Nullable private BgpRoute processNeighborSpecificGeneratedRoute(@Nonnull GeneratedRoute generatedRoute) { String policyName = generatedRoute.getGenerationPolicy(); RoutingPolicy policy = policyName != null ? _c.getRoutingPolicies().get(policyName) : null; GeneratedRoute.Builder builder = GeneratedRouteHelper.activateGeneratedRoute( generatedRoute, policy, _mainRib.getRoutes(), _vrf.getName()); return builder != null ? BgpProtocolHelper.convertGeneratedRouteToBgp( builder.build(), _vrf.getBgpProcess().getRouterId(), false) : null; }
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); }
@Nullable public RoutingPolicy findImportRoutingPolicy(String router, Protocol proto, GraphEdge ge) { Configuration conf = _configurations.get(router); if (proto.isConnected()) { return null; } if (proto.isStatic()) { return null; } if (proto.isOspf()) { return null; } if (proto.isBgp()) { BgpPeerConfig n = findBgpNeighbor(ge); if (n == null || n.getImportPolicy() == null) { return null; } return conf.getRoutingPolicies().get(n.getImportPolicy()); } throw new BatfishException("TODO: findImportRoutingPolicy: " + proto.name()); }
public void visit( Configuration conf, Statement s, Consumer<Statement> fs, Consumer<BooleanExpr> fe) { fs.accept(s); if (s instanceof If) { If i = (If) s; visit(conf, i.getGuard(), fs, fe); visit(conf, i.getTrueStatements(), fs, fe); visit(conf, i.getFalseStatements(), fs, fe); } else if (s instanceof SetDefaultPolicy) { SetDefaultPolicy p = (SetDefaultPolicy) s; RoutingPolicy rp = conf.getRoutingPolicies().get(p.getDefaultPolicy()); visit(conf, rp.getStatements(), fs, fe); } }
private boolean computeKeepLocalPref() { if (!Optimizations.ENABLE_SLICING_OPTIMIZATION) { return true; } Boolean[] val = new Boolean[1]; val[0] = false; _encoderSlice .getGraph() .getConfigurations() .forEach( (router, conf) -> conf.getRoutingPolicies() .forEach( (name, pol) -> { AstVisitor v = new AstVisitor(); v.visit( conf, pol.getStatements(), stmt -> { if (stmt instanceof SetLocalPreference) { val[0] = true; } }, expr -> {}); })); return val[0]; }
private boolean computeKeepAdminDistance() { if (!Optimizations.ENABLE_SLICING_OPTIMIZATION) { return true; } AstVisitor v = new AstVisitor(); Boolean[] val = new Boolean[1]; val[0] = false; _encoderSlice .getGraph() .getConfigurations() .forEach( (router, conf) -> conf.getRoutingPolicies() .forEach( (name, pol) -> v.visit( conf, pol.getStatements(), stmt -> { if (stmt instanceof SetOspfMetricType) { val[0] = true; } }, expr -> {}))); return val[0]; }
/** * 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; }
@Override public Result execute(Environment environment) { RoutingPolicy policy = environment.getConfiguration().getRoutingPolicies().get(_calledPolicyName); Result result; if (policy == null) { result = new Result(); environment.setError(true); result.setBooleanValue(false); } else { boolean oldCallStatementContext = environment.getCallStatementContext(); environment.setCallStatementContext(true); result = policy.call(environment); result.setReturn(false); environment.setCallStatementContext(oldCallStatementContext); } return result; }
/** * Activate generated routes. * * @return a new {@link RibDelta} if a new route has been activated, otherwise {@code null} */ @VisibleForTesting RibDelta<AbstractRoute> activateGeneratedRoutes() { RibDelta.Builder<AbstractRoute> builder = RibDelta.builder(); /* * Loop over all generated routes and check whether any of the contributing routes can trigger * activation. */ for (GeneratedRoute gr : _vrf.getGeneratedRoutes()) { String policyName = gr.getGenerationPolicy(); RoutingPolicy generationPolicy = policyName != null ? _c.getRoutingPolicies().get(gr.getGenerationPolicy()) : null; GeneratedRoute.Builder grb = GeneratedRouteHelper.activateGeneratedRoute( gr, generationPolicy, _mainRib.getRoutes(), _vrf.getName()); if (grb != null) { GeneratedRoute newGr = grb.build(); // Routes have been changed RibDelta<AbstractRoute> d = _generatedRib.mergeRouteGetDelta(newGr); builder.from(d); } } return builder.build(); }
@Override public Result evaluate(Environment environment) { RoutingPolicy policy = environment.getConfiguration().getRoutingPolicies().get(_calledPolicyName); Result result; if (policy == null) { result = new Result(); environment.setError(true); result.setBooleanValue(false); } else { boolean oldCallExprContext = environment.getCallExprContext(); boolean oldLocalDefaultAction = environment.getLocalDefaultAction(); environment.setCallExprContext(true); result = policy.call(environment); result.setReturn(false); environment.setCallExprContext(oldCallExprContext); environment.setLocalDefaultAction(oldLocalDefaultAction); } return result; }
RoutingPolicy rp = conf.getRoutingPolicies().get(c.getCalledPolicyName()); visit(conf, rp.getStatements(), fs, fe);