Search in sources :

Example 16 with Configuration

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

the class Batfish method processNodeRoles.

/**
 * Set the roles of each configuration. Use an explicitly provided {@link NodeRoleSpecifier} if
 * one exists; otherwise use the results of our node-role inference. Also set the inferred role
 * dimensions of each node, based on its name.
 */
private void processNodeRoles(Map<String, Configuration> configurations, ValidateEnvironmentAnswerElement veae) {
    NodeRoleSpecifier specifier = getNodeRoleSpecifier(false);
    SortedMap<String, SortedSet<String>> nodeRoles = specifier.createNodeRolesMap(configurations.keySet());
    for (Entry<String, SortedSet<String>> nodeRolesEntry : nodeRoles.entrySet()) {
        String hostname = nodeRolesEntry.getKey();
        Configuration config = configurations.get(hostname);
        if (config == null) {
            veae.setValid(false);
            veae.getUndefinedNodeRoleSpecifierNodes().add(hostname);
        } else {
            SortedSet<String> roles = nodeRolesEntry.getValue();
            config.setRoles(roles);
        }
    }
    Map<String, NavigableMap<Integer, String>> roleDimensions = InferRoles.getRoleDimensions(configurations);
    for (Map.Entry<String, NavigableMap<Integer, String>> entry : roleDimensions.entrySet()) {
        String nodeName = entry.getKey();
        Configuration config = configurations.get(nodeName);
        if (config == null) {
            veae.setValid(false);
        } else {
            config.setRoleDimensions(entry.getValue());
        }
    }
}
Also used : HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) NavigableMap(java.util.NavigableMap) NodeRoleSpecifier(org.batfish.datamodel.NodeRoleSpecifier) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) SortedSet(java.util.SortedSet) 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 17 with Configuration

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

the class Batfish method parseEnvironmentBgpTables.

private SortedMap<String, BgpAdvertisementsByVrf> parseEnvironmentBgpTables(SortedMap<Path, String> inputData, ParseEnvironmentBgpTablesAnswerElement answerElement) {
    _logger.info("\n*** PARSING ENVIRONMENT BGP TABLES ***\n");
    _logger.resetTimer();
    SortedMap<String, BgpAdvertisementsByVrf> bgpTables = new TreeMap<>();
    List<ParseEnvironmentBgpTableJob> jobs = new ArrayList<>();
    SortedMap<String, Configuration> configurations = loadConfigurations();
    for (Entry<Path, String> bgpFile : inputData.entrySet()) {
        Path currentFile = bgpFile.getKey();
        String fileText = bgpFile.getValue();
        String hostname = currentFile.getFileName().toString();
        String optionalSuffix = ".bgp";
        if (hostname.endsWith(optionalSuffix)) {
            hostname = hostname.substring(0, hostname.length() - optionalSuffix.length());
        }
        if (!configurations.containsKey(hostname)) {
            continue;
        }
        Warnings warnings = buildWarnings(_settings);
        ParseEnvironmentBgpTableJob job = new ParseEnvironmentBgpTableJob(_settings, fileText, hostname, currentFile, warnings, _bgpTablePlugins);
        jobs.add(job);
    }
    BatfishJobExecutor.runJobsInExecutor(_settings, _logger, jobs, bgpTables, answerElement, _settings.getHaltOnParseError(), "Parse environment BGP tables");
    _logger.printElapsedTime();
    return bgpTables;
}
Also used : Path(java.nio.file.Path) HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ParseEnvironmentBgpTableJob(org.batfish.job.ParseEnvironmentBgpTableJob) Warnings(org.batfish.common.Warnings) BgpAdvertisementsByVrf(org.batfish.datamodel.collections.BgpAdvertisementsByVrf)

Example 18 with Configuration

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

the class Batfish method initBgpAdvertisements.

@Override
public void initBgpAdvertisements(Map<String, Configuration> configurations) {
    Set<BgpAdvertisement> globalBgpAdvertisements = getDataPlanePlugin().getAdvertisements();
    for (Configuration node : configurations.values()) {
        node.initBgpAdvertisements();
        for (Vrf vrf : node.getVrfs().values()) {
            vrf.initBgpAdvertisements();
        }
    }
    for (BgpAdvertisement bgpAdvertisement : globalBgpAdvertisements) {
        BgpAdvertisementType type = bgpAdvertisement.getType();
        String srcVrf = bgpAdvertisement.getSrcVrf();
        String dstVrf = bgpAdvertisement.getDstVrf();
        switch(type) {
            case EBGP_ORIGINATED:
                {
                    String originationNodeName = bgpAdvertisement.getSrcNode();
                    Configuration originationNode = configurations.get(originationNodeName);
                    if (originationNode != null) {
                        originationNode.getBgpAdvertisements().add(bgpAdvertisement);
                        originationNode.getOriginatedAdvertisements().add(bgpAdvertisement);
                        originationNode.getOriginatedEbgpAdvertisements().add(bgpAdvertisement);
                        Vrf originationVrf = originationNode.getVrfs().get(srcVrf);
                        originationVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        originationVrf.getOriginatedAdvertisements().add(bgpAdvertisement);
                        originationVrf.getOriginatedEbgpAdvertisements().add(bgpAdvertisement);
                    } else {
                        throw new BatfishException("Originated bgp advertisement refers to missing node: \"" + originationNodeName + "\"");
                    }
                    break;
                }
            case IBGP_ORIGINATED:
                {
                    String originationNodeName = bgpAdvertisement.getSrcNode();
                    Configuration originationNode = configurations.get(originationNodeName);
                    if (originationNode != null) {
                        originationNode.getBgpAdvertisements().add(bgpAdvertisement);
                        originationNode.getOriginatedAdvertisements().add(bgpAdvertisement);
                        originationNode.getOriginatedIbgpAdvertisements().add(bgpAdvertisement);
                        Vrf originationVrf = originationNode.getVrfs().get(srcVrf);
                        originationVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        originationVrf.getOriginatedAdvertisements().add(bgpAdvertisement);
                        originationVrf.getOriginatedIbgpAdvertisements().add(bgpAdvertisement);
                    } else {
                        throw new BatfishException("Originated bgp advertisement refers to missing node: \"" + originationNodeName + "\"");
                    }
                    break;
                }
            case EBGP_RECEIVED:
                {
                    String recevingNodeName = bgpAdvertisement.getDstNode();
                    Configuration receivingNode = configurations.get(recevingNodeName);
                    if (receivingNode != null) {
                        receivingNode.getBgpAdvertisements().add(bgpAdvertisement);
                        receivingNode.getReceivedAdvertisements().add(bgpAdvertisement);
                        receivingNode.getReceivedEbgpAdvertisements().add(bgpAdvertisement);
                        Vrf receivingVrf = receivingNode.getVrfs().get(dstVrf);
                        receivingVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        receivingVrf.getReceivedAdvertisements().add(bgpAdvertisement);
                        receivingVrf.getReceivedEbgpAdvertisements().add(bgpAdvertisement);
                    }
                    break;
                }
            case IBGP_RECEIVED:
                {
                    String recevingNodeName = bgpAdvertisement.getDstNode();
                    Configuration receivingNode = configurations.get(recevingNodeName);
                    if (receivingNode != null) {
                        receivingNode.getBgpAdvertisements().add(bgpAdvertisement);
                        receivingNode.getReceivedAdvertisements().add(bgpAdvertisement);
                        receivingNode.getReceivedIbgpAdvertisements().add(bgpAdvertisement);
                        Vrf receivingVrf = receivingNode.getVrfs().get(dstVrf);
                        receivingVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        receivingVrf.getReceivedAdvertisements().add(bgpAdvertisement);
                        receivingVrf.getReceivedIbgpAdvertisements().add(bgpAdvertisement);
                    }
                    break;
                }
            case EBGP_SENT:
                {
                    String sendingNodeName = bgpAdvertisement.getSrcNode();
                    Configuration sendingNode = configurations.get(sendingNodeName);
                    if (sendingNode != null) {
                        sendingNode.getBgpAdvertisements().add(bgpAdvertisement);
                        sendingNode.getSentAdvertisements().add(bgpAdvertisement);
                        sendingNode.getSentEbgpAdvertisements().add(bgpAdvertisement);
                        Vrf sendingVrf = sendingNode.getVrfs().get(srcVrf);
                        sendingVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        sendingVrf.getSentAdvertisements().add(bgpAdvertisement);
                        sendingVrf.getSentEbgpAdvertisements().add(bgpAdvertisement);
                    }
                    break;
                }
            case IBGP_SENT:
                {
                    String sendingNodeName = bgpAdvertisement.getSrcNode();
                    Configuration sendingNode = configurations.get(sendingNodeName);
                    if (sendingNode != null) {
                        sendingNode.getBgpAdvertisements().add(bgpAdvertisement);
                        sendingNode.getSentAdvertisements().add(bgpAdvertisement);
                        sendingNode.getSentIbgpAdvertisements().add(bgpAdvertisement);
                        Vrf sendingVrf = sendingNode.getVrfs().get(srcVrf);
                        sendingVrf.getBgpAdvertisements().add(bgpAdvertisement);
                        sendingVrf.getSentAdvertisements().add(bgpAdvertisement);
                        sendingVrf.getSentIbgpAdvertisements().add(bgpAdvertisement);
                    }
                    break;
                }
            default:
                throw new BatfishException("Invalid bgp advertisement type");
        }
    }
}
Also used : CleanBatfishException(org.batfish.common.CleanBatfishException) BatfishException(org.batfish.common.BatfishException) BgpAdvertisement(org.batfish.datamodel.BgpAdvertisement) HostConfiguration(org.batfish.representation.host.HostConfiguration) Configuration(org.batfish.datamodel.Configuration) ImmutableConfiguration(org.apache.commons.configuration2.ImmutableConfiguration) AwsConfiguration(org.batfish.representation.aws.AwsConfiguration) IptablesVendorConfiguration(org.batfish.representation.iptables.IptablesVendorConfiguration) VendorConfiguration(org.batfish.vendor.VendorConfiguration) BgpAdvertisementType(org.batfish.datamodel.BgpAdvertisement.BgpAdvertisementType) RoutesByVrf(org.batfish.datamodel.collections.RoutesByVrf) Vrf(org.batfish.datamodel.Vrf) BgpAdvertisementsByVrf(org.batfish.datamodel.collections.BgpAdvertisementsByVrf)

Example 19 with Configuration

use of org.batfish.datamodel.Configuration 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 20 with Configuration

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

the class ConvertConfigurationResult method applyTo.

@Override
public void applyTo(Map<String, Configuration> configurations, BatfishLogger logger, ConvertConfigurationAnswerElement answerElement) {
    appendHistory(logger);
    if (_configurations != null) {
        for (Entry<String, Configuration> hostConfig : _configurations.entrySet()) {
            String hostname = hostConfig.getKey();
            Configuration config = hostConfig.getValue();
            if (configurations.containsKey(hostname)) {
                throw new BatfishException("Duplicate hostname: " + hostname);
            } else {
                configurations.put(hostname, config);
                if (_warningsByHost.containsKey(hostname) && !_warningsByHost.get(hostname).isEmpty()) {
                    answerElement.getWarnings().put(hostname, _warningsByHost.get(hostname));
                }
                if (!_answerElement.getUnusedStructures().isEmpty()) {
                    answerElement.getUnusedStructures().putAll(_answerElement.getUnusedStructures());
                }
                if (!_answerElement.getUndefinedReferences().isEmpty()) {
                    answerElement.getUndefinedReferences().putAll(_answerElement.getUndefinedReferences());
                }
            }
        }
    } else {
        answerElement.getFailed().add(_name);
        answerElement.getErrors().put(_name, ((BatfishException) _failureCause).getBatfishStackTrace());
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) Configuration(org.batfish.datamodel.Configuration)

Aggregations

Configuration (org.batfish.datamodel.Configuration)170 Test (org.junit.Test)69 Interface (org.batfish.datamodel.Interface)55 Ip (org.batfish.datamodel.Ip)49 Vrf (org.batfish.datamodel.Vrf)45 HashMap (java.util.HashMap)44 Topology (org.batfish.datamodel.Topology)38 VendorConfiguration (org.batfish.vendor.VendorConfiguration)35 Prefix (org.batfish.datamodel.Prefix)33 Edge (org.batfish.datamodel.Edge)32 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)30 Map (java.util.Map)29 Set (java.util.Set)29 TreeMap (java.util.TreeMap)29 BatfishException (org.batfish.common.BatfishException)28 IpAccessList (org.batfish.datamodel.IpAccessList)26 ArrayList (java.util.ArrayList)25 HashSet (java.util.HashSet)25 List (java.util.List)25 SortedSet (java.util.SortedSet)24