Search in sources :

Example 21 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class PluginConsumer method serializeToLz4Data.

/**
 * Serializes the given object to the given stream, using LZ4 compression.
 */
private void serializeToLz4Data(Serializable object, OutputStream out) {
    // This is a hack:
    // XStream requires that its streams be closed to properly finish serialization,
    // but we do not actually want to close the passed-in output stream.
    out = new CloseIgnoringOutputStream(out);
    try (Closer closer = Closer.create()) {
        OutputStream los = closer.register(new LZ4FrameOutputStream(out));
        ObjectOutputStream oos;
        if (_serializeToText) {
            XStream xstream = new XStream(new DomDriver("UTF-8"));
            oos = closer.register(xstream.createObjectOutputStream(los));
        } else {
            oos = closer.register(new ObjectOutputStream(los));
        }
        oos.writeObject(object);
    } catch (IOException e) {
        throw new BatfishException("Failed to convert object to LZ4 data", e);
    }
}
Also used : Closer(com.google.common.io.Closer) BatfishException(org.batfish.common.BatfishException) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) XStream(com.thoughtworks.xstream.XStream) LZ4FrameOutputStream(net.jpountz.lz4.LZ4FrameOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FilterOutputStream(java.io.FilterOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) LZ4FrameOutputStream(net.jpountz.lz4.LZ4FrameOutputStream)

Example 22 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class NodeRoleSpecifier method addToRoleNodesMap.

private void addToRoleNodesMap(SortedMap<String, SortedSet<String>> roleNodesMap, Set<String> nodes) {
    List<Pattern> patList = new ArrayList<>();
    for (String regex : _roleRegexes) {
        try {
            patList.add(Pattern.compile(regex));
        } catch (PatternSyntaxException e) {
            throw new BatfishException("Supplied regex is not a valid Java regex: \"" + regex + "\"", e);
        }
    }
    for (String node : nodes) {
        for (Pattern pattern : patList) {
            Matcher matcher = pattern.matcher(node);
            int numGroups = matcher.groupCount();
            if (matcher.matches()) {
                try {
                    List<String> roleParts = IntStream.range(1, numGroups + 1).mapToObj(matcher::group).collect(Collectors.toList());
                    String role = String.join("-", roleParts);
                    SortedSet<String> currNodes = roleNodesMap.computeIfAbsent(role, k -> new TreeSet<>());
                    currNodes.add(node);
                } catch (IndexOutOfBoundsException e) {
                    throw new BatfishException("Supplied regex does not contain a group: \"" + pattern.pattern() + "\"", e);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) BatfishException(org.batfish.common.BatfishException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 23 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class ConjunctionChain method evaluate.

@Override
public Result evaluate(Environment environment) {
    Result subroutineResult = new Result();
    subroutineResult.setFallThrough(true);
    for (BooleanExpr subroutine : _subroutines) {
        subroutineResult = subroutine.evaluate(environment);
        if (subroutineResult.getExit()) {
            return subroutineResult;
        } else if (!subroutineResult.getFallThrough() && !subroutineResult.getBooleanValue()) {
            subroutineResult.setReturn(false);
            return subroutineResult;
        }
    }
    if (!subroutineResult.getFallThrough()) {
        return subroutineResult;
    } else {
        String defaultPolicy = environment.getDefaultPolicy();
        if (defaultPolicy != null) {
            CallExpr callDefaultPolicy = new CallExpr(environment.getDefaultPolicy());
            Result defaultPolicyResult = callDefaultPolicy.evaluate(environment);
            return defaultPolicyResult;
        } else {
            throw new BatfishException("Default policy not set");
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) Result(org.batfish.datamodel.routing_policy.Result)

Example 24 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class Prefix method parse.

@JsonCreator
public static Prefix parse(String text) {
    String[] parts = text.split("/");
    if (parts.length != 2) {
        throw new BatfishException("Invalid prefix string: \"" + text + "\"");
    }
    Ip ip = new Ip(parts[0]);
    int prefixLength;
    try {
        prefixLength = Integer.parseInt(parts[1]);
    } catch (NumberFormatException e) {
        throw new BatfishException("Invalid prefix length: \"" + parts[1] + "\"", e);
    }
    return new Prefix(ip, prefixLength);
}
Also used : BatfishException(org.batfish.common.BatfishException) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Example 25 with BatfishException

use of org.batfish.common.BatfishException in project batfish by batfish.

the class Answer method append.

public void append(Answer answer) {
    if (answer._question != null) {
        _question = answer._question;
    }
    _answerElements.addAll(answer._answerElements);
    _status = answer._status;
    _summary.combine(answer.getSummary());
    for (AnswerElement answerElement : answer._answerElements) {
        if (answerElement instanceof BatfishStackTrace) {
            BatfishException e = ((BatfishStackTrace) answerElement).getException();
            throw new QuestionException("Exception answering question", e, this);
        }
    }
}
Also used : BatfishException(org.batfish.common.BatfishException) QuestionException(org.batfish.common.QuestionException) BatfishStackTrace(org.batfish.common.BatfishException.BatfishStackTrace)

Aggregations

BatfishException (org.batfish.common.BatfishException)264 IOException (java.io.IOException)61 Path (java.nio.file.Path)54 CleanBatfishException (org.batfish.common.CleanBatfishException)35 RedFlagBatfishException (org.batfish.common.RedFlagBatfishException)34 TreeMap (java.util.TreeMap)31 ArrayList (java.util.ArrayList)30 JSONException (org.codehaus.jettison.json.JSONException)30 Ip (org.batfish.datamodel.Ip)25 JSONObject (org.codehaus.jettison.json.JSONObject)25 Configuration (org.batfish.datamodel.Configuration)24 Map (java.util.Map)23 Prefix (org.batfish.datamodel.Prefix)22 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)20 TreeSet (java.util.TreeSet)20 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)18 Test (org.junit.Test)18 Set (java.util.Set)17 SortedMap (java.util.SortedMap)17