Search in sources :

Example 66 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class ConfigurationBuilder method enterOa_interface.

@Override
public void enterOa_interface(Oa_interfaceContext ctx) {
    Map<String, Interface> interfaces = _configuration.getInterfaces();
    String unitFullName = null;
    if (ctx.ALL() != null) {
        _currentOspfInterface = _currentRoutingInstance.getGlobalMasterInterface();
    } else if (ctx.ip != null) {
        Ip ip = new Ip(ctx.ip.getText());
        for (Interface iface : interfaces.values()) {
            for (Interface unit : iface.getUnits().values()) {
                if (unit.getAllAddressIps().contains(ip)) {
                    _currentOspfInterface = unit;
                    unitFullName = unit.getName();
                }
            }
        }
        if (_currentOspfInterface == null) {
            throw new BatfishException("Could not find interface with ip address: " + ip);
        }
    } else {
        _currentOspfInterface = initInterface(ctx.id);
        unitFullName = _currentOspfInterface.getName();
    }
    Ip currentArea = new Ip(_currentArea.getName());
    if (ctx.oai_passive() != null) {
        _currentOspfInterface.getOspfPassiveAreas().add(currentArea);
    } else {
        Ip interfaceActiveArea = _currentOspfInterface.getOspfActiveArea();
        if (interfaceActiveArea != null && !currentArea.equals(interfaceActiveArea)) {
            throw new BatfishException("Interface: \"" + unitFullName + "\" assigned to multiple active areas");
        }
        _currentOspfInterface.setOspfActiveArea(currentArea);
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) PsThenNextHopIp(org.batfish.representation.juniper.PsThenNextHopIp) FwThenNextIp(org.batfish.representation.juniper.FwThenNextIp) Ip(org.batfish.datamodel.Ip) PsFromInterface(org.batfish.representation.juniper.PsFromInterface) Interface(org.batfish.representation.juniper.Interface)

Example 67 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class ConfigurationBuilder method enterIfi_address.

@Override
public void enterIfi_address(Ifi_addressContext ctx) {
    Set<InterfaceAddress> allAddresses = _currentInterface.getAllAddresses();
    InterfaceAddress address;
    if (ctx.IP_PREFIX() != null) {
        address = new InterfaceAddress(ctx.IP_PREFIX().getText());
    } else if (ctx.IP_ADDRESS() != null) {
        Ip ip = new Ip(ctx.IP_ADDRESS().getText());
        address = new InterfaceAddress(ip, Prefix.MAX_PREFIX_LENGTH);
    } else {
        throw new BatfishException("Invalid or missing address");
    }
    _currentInterfaceAddress = address;
    if (_currentInterface.getPrimaryAddress() == null) {
        _currentInterface.setPrimaryAddress(address);
    }
    if (_currentInterface.getPreferredAddress() == null) {
        _currentInterface.setPreferredAddress(address);
    }
    allAddresses.add(address);
    Ip ip = address.getIp();
    _currentInterface.getAllAddressIps().add(ip);
}
Also used : BatfishException(org.batfish.common.BatfishException) InterfaceAddress(org.batfish.datamodel.InterfaceAddress) PsThenNextHopIp(org.batfish.representation.juniper.PsThenNextHopIp) FwThenNextIp(org.batfish.representation.juniper.FwThenNextIp) Ip(org.batfish.datamodel.Ip)

Example 68 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class ConfigurationBuilder method exitFftf_icmp_type.

@Override
public void exitFftf_icmp_type(Fftf_icmp_typeContext ctx) {
    if (_currentFirewallFamily == Family.INET6) {
        // TODO: support icmpv6
        return;
    }
    SubRange icmpTypeRange;
    if (ctx.subrange() != null) {
        icmpTypeRange = toSubRange(ctx.subrange());
    } else if (ctx.icmp_type() != null) {
        int icmpType = toIcmpType(ctx.icmp_type());
        icmpTypeRange = new SubRange(icmpType, icmpType);
    } else {
        throw new BatfishException("Invalid icmp-type");
    }
    FwFrom from = new FwFromIcmpType(icmpTypeRange);
    _currentFwTerm.getFroms().add(from);
}
Also used : BatfishException(org.batfish.common.BatfishException) FwFromIcmpType(org.batfish.representation.juniper.FwFromIcmpType) FwFrom(org.batfish.representation.juniper.FwFrom) SubRange(org.batfish.datamodel.SubRange)

Example 69 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class Batfish method getQuestionTemplates.

@Override
public Map<String, String> getQuestionTemplates() {
    if (_settings.getCoordinatorHost() == null) {
        throw new BatfishException("Cannot get question templates: coordinator host is not set");
    }
    String protocol = _settings.getSslDisable() ? "http" : "https";
    String url = String.format("%s://%s:%s%s/%s", protocol, _settings.getCoordinatorHost(), _settings.getCoordinatorPoolPort(), CoordConsts.SVC_CFG_POOL_MGR, CoordConsts.SVC_RSC_POOL_GET_QUESTION_TEMPLATES);
    Map<String, String> params = new HashMap<>();
    params.put(CoordConsts.SVC_KEY_VERSION, Version.getVersion());
    JSONObject response = (JSONObject) Driver.talkToCoordinator(url, params, _logger);
    if (response == null) {
        throw new BatfishException("Could not get question templates: Got null response");
    }
    if (!response.has(CoordConsts.SVC_KEY_QUESTION_LIST)) {
        throw new BatfishException("Could not get question templates: Response lacks question list");
    }
    try {
        Map<String, String> templates = BatfishObjectMapper.mapper().readValue(response.get(CoordConsts.SVC_KEY_QUESTION_LIST).toString(), new TypeReference<Map<String, String>>() {
        });
        return templates;
    } catch (JSONException | IOException e) {
        throw new BatfishException("Could not cast response to Map: ", e);
    }
}
Also used : CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) JSONObject(org.codehaus.jettison.json.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) Map(java.util.Map) TreeMap(java.util.TreeMap) Collectors.toMap(java.util.stream.Collectors.toMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap)

Example 70 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class Batfish method serializeNetworkConfigs.

private void serializeNetworkConfigs(Path testRigPath, Path outputPath, ParseVendorConfigurationAnswerElement answerElement, SortedMap<String, VendorConfiguration> overlayHostConfigurations) {
    Map<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_CONFIGURATIONS_DIR);
    Map<String, VendorConfiguration> vendorConfigurations;
    try (ActiveSpan parseNetworkConfigsSpan = GlobalTracer.get().buildSpan("Parse network configs").startActive()) {
        // avoid unused warning
        assert parseNetworkConfigsSpan != null;
        vendorConfigurations = parseVendorConfigurations(configurationData, answerElement, ConfigurationFormat.UNKNOWN);
    }
    if (vendorConfigurations == null) {
        throw new BatfishException("Exiting due to parser errors");
    }
    _logger.infof("Testrig:%s in container:%s has total number of network configs:%d", getTestrigName(), getContainerName(), vendorConfigurations.size());
    _logger.info("\n*** SERIALIZING VENDOR CONFIGURATION STRUCTURES ***\n");
    _logger.resetTimer();
    CommonUtil.createDirectories(outputPath);
    Map<Path, VendorConfiguration> output = new TreeMap<>();
    vendorConfigurations.forEach((name, vc) -> {
        if (name.contains(File.separator)) {
            // iptables will get a hostname like configs/iptables-save if they
            // are not set up correctly using host files
            _logger.errorf("Cannot serialize configuration with hostname %s\n", name);
            answerElement.addRedFlagWarning(name, new Warning("Cannot serialize network config. Bad hostname " + name.replace("\\", "/"), "MISCELLANEOUS"));
        } else {
            // apply overlay if it exists
            VendorConfiguration overlayConfig = overlayHostConfigurations.get(name);
            if (overlayConfig != null) {
                vc.setOverlayConfiguration(overlayConfig);
                overlayHostConfigurations.remove(name);
            }
            Path currentOutputPath = outputPath.resolve(name);
            output.put(currentOutputPath, vc);
        }
    });
    // warn about unused overlays
    overlayHostConfigurations.forEach((name, overlay) -> {
        answerElement.getParseStatus().put(name, ParseStatus.ORPHANED);
    });
    serializeObjects(output);
    _logger.printElapsedTime();
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Warning(org.batfish.common.Warning) ActiveSpan(io.opentracing.ActiveSpan) TreeMap(java.util.TreeMap)

Aggregations

BatfishException (org.batfish.common.BatfishException)264 IOException (java.io.IOException)61 Path (java.nio.file.Path)54 CleanBatfishException (org.batfish.common.CleanBatfishException)35 RedFlagBatfishException (org.batfish.common.RedFlagBatfishException)34 TreeMap (java.util.TreeMap)31 ArrayList (java.util.ArrayList)30 JSONException (org.codehaus.jettison.json.JSONException)30 Ip (org.batfish.datamodel.Ip)25 JSONObject (org.codehaus.jettison.json.JSONObject)25 Configuration (org.batfish.datamodel.Configuration)24 Map (java.util.Map)23 Prefix (org.batfish.datamodel.Prefix)22 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)20 TreeSet (java.util.TreeSet)20 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)18 Test (org.junit.Test)18 Set (java.util.Set)17 SortedMap (java.util.SortedMap)17