<commit_before>package batfish.grammar.cisco.controlplane;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import batfish.grammar.BatfishCombinedParser;
import batfish.grammar.ControlPlaneExtractor;
import batfish.grammar.ParseTreePrettyPrinter;
import batfish.grammar.cisco.CiscoParser.*;
import batfish.grammar.cisco.*;
import batfish.grammar.cisco.CiscoParser.As_path_regex_rangeContext;
import batfish.main.BatfishException;
import batfish.main.PedanticBatfishException;
import batfish.representation.Ip;
import batfish.representation.IpProtocol;
import batfish.representation.LineAction;
import batfish.representation.OriginType;
import batfish.representation.OspfMetricType;
import batfish.representation.Prefix;
import batfish.representation.RoutingProtocol;
import batfish.representation.SwitchportEncapsulationType;
import batfish.representation.SwitchportMode;
import batfish.representation.VendorConfiguration;
import batfish.representation.cisco.BgpAggregateNetwork;
import batfish.representation.cisco.BgpPeerGroup;
import batfish.representation.cisco.BgpProcess;
import batfish.representation.cisco.BgpRedistributionPolicy;
import batfish.representation.cisco.CiscoConfiguration;
import batfish.representation.cisco.CiscoVendorConfiguration;
import batfish.representation.cisco.DynamicBgpPeerGroup;
import batfish.representation.cisco.ExpandedCommunityList;
import batfish.representation.cisco.ExpandedCommunityListLine;
import batfish.representation.cisco.ExtendedAccessList;
import batfish.representation.cisco.ExtendedAccessListLine;
import batfish.representation.cisco.Interface;
import batfish.representation.cisco.IpAsPathAccessList;
import batfish.representation.cisco.IpAsPathAccessListLine;
import batfish.representation.cisco.IpBgpPeerGroup;
import batfish.representation.cisco.Ipv6BgpPeerGroup;
import batfish.representation.cisco.MasterBgpPeerGroup;
import batfish.representation.cisco.NamedBgpPeerGroup;
import batfish.representation.cisco.OspfProcess;
import batfish.representation.cisco.OspfRedistributionPolicy;
import batfish.representation.cisco.OspfWildcardNetwork;
import batfish.representation.cisco.PrefixList;
import batfish.representation.cisco.PrefixListLine;
import batfish.representation.cisco.RouteMap;
import batfish.representation.cisco.RouteMapClause;
import batfish.representation.cisco.RouteMapMatchAsPathAccessListLine;
import batfish.representation.cisco.RouteMapMatchCommunityListLine;
import batfish.representation.cisco.RouteMapMatchIpAccessListLine;
import batfish.representation.cisco.RouteMapMatchIpPrefixListLine;
import batfish.representation.cisco.RouteMapMatchTagLine;
import batfish.representation.cisco.RouteMapSetAdditiveCommunityLine;
import batfish.representation.cisco.RouteMapSetAsPathPrependLine;
import batfish.representation.cisco.RouteMapSetCommunityLine;
import batfish.representation.cisco.RouteMapSetCommunityNoneLine;
import batfish.representation.cisco.RouteMapSetDeleteCommunityLine;
import batfish.representation.cisco.RouteMapSetLine;
import batfish.representation.cisco.RouteMapSetLocalPreferenceLine;
import batfish.representation.cisco.RouteMapSetMetricLine;
import batfish.representation.cisco.RouteMapSetNextHopLine;
import batfish.representation.cisco.RouteMapSetOriginTypeLine;
import batfish.representation.cisco.StandardAccessList;
import batfish.representation.cisco.StandardAccessListLine;
import batfish.representation.cisco.StandardCommunityList;
import batfish.representation.cisco.StaticRoute;
import batfish.util.SubRange;
import batfish.util.Util;

public class CiscoControlPlaneExtractor extends CiscoParserBaseListener
      implements ControlPlaneExtractor {

   private static final Map<String, String> CISCO_INTERFACE_PREFIXES = getCiscoInterfacePrefixes();

   private static final int DEFAULT_STATIC_ROUTE_DISTANCE = 1;

   private static final String NXOS_MANAGEMENT_INTERFACE_PREFIX = "mgmt";

   public static LineAction getAccessListAction(Access_list_actionContext ctx) {
      if (ctx.PERMIT() != null) {
         return LineAction.ACCEPT;
      }
      else if (ctx.DENY() != null) {
         return LineAction.REJECT;
      }
      else {
         throw new BatfishException("bad LineAction");
      }
   }

   private static String getCanonicalInterfaceNamePrefix(String prefix) {
      for (Entry<String, String> e : CISCO_INTERFACE_PREFIXES.entrySet()) {
         String matchPrefix = e.getKey();
         String canonicalPrefix = e.getValue();
         if (matchPrefix.toLowerCase().startsWith(prefix.toLowerCase())) {
            return canonicalPrefix;
         }
      }
      throw new BatfishException("Invalid interface name prefix: \"" + prefix
            + "\"");
   }

   private static Map<String, String> getCiscoInterfacePrefixes() {
      Map<String, String> prefixes = new LinkedHashMap<String, String>();
      prefixes.put("ATM", "ATM");
      prefixes.put("cmp-mgmt", "cmp-mgmt");
      prefixes.put("Embedded-Service-Engine", "Embedded-Service-Engine");
      prefixes.put("Ethernet", "Ethernet");
      prefixes.put("FastEthernet", "FastEthernet");
      prefixes.put("fe", "FastEthernet");
      prefixes.put("GigabitEthernet", "GigabitEthernet");
      prefixes.put("ge", "GigabitEthernet");
      prefixes.put("GMPLS", "GMPLS");
      prefixes.put("Loopback", "Loopback");
      prefixes.put("Management", "Management");
      prefixes.put("mgmt", NXOS_MANAGEMENT_INTERFACE_PREFIX);
      prefixes.put("Null", "Null");
      prefixes.put("Port-channel", "Port-channel");
      prefixes.put("Serial", "Serial");
      prefixes.put("TenGigabitEthernet", "TenGigabitEthernet");
      prefixes.put("te", "TenGigabitEthernet");
      prefixes.put("Tunnel", "Tunnel");
      prefixes.put("Vlan", "Vlan");
      return prefixes;
   }

   public static Ip getIp(Access_list_ip_rangeContext ctx) {
      if (ctx.ip != null) {
         return toIp(ctx.ip);<commit_msg>cisco add Async interface prefix<commit_after>package batfish.grammar.cisco.controlplane;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import batfish.grammar.BatfishCombinedParser;
import batfish.grammar.ControlPlaneExtractor;
import batfish.grammar.ParseTreePrettyPrinter;
import batfish.grammar.cisco.CiscoParser.*;
import batfish.grammar.cisco.*;
import batfish.grammar.cisco.CiscoParser.As_path_regex_rangeContext;
import batfish.main.BatfishException;
import batfish.main.PedanticBatfishException;
import batfish.representation.Ip;
import batfish.representation.IpProtocol;
import batfish.representation.LineAction;
import batfish.representation.OriginType;
import batfish.representation.OspfMetricType;
import batfish.representation.Prefix;
import batfish.representation.RoutingProtocol;
import batfish.representation.SwitchportEncapsulationType;
import batfish.representation.SwitchportMode;
import batfish.representation.VendorConfiguration;
import batfish.representation.cisco.BgpAggregateNetwork;
import batfish.representation.cisco.BgpPeerGroup;
import batfish.representation.cisco.BgpProcess;
import batfish.representation.cisco.BgpRedistributionPolicy;
import batfish.representation.cisco.CiscoConfiguration;
import batfish.representation.cisco.CiscoVendorConfiguration;
import batfish.representation.cisco.DynamicBgpPeerGroup;
import batfish.representation.cisco.ExpandedCommunityList;
import batfish.representation.cisco.ExpandedCommunityListLine;
import batfish.representation.cisco.ExtendedAccessList;
import batfish.representation.cisco.ExtendedAccessListLine;
import batfish.representation.cisco.Interface;
import batfish.representation.cisco.IpAsPathAccessList;
import batfish.representation.cisco.IpAsPathAccessListLine;
import batfish.representation.cisco.IpBgpPeerGroup;
import batfish.representation.cisco.Ipv6BgpPeerGroup;
import batfish.representation.cisco.MasterBgpPeerGroup;
import batfish.representation.cisco.NamedBgpPeerGroup;
import batfish.representation.cisco.OspfProcess;
import batfish.representation.cisco.OspfRedistributionPolicy;
import batfish.representation.cisco.OspfWildcardNetwork;
import batfish.representation.cisco.PrefixList;
import batfish.representation.cisco.PrefixListLine;
import batfish.representation.cisco.RouteMap;
import batfish.representation.cisco.RouteMapClause;
import batfish.representation.cisco.RouteMapMatchAsPathAccessListLine;
import batfish.representation.cisco.RouteMapMatchCommunityListLine;
import batfish.representation.cisco.RouteMapMatchIpAccessListLine;
import batfish.representation.cisco.RouteMapMatchIpPrefixListLine;
import batfish.representation.cisco.RouteMapMatchTagLine;
import batfish.representation.cisco.RouteMapSetAdditiveCommunityLine;
import batfish.representation.cisco.RouteMapSetAsPathPrependLine;
import batfish.representation.cisco.RouteMapSetCommunityLine;
import batfish.representation.cisco.RouteMapSetCommunityNoneLine;
import batfish.representation.cisco.RouteMapSetDeleteCommunityLine;
import batfish.representation.cisco.RouteMapSetLine;
import batfish.representation.cisco.RouteMapSetLocalPreferenceLine;
import batfish.representation.cisco.RouteMapSetMetricLine;
import batfish.representation.cisco.RouteMapSetNextHopLine;
import batfish.representation.cisco.RouteMapSetOriginTypeLine;
import batfish.representation.cisco.StandardAccessList;
import batfish.representation.cisco.StandardAccessListLine;
import batfish.representation.cisco.StandardCommunityList;
import batfish.representation.cisco.StaticRoute;
import batfish.util.SubRange;
import batfish.util.Util;

public class CiscoControlPlaneExtractor extends CiscoParserBaseListener
      implements ControlPlaneExtractor {

   private static final Map<String, String> CISCO_INTERFACE_PREFIXES = getCiscoInterfacePrefixes();

   private static final int DEFAULT_STATIC_ROUTE_DISTANCE = 1;

   private static final String NXOS_MANAGEMENT_INTERFACE_PREFIX = "mgmt";

   public static LineAction getAccessListAction(Access_list_actionContext ctx) {
      if (ctx.PERMIT() != null) {
         return LineAction.ACCEPT;
      }
      else if (ctx.DENY() != null) {
         return LineAction.REJECT;
      }
      else {
         throw new BatfishException("bad LineAction");
      }
   }

   private static String getCanonicalInterfaceNamePrefix(String prefix) {
      for (Entry<String, String> e : CISCO_INTERFACE_PREFIXES.entrySet()) {
         String matchPrefix = e.getKey();
         String canonicalPrefix = e.getValue();
         if (matchPrefix.toLowerCase().startsWith(prefix.toLowerCase())) {
            return canonicalPrefix;
         }
      }
      throw new BatfishException("Invalid interface name prefix: \"" + prefix
            + "\"");
   }

   private static Map<String, String> getCiscoInterfacePrefixes() {
      Map<String, String> prefixes = new LinkedHashMap<String, String>();
      prefixes.put("Async", "Async");
      prefixes.put("ATM", "ATM");
      prefixes.put("cmp-mgmt", "cmp-mgmt");
      prefixes.put("Embedded-Service-Engine", "Embedded-Service-Engine");
      prefixes.put("Ethernet", "Ethernet");
      prefixes.put("FastEthernet", "FastEthernet");
      prefixes.put("fe", "FastEthernet");
      prefixes.put("GigabitEthernet", "GigabitEthernet");
      prefixes.put("ge", "GigabitEthernet");
      prefixes.put("GMPLS", "GMPLS");
      prefixes.put("Loopback", "Loopback");
      prefixes.put("Management", "Management");
      prefixes.put("mgmt", NXOS_MANAGEMENT_INTERFACE_PREFIX);
      prefixes.put("Null", "Null");
      prefixes.put("Port-channel", "Port-channel");
      prefixes.put("Serial", "Serial");
      prefixes.put("TenGigabitEthernet", "TenGigabitEthernet");
      prefixes.put("te", "TenGigabitEthernet");
      prefixes.put("Tunnel", "Tunnel");
      prefixes.put("Vlan", "Vlan");
      return prefixes;
   }

   public static Ip getIp(Access_list_ip_rangeContext ctx) {
      if (ctx.ip != null) {
         return toIp(ctx.ip);
{'fraction_non_alphanumeric': 0.09732263446412966, 'fraction_numerical': 0.0009533645825057599, 'mean_word_length': 5.863685932388223, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5329742', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3673, 'n_words': 587}