Search in sources :

Example 86 with Prefix

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);
            }
        }
    }
}
Also used : ConnectedRoute(org.batfish.datamodel.ConnectedRoute) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Prefix(org.batfish.datamodel.Prefix) Interface(org.batfish.datamodel.Interface) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 87 with Prefix

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;
}
Also used : AbstractRoute(org.batfish.datamodel.AbstractRoute) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Prefix(org.batfish.datamodel.Prefix) TreeMap(java.util.TreeMap) SortedSet(java.util.SortedSet) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) SortedMap(java.util.SortedMap) AbstractRoute(org.batfish.datamodel.AbstractRoute) Route(org.batfish.datamodel.Route)

Example 88 with Prefix

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;
}
Also used : DefinedStructure(org.batfish.common.util.DefinedStructure) Prefix6Range(org.batfish.datamodel.Prefix6Range) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Arrays(java.util.Arrays) OspfAreaSummary(org.batfish.datamodel.OspfAreaSummary) Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) CommunityListLine(org.batfish.datamodel.CommunityListLine) RouteFilterList(org.batfish.datamodel.RouteFilterList) TunnelMode(org.batfish.representation.cisco.Tunnel.TunnelMode) PrefixSpace(org.batfish.datamodel.PrefixSpace) Matcher(java.util.regex.Matcher) GeneratedRoute6(org.batfish.datamodel.GeneratedRoute6) Ip6AccessList(org.batfish.datamodel.Ip6AccessList) Aaa(org.batfish.datamodel.vendor_family.cisco.Aaa) Map(java.util.Map) CiscoFamily(org.batfish.datamodel.vendor_family.cisco.CiscoFamily) BigInteger(java.math.BigInteger) ConfigurationFormat(org.batfish.datamodel.ConfigurationFormat) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Set(java.util.Set) SelfNextHop(org.batfish.datamodel.routing_policy.expr.SelfNextHop) Cable(org.batfish.datamodel.vendor_family.cisco.Cable) State(org.batfish.datamodel.State) SourceNat(org.batfish.datamodel.SourceNat) MultipathEquivalentAsPathMatchMode(org.batfish.datamodel.MultipathEquivalentAsPathMatchMode) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) Route6FilterList(org.batfish.datamodel.Route6FilterList) NamedPrefixSet(org.batfish.datamodel.routing_policy.expr.NamedPrefixSet) If(org.batfish.datamodel.routing_policy.statement.If) Statements(org.batfish.datamodel.routing_policy.statement.Statements) CommonUtil(org.batfish.common.util.CommonUtil) Ip6AccessListLine(org.batfish.datamodel.Ip6AccessListLine) TreeSet(java.util.TreeSet) SetNextHop(org.batfish.datamodel.routing_policy.statement.SetNextHop) ArrayList(java.util.ArrayList) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) TcpFlags(org.batfish.datamodel.TcpFlags) CommunityList(org.batfish.datamodel.CommunityList) SnmpServer(org.batfish.datamodel.SnmpServer) Ip6(org.batfish.datamodel.Ip6) LineAction(org.batfish.datamodel.LineAction) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) DestinationNetwork6(org.batfish.datamodel.routing_policy.expr.DestinationNetwork6) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) WithEnvironmentExpr(org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) BgpTieBreaker(org.batfish.datamodel.BgpTieBreaker) AaaAuthentication(org.batfish.datamodel.vendor_family.cisco.AaaAuthentication) TreeMap(java.util.TreeMap) AaaAuthenticationLogin(org.batfish.datamodel.vendor_family.cisco.AaaAuthenticationLogin) GeneratedRoute(org.batfish.datamodel.GeneratedRoute) SetMetric(org.batfish.datamodel.routing_policy.statement.SetMetric) IpsecVpn(org.batfish.datamodel.IpsecVpn) IpProtocol(org.batfish.datamodel.IpProtocol) SortedSet(java.util.SortedSet) Not(org.batfish.datamodel.routing_policy.expr.Not) IkePolicy(org.batfish.datamodel.IkePolicy) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) Ip6Wildcard(org.batfish.datamodel.Ip6Wildcard) IsisInterfaceMode(org.batfish.datamodel.IsisInterfaceMode) Prefix6(org.batfish.datamodel.Prefix6) Route6FilterLine(org.batfish.datamodel.Route6FilterLine) MatchPrefix6Set(org.batfish.datamodel.routing_policy.expr.MatchPrefix6Set) AsPathAccessList(org.batfish.datamodel.AsPathAccessList) OspfArea(org.batfish.datamodel.OspfArea) Statement(org.batfish.datamodel.routing_policy.statement.Statement) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Line(org.batfish.datamodel.vendor_family.cisco.Line) NavigableSet(java.util.NavigableSet) OriginType(org.batfish.datamodel.OriginType) Objects(java.util.Objects) List(java.util.List) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Entry(java.util.Map.Entry) BooleanExprs(org.batfish.datamodel.routing_policy.expr.BooleanExprs) Pattern(java.util.regex.Pattern) BgpNeighbor(org.batfish.datamodel.BgpNeighbor) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) SortedMap(java.util.SortedMap) IpWildcard(org.batfish.datamodel.IpWildcard) SwitchportEncapsulationType(org.batfish.datamodel.SwitchportEncapsulationType) Ip(org.batfish.datamodel.Ip) OspfMetricType(org.batfish.datamodel.OspfMetricType) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) RoutingProtocol(org.batfish.datamodel.RoutingProtocol) HashMap(java.util.HashMap) AsPathSetElem(org.batfish.datamodel.routing_policy.expr.AsPathSetElem) BatfishException(org.batfish.common.BatfishException) IpAccessList(org.batfish.datamodel.IpAccessList) SetOrigin(org.batfish.datamodel.routing_policy.statement.SetOrigin) HashSet(java.util.HashSet) IpsecPolicy(org.batfish.datamodel.IpsecPolicy) LiteralOrigin(org.batfish.datamodel.routing_policy.expr.LiteralOrigin) IkeGateway(org.batfish.datamodel.IkeGateway) ImmutableList(com.google.common.collect.ImmutableList) RouteFilterLine(org.batfish.datamodel.RouteFilterLine) SubRange(org.batfish.datamodel.SubRange) Configuration(org.batfish.datamodel.Configuration) AsPathAccessListLine(org.batfish.datamodel.AsPathAccessListLine) ReferenceCountedStructure(org.batfish.common.util.ReferenceCountedStructure) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) PrefixRange(org.batfish.datamodel.PrefixRange) ExplicitPrefix6Set(org.batfish.datamodel.routing_policy.expr.ExplicitPrefix6Set) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Prefix6Space(org.batfish.datamodel.Prefix6Space) Comparator(java.util.Comparator) Collections(java.util.Collections) VendorConversionException(org.batfish.common.VendorConversionException) Prefix(org.batfish.datamodel.Prefix) SetOspfMetricType(org.batfish.datamodel.routing_policy.statement.SetOspfMetricType) Prefix(org.batfish.datamodel.Prefix) ImmutableSet(com.google.common.collect.ImmutableSet) SwitchportEncapsulationType(org.batfish.datamodel.SwitchportEncapsulationType) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) VendorConversionException(org.batfish.common.VendorConversionException) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) Objects(java.util.Objects) IpAccessList(org.batfish.datamodel.IpAccessList)

Example 89 with Prefix

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());
        }
    }
}
Also used : IkeGateway(org.batfish.datamodel.IkeGateway) Ip(org.batfish.datamodel.Ip) Prefix(org.batfish.datamodel.Prefix) IkePolicy(org.batfish.datamodel.IkePolicy)

Example 90 with Prefix

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;
}
Also used : Prefix(org.batfish.datamodel.Prefix) SetMetric(org.batfish.datamodel.routing_policy.statement.SetMetric) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) SubRange(org.batfish.datamodel.SubRange) PrefixRange(org.batfish.datamodel.PrefixRange) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) CallStatement(org.batfish.datamodel.routing_policy.statement.CallStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) PrefixSpace(org.batfish.datamodel.PrefixSpace) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Not(org.batfish.datamodel.routing_policy.expr.Not) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) ExplicitPrefixSet(org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet) LiteralLong(org.batfish.datamodel.routing_policy.expr.LiteralLong) GeneratedRoute(org.batfish.datamodel.GeneratedRoute) If(org.batfish.datamodel.routing_policy.statement.If)

Aggregations

Prefix (org.batfish.datamodel.Prefix)133 Ip (org.batfish.datamodel.Ip)53 Configuration (org.batfish.datamodel.Configuration)33 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)29 Interface (org.batfish.datamodel.Interface)28 BatfishException (org.batfish.common.BatfishException)22 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)20 SubRange (org.batfish.datamodel.SubRange)19 HashMap (java.util.HashMap)18 StaticRoute (org.batfish.datamodel.StaticRoute)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 BgpNeighbor (org.batfish.datamodel.BgpNeighbor)17 BgpProcess (org.batfish.datamodel.BgpProcess)17 SortedSet (java.util.SortedSet)16 TreeSet (java.util.TreeSet)16 AbstractRoute (org.batfish.datamodel.AbstractRoute)16 RoutingProtocol (org.batfish.datamodel.RoutingProtocol)16 TreeMap (java.util.TreeMap)14 HashSet (java.util.HashSet)13