use of org.batfish.datamodel.BgpProcess in project batfish by batfish.
the class AutoAs method evaluate.
@Override
public int evaluate(Environment environment) {
BgpProcess proc = environment.getVrf().getBgpProcess();
if (proc == null) {
throw new BatfishException("Expected BGP process");
}
Direction direction = environment.getDirection();
int as;
Ip peerAddress = environment.getPeerAddress();
if (peerAddress == null) {
throw new BatfishException("Expected a peer address");
}
Prefix peerPrefix = new Prefix(peerAddress, Prefix.MAX_PREFIX_LENGTH);
BgpNeighbor neighbor = proc.getNeighbors().get(peerPrefix);
if (neighbor == null) {
throw new BatfishException("Expected a peer with address: " + peerAddress);
}
if (direction == Direction.IN) {
as = neighbor.getRemoteAs();
} else if (direction == Direction.OUT) {
as = neighbor.getLocalAs();
} else {
throw new BatfishException("Expected to be applied in a direction");
}
return as;
}
use of org.batfish.datamodel.BgpProcess in project batfish by batfish.
the class Optimizations method computeRouterIdNeeded.
/*
* Check if we need to model the router ID
*/
private void computeRouterIdNeeded() {
_encoderSlice.getGraph().getConfigurations().forEach((router, conf) -> {
// If iBGP is used, and no multipath, then we need the routerId
boolean usesIbgp = false;
for (GraphEdge ge : _encoderSlice.getGraph().getEdgeMap().get(router)) {
if (_encoderSlice.getGraph().getIbgpNeighbors().get(ge) != null) {
usesIbgp = true;
break;
}
}
// If eBGP is used, and no multipath, then we need the routerId
boolean usesEbgp = _encoderSlice.getProtocols().get(router).contains(Protocol.BGP);
// check if multipath is used
boolean ibgpMultipath = false;
boolean ebgpMultipath = false;
BgpProcess p = conf.getDefaultVrf().getBgpProcess();
if (p != null) {
ibgpMultipath = p.getMultipathIbgp();
ebgpMultipath = p.getMultipathEbgp();
}
if ((usesIbgp && !ibgpMultipath) || (usesEbgp && !ebgpMultipath)) {
_needRouterId.add(router);
}
});
}
use of org.batfish.datamodel.BgpProcess in project batfish by batfish.
the class Graph method initIbgpNeighbors.
// TODO: very inefficient
/*
* Initialize iBGP neighbors by looking for nieghbors
* with the same AS number.
*/
private void initIbgpNeighbors() {
Map<String, Ip> ips = new HashMap<>();
Table2<String, String, BgpNeighbor> neighbors = new Table2<>();
// Match iBGP sessions with pairs of routers and BgpNeighbor
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
BgpProcess p = conf.getDefaultVrf().getBgpProcess();
if (p != null) {
for (BgpNeighbor n : p.getNeighbors().values()) {
if (n.getLocalAs().equals(n.getRemoteAs())) {
ips.put(router, n.getLocalIp());
}
}
}
}
for (Entry<String, Configuration> entry : _configurations.entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
BgpProcess p = conf.getDefaultVrf().getBgpProcess();
if (p != null) {
for (Entry<Prefix, BgpNeighbor> entry2 : p.getNeighbors().entrySet()) {
Prefix pfx = entry2.getKey();
BgpNeighbor n = entry2.getValue();
if (n.getLocalAs().equals(n.getRemoteAs())) {
for (Entry<String, Ip> ipEntry : ips.entrySet()) {
String r = ipEntry.getKey();
Ip ip = ipEntry.getValue();
if (!router.equals(r) && pfx.containsIp(ip)) {
neighbors.put(router, r, n);
}
}
}
}
}
}
// Add abstract graph edges for iBGP sessions
Table2<String, String, GraphEdge> reverse = new Table2<>();
neighbors.forEach((r1, r2, n1) -> {
Interface iface1 = createIbgpInterface(n1, r2);
BgpNeighbor n2 = neighbors.get(r2, r1);
GraphEdge ge;
if (n2 != null) {
Interface iface2 = createIbgpInterface(n2, r1);
ge = new GraphEdge(iface1, iface2, r1, r2, true, false);
} else {
ge = new GraphEdge(iface1, null, r1, null, true, false);
}
_allEdges.add(ge);
_ibgpNeighbors.put(ge, n1);
reverse.put(r1, r2, ge);
List<GraphEdge> edges = _edgeMap.get(r1);
if (edges != null) {
edges.add(ge);
} else {
edges = new ArrayList<>();
edges.add(ge);
_edgeMap.put(r1, edges);
}
});
// Add other end to ibgp edges
reverse.forEach((r1, r2, ge1) -> {
GraphEdge ge2 = reverse.get(r2, r1);
_otherEnd.put(ge1, ge2);
});
// Configure Route Reflector information
Integer[] id = new Integer[1];
id[0] = 1;
neighbors.forEach((r1, ns) -> {
if (!_originatorId.containsKey(r1)) {
_originatorId.put(r1, id[0]);
id[0]++;
}
Set<String> clients = new HashSet<>();
ns.forEach((r2, n) -> {
if (n.getRouteReflectorClient()) {
clients.add(r2);
_routeReflectorParent.put(r2, r1);
}
});
_routeReflectorClients.put(r1, clients);
});
}
use of org.batfish.datamodel.BgpProcess in project batfish by batfish.
the class JuniperConfiguration method toVendorIndependentConfiguration.
@Override
public Configuration toVendorIndependentConfiguration() throws VendorConversionException {
String hostname = getHostname();
_c = new Configuration(hostname, _vendor);
_c.setAuthenticationKeyChains(convertAuthenticationKeyChains(_authenticationKeyChains));
_c.setRoles(_roles);
_c.setDnsServers(_dnsServers);
_c.setDomainName(_defaultRoutingInstance.getDomainName());
_c.setLoggingServers(_syslogHosts);
_c.setNtpServers(_ntpServers);
_c.setTacacsServers(_tacplusServers);
_c.getVendorFamily().setJuniper(_jf);
for (String riName : _routingInstances.keySet()) {
_c.getVrfs().put(riName, new Vrf(riName));
}
// convert prefix lists to route filter lists
for (Entry<String, PrefixList> e : _prefixLists.entrySet()) {
String name = e.getKey();
PrefixList pl = e.getValue();
RouteFilterList rfl = new RouteFilterList(name);
for (Prefix prefix : pl.getPrefixes()) {
int prefixLength = prefix.getPrefixLength();
org.batfish.datamodel.RouteFilterLine line = new org.batfish.datamodel.RouteFilterLine(LineAction.ACCEPT, prefix, new SubRange(prefixLength, prefixLength));
rfl.addLine(line);
}
_c.getRouteFilterLists().put(name, rfl);
}
// remove ipv6 lines from firewall filters
for (FirewallFilter filter : _filters.values()) {
Set<String> toRemove = new HashSet<>();
for (Entry<String, FwTerm> e2 : filter.getTerms().entrySet()) {
String termName = e2.getKey();
FwTerm term = e2.getValue();
if (term.getIpv6()) {
toRemove.add(termName);
}
}
for (String termName : toRemove) {
filter.getTerms().remove(termName);
}
}
// remove empty firewall filters (ipv6-only filters)
Map<String, FirewallFilter> allFilters = new LinkedHashMap<>();
allFilters.putAll(_filters);
for (Entry<String, FirewallFilter> e : allFilters.entrySet()) {
String name = e.getKey();
FirewallFilter filter = e.getValue();
if (filter.getTerms().size() == 0) {
_filters.remove(name);
}
}
// convert firewall filters to ipaccesslists
for (Entry<String, FirewallFilter> e : _filters.entrySet()) {
String name = e.getKey();
FirewallFilter filter = e.getValue();
// TODO: support other filter families
if (filter.getFamily() != Family.INET) {
continue;
}
IpAccessList list = toIpAccessList(filter);
_c.getIpAccessLists().put(name, list);
}
// objects
for (Entry<String, FirewallFilter> e : _filters.entrySet()) {
String name = e.getKey();
FirewallFilter filter = e.getValue();
if (filter.getRoutingPolicy()) {
// TODO: support other filter families
if (filter.getFamily() != Family.INET) {
continue;
}
RoutingPolicy routingPolicy = toRoutingPolicy(filter);
_c.getRoutingPolicies().put(name, routingPolicy);
}
}
// convert route filters to route filter lists
for (Entry<String, RouteFilter> e : _routeFilters.entrySet()) {
String name = e.getKey();
RouteFilter rf = e.getValue();
if (rf.getIpv4()) {
RouteFilterList rfl = new RouteFilterList(name);
for (RouteFilterLine line : rf.getLines()) {
if (line.getThens().size() == 0) {
line.applyTo(rfl);
}
}
_c.getRouteFilterLists().put(name, rfl);
}
if (rf.getIpv6()) {
Route6FilterList rfl = new Route6FilterList(name);
for (RouteFilterLine line : rf.getLines()) {
if (line.getThens().size() == 0) {
line.applyTo(rfl);
}
}
_c.getRoute6FilterLists().put(name, rfl);
}
}
// convert community lists
for (Entry<String, CommunityList> e : _communityLists.entrySet()) {
String name = e.getKey();
CommunityList cl = e.getValue();
org.batfish.datamodel.CommunityList newCl = toCommunityList(cl);
_c.getCommunityLists().put(name, newCl);
}
// convert policy-statements to RoutingPolicy objects
for (Entry<String, PolicyStatement> e : _policyStatements.entrySet()) {
String name = e.getKey();
PolicyStatement ps = e.getValue();
RoutingPolicy routingPolicy = toRoutingPolicy(ps);
_c.getRoutingPolicies().put(name, routingPolicy);
}
// convert interfaces
Map<String, Interface> allInterfaces = new LinkedHashMap<>();
for (Interface iface : _interfaces.values()) {
allInterfaces.putAll(iface.getUnits());
}
for (NodeDevice nd : _nodeDevices.values()) {
for (Interface iface : nd.getInterfaces().values()) {
allInterfaces.putAll(iface.getUnits());
}
}
for (Entry<String, Interface> eUnit : allInterfaces.entrySet()) {
String unitName = eUnit.getKey();
Interface unitIface = eUnit.getValue();
unitIface.inheritUnsetFields();
org.batfish.datamodel.Interface newUnitIface = toInterface(unitIface);
_c.getInterfaces().put(unitName, newUnitIface);
Vrf vrf = newUnitIface.getVrf();
String vrfName = vrf.getName();
vrf.getInterfaces().put(unitName, newUnitIface);
_routingInstances.get(vrfName).getInterfaces().put(unitName, unitIface);
}
// set router-id
if (_defaultRoutingInstance.getRouterId() == null) {
Interface loopback0 = _defaultRoutingInstance.getInterfaces().get(FIRST_LOOPBACK_INTERFACE_NAME);
if (loopback0 != null) {
Interface loopback0unit0 = loopback0.getUnits().get(FIRST_LOOPBACK_INTERFACE_NAME + ".0");
if (loopback0unit0 != null) {
InterfaceAddress address = loopback0unit0.getPrimaryAddress();
if (address != null) {
// now we should set router-id
Ip routerId = address.getIp();
_defaultRoutingInstance.setRouterId(routerId);
}
}
}
}
// copy ike proposals
_c.getIkeProposals().putAll(_ikeProposals);
// convert ike policies
for (Entry<String, IkePolicy> e : _ikePolicies.entrySet()) {
String name = e.getKey();
IkePolicy oldIkePolicy = e.getValue();
org.batfish.datamodel.IkePolicy newPolicy = toIkePolicy(oldIkePolicy);
_c.getIkePolicies().put(name, newPolicy);
}
// convert ike gateways
for (Entry<String, IkeGateway> e : _ikeGateways.entrySet()) {
String name = e.getKey();
IkeGateway oldIkeGateway = e.getValue();
org.batfish.datamodel.IkeGateway newIkeGateway = toIkeGateway(oldIkeGateway);
_c.getIkeGateways().put(name, newIkeGateway);
}
// copy ipsec proposals
_c.getIpsecProposals().putAll(_ipsecProposals);
// convert ipsec policies
for (Entry<String, IpsecPolicy> e : _ipsecPolicies.entrySet()) {
String name = e.getKey();
IpsecPolicy oldIpsecPolicy = e.getValue();
org.batfish.datamodel.IpsecPolicy newPolicy = toIpsecPolicy(oldIpsecPolicy);
_c.getIpsecPolicies().put(name, newPolicy);
}
// convert ipsec vpns
for (Entry<String, IpsecVpn> e : _ipsecVpns.entrySet()) {
String name = e.getKey();
IpsecVpn oldIpsecVpn = e.getValue();
org.batfish.datamodel.IpsecVpn newIpsecVpn = toIpsecVpn(oldIpsecVpn);
_c.getIpsecVpns().put(name, newIpsecVpn);
}
// zones
for (Zone zone : _zones.values()) {
org.batfish.datamodel.Zone newZone = toZone(zone);
_c.getZones().put(zone.getName(), newZone);
}
// default zone behavior
_c.setDefaultCrossZoneAction(_defaultCrossZoneAction);
_c.setDefaultInboundAction(_defaultInboundAction);
for (Entry<String, RoutingInstance> e : _routingInstances.entrySet()) {
String riName = e.getKey();
RoutingInstance ri = e.getValue();
Vrf vrf = _c.getVrfs().get(riName);
// dhcp relay
for (Entry<String, DhcpRelayGroup> e2 : ri.getDhcpRelayGroups().entrySet()) {
DhcpRelayGroup rg = e2.getValue();
List<org.batfish.datamodel.Interface> interfaces = new ArrayList<>();
if (rg.getAllInterfaces()) {
interfaces.addAll(_c.getInterfaces().values());
} else {
for (String ifaceName : rg.getInterfaces()) {
org.batfish.datamodel.Interface iface = _c.getInterfaces().get(ifaceName);
interfaces.add(iface);
}
}
String asgName = rg.getActiveServerGroup();
if (asgName != null) {
DhcpRelayServerGroup asg = ri.getDhcpRelayServerGroups().get(asgName);
if (asg == null) {
int asgLine = rg.getActiveServerGroupLine();
undefined(JuniperStructureType.DHCP_RELAY_SERVER_GROUP, asgName, JuniperStructureUsage.DHCP_RELAY_GROUP_ACTIVE_SERVER_GROUP, asgLine);
} else {
for (org.batfish.datamodel.Interface iface : interfaces) {
iface.getDhcpRelayAddresses().addAll(asg.getServers());
}
}
}
}
// snmp
SnmpServer snmpServer = ri.getSnmpServer();
vrf.setSnmpServer(snmpServer);
if (snmpServer != null) {
for (SnmpCommunity community : snmpServer.getCommunities().values()) {
String listName = community.getAccessList();
if (listName != null) {
int listLine = community.getAccessListLine();
PrefixList prefixList = _prefixLists.get(listName);
if (prefixList != null) {
prefixList.getReferers().put(community, "prefix-list for community: " + community.getName());
} else {
undefined(JuniperStructureType.PREFIX_LIST, listName, JuniperStructureUsage.SNMP_COMMUNITY_PREFIX_LIST, listLine);
}
}
}
}
// static routes
for (StaticRoute route : _defaultRoutingInstance.getRibs().get(RoutingInformationBase.RIB_IPV4_UNICAST).getStaticRoutes().values()) {
org.batfish.datamodel.StaticRoute newStaticRoute = toStaticRoute(route);
vrf.getStaticRoutes().add(newStaticRoute);
}
// aggregate routes
for (AggregateRoute route : _defaultRoutingInstance.getRibs().get(RoutingInformationBase.RIB_IPV4_UNICAST).getAggregateRoutes().values()) {
org.batfish.datamodel.GeneratedRoute newAggregateRoute = toAggregateRoute(route);
vrf.getGeneratedRoutes().add(newAggregateRoute);
}
// generated routes
for (GeneratedRoute route : _defaultRoutingInstance.getRibs().get(RoutingInformationBase.RIB_IPV4_UNICAST).getGeneratedRoutes().values()) {
org.batfish.datamodel.GeneratedRoute newGeneratedRoute = toGeneratedRoute(route);
vrf.getGeneratedRoutes().add(newGeneratedRoute);
}
// create ospf process
if (ri.getOspfAreas().size() > 0) {
OspfProcess oproc = createOspfProcess(ri);
vrf.setOspfProcess(oproc);
}
// create is-is process
// is-is runs only if iso address is configured on lo0 unit 0
Interface loopback0 = _defaultRoutingInstance.getInterfaces().get(FIRST_LOOPBACK_INTERFACE_NAME);
if (loopback0 != null) {
Interface loopback0unit0 = loopback0.getUnits().get(FIRST_LOOPBACK_INTERFACE_NAME + ".0");
if (loopback0unit0 != null) {
IsoAddress isisNet = loopback0unit0.getIsoAddress();
if (isisNet != null) {
// now we should create is-is process
IsisProcess proc = createIsisProcess(ri, isisNet);
vrf.setIsisProcess(proc);
}
}
}
// create bgp process
if (ri.getNamedBgpGroups().size() > 0 || ri.getIpBgpGroups().size() > 0) {
BgpProcess proc = createBgpProcess(ri);
vrf.setBgpProcess(proc);
}
}
// mark forwarding table export policy if it exists
String forwardingTableExportPolicyName = _defaultRoutingInstance.getForwardingTableExportPolicy();
if (forwardingTableExportPolicyName != null) {
int forwardingTableExportPolicyLine = _defaultRoutingInstance.getForwardingTableExportPolicyLine();
PolicyStatement forwardingTableExportPolicy = _policyStatements.get(forwardingTableExportPolicyName);
if (forwardingTableExportPolicy != null) {
setPolicyStatementReferent(forwardingTableExportPolicyName, _defaultRoutingInstance, "Forwarding-table export policy");
} else {
undefined(JuniperStructureType.POLICY_STATEMENT, forwardingTableExportPolicyName, JuniperStructureUsage.FORWARDING_TABLE_EXPORT_POLICY, forwardingTableExportPolicyLine);
}
}
// mark references to authentication key chain that may not appear in data model
markAuthenticationKeyChains(JuniperStructureUsage.AUTHENTICATION_KEY_CHAINS_POLICY, _c);
markStructure(JuniperStructureType.FIREWALL_FILTER, JuniperStructureUsage.INTERFACE_FILTER, _filters);
// warn about unreferenced data structures
warnUnreferencedAuthenticationKeyChains();
warnUnreferencedBgpGroups();
warnUnreferencedDhcpRelayServerGroups();
warnUnreferencedPolicyStatements();
warnUnreferencedFirewallFilters();
warnUnreferencedIkeProposals();
warnUnreferencedIkePolicies();
warnUnreferencedIkeGateways();
warnUnreferencedIpsecProposals();
warnUnreferencedIpsecPolicies();
warnUnusedPrefixLists();
warnEmptyPrefixLists();
warnAndDisableUnreferencedStInterfaces();
_c.computeRoutingPolicySources(_w);
return _c;
}
use of org.batfish.datamodel.BgpProcess in project batfish by batfish.
the class BdpDataPlanePluginTest method generateNetworkWithDuplicates.
private SortedMap<String, Configuration> generateNetworkWithDuplicates() {
Ip coreId = new Ip("1.1.1.1");
Ip neighborId = new Ip("1.1.1.2");
final int interfcePrefixBits = 24;
_vb.setName(DEFAULT_VRF_NAME);
Configuration core = _cb.setHostname(CORE_NAME).setConfigurationFormat(ConfigurationFormat.CISCO_IOS).build();
_epb.setStatements(ImmutableList.of(new SetDefaultPolicy("DEF")));
Vrf corevrf = _vb.setOwner(core).build();
_ib.setOwner(core).setVrf(corevrf).setActive(true);
_ib.setAddress(new InterfaceAddress(coreId, interfcePrefixBits)).build();
_ib.setAddress(new InterfaceAddress(new Ip("9.9.9.9"), interfcePrefixBits)).setName("OUT").build();
BgpProcess coreProc = _pb.setRouterId(coreId).setVrf(corevrf).build();
_nb.setOwner(core).setVrf(corevrf).setBgpProcess(coreProc).setRemoteAs(1).setLocalAs(1).setLocalIp(coreId).setPeerAddress(neighborId).setExportPolicy(_epb.setOwner(core).build().getName()).build();
_nb.setRemoteAs(1).setLocalAs(1).setLocalIp(coreId).setPeerAddress(neighborId).build();
Configuration n1 = _cb.setHostname("n1").build();
Vrf n1Vrf = _vb.setOwner(n1).build();
_ib.setOwner(n1).setVrf(n1Vrf);
BgpProcess n1Proc = _pb.setRouterId(neighborId).setVrf(n1Vrf).build();
_nb.setOwner(n1).setVrf(n1Vrf).setBgpProcess(n1Proc).setRemoteAs(1).setLocalAs(1).setLocalIp(neighborId).setPeerAddress(coreId).setExportPolicy(_epb.setOwner(n1).build().getName()).build();
Configuration n2 = _cb.setHostname("n2").build();
Vrf n2Vrf = _vb.setOwner(n2).build();
_ib.setOwner(n2).setVrf(n2Vrf);
_ib.setAddress(new InterfaceAddress(neighborId, interfcePrefixBits)).setVrf(n2Vrf).build();
BgpProcess n2Proc = _pb.setRouterId(neighborId).setVrf(n2Vrf).build();
_nb.setOwner(n2).setVrf(n2Vrf).setBgpProcess(n2Proc).setRemoteAs(1).setLocalAs(1).setLocalIp(neighborId).setPeerAddress(coreId).setExportPolicy(_epb.setOwner(n2).build().getName()).build();
return ImmutableSortedMap.of(CORE_NAME, core, "n1", n1, "n2", n2);
}
Aggregations