Search in sources :

Example 1 with SubRange

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));
    }
}
Also used : IpWildcard(org.batfish.datamodel.IpWildcard) BatfishException(org.batfish.common.BatfishException) Variable(org.batfish.datamodel.questions.Question.InstanceData.Variable) Ip(org.batfish.datamodel.Ip) JSONObject(org.codehaus.jettison.json.JSONObject) SubRange(org.batfish.datamodel.SubRange) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with SubRange

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;
}
Also used : AaaAccountingCommands(org.batfish.datamodel.vendor_family.cisco.AaaAccountingCommands) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SubRange(org.batfish.datamodel.SubRange)

Example 3 with SubRange

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);
}
Also used : FwFromFragmentOffset(org.batfish.representation.juniper.FwFromFragmentOffset) FwFrom(org.batfish.representation.juniper.FwFrom) SubRange(org.batfish.datamodel.SubRange)

Example 4 with SubRange

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);
}
Also used : FwFromPacketLength(org.batfish.representation.juniper.FwFromPacketLength) FwFrom(org.batfish.representation.juniper.FwFrom) SubRange(org.batfish.datamodel.SubRange)

Example 5 with SubRange

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);
}
Also used : FwFromFragmentOffset(org.batfish.representation.juniper.FwFromFragmentOffset) FwFrom(org.batfish.representation.juniper.FwFrom) SubRange(org.batfish.datamodel.SubRange)

Aggregations

SubRange (org.batfish.datamodel.SubRange)74 Prefix (org.batfish.datamodel.Prefix)18 IpWildcard (org.batfish.datamodel.IpWildcard)16 ArrayList (java.util.ArrayList)15 IpAccessListLine (org.batfish.datamodel.IpAccessListLine)13 Ip (org.batfish.datamodel.Ip)11 FwFrom (org.batfish.representation.juniper.FwFrom)11 Test (org.junit.Test)11 BatfishException (org.batfish.common.BatfishException)9 LineAction (org.batfish.datamodel.LineAction)9 RouteFilterLine (org.batfish.datamodel.RouteFilterLine)9 LinkedList (java.util.LinkedList)8 IpProtocol (org.batfish.datamodel.IpProtocol)8 RouteFilterList (org.batfish.datamodel.RouteFilterList)8 BoolExpr (com.microsoft.z3.BoolExpr)7 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)7 DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)7 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)7 IpAccessList (org.batfish.datamodel.IpAccessList)6 PrefixRange (org.batfish.datamodel.PrefixRange)6