use of org.batfish.datamodel.SubRange in project batfish by batfish.
the class Client method validateType.
/**
* Validate the contents contained in json-encoded {@code value} matches the type required by
* {@code variable}, and the length of input string meets the requirement of minimum length if
* specified in {@code variable}. Call {@link Variable#getType()} on {@code variable} gives the
* expected type.
*
* @throws BatfishException if the content encoded in input {@code value} does not satisfy the
* requirements specified in {@code variable}.
*/
static void validateType(JsonNode value, Variable variable) throws BatfishException {
int minLength = variable.getMinLength() == null ? 0 : variable.getMinLength();
if (value.isTextual() && value.textValue().length() < minLength) {
throw new BatfishException(String.format("Must be at least %s characters in length", minLength));
}
Variable.Type expectedType = variable.getType();
switch(expectedType) {
case BOOLEAN:
if (!value.isBoolean()) {
throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
}
break;
case COMPARATOR:
if (!(COMPARATORS.contains(value.textValue()))) {
throw new BatfishException(String.format("It is not a known %s. Valid options are:" + " %s", expectedType.getName(), COMPARATORS));
}
break;
case DOUBLE:
if (!value.isDouble()) {
throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
}
break;
case FLOAT:
if (!value.isFloat()) {
throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
}
break;
case INTEGER:
if (!value.isInt()) {
throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
}
break;
case LONG:
if (!value.isLong()) {
throw new BatfishException(String.format("It is not a valid JSON %s value", expectedType.getName()));
}
break;
case IP:
// TODO: Need to double check isInetAddress()
if (!(value.isTextual())) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
new Ip(value.textValue());
break;
case IP_PROTOCOL:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
try {
IpProtocol.fromString(value.textValue());
} catch (IllegalArgumentException e) {
throw new BatfishException(String.format("Unknown %s string", expectedType.getName()));
}
break;
case IP_WILDCARD:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
new IpWildcard(value.textValue());
break;
case JAVA_REGEX:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
try {
Pattern.compile(value.textValue());
} catch (PatternSyntaxException e) {
throw new BatfishException("It is not a valid Java regular " + "expression", e);
}
break;
case JSON_PATH_REGEX:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
validateJsonPathRegex(value.textValue());
break;
case PREFIX:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
Prefix.parse(value.textValue());
break;
case PREFIX_RANGE:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
PrefixRange.fromString(value.textValue());
break;
case QUESTION:
// TODO: Implement
break;
case STRING:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
break;
case SUBRANGE:
if (!(value.isTextual() || value.isInt())) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string or " + "integer", expectedType.getName()));
}
Object actualValue = value.isTextual() ? value.textValue() : value.asInt();
new SubRange(actualValue);
break;
case PROTOCOL:
if (!value.isTextual()) {
throw new BatfishException(String.format("A Batfish %s must be a JSON string", expectedType.getName()));
}
Protocol.fromString(value.textValue());
break;
case JSON_PATH:
validateJsonPath(value);
break;
default:
throw new BatfishException(String.format("Unsupported parameter type: %s", expectedType));
}
}
use of org.batfish.datamodel.SubRange in project batfish by batfish.
the class CiscoControlPlaneExtractor method enterAaa_accounting_commands_line.
@Override
public void enterAaa_accounting_commands_line(Aaa_accounting_commands_lineContext ctx) {
Map<String, AaaAccountingCommands> commands = _configuration.getCf().getAaa().getAccounting().getCommands();
Set<String> levels = new TreeSet<>();
if (ctx.levels != null) {
List<SubRange> range = toRange(ctx.levels);
for (SubRange subRange : range) {
for (int i = subRange.getStart(); i <= subRange.getEnd(); i++) {
String level = Integer.toString(i);
levels.add(level);
}
}
} else {
levels.add(AaaAccounting.DEFAULT_COMMANDS);
}
List<AaaAccountingCommands> currentAaaAccountingCommands = new ArrayList<>();
for (String level : levels) {
AaaAccountingCommands c = commands.computeIfAbsent(level, k -> new AaaAccountingCommands());
currentAaaAccountingCommands.add(c);
}
_currentAaaAccountingCommands = currentAaaAccountingCommands;
}
use of org.batfish.datamodel.SubRange in project batfish by batfish.
the class ConfigurationBuilder method exitFftf_fragment_offset.
@Override
public void exitFftf_fragment_offset(Fftf_fragment_offsetContext ctx) {
SubRange subRange = toSubRange(ctx.subrange());
FwFrom from = new FwFromFragmentOffset(subRange, false);
_currentFwTerm.getFroms().add(from);
}
use of org.batfish.datamodel.SubRange in project batfish by batfish.
the class ConfigurationBuilder method exitFftf_packet_length_except.
@Override
public void exitFftf_packet_length_except(Fftf_packet_length_exceptContext ctx) {
List<SubRange> range = toRange(ctx.range());
FwFrom from = new FwFromPacketLength(range, true);
_currentFwTerm.getFroms().add(from);
}
use of org.batfish.datamodel.SubRange in project batfish by batfish.
the class ConfigurationBuilder method exitFftf_is_fragment.
@Override
public void exitFftf_is_fragment(Fftf_is_fragmentContext ctx) {
SubRange subRange = new SubRange(0, 0);
FwFrom from = new FwFromFragmentOffset(subRange, true);
_currentFwTerm.getFroms().add(from);
}
Aggregations