Search in sources :

Example 1 with AwsConfiguration

use of org.batfish.representation.aws.AwsConfiguration 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 2 with AwsConfiguration

use of org.batfish.representation.aws.AwsConfiguration in project batfish by batfish.

the class Batfish method serializeAwsConfigs.

private Answer serializeAwsConfigs(Path testRigPath, Path outputPath) {
    Answer answer = new Answer();
    Map<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_AWS_CONFIGS_DIR);
    AwsConfiguration config;
    try (ActiveSpan parseAwsConfigsSpan = GlobalTracer.get().buildSpan("Parse AWS configs").startActive()) {
        // avoid unused warning
        assert parseAwsConfigsSpan != null;
        config = parseAwsConfigurations(configurationData);
    }
    _logger.info("\n*** SERIALIZING AWS CONFIGURATION STRUCTURES ***\n");
    _logger.resetTimer();
    outputPath.toFile().mkdirs();
    Path currentOutputPath = outputPath.resolve(BfConsts.RELPATH_AWS_CONFIGS_FILE);
    _logger.debugf("Serializing AWS to \"%s\"...", currentOutputPath);
    serializeObject(config, currentOutputPath);
    _logger.debug("OK\n");
    _logger.printElapsedTime();
    return answer;
}
Also used : Path(java.nio.file.Path) Answer(org.batfish.datamodel.answers.Answer) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) ActiveSpan(io.opentracing.ActiveSpan)

Example 3 with AwsConfiguration

use of org.batfish.representation.aws.AwsConfiguration in project batfish by batfish.

the class Batfish method parseAwsConfigurations.

private AwsConfiguration parseAwsConfigurations(Map<Path, String> configurationData) {
    AwsConfiguration config = new AwsConfiguration();
    for (Entry<Path, String> configFile : configurationData.entrySet()) {
        Path file = configFile.getKey();
        String fileText = configFile.getValue();
        // parent dir name
        String regionName = file.getName(file.getNameCount() - 2).toString();
        // processing
        if (file.toString().contains("classic-link")) {
            _logger.errorf("%s has classic link configuration\n", file);
            continue;
        }
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(fileText);
        } catch (JSONException e) {
            _logger.errorf("%s does not have valid json\n", file);
        }
        if (jsonObj != null) {
            try {
                config.addConfigElement(regionName, jsonObj, _logger);
            } catch (JSONException e) {
                throw new BatfishException("Problems parsing JSON in " + file, e);
            }
        }
    }
    return config;
}
Also used : Path(java.nio.file.Path) CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException)

Aggregations

AwsConfiguration (org.batfish.representation.aws.AwsConfiguration)3 Path (java.nio.file.Path)2 BatfishException (org.batfish.common.BatfishException)2 ActiveSpan (io.opentracing.ActiveSpan)1 HashMap (java.util.HashMap)1 CleanBatfishException (org.batfish.common.CleanBatfishException)1 Warnings (org.batfish.common.Warnings)1 Configuration (org.batfish.datamodel.Configuration)1 Answer (org.batfish.datamodel.answers.Answer)1 ConvertConfigurationAnswerElement (org.batfish.datamodel.answers.ConvertConfigurationAnswerElement)1 HostConfiguration (org.batfish.representation.host.HostConfiguration)1 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)1 VendorConfiguration (org.batfish.vendor.VendorConfiguration)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1