use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class Graph method initStaticRoutes.
/*
* Collect all static routes after inferring which interface they indicate
* should be used for the next-hop.
*/
private void initStaticRoutes() {
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
Map<String, List<StaticRoute>> map = new HashMap<>();
_staticRoutes.put(router, map);
for (StaticRoute sr : conf.getDefaultVrf().getStaticRoutes()) {
boolean someIface = false;
for (GraphEdge ge : _edgeMap.get(router)) {
Interface here = ge.getStart();
Interface there = ge.getEnd();
// Check if next-hop interface is specified
String hereName = here.getName();
someIface = true;
if (hereName.equals(sr.getNextHopInterface())) {
List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
srs.add(sr);
map.put(hereName, srs);
}
// Check if next-hop ip corresponds to direct interface
Ip nhIp = sr.getNextHopIp();
boolean isNextHop = there != null && there.getAddress() != null && there.getAddress().getIp().equals(nhIp);
if (isNextHop) {
someIface = true;
List<StaticRoute> srs = map.computeIfAbsent(hereName, k -> new ArrayList<>());
srs.add(sr);
map.put(here.getName(), srs);
}
}
if (Graph.isNullRouted(sr)) {
List<StaticRoute> nulls = _nullStaticRoutes.computeIfAbsent(router, k -> new ArrayList<>());
nulls.add(sr);
}
if (!someIface && !Graph.isNullRouted(sr)) {
_hasStaticRouteWithDynamicNextHop = true;
}
}
}
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class Graph method findExportRoutingPolicy.
/*
* Find the export routing policy for a given edge
*/
@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()) {
BgpNeighbor 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());
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class AbstractionBuilder method createAbstractConfig.
/*
* Creates a new Configuration from an old one for an abstract router
* by copying the old configuration, but removing any concrete interfaces,
* neighbors etc that do not correpond to any abstract neighbors.
*/
private Configuration createAbstractConfig(Set<String> abstractRouters, Configuration conf) {
Configuration abstractConf = new Configuration(conf.getHostname(), conf.getConfigurationFormat());
abstractConf.setDnsServers(conf.getDnsServers());
abstractConf.setDnsSourceInterface(conf.getDnsSourceInterface());
abstractConf.setDomainName(conf.getDomainName());
abstractConf.setAuthenticationKeyChains(conf.getAuthenticationKeyChains());
abstractConf.setIkeGateways(conf.getIkeGateways());
abstractConf.setDefaultCrossZoneAction(conf.getDefaultCrossZoneAction());
abstractConf.setIkePolicies(conf.getIkePolicies());
abstractConf.setIkeProposals(conf.getIkeProposals());
abstractConf.setDefaultInboundAction(conf.getDefaultInboundAction());
abstractConf.setIpAccessLists(conf.getIpAccessLists());
abstractConf.setIp6AccessLists(conf.getIp6AccessLists());
abstractConf.setRouteFilterLists(conf.getRouteFilterLists());
abstractConf.setRoute6FilterLists(conf.getRoute6FilterLists());
abstractConf.setIpsecPolicies(conf.getIpsecPolicies());
abstractConf.setIpsecProposals(conf.getIpsecProposals());
abstractConf.setIpsecVpns(conf.getIpsecVpns());
abstractConf.setLoggingServers(conf.getLoggingServers());
abstractConf.setLoggingSourceInterface(conf.getLoggingSourceInterface());
abstractConf.setNormalVlanRange(conf.getNormalVlanRange());
abstractConf.setNtpServers(conf.getNtpServers());
abstractConf.setNtpSourceInterface(conf.getNtpSourceInterface());
abstractConf.setRoles(conf.getRoles());
abstractConf.setSnmpSourceInterface(conf.getSnmpSourceInterface());
abstractConf.setSnmpTrapServers(conf.getSnmpTrapServers());
abstractConf.setTacacsServers(conf.getTacacsServers());
abstractConf.setTacacsSourceInterface(conf.getTacacsSourceInterface());
abstractConf.setVendorFamily(conf.getVendorFamily());
abstractConf.setZones(conf.getZones());
abstractConf.setCommunityLists(conf.getCommunityLists());
abstractConf.setRoutingPolicies(conf.getRoutingPolicies());
abstractConf.setRoute6FilterLists(conf.getRoute6FilterLists());
SortedSet<Interface> toRetain = new TreeSet<>();
SortedSet<IpLink> ipNeighbors = new TreeSet<>();
SortedSet<BgpNeighbor> bgpNeighbors = new TreeSet<>();
List<GraphEdge> edges = _graph.getEdgeMap().get(conf.getName());
for (GraphEdge ge : edges) {
boolean leavesNetwork = (ge.getPeer() == null);
if (leavesNetwork || (abstractRouters.contains(ge.getRouter()) && abstractRouters.contains(ge.getPeer()))) {
toRetain.add(ge.getStart());
Ip start = ge.getStart().getAddress().getIp();
if (!leavesNetwork) {
Ip end = ge.getEnd().getAddress().getIp();
ipNeighbors.add(new IpLink(start, end));
}
BgpNeighbor n = _graph.getEbgpNeighbors().get(ge);
if (n != null) {
bgpNeighbors.add(n);
}
}
}
// Update interfaces
NavigableMap<String, Interface> abstractInterfaces = new TreeMap<>();
for (Entry<String, Interface> entry : conf.getInterfaces().entrySet()) {
String name = entry.getKey();
Interface iface = entry.getValue();
if (toRetain.contains(iface)) {
abstractInterfaces.put(name, iface);
}
}
abstractConf.setInterfaces(abstractInterfaces);
// Update VRFs
Map<String, Vrf> abstractVrfs = new HashMap<>();
for (Entry<String, Vrf> entry : conf.getVrfs().entrySet()) {
String name = entry.getKey();
Vrf vrf = entry.getValue();
Vrf abstractVrf = new Vrf(name);
abstractVrf.setStaticRoutes(vrf.getStaticRoutes());
abstractVrf.setIsisProcess(vrf.getIsisProcess());
abstractVrf.setRipProcess(vrf.getRipProcess());
abstractVrf.setSnmpServer(vrf.getSnmpServer());
NavigableMap<String, Interface> abstractVrfInterfaces = new TreeMap<>();
for (Entry<String, Interface> entry2 : vrf.getInterfaces().entrySet()) {
String iname = entry2.getKey();
Interface iface = entry2.getValue();
if (toRetain.contains(iface)) {
abstractVrfInterfaces.put(iname, iface);
}
}
abstractVrf.setInterfaces(abstractVrfInterfaces);
abstractVrf.setInterfaceNames(new TreeSet<>(abstractVrfInterfaces.keySet()));
OspfProcess ospf = vrf.getOspfProcess();
if (ospf != null) {
OspfProcess abstractOspf = new OspfProcess();
abstractOspf.setAreas(ospf.getAreas());
abstractOspf.setExportPolicy(ospf.getExportPolicy());
abstractOspf.setReferenceBandwidth(ospf.getReferenceBandwidth());
abstractOspf.setRouterId(ospf.getRouterId());
// Copy over neighbors
Map<IpLink, OspfNeighbor> abstractNeighbors = new HashMap<>();
if (ospf.getOspfNeighbors() != null) {
for (Entry<IpLink, OspfNeighbor> entry2 : ospf.getOspfNeighbors().entrySet()) {
IpLink link = entry2.getKey();
OspfNeighbor neighbor = entry2.getValue();
if (ipNeighbors.contains(link)) {
abstractNeighbors.put(link, neighbor);
}
}
}
abstractOspf.setOspfNeighbors(abstractNeighbors);
abstractVrf.setOspfProcess(abstractOspf);
}
BgpProcess bgp = vrf.getBgpProcess();
if (bgp != null) {
BgpProcess abstractBgp = new BgpProcess();
abstractBgp.setMultipathEbgp(bgp.getMultipathEbgp());
abstractBgp.setMultipathIbgp(bgp.getMultipathIbgp());
abstractBgp.setRouterId(bgp.getRouterId());
abstractBgp.setOriginationSpace(bgp.getOriginationSpace());
// TODO: set bgp neighbors accordingly
// Copy over neighbors
SortedMap<Prefix, BgpNeighbor> abstractBgpNeighbors = new TreeMap<>();
if (bgp.getNeighbors() != null) {
for (Entry<Prefix, BgpNeighbor> entry2 : bgp.getNeighbors().entrySet()) {
Prefix prefix = entry2.getKey();
BgpNeighbor neighbor = entry2.getValue();
if (bgpNeighbors.contains(neighbor)) {
abstractBgpNeighbors.put(prefix, neighbor);
}
}
}
abstractBgp.setNeighbors(abstractBgpNeighbors);
abstractVrf.setBgpProcess(abstractBgp);
}
abstractVrfs.put(name, abstractVrf);
}
abstractConf.setVrfs(abstractVrfs);
return abstractConf;
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class BatfishCompressor method applyFilters.
/**
* Update RoutingPolicies to filter traffic according to filtersByRouter. This mutates the
* _graph's configurations.
*
* @param filtersByRouter Filters for each router/graph edge.
* @return A new network with the updated configs.
*/
private Map<String, Configuration> applyFilters(Table2<String, GraphEdge, EquivalenceClassFilter> filtersByRouter) {
Map<String, Configuration> newConfigs = new HashMap<>();
for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
String router = entry.getKey();
Map<GraphEdge, EquivalenceClassFilter> filters = filtersByRouter.get(router);
if (filters != null) {
Configuration config = entry.getValue();
// Include this config in the compressed network.
newConfigs.put(router, config);
// Mutate the config by adding import/export filters
for (GraphEdge ge : _graph.getEdgeMap().get(router)) {
EquivalenceClassFilter tup = filters.get(ge);
RoutingPolicy ipol = _graph.findImportRoutingPolicy(router, Protocol.BGP, ge);
if (ipol != null) {
RoutingPolicy newIpol = new RoutingPolicy(ipol.getName(), config);
newIpol.setStatements(applyFilters(ipol.getStatements(), tup));
config.getRoutingPolicies().put(newIpol.getName(), newIpol);
}
RoutingPolicy epol = _graph.findExportRoutingPolicy(router, Protocol.BGP, ge);
if (epol != null) {
RoutingPolicy newEpol = new RoutingPolicy(epol.getName(), config);
newEpol.setStatements(applyFilters(epol.getStatements(), tup));
config.getRoutingPolicies().put(newEpol.getName(), newEpol);
}
}
}
}
return newConfigs;
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class DestinationClasses method buildProtocolMap.
/*
* Initialize a mapping from router to collection of protocol
*/
private Map<String, List<Protocol>> buildProtocolMap() {
// Figure out which protocols are running on which devices
Map<String, List<Protocol>> protocols = new HashMap<>();
for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
List<Protocol> protos = new ArrayList<>();
protocols.put(router, protos);
if (conf.getDefaultVrf().getOspfProcess() != null) {
protos.add(Protocol.OSPF);
}
if (conf.getDefaultVrf().getBgpProcess() != null) {
protos.add(Protocol.BGP);
}
if (!conf.getDefaultVrf().getStaticRoutes().isEmpty()) {
protos.add(Protocol.STATIC);
}
if (!conf.getInterfaces().isEmpty()) {
protos.add(Protocol.CONNECTED);
}
}
return protocols;
}
Aggregations