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