Search in sources :

Example 1 with OspfNeighbor

use of org.batfish.datamodel.OspfNeighbor 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;
}
Also used : IpLink(org.batfish.datamodel.IpLink) Configuration(org.batfish.datamodel.Configuration) HashMap(java.util.HashMap) BgpProcess(org.batfish.datamodel.BgpProcess) Ip(org.batfish.datamodel.Ip) Vrf(org.batfish.datamodel.Vrf) Prefix(org.batfish.datamodel.Prefix) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) TreeSet(java.util.TreeSet) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) OspfProcess(org.batfish.datamodel.OspfProcess) TreeMap(java.util.TreeMap) GraphEdge(org.batfish.symbolic.GraphEdge) Interface(org.batfish.datamodel.Interface)

Example 2 with OspfNeighbor

use of org.batfish.datamodel.OspfNeighbor in project batfish by batfish.

the class OspfTest method testOspfNonDefaultVrfAdjacency.

@Test
public void testOspfNonDefaultVrfAdjacency() {
    OspfArea.Builder oab = _nf.ospfAreaBuilder().setNumber(1L);
    Prefix adjacencyPrefix = Prefix.parse("1.0.0.0/31");
    InterfaceAddress i1Address = new InterfaceAddress(adjacencyPrefix.getStartIp(), adjacencyPrefix.getPrefixLength());
    InterfaceAddress i2Address = new InterfaceAddress(adjacencyPrefix.getEndIp(), adjacencyPrefix.getPrefixLength());
    Configuration c1 = _cb.build();
    Vrf v1 = _vb.setOwner(c1).build();
    OspfProcess o1 = _opb.setVrf(v1).build();
    OspfArea oa1 = oab.setOspfProcess(o1).build();
    // Interface in default VRF on c1
    _ib.setOwner(c1).setVrf(v1).setAddress(i1Address).setOspfArea(oa1).build();
    Configuration c2 = _cb.build();
    // default vrf
    Vrf v2Default = _vb.setOwner(c2).build();
    // default OSPF process
    _opb.setVrf(v2Default).build();
    Vrf v2Other = _vb.setName("v2Other").build();
    OspfProcess o2Other = _opb.setVrf(v2Other).build();
    OspfArea oa2 = oab.setOspfProcess(o2Other).build();
    // Interface in VRF v2Other on c2
    _ib.setOwner(c2).setVrf(v2Other).setAddress(i2Address).setOspfArea(oa2).build();
    Map<String, Configuration> configurations = ImmutableMap.of(c1.getName(), c1, c2.getName(), c2);
    Map<Ip, Set<String>> ipOwners = CommonUtil.computeIpOwners(configurations, true);
    Topology topology = CommonUtil.synthesizeTopology(configurations);
    CommonUtil.initRemoteOspfNeighbors(configurations, ipOwners, topology);
    IpLink expectedIpEdge1 = new IpLink(i1Address.getIp(), i2Address.getIp());
    IpLink expectedIpEdge2 = new IpLink(i2Address.getIp(), i1Address.getIp());
    assertThat(o1, hasOspfNeighbors(hasKey(expectedIpEdge1)));
    OspfNeighbor on1 = o1.getOspfNeighbors().get(expectedIpEdge1);
    assertThat(o2Other, hasOspfNeighbors(hasKey(expectedIpEdge2)));
    OspfNeighbor on2 = o2Other.getOspfNeighbors().get(expectedIpEdge2);
    assertThat(on1, hasRemoteOspfNeighbor(sameInstance(on2)));
    assertThat(on2, hasRemoteOspfNeighbor(sameInstance(on1)));
}
Also used : IpLink(org.batfish.datamodel.IpLink) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) OspfNeighborMatchers.hasRemoteOspfNeighbor(org.batfish.datamodel.matchers.OspfNeighborMatchers.hasRemoteOspfNeighbor) OspfArea(org.batfish.datamodel.OspfArea) SortedSet(java.util.SortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) Configuration(org.batfish.datamodel.Configuration) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip(org.batfish.datamodel.Ip) OspfProcess(org.batfish.datamodel.OspfProcess) AbstractRouteMatchers.hasPrefix(org.batfish.datamodel.matchers.AbstractRouteMatchers.hasPrefix) Prefix(org.batfish.datamodel.Prefix) Vrf(org.batfish.datamodel.Vrf) Topology(org.batfish.datamodel.Topology) Test(org.junit.Test)

Example 3 with OspfNeighbor

use of org.batfish.datamodel.OspfNeighbor in project batfish by batfish.

the class CommonUtil method initRemoteOspfNeighbors.

public static void initRemoteOspfNeighbors(Map<String, Configuration> configurations, Map<Ip, Set<String>> ipOwners, Topology topology) {
    for (Entry<String, Configuration> e : configurations.entrySet()) {
        String hostname = e.getKey();
        Configuration c = e.getValue();
        for (Entry<String, Vrf> e2 : c.getVrfs().entrySet()) {
            Vrf vrf = e2.getValue();
            OspfProcess proc = vrf.getOspfProcess();
            if (proc != null) {
                proc.setOspfNeighbors(new TreeMap<>());
                String vrfName = e2.getKey();
                for (Entry<Long, OspfArea> e3 : proc.getAreas().entrySet()) {
                    long areaNum = e3.getKey();
                    OspfArea area = e3.getValue();
                    for (String ifaceName : area.getInterfaces()) {
                        Interface iface = c.getInterfaces().get(ifaceName);
                        if (iface.getOspfPassive()) {
                            continue;
                        }
                        SortedSet<Edge> ifaceEdges = topology.getInterfaceEdges().get(new NodeInterfacePair(hostname, ifaceName));
                        boolean hasNeighbor = false;
                        Ip localIp = iface.getAddress().getIp();
                        if (ifaceEdges != null) {
                            for (Edge edge : ifaceEdges) {
                                if (edge.getNode1().equals(hostname)) {
                                    String remoteHostname = edge.getNode2();
                                    String remoteIfaceName = edge.getInt2();
                                    Configuration remoteNode = configurations.get(remoteHostname);
                                    Interface remoteIface = remoteNode.getInterfaces().get(remoteIfaceName);
                                    if (remoteIface.getOspfPassive()) {
                                        continue;
                                    }
                                    Vrf remoteVrf = remoteIface.getVrf();
                                    String remoteVrfName = remoteVrf.getName();
                                    OspfProcess remoteProc = remoteVrf.getOspfProcess();
                                    if (remoteProc != null) {
                                        if (remoteProc.getOspfNeighbors() == null) {
                                            remoteProc.setOspfNeighbors(new TreeMap<>());
                                        }
                                        OspfArea remoteArea = remoteProc.getAreas().get(areaNum);
                                        if (remoteArea != null && remoteArea.getInterfaces().contains(remoteIfaceName)) {
                                            Ip remoteIp = remoteIface.getAddress().getIp();
                                            IpLink localKey = new IpLink(localIp, remoteIp);
                                            OspfNeighbor neighbor = proc.getOspfNeighbors().get(localKey);
                                            if (neighbor == null) {
                                                hasNeighbor = true;
                                                // initialize local neighbor
                                                neighbor = new OspfNeighbor(localKey);
                                                neighbor.setArea(areaNum);
                                                neighbor.setVrf(vrfName);
                                                neighbor.setOwner(c);
                                                neighbor.setInterface(iface);
                                                proc.getOspfNeighbors().put(localKey, neighbor);
                                                // initialize remote neighbor
                                                IpLink remoteKey = new IpLink(remoteIp, localIp);
                                                OspfNeighbor remoteNeighbor = new OspfNeighbor(remoteKey);
                                                remoteNeighbor.setArea(areaNum);
                                                remoteNeighbor.setVrf(remoteVrfName);
                                                remoteNeighbor.setOwner(remoteNode);
                                                remoteNeighbor.setInterface(remoteIface);
                                                remoteProc.getOspfNeighbors().put(remoteKey, remoteNeighbor);
                                                // link neighbors
                                                neighbor.setRemoteOspfNeighbor(remoteNeighbor);
                                                remoteNeighbor.setRemoteOspfNeighbor(neighbor);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (!hasNeighbor) {
                            IpLink key = new IpLink(localIp, Ip.ZERO);
                            OspfNeighbor neighbor = new OspfNeighbor(key);
                            neighbor.setArea(areaNum);
                            neighbor.setVrf(vrfName);
                            neighbor.setOwner(c);
                            neighbor.setInterface(iface);
                            proc.getOspfNeighbors().put(key, neighbor);
                        }
                    }
                }
            }
        }
    }
}
Also used : IpLink(org.batfish.datamodel.IpLink) OspfNeighbor(org.batfish.datamodel.OspfNeighbor) OspfArea(org.batfish.datamodel.OspfArea) Configuration(org.batfish.datamodel.Configuration) NodeInterfacePair(org.batfish.datamodel.collections.NodeInterfacePair) Ip(org.batfish.datamodel.Ip) OspfProcess(org.batfish.datamodel.OspfProcess) Vrf(org.batfish.datamodel.Vrf) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface)

Aggregations

Configuration (org.batfish.datamodel.Configuration)3 Ip (org.batfish.datamodel.Ip)3 IpLink (org.batfish.datamodel.IpLink)3 OspfNeighbor (org.batfish.datamodel.OspfNeighbor)3 OspfProcess (org.batfish.datamodel.OspfProcess)3 Vrf (org.batfish.datamodel.Vrf)3 Interface (org.batfish.datamodel.Interface)2 OspfArea (org.batfish.datamodel.OspfArea)2 Prefix (org.batfish.datamodel.Prefix)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 SortedSet (java.util.SortedSet)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)1 BgpProcess (org.batfish.datamodel.BgpProcess)1 Edge (org.batfish.datamodel.Edge)1 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)1 Topology (org.batfish.datamodel.Topology)1