Search in sources :

Example 6 with VendorConfiguration

use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.

the class ConvertConfigurationJob method call.

@Override
public ConvertConfigurationResult call() {
    long startTime = System.currentTimeMillis();
    long elapsedTime;
    _logger.infof("Processing: \"%s\"", _name);
    Map<String, Configuration> configurations = new HashMap<>();
    Map<String, Warnings> warningsByHost = new HashMap<>();
    ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
    try {
        // We have only two options: AWS VPCs or router configs
        if (VendorConfiguration.class.isInstance(_configObject)) {
            Warnings warnings = Batfish.buildWarnings(_settings);
            VendorConfiguration vendorConfiguration = ((VendorConfiguration) _configObject);
            vendorConfiguration.setWarnings(warnings);
            vendorConfiguration.setAnswerElement(answerElement);
            Configuration configuration = vendorConfiguration.toVendorIndependentConfiguration();
            if (configuration.getDefaultCrossZoneAction() == null) {
                throw new BatfishException("Implementation error: missing default cross-zone action for host: '" + configuration.getHostname() + "'");
            }
            if (configuration.getDefaultInboundAction() == null) {
                throw new BatfishException("Implementation error: missing default inbound action for host: '" + configuration.getHostname() + "'");
            }
            // get iptables if applicable
            IptablesVendorConfiguration iptablesConfig = null;
            VendorConfiguration ov = vendorConfiguration.getOverlayConfiguration();
            if (ov != null) {
                // apply overlay
                HostConfiguration oh = (HostConfiguration) ov;
                iptablesConfig = oh.getIptablesVendorConfig();
            } else if (vendorConfiguration instanceof HostConfiguration) {
            // TODO: To enable below, we need to reconcile overlay and non-overlay iptables semantics.
            // HostConfiguration oh = (HostConfiguration)vendorConfiguration;
            // iptablesConfig = oh.getIptablesVendorConfig();
            }
            if (iptablesConfig != null) {
                iptablesConfig.addAsIpAccessLists(configuration, vendorConfiguration, warnings);
                iptablesConfig.applyAsOverlay(configuration, warnings);
            }
            configurations.put(_name, configuration);
            warningsByHost.put(_name, warnings);
        } else {
            configurations = ((AwsConfiguration) _configObject).toConfigurations(_settings, warningsByHost);
        }
        _logger.info(" ...OK\n");
    } catch (Exception e) {
        String error = "Conversion error for node with hostname '" + _name + "'";
        elapsedTime = System.currentTimeMillis() - startTime;
        return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), _name, new BatfishException(error, e));
    } finally {
        warningsByHost.forEach((hostname, warnings) -> Batfish.logWarnings(_logger, warnings));
        ;
    }
    elapsedTime = System.currentTimeMillis() - startTime;
    return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), warningsByHost, _name, configurations, answerElement);
}
Also used : BatfishException(org.batfish.common.BatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) Configuration(org.batfish.datamodel.Configuration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) HostConfiguration(org.batfish.representation.host.HostConfiguration) HashMap(java.util.HashMap) HostConfiguration(org.batfish.representation.host.HostConfiguration) BatfishException(org.batfish.common.BatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) ConvertConfigurationAnswerElement(org.batfish.datamodel.answers.ConvertConfigurationAnswerElement) Warnings(org.batfish.common.Warnings) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration)

Example 7 with VendorConfiguration

use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.

the class BatfishTest method testReadMissingIptableFile.

@Test
public void testReadMissingIptableFile() throws IOException {
    HostConfiguration host1 = new HostConfiguration();
    host1.setHostname("host1");
    host1.setIptablesFile(Paths.get("iptables").resolve("host1.iptables").toString());
    SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
    hostConfigurations.put("host1", host1);
    SortedMap<Path, String> iptablesData = new TreeMap<>();
    Path testRigPath = _folder.newFolder("testrig").toPath();
    ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
    answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
    Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
    String failureMessage = "Iptables file iptables" + File.separator + "host1.iptables for host host1 is not contained within the testrig";
    batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
    assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.FAILED));
    assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
    // When host file failed, verify that error message contains both failure messages
    answerElement.getErrors().clear();
    answerElement.getErrors().put("host1", new BatfishException("Failed to parse host file: host1").getBatfishStackTrace());
    batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
    assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
    assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString("Failed to parse host file: host1"));
    // When the haltonparseerror flag is set to true
    batfish.getSettings().setHaltOnParseError(true);
    answerElement.getErrors().clear();
    String parseErrorMessage = "Fatal exception due to at least one Iptables file is not contained" + " within the testrig";
    _thrown.expect(BatfishException.class);
    _thrown.expectMessage(parseErrorMessage);
    batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) HostConfiguration(org.batfish.representation.host.HostConfiguration) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 8 with VendorConfiguration

use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.

the class BatfishTest method testReadValidIptableFile.

@Test
public void testReadValidIptableFile() throws IOException {
    HostConfiguration host1 = new HostConfiguration();
    host1.setHostname("host1");
    Path iptablePath = Paths.get("iptables").resolve("host1.iptables");
    host1.setIptablesFile(iptablePath.toString());
    SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
    hostConfigurations.put("host1", host1);
    SortedMap<Path, String> iptablesData = new TreeMap<>();
    Path testRigPath = _folder.newFolder("testrig").toPath();
    File iptableFile = Paths.get(testRigPath.toString(), iptablePath.toString()).toFile();
    iptableFile.getParentFile().mkdir();
    assertThat(iptableFile.createNewFile(), is(true));
    ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
    answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
    Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
    batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
    assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.PASSED));
    assertThat(answerElement.getErrors().size(), is(0));
}
Also used : Path(java.nio.file.Path) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) HostConfiguration(org.batfish.representation.host.HostConfiguration) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TreeMap(java.util.TreeMap) File(java.io.File) Test(org.junit.Test)

Example 9 with VendorConfiguration

use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.

the class Batfish method serializeVendorConfigs.

Answer serializeVendorConfigs(Path testRigPath, Path outputPath) {
    Answer answer = new Answer();
    boolean configsFound = false;
    // look for network configs
    Path networkConfigsPath = testRigPath.resolve(BfConsts.RELPATH_CONFIGURATIONS_DIR);
    ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
    answerElement.setVersion(Version.getVersion());
    if (_settings.getVerboseParse()) {
        answer.addAnswerElement(answerElement);
    }
    // look for host configs and overlay configs
    SortedMap<String, VendorConfiguration> overlayHostConfigurations = new TreeMap<>();
    Path hostConfigsPath = testRigPath.resolve(BfConsts.RELPATH_HOST_CONFIGS_DIR);
    if (Files.exists(hostConfigsPath)) {
        overlayHostConfigurations = serializeHostConfigs(testRigPath, outputPath, answerElement);
        configsFound = true;
    }
    if (Files.exists(networkConfigsPath)) {
        serializeNetworkConfigs(testRigPath, outputPath, answerElement, overlayHostConfigurations);
        configsFound = true;
    }
    // look for AWS VPC configs
    Path awsVpcConfigsPath = testRigPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_DIR);
    if (Files.exists(awsVpcConfigsPath)) {
        answer.append(serializeAwsConfigs(testRigPath, outputPath));
        configsFound = true;
    }
    if (!configsFound) {
        throw new BatfishException("No valid configurations found");
    }
    // serialize warnings
    serializeObject(answerElement, _testrigSettings.getParseAnswerPath());
    return answer;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) TreeMap(java.util.TreeMap)

Example 10 with VendorConfiguration

use of org.batfish.vendor.VendorConfiguration in project batfish by batfish.

the class Batfish method histogram.

private void histogram(Path testRigPath) {
    Map<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_CONFIGURATIONS_DIR);
    // todo: either remove histogram function or do something userful with
    // answer
    Map<String, VendorConfiguration> vendorConfigurations = parseVendorConfigurations(configurationData, new ParseVendorConfigurationAnswerElement(), ConfigurationFormat.UNKNOWN);
    _logger.info("Building feature histogram...");
    MultiSet<String> histogram = new TreeMultiSet<>();
    for (VendorConfiguration vc : vendorConfigurations.values()) {
        Set<String> unimplementedFeatures = vc.getUnimplementedFeatures();
        histogram.add(unimplementedFeatures);
    }
    _logger.info("OK\n");
    for (String feature : histogram.elements()) {
        int count = histogram.count(feature);
        _logger.outputf("%s: %s\n", feature, count);
    }
}
Also used : Path(java.nio.file.Path) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ParseVendorConfigurationAnswerElement(org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement) TreeMultiSet(org.batfish.datamodel.collections.TreeMultiSet)

Aggregations

VendorConfiguration (org.batfish.vendor.VendorConfiguration)10 Path (java.nio.file.Path)8 BatfishException (org.batfish.common.BatfishException)7 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)7 TreeMap (java.util.TreeMap)6 ParseVendorConfigurationAnswerElement (org.batfish.datamodel.answers.ParseVendorConfigurationAnswerElement)5 HostConfiguration (org.batfish.representation.host.HostConfiguration)5 CleanBatfishException (org.batfish.common.CleanBatfishException)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Warnings (org.batfish.common.Warnings)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 Cache (com.google.common.cache.Cache)2 ActiveSpan (io.opentracing.ActiveSpan)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 Nullable (javax.annotation.Nullable)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 BatfishStackTrace (org.batfish.common.BatfishException.BatfishStackTrace)2