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());
}
}
}
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;
}
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");
}
}
}
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);
}
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());
}
}
Aggregations