use of org.batfish.datamodel.routing_policy.statement.CallStatement in project batfish by batfish.
the class CiscoConfiguration method toRoutingPolicies.
private RoutingPolicy toRoutingPolicies(Configuration c, RouteMap map) {
RoutingPolicy output = new RoutingPolicy(map.getName(), c);
List<Statement> statements = output.getStatements();
Map<Integer, RoutingPolicy> clauses = new HashMap<>();
// descend map so continue targets are available
RoutingPolicy followingClause = null;
Integer followingClauseNumber = null;
for (Entry<Integer, RouteMapClause> e : map.getClauses().descendingMap().entrySet()) {
int clauseNumber = e.getKey();
RouteMapClause rmClause = e.getValue();
String clausePolicyName = getRouteMapClausePolicyName(map, clauseNumber);
Conjunction conj = new Conjunction();
// match ipv4s must be disjoined with match ipv6
Disjunction matchIpOrPrefix = new Disjunction();
for (RouteMapMatchLine rmMatch : rmClause.getMatchList()) {
BooleanExpr matchExpr = rmMatch.toBooleanExpr(c, this, _w);
if (rmMatch instanceof RouteMapMatchIpAccessListLine || rmMatch instanceof RouteMapMatchIpPrefixListLine || rmMatch instanceof RouteMapMatchIpv6AccessListLine || rmMatch instanceof RouteMapMatchIpv6PrefixListLine) {
matchIpOrPrefix.getDisjuncts().add(matchExpr);
} else {
conj.getConjuncts().add(matchExpr);
}
}
if (!matchIpOrPrefix.getDisjuncts().isEmpty()) {
conj.getConjuncts().add(matchIpOrPrefix);
}
RoutingPolicy clausePolicy = new RoutingPolicy(clausePolicyName, c);
c.getRoutingPolicies().put(clausePolicyName, clausePolicy);
If ifStatement = new If();
clausePolicy.getStatements().add(ifStatement);
clauses.put(clauseNumber, clausePolicy);
ifStatement.setComment(clausePolicyName);
ifStatement.setGuard(conj);
List<Statement> onMatchStatements = ifStatement.getTrueStatements();
for (RouteMapSetLine rmSet : rmClause.getSetList()) {
rmSet.applyTo(onMatchStatements, this, c, _w);
}
RouteMapContinue continueStatement = rmClause.getContinueLine();
Integer continueTarget = null;
RoutingPolicy continueTargetPolicy = null;
if (continueStatement != null) {
continueTarget = continueStatement.getTarget();
int statementLine = continueStatement.getStatementLine();
if (continueTarget == null) {
continueTarget = followingClauseNumber;
}
if (continueTarget != null) {
if (continueTarget <= clauseNumber) {
throw new BatfishException("Can only continue to later clause");
}
continueTargetPolicy = clauses.get(continueTarget);
if (continueTargetPolicy == null) {
String name = "clause: '" + continueTarget + "' in route-map: '" + map.getName() + "'";
undefined(CiscoStructureType.ROUTE_MAP_CLAUSE, name, CiscoStructureUsage.ROUTE_MAP_CONTINUE, statementLine);
continueStatement = null;
}
} else {
continueStatement = null;
}
}
switch(rmClause.getAction()) {
case ACCEPT:
if (continueStatement == null) {
onMatchStatements.add(Statements.ReturnTrue.toStaticStatement());
} else {
onMatchStatements.add(Statements.SetLocalDefaultActionAccept.toStaticStatement());
onMatchStatements.add(new CallStatement(continueTargetPolicy.getName()));
}
break;
case REJECT:
onMatchStatements.add(Statements.ReturnFalse.toStaticStatement());
break;
default:
throw new BatfishException("Invalid action");
}
if (followingClause != null) {
ifStatement.getFalseStatements().add(new CallStatement(followingClause.getName()));
} else {
ifStatement.getFalseStatements().add(Statements.ReturnLocalDefaultAction.toStaticStatement());
}
followingClause = clausePolicy;
followingClauseNumber = clauseNumber;
}
statements.add(new CallStatement(followingClause.getName()));
return output;
}
Aggregations