use of org.batfish.datamodel.Prefix in project batfish by batfish.
the class VirtualRouter method initConnectedRib.
/**
* Initialize the connected RIB -- a RIB containing connected routes (i.e., direct connections to
* neighbors).
*/
@VisibleForTesting
void initConnectedRib() {
// Look at all connected interfaces
for (Interface i : _vrf.getInterfaces().values()) {
if (i.getActive()) {
// Create a route for each interface prefix
for (InterfaceAddress ifaceAddress : i.getAllAddresses()) {
Prefix prefix = ifaceAddress.getPrefix();
ConnectedRoute cr = new ConnectedRoute(prefix, i.getName());
_connectedRib.mergeRoute(cr);
}
}
}
}
use of org.batfish.datamodel.Prefix in project batfish by batfish.
the class BdpEngine method collectOscillatingPrefixes.
private SortedSet<Prefix> collectOscillatingPrefixes(Map<Integer, SortedSet<Route>> iterationRoutes, Map<Integer, SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>>> iterationAbsRoutes, int first, int last) {
SortedSet<Prefix> oscillatingPrefixes = new TreeSet<>();
if (_settings.getBdpDetail()) {
for (int i = first + 1; i <= last; i++) {
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> baseRoutesByHostname = iterationAbsRoutes.get(i - 1);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> deltaRoutesByHostname = iterationAbsRoutes.get(i);
SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>> routesByHostname = new TreeMap<>();
Set<String> hosts = new LinkedHashSet<>();
hosts.addAll(baseRoutesByHostname.keySet());
hosts.addAll(deltaRoutesByHostname.keySet());
for (String hostname : hosts) {
SortedMap<String, SortedSet<AbstractRoute>> routesByVrf = new TreeMap<>();
routesByHostname.put(hostname, routesByVrf);
SortedMap<String, SortedSet<AbstractRoute>> baseRoutesByVrf = baseRoutesByHostname.get(hostname);
SortedMap<String, SortedSet<AbstractRoute>> deltaRoutesByVrf = deltaRoutesByHostname.get(hostname);
if (baseRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : deltaRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> deltaRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute deltaRoute : deltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
}
} else if (deltaRoutesByVrf == null) {
for (Entry<String, SortedSet<AbstractRoute>> e : baseRoutesByVrf.entrySet()) {
String vrfName = e.getKey();
SortedSet<AbstractRoute> baseRoutes = e.getValue();
SortedSet<AbstractRoute> routes = new TreeSet<>();
routesByVrf.put(vrfName, routes);
for (AbstractRoute baseRoute : baseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
}
} else {
Set<String> vrfNames = new LinkedHashSet<>();
vrfNames.addAll(baseRoutesByVrf.keySet());
vrfNames.addAll(deltaRoutesByVrf.keySet());
for (String vrfName : vrfNames) {
SortedSet<AbstractRoute> baseRoutes = baseRoutesByVrf.get(vrfName);
SortedSet<AbstractRoute> deltaRoutes = deltaRoutesByVrf.get(vrfName);
if (baseRoutes == null) {
for (AbstractRoute deltaRoute : deltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
} else if (deltaRoutes == null) {
for (AbstractRoute baseRoute : baseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
} else {
SortedSet<AbstractRoute> prunedBaseRoutes = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet::new);
SortedSet<AbstractRoute> prunedDeltaRoutes = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet::new);
for (AbstractRoute baseRoute : prunedBaseRoutes) {
oscillatingPrefixes.add(baseRoute.getNetwork());
}
for (AbstractRoute deltaRoute : prunedDeltaRoutes) {
oscillatingPrefixes.add(deltaRoute.getNetwork());
}
}
}
}
}
}
} else {
for (int i = first + 1; i <= last; i++) {
SortedSet<Route> baseRoutes = iterationRoutes.get(i - 1);
SortedSet<Route> deltaRoutes = iterationRoutes.get(i);
SortedSet<Route> added = CommonUtil.difference(deltaRoutes, baseRoutes, TreeSet<Route>::new);
SortedSet<Route> removed = CommonUtil.difference(baseRoutes, deltaRoutes, TreeSet<Route>::new);
SortedSet<Route> changed = CommonUtil.union(added, removed, TreeSet<Route>::new);
for (Route route : changed) {
oscillatingPrefixes.add(route.getNetwork());
}
}
}
return oscillatingPrefixes;
}
use of org.batfish.datamodel.Prefix in project batfish by batfish.
the class CiscoConfiguration method toInterface.
private org.batfish.datamodel.Interface toInterface(Interface iface, Map<String, IpAccessList> ipAccessLists, Configuration c) {
String name = iface.getName();
org.batfish.datamodel.Interface newIface = new org.batfish.datamodel.Interface(name, c);
String vrfName = iface.getVrf();
Vrf vrf = _vrfs.computeIfAbsent(vrfName, Vrf::new);
newIface.setDescription(iface.getDescription());
newIface.setActive(iface.getActive());
newIface.setAutoState(iface.getAutoState());
newIface.setVrf(c.getVrfs().get(vrfName));
newIface.setBandwidth(iface.getBandwidth());
if (iface.getDhcpRelayClient()) {
newIface.getDhcpRelayAddresses().addAll(_dhcpRelayServers);
} else {
newIface.getDhcpRelayAddresses().addAll(iface.getDhcpRelayAddresses());
}
newIface.setMtu(getInterfaceMtu(iface));
newIface.setOspfPointToPoint(iface.getOspfPointToPoint());
newIface.setProxyArp(iface.getProxyArp());
newIface.setSpanningTreePortfast(iface.getSpanningTreePortfast());
newIface.setSwitchport(iface.getSwitchport());
newIface.setDeclaredNames(ImmutableSortedSet.copyOf(iface.getDeclaredNames()));
// All prefixes is the combination of the interface prefix + any secondary prefixes.
ImmutableSet.Builder<InterfaceAddress> allPrefixes = ImmutableSet.builder();
if (iface.getAddress() != null) {
newIface.setAddress(iface.getAddress());
allPrefixes.add(iface.getAddress());
}
allPrefixes.addAll(iface.getSecondaryAddresses());
newIface.setAllAddresses(allPrefixes.build());
Long ospfAreaLong = iface.getOspfArea();
if (ospfAreaLong != null) {
OspfProcess proc = vrf.getOspfProcess();
if (proc != null) {
if (iface.getOspfActive()) {
proc.getActiveInterfaceList().add(name);
}
if (iface.getOspfPassive()) {
proc.getPassiveInterfaceList().add(name);
}
for (InterfaceAddress address : newIface.getAllAddresses()) {
Prefix prefix = address.getPrefix();
OspfNetwork ospfNetwork = new OspfNetwork(prefix, ospfAreaLong);
proc.getNetworks().add(ospfNetwork);
}
} else {
_w.redFlag("Interface: '" + name + "' contains OSPF settings, but there is no OSPF process");
}
}
boolean level1 = false;
boolean level2 = false;
IsisProcess isisProcess = vrf.getIsisProcess();
if (isisProcess != null) {
switch(isisProcess.getLevel()) {
case LEVEL_1:
level1 = true;
break;
case LEVEL_1_2:
level1 = true;
level2 = true;
break;
case LEVEL_2:
level2 = true;
break;
default:
throw new VendorConversionException("Invalid IS-IS level");
}
}
if (level1) {
newIface.setIsisL1InterfaceMode(iface.getIsisInterfaceMode());
} else {
newIface.setIsisL1InterfaceMode(IsisInterfaceMode.UNSET);
}
if (level2) {
newIface.setIsisL2InterfaceMode(iface.getIsisInterfaceMode());
} else {
newIface.setIsisL2InterfaceMode(IsisInterfaceMode.UNSET);
}
newIface.setIsisCost(iface.getIsisCost());
newIface.setOspfCost(iface.getOspfCost());
newIface.setOspfDeadInterval(iface.getOspfDeadInterval());
newIface.setOspfHelloMultiplier(iface.getOspfHelloMultiplier());
// switch settings
newIface.setAccessVlan(iface.getAccessVlan());
newIface.setNativeVlan(iface.getNativeVlan());
newIface.setSwitchportMode(iface.getSwitchportMode());
SwitchportEncapsulationType encapsulation = iface.getSwitchportTrunkEncapsulation();
if (encapsulation == null) {
// no encapsulation set, so use default..
// TODO: check if this is OK
encapsulation = SwitchportEncapsulationType.DOT1Q;
}
newIface.setSwitchportTrunkEncapsulation(encapsulation);
newIface.addAllowedRanges(iface.getAllowedVlans());
String incomingFilterName = iface.getIncomingFilter();
if (incomingFilterName != null) {
int incomingFilterLine = iface.getIncomingFilterLine();
IpAccessList incomingFilter = ipAccessLists.get(incomingFilterName);
if (incomingFilter == null) {
undefined(CiscoStructureType.IP_ACCESS_LIST, incomingFilterName, CiscoStructureUsage.INTERFACE_INCOMING_FILTER, incomingFilterLine);
} else {
String msg = "incoming acl for interface: " + iface.getName();
ExtendedAccessList incomingExtendedAccessList = _extendedAccessLists.get(incomingFilterName);
if (incomingExtendedAccessList != null) {
incomingExtendedAccessList.getReferers().put(iface, msg);
}
StandardAccessList incomingStandardAccessList = _standardAccessLists.get(incomingFilterName);
if (incomingStandardAccessList != null) {
incomingStandardAccessList.getReferers().put(iface, msg);
}
}
newIface.setIncomingFilter(incomingFilter);
}
String outgoingFilterName = iface.getOutgoingFilter();
if (outgoingFilterName != null) {
int outgoingFilterLine = iface.getOutgoingFilterLine();
IpAccessList outgoingFilter = ipAccessLists.get(outgoingFilterName);
if (outgoingFilter == null) {
undefined(CiscoStructureType.IP_ACCESS_LIST, outgoingFilterName, CiscoStructureUsage.INTERFACE_OUTGOING_FILTER, outgoingFilterLine);
} else {
String msg = "outgoing acl for interface: " + iface.getName();
ExtendedAccessList outgoingExtendedAccessList = _extendedAccessLists.get(outgoingFilterName);
if (outgoingExtendedAccessList != null) {
outgoingExtendedAccessList.getReferers().put(iface, msg);
}
StandardAccessList outgoingStandardAccessList = _standardAccessLists.get(outgoingFilterName);
if (outgoingStandardAccessList != null) {
outgoingStandardAccessList.getReferers().put(iface, msg);
}
}
newIface.setOutgoingFilter(outgoingFilter);
}
List<CiscoSourceNat> origSourceNats = iface.getSourceNats();
if (origSourceNats != null) {
// Process each of the CiscoSourceNats:
// 1) Collect references to ACLs and NAT pools.
// 2) For valid CiscoSourceNat rules, add them to the newIface source NATs list.
newIface.setSourceNats(origSourceNats.stream().map(nat -> processSourceNat(nat, iface, ipAccessLists)).filter(Objects::nonNull).collect(ImmutableList.toImmutableList()));
}
String routingPolicyName = iface.getRoutingPolicy();
if (routingPolicyName != null) {
int routingPolicyLine = iface.getRoutingPolicyLine();
RouteMap routingPolicyRouteMap = _routeMaps.get(routingPolicyName);
if (routingPolicyRouteMap == null) {
undefined(CiscoStructureType.ROUTE_MAP, routingPolicyName, CiscoStructureUsage.INTERFACE_POLICY_ROUTING_MAP, routingPolicyLine);
} else {
routingPolicyRouteMap.getReferers().put(iface, "routing policy for interface: " + iface.getName());
}
newIface.setRoutingPolicy(routingPolicyName);
}
return newIface;
}
use of org.batfish.datamodel.Prefix in project batfish by batfish.
the class CiscoConfiguration method addIkePoliciesAndGateways.
private void addIkePoliciesAndGateways(Configuration c) {
// get IKE gateways and policies from Cisco isakmp profiles and keyrings
for (Entry<String, IsakmpProfile> e : _isakmpProfiles.entrySet()) {
String name = e.getKey();
IsakmpProfile isakmpProfile = e.getValue();
IkePolicy ikePolicy = new IkePolicy(name);
c.getIkePolicies().put(name, ikePolicy);
ikePolicy.setProposals(c.getIkeProposals());
String keyringName = isakmpProfile.getKeyring();
if (keyringName == null) {
_w.redFlag("Cannot get PSK hash since keyring not configured for isakmpProfile " + name);
} else if (_keyrings.containsKey(keyringName)) {
Keyring keyring = _keyrings.get(keyringName);
if (keyring.match(isakmpProfile.getLocalAddress(), isakmpProfile.getMatchIdentity())) {
ikePolicy.setPreSharedKeyHash(keyring.getKey());
} else {
_w.redFlag("The addresses of keyring " + keyringName + " do not match isakmpProfile " + name);
}
}
Ip localAddress = isakmpProfile.getLocalAddress();
Prefix remotePrefix = isakmpProfile.getMatchIdentity();
if (localAddress == null || remotePrefix == null) {
_w.redFlag("Can't get IkeGateway: Local or remote address is not set for isakmpProfile " + name);
} else {
IkeGateway ikeGateway = new IkeGateway(e.getKey());
c.getIkeGateways().put(name, ikeGateway);
ikeGateway.setAddress(remotePrefix.getStartIp());
Interface oldIface = getInterfaceByTunnelAddresses(localAddress, remotePrefix);
if (oldIface != null) {
ikeGateway.setExternalInterface(c.getInterfaces().get(oldIface.getName()));
} else {
_w.redFlag("External interface not found for ikeGateway for isakmpProfile " + name);
}
ikeGateway.setIkePolicy(ikePolicy);
ikeGateway.setLocalIp(isakmpProfile.getLocalAddress());
}
}
}
use of org.batfish.datamodel.Prefix in project batfish by batfish.
the class CiscoConfiguration method toRipProcess.
private org.batfish.datamodel.RipProcess toRipProcess(RipProcess proc, String vrfName, Configuration c, CiscoConfiguration oldConfig) {
org.batfish.datamodel.RipProcess newProcess = new org.batfish.datamodel.RipProcess();
org.batfish.datamodel.Vrf vrf = c.getVrfs().get(vrfName);
// establish areas and associated interfaces
SortedSet<Prefix> networks = proc.getNetworks();
for (Entry<String, org.batfish.datamodel.Interface> e : vrf.getInterfaces().entrySet()) {
String ifaceName = e.getKey();
org.batfish.datamodel.Interface i = e.getValue();
InterfaceAddress interfaceAddress = i.getAddress();
if (interfaceAddress == null) {
continue;
}
Prefix interfaceNetwork = interfaceAddress.getPrefix();
if (networks.contains(interfaceNetwork)) {
newProcess.getInterfaces().add(ifaceName);
i.setRipEnabled(true);
boolean passive = proc.getPassiveInterfaceList().contains(i.getName()) || (proc.getPassiveInterfaceDefault() && !proc.getActiveInterfaceList().contains(ifaceName));
i.setOspfPassive(passive);
}
}
String ripExportPolicyName = "~RIP_EXPORT_POLICY:" + vrfName + "~";
RoutingPolicy ripExportPolicy = new RoutingPolicy(ripExportPolicyName, c);
c.getRoutingPolicies().put(ripExportPolicyName, ripExportPolicy);
List<Statement> ripExportStatements = ripExportPolicy.getStatements();
newProcess.setExportPolicy(ripExportPolicyName);
// policy map for default information
if (proc.getDefaultInformationOriginate()) {
If ripExportDefault = new If();
ripExportStatements.add(ripExportDefault);
ripExportDefault.setComment("RIP export default route");
Conjunction ripExportDefaultConditions = new Conjunction();
List<Statement> ripExportDefaultStatements = ripExportDefault.getTrueStatements();
ripExportDefaultConditions.getConjuncts().add(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(new PrefixRange(Prefix.ZERO, new SubRange(0, 0)))))));
long metric = proc.getDefaultInformationMetric();
ripExportDefaultStatements.add(new SetMetric(new LiteralLong(metric)));
// add default export map with metric
String defaultOriginateMapName = proc.getDefaultInformationOriginateMap();
if (defaultOriginateMapName != null) {
int defaultOriginateMapLine = proc.getDefaultInformationOriginateMapLine();
RoutingPolicy ripDefaultGenerationPolicy = c.getRoutingPolicies().get(defaultOriginateMapName);
if (ripDefaultGenerationPolicy == null) {
undefined(CiscoStructureType.ROUTE_MAP, defaultOriginateMapName, CiscoStructureUsage.RIP_DEFAULT_ORIGINATE_ROUTE_MAP, defaultOriginateMapLine);
} else {
RouteMap generationRouteMap = _routeMaps.get(defaultOriginateMapName);
generationRouteMap.getReferers().put(proc, "rip default-originate route-map");
GeneratedRoute.Builder route = new GeneratedRoute.Builder();
route.setNetwork(Prefix.ZERO);
route.setAdmin(MAX_ADMINISTRATIVE_COST);
route.setGenerationPolicy(defaultOriginateMapName);
newProcess.getGeneratedRoutes().add(route.build());
}
} else {
// add generated aggregate with no precondition
GeneratedRoute.Builder route = new GeneratedRoute.Builder();
route.setNetwork(Prefix.ZERO);
route.setAdmin(MAX_ADMINISTRATIVE_COST);
newProcess.getGeneratedRoutes().add(route.build());
}
ripExportDefaultConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.AGGREGATE));
ripExportDefaultStatements.add(Statements.ExitAccept.toStaticStatement());
ripExportDefault.setGuard(ripExportDefaultConditions);
}
// policy for redistributing connected routes
RipRedistributionPolicy rcp = proc.getRedistributionPolicies().get(RoutingProtocol.CONNECTED);
if (rcp != null) {
If ripExportConnected = new If();
ripExportConnected.setComment("RIP export connected routes");
Conjunction ripExportConnectedConditions = new Conjunction();
ripExportConnectedConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.CONNECTED));
List<Statement> ripExportConnectedStatements = ripExportConnected.getTrueStatements();
Long metric = rcp.getMetric();
boolean explicitMetric = metric != null;
if (!explicitMetric) {
metric = RipRedistributionPolicy.DEFAULT_REDISTRIBUTE_CONNECTED_METRIC;
}
ripExportStatements.add(new SetMetric(new LiteralLong(metric)));
ripExportStatements.add(ripExportConnected);
// add default export map with metric
String exportConnectedRouteMapName = rcp.getRouteMap();
if (exportConnectedRouteMapName != null) {
int exportConnectedRouteMapLine = rcp.getRouteMapLine();
RouteMap exportConnectedRouteMap = _routeMaps.get(exportConnectedRouteMapName);
if (exportConnectedRouteMap == null) {
undefined(CiscoStructureType.ROUTE_MAP, exportConnectedRouteMapName, CiscoStructureUsage.RIP_REDISTRIBUTE_CONNECTED_MAP, exportConnectedRouteMapLine);
} else {
exportConnectedRouteMap.getReferers().put(proc, "rip redistribute connected route-map");
ripExportConnectedConditions.getConjuncts().add(new CallExpr(exportConnectedRouteMapName));
}
}
ripExportConnectedStatements.add(Statements.ExitAccept.toStaticStatement());
ripExportConnected.setGuard(ripExportConnectedConditions);
}
// policy map for redistributing static routes
RipRedistributionPolicy rsp = proc.getRedistributionPolicies().get(RoutingProtocol.STATIC);
if (rsp != null) {
If ripExportStatic = new If();
ripExportStatic.setComment("RIP export static routes");
Conjunction ripExportStaticConditions = new Conjunction();
ripExportStaticConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.STATIC));
List<Statement> ripExportStaticStatements = ripExportStatic.getTrueStatements();
ripExportStaticConditions.getConjuncts().add(new Not(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(new PrefixRange(Prefix.ZERO, new SubRange(0, 0))))))));
Long metric = rsp.getMetric();
boolean explicitMetric = metric != null;
if (!explicitMetric) {
metric = RipRedistributionPolicy.DEFAULT_REDISTRIBUTE_STATIC_METRIC;
}
ripExportStatements.add(new SetMetric(new LiteralLong(metric)));
ripExportStatements.add(ripExportStatic);
// add export map with metric
String exportStaticRouteMapName = rsp.getRouteMap();
if (exportStaticRouteMapName != null) {
int exportStaticRouteMapLine = rsp.getRouteMapLine();
RouteMap exportStaticRouteMap = _routeMaps.get(exportStaticRouteMapName);
if (exportStaticRouteMap == null) {
undefined(CiscoStructureType.ROUTE_MAP, exportStaticRouteMapName, CiscoStructureUsage.RIP_REDISTRIBUTE_STATIC_MAP, exportStaticRouteMapLine);
} else {
exportStaticRouteMap.getReferers().put(proc, "rip redistribute static route-map");
ripExportStaticConditions.getConjuncts().add(new CallExpr(exportStaticRouteMapName));
}
}
ripExportStaticStatements.add(Statements.ExitAccept.toStaticStatement());
ripExportStatic.setGuard(ripExportStaticConditions);
}
// policy map for redistributing bgp routes
RipRedistributionPolicy rbp = proc.getRedistributionPolicies().get(RoutingProtocol.BGP);
if (rbp != null) {
If ripExportBgp = new If();
ripExportBgp.setComment("RIP export bgp routes");
Conjunction ripExportBgpConditions = new Conjunction();
ripExportBgpConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.BGP));
List<Statement> ripExportBgpStatements = ripExportBgp.getTrueStatements();
ripExportBgpConditions.getConjuncts().add(new Not(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(new PrefixRange(Prefix.ZERO, new SubRange(0, 0))))))));
Long metric = rbp.getMetric();
boolean explicitMetric = metric != null;
if (!explicitMetric) {
metric = RipRedistributionPolicy.DEFAULT_REDISTRIBUTE_BGP_METRIC;
}
ripExportStatements.add(new SetMetric(new LiteralLong(metric)));
ripExportStatements.add(ripExportBgp);
// add export map with metric
String exportBgpRouteMapName = rbp.getRouteMap();
if (exportBgpRouteMapName != null) {
int exportBgpRouteMapLine = rbp.getRouteMapLine();
RouteMap exportBgpRouteMap = _routeMaps.get(exportBgpRouteMapName);
if (exportBgpRouteMap == null) {
undefined(CiscoStructureType.ROUTE_MAP, exportBgpRouteMapName, CiscoStructureUsage.RIP_REDISTRIBUTE_BGP_MAP, exportBgpRouteMapLine);
} else {
exportBgpRouteMap.getReferers().put(proc, "rip redistribute bgp route-map");
ripExportBgpConditions.getConjuncts().add(new CallExpr(exportBgpRouteMapName));
}
}
ripExportBgpStatements.add(Statements.ExitAccept.toStaticStatement());
ripExportBgp.setGuard(ripExportBgpConditions);
}
return newProcess;
}
Aggregations