Search in sources :

Example 1 with SwitchportEncapsulationType

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

the class JuniperConfiguration method toInterface.

private org.batfish.datamodel.Interface toInterface(Interface iface) {
    String name = iface.getName();
    org.batfish.datamodel.Interface newIface = new org.batfish.datamodel.Interface(name, _c);
    newIface.setDeclaredNames(ImmutableSortedSet.of(name));
    Integer mtu = iface.getMtu();
    if (mtu != null) {
        newIface.setMtu(mtu);
    }
    newIface.setVrrpGroups(iface.getVrrpGroups());
    newIface.setVrf(_c.getVrfs().get(iface.getRoutingInstance()));
    Zone zone = _interfaceZones.get(iface);
    if (zone != null) {
        String zoneName = zone.getName();
        // filter for interface in zone
        FirewallFilter zoneInboundInterfaceFilter = zone.getInboundInterfaceFilters().get(iface);
        if (zoneInboundInterfaceFilter != null) {
            String zoneInboundInterfaceFilterName = zoneInboundInterfaceFilter.getName();
            zoneInboundInterfaceFilter.getReferers().put(iface, "Interface: '" + iface.getName() + "' refers to inbound filter for interface in zone : '" + zoneName + "'");
            IpAccessList zoneInboundInterfaceFilterList = _c.getIpAccessLists().get(zoneInboundInterfaceFilterName);
            newIface.setInboundFilter(zoneInboundInterfaceFilterList);
        } else {
            // filter for zone
            FirewallFilter zoneInboundFilter = zone.getInboundFilter();
            String zoneInboundFilterName = zoneInboundFilter.getName();
            zoneInboundFilter.getReferers().put(iface, "Interface: '" + iface.getName() + "' refers to inbound filter for zone : '" + zoneName + "'");
            IpAccessList zoneInboundFilterList = _c.getIpAccessLists().get(zoneInboundFilterName);
            newIface.setInboundFilter(zoneInboundFilterList);
        }
    }
    String inAclName = iface.getIncomingFilter();
    if (inAclName != null) {
        int inAclLine = iface.getIncomingFilterLine();
        IpAccessList inAcl = _c.getIpAccessLists().get(inAclName);
        if (inAcl == null) {
            undefined(JuniperStructureType.FIREWALL_FILTER, inAclName, JuniperStructureUsage.INTERFACE_INCOMING_FILTER, inAclLine);
        } else {
            FirewallFilter inFilter = _filters.get(inAclName);
            inFilter.getReferers().put(iface, "Incoming ACL for interface: " + iface.getName());
            newIface.setIncomingFilter(inAcl);
            if (inFilter.getRoutingPolicy()) {
                RoutingPolicy routingPolicy = _c.getRoutingPolicies().get(inAclName);
                if (routingPolicy != null) {
                    newIface.setRoutingPolicy(inAclName);
                } else {
                    throw new BatfishException("Expected interface routing-policy to exist");
                }
            }
        }
    }
    String outAclName = iface.getOutgoingFilter();
    if (outAclName != null) {
        int outAclLine = iface.getOutgoingFilterLine();
        IpAccessList outAcl = _c.getIpAccessLists().get(outAclName);
        if (outAcl == null) {
            undefined(JuniperStructureType.FIREWALL_FILTER, outAclName, JuniperStructureUsage.INTERFACE_OUTGOING_FILTER, outAclLine);
        } else {
            _filters.get(outAclName).getReferers().put(iface, "Outgoing ACL for interface: " + iface.getName());
            newIface.setOutgoingFilter(outAcl);
        }
    }
    if (iface.getPrimaryAddress() != null) {
        newIface.setAddress(iface.getPrimaryAddress());
    }
    newIface.setAllAddresses(iface.getAllAddresses());
    newIface.setActive(iface.getActive());
    newIface.setAccessVlan(iface.getAccessVlan());
    newIface.setNativeVlan(iface.getNativeVlan());
    newIface.setSwitchportMode(iface.getSwitchportMode());
    SwitchportEncapsulationType swe = iface.getSwitchportTrunkEncapsulation();
    if (swe == null) {
        swe = SwitchportEncapsulationType.DOT1Q;
    }
    newIface.setSwitchportTrunkEncapsulation(swe);
    newIface.setBandwidth(iface.getBandwidth());
    // isis settings
    IsisInterfaceSettings isisSettings = iface.getIsisSettings();
    IsisInterfaceLevelSettings isisL1Settings = isisSettings.getLevel1Settings();
    newIface.setIsisL1InterfaceMode(IsisInterfaceMode.UNSET);
    if (isisL1Settings.getEnabled()) {
        if (isisSettings.getPassive()) {
            newIface.setIsisL1InterfaceMode(IsisInterfaceMode.PASSIVE);
        } else if (isisSettings.getEnabled()) {
            newIface.setIsisL1InterfaceMode(IsisInterfaceMode.ACTIVE);
        }
    }
    IsisInterfaceLevelSettings isisL2Settings = isisSettings.getLevel2Settings();
    newIface.setIsisL2InterfaceMode(IsisInterfaceMode.UNSET);
    if (isisL2Settings.getEnabled()) {
        if (isisSettings.getPassive()) {
            newIface.setIsisL2InterfaceMode(IsisInterfaceMode.PASSIVE);
        } else if (isisSettings.getEnabled()) {
            newIface.setIsisL2InterfaceMode(IsisInterfaceMode.ACTIVE);
        }
    }
    Integer l1Metric = isisSettings.getLevel1Settings().getMetric();
    Integer l2Metric = isisSettings.getLevel2Settings().getMetric();
    if (l1Metric != null && l2Metric != null && (l1Metric.intValue() != l2Metric.intValue())) {
        _w.unimplemented("distinct metrics for is-is level1 and level2 on an interface");
    } else if (l1Metric != null) {
        newIface.setIsisCost(l1Metric);
    } else if (l2Metric != null) {
        newIface.setIsisCost(l2Metric);
    }
    // TODO: enable/disable individual levels
    return newIface;
}
Also used : BatfishException(org.batfish.common.BatfishException) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) IpAccessList(org.batfish.datamodel.IpAccessList) SwitchportEncapsulationType(org.batfish.datamodel.SwitchportEncapsulationType)

Example 2 with SwitchportEncapsulationType

use of org.batfish.datamodel.SwitchportEncapsulationType 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)

Aggregations

BatfishException (org.batfish.common.BatfishException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 NavigableSet (java.util.NavigableSet)1 Objects (java.util.Objects)1 Set (java.util.Set)1