Search in sources :

Example 11 with Pair

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

the class TransferResult method mergeChangedVariables.

// TODO: this really needs to use persistent set data types
public PList<Pair<String, Pair<Expr, Expr>>> mergeChangedVariables(TransferResult<U, T> other) {
    Set<String> seen = new HashSet<>();
    PList<Pair<String, Pair<Expr, Expr>>> vars = PList.empty();
    for (Pair<String, Expr> cv1 : this._changedVariables) {
        String s = cv1.getFirst();
        Expr x = cv1.getSecond();
        if (!seen.contains(s)) {
            seen.add(s);
            Expr e = find(other._changedVariables, s);
            Pair<Expr, Expr> pair = new Pair<>(x, e);
            vars = vars.plus(new Pair<>(s, pair));
        }
    }
    for (Pair<String, Expr> cv1 : other._changedVariables) {
        String s = cv1.getFirst();
        Expr x = cv1.getSecond();
        if (!seen.contains(s)) {
            seen.add(s);
            Expr e = find(this._changedVariables, s);
            // preserve order
            Pair<Expr, Expr> pair = new Pair<>(e, x);
            vars = vars.plus(new Pair<>(s, pair));
        }
    }
    return vars;
}
Also used : Expr(com.microsoft.z3.Expr) HashSet(java.util.HashSet) Pair(org.batfish.common.Pair)

Example 12 with Pair

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

the class BDDNetwork method computeInterfacePolicies.

/*
   * For each interface in the network, creates a canonical
   * representation of the import and export policies on this interface.
   */
private void computeInterfacePolicies() {
    for (Entry<String, Configuration> entry : _graph.getConfigurations().entrySet()) {
        String router = entry.getKey();
        // Skip if doesn't match the node regex
        Matcher m = _nodeSpecifier.getRegex().matcher(router);
        if (!m.matches()) {
            continue;
        }
        Configuration conf = entry.getValue();
        List<GraphEdge> edges = _graph.getEdgeMap().get(router);
        for (GraphEdge ge : edges) {
            // Import BGP policy
            RoutingPolicy importBgp = _graph.findImportRoutingPolicy(router, Protocol.BGP, ge);
            if (importBgp != null) {
                BDDRoute rec = computeBDD(_graph, conf, importBgp, true);
                _importBgpPolicies.put(ge, rec);
            }
            // Export BGP policy
            RoutingPolicy exportBgp = _graph.findExportRoutingPolicy(router, Protocol.BGP, ge);
            if (exportBgp != null) {
                BDDRoute rec = computeBDD(_graph, conf, exportBgp, true);
                _exportBgpPolicies.put(ge, rec);
            }
            IpAccessList in = ge.getStart().getIncomingFilter();
            IpAccessList out = ge.getStart().getOutgoingFilter();
            // Incoming ACL
            if (in != null) {
                BDDAcl x = BDDAcl.create(conf, in, true);
                _inAcls.put(ge, x);
            }
            // Outgoing ACL
            if (out != null) {
                BDDAcl x = BDDAcl.create(conf, out, true);
                _outAcls.put(ge, x);
            }
        }
    }
    for (Entry<String, List<GraphEdge>> entry : _graph.getEdgeMap().entrySet()) {
        String router = entry.getKey();
        // Skip if doesn't match the node regex
        Matcher m = _nodeSpecifier.getRegex().matcher(router);
        if (!m.matches()) {
            continue;
        }
        List<GraphEdge> edges = entry.getValue();
        Configuration conf = _graph.getConfigurations().get(router);
        for (GraphEdge ge : edges) {
            BDDRoute bgpIn = _importBgpPolicies.get(ge);
            BDDRoute bgpOut = _exportBgpPolicies.get(ge);
            BDDAcl aclIn = _inAcls.get(ge);
            BDDAcl aclOut = _outAcls.get(ge);
            Integer ospfCost = ge.getStart().getOspfCost();
            SortedSet<Pair<Prefix, Integer>> staticPrefixes = new TreeSet<>();
            SortedSet<StaticRoute> staticRoutes = conf.getDefaultVrf().getStaticRoutes();
            for (StaticRoute sr : staticRoutes) {
                Prefix pfx = sr.getNetwork();
                Integer adminCost = sr.getAdministrativeCost();
                Pair<Prefix, Integer> tup = new Pair<>(pfx, adminCost);
                staticPrefixes.add(tup);
            }
            InterfacePolicy ipol = new InterfacePolicy(aclIn, bgpIn, null, staticPrefixes);
            InterfacePolicy epol = new InterfacePolicy(aclOut, bgpOut, ospfCost, null);
            _importPolicyMap.put(ge, ipol);
            _exportPolicyMap.put(ge, epol);
        }
    }
}
Also used : StaticRoute(org.batfish.datamodel.StaticRoute) Configuration(org.batfish.datamodel.Configuration) Matcher(java.util.regex.Matcher) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) Prefix(org.batfish.datamodel.Prefix) InterfacePolicy(org.batfish.symbolic.abstraction.InterfacePolicy) TreeSet(java.util.TreeSet) IpAccessList(org.batfish.datamodel.IpAccessList) List(java.util.List) IpAccessList(org.batfish.datamodel.IpAccessList) GraphEdge(org.batfish.symbolic.GraphEdge) Pair(org.batfish.common.Pair)

Example 13 with Pair

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

the class WorkMgr method computeWorkDetails.

private WorkDetails computeWorkDetails(WorkItem workItem) {
    WorkType workType = WorkType.UNKNOWN;
    if (WorkItemBuilder.isParsingWorkItem(workItem)) {
        workType = WorkType.PARSING;
    }
    if (WorkItemBuilder.isDataplaningWorkItem(workItem)) {
        if (workType != WorkType.UNKNOWN) {
            throw new BatfishException("Cannot do composite work. Separate PARSING and DATAPLANING.");
        }
        workType = WorkType.DATAPLANING;
    }
    if (WorkItemBuilder.isAnsweringWorkItem(workItem)) {
        if (workType != WorkType.UNKNOWN) {
            throw new BatfishException("Cannot do composite work. Separate ANSWER from other work.");
        }
        String qName = WorkItemBuilder.getQuestionName(workItem);
        if (qName == null) {
            throw new BatfishException("Question name not provided for ANSWER work");
        }
        Path qFile = getpathContainerQuestion(workItem.getContainerName(), qName);
        Question question = Question.parseQuestion(qFile);
        workType = question.getIndependent() ? WorkType.INDEPENDENT_ANSWERING : question.getDataPlane() ? WorkType.DATAPLANE_DEPENDENT_ANSWERING : WorkType.PARSING_DEPENDENT_ANSWERING;
    }
    if (WorkItemBuilder.isAnalyzingWorkItem(workItem)) {
        if (workType != WorkType.UNKNOWN) {
            throw new BatfishException("Cannot do composite work. Separate ANALYZE from other work.");
        }
        String aName = WorkItemBuilder.getAnalysisName(workItem);
        if (aName == null) {
            throw new BatfishException("Analysis name not provided for ANALYZE work");
        }
        Set<String> qNames = listAnalysisQuestions(workItem.getContainerName(), aName);
        // compute the strongest dependency among the embedded questions
        workType = WorkType.INDEPENDENT_ANSWERING;
        for (String qName : qNames) {
            Path qFile = getpathAnalysisQuestion(workItem.getContainerName(), aName, qName);
            Question question = Question.parseQuestion(qFile);
            if (question.getDataPlane()) {
                workType = WorkType.DATAPLANE_DEPENDENT_ANSWERING;
                break;
            }
            if (!question.getIndependent()) {
                workType = WorkType.PARSING_DEPENDENT_ANSWERING;
            }
        }
    }
    Pair<Pair<String, String>, Pair<String, String>> settings = WorkItemBuilder.getBaseAndDeltaSettings(workItem);
    WorkDetails details = new WorkDetails(WorkItemBuilder.getBaseTestrig(settings), WorkItemBuilder.getBaseEnvironment(settings), WorkItemBuilder.getDeltaTestrig(settings), WorkItemBuilder.getDeltaEnvironment(settings), WorkItemBuilder.isDifferential(workItem), workType);
    return details;
}
Also used : Path(java.nio.file.Path) BatfishException(org.batfish.common.BatfishException) WorkType(org.batfish.coordinator.WorkDetails.WorkType) Question(org.batfish.datamodel.questions.Question) Pair(org.batfish.common.Pair)

Example 14 with Pair

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

the class NodJobTest method getNodJob.

private NodJob getNodJob(HeaderSpace headerSpace, Boolean srcNatted) {
    StandardReachabilityQuerySynthesizer querySynthesizer = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT)).setFinalNodes(ImmutableSet.of(_dstNode.getHostname())).setHeaderSpace(headerSpace).setIngressNodeVrfs(ImmutableMap.of(_srcNode.getHostname(), ImmutableSet.of(_srcVrf.getName()))).setSrcNatted(srcNatted).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).build();
    SortedSet<Pair<String, String>> ingressNodes = ImmutableSortedSet.of(new Pair<>(_srcNode.getHostname(), _srcVrf.getName()));
    return new NodJob(new Settings(), _synthesizer, querySynthesizer, ingressNodes, "tag", false);
}
Also used : Settings(org.batfish.config.Settings) Pair(org.batfish.common.Pair)

Example 15 with Pair

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

the class SynthesizerInputImpl method computeEnabledAcls.

private Map<String, Map<String, IpAccessList>> computeEnabledAcls() {
    if (_topologyInterfaces != null) {
        return toImmutableMap(_topologyInterfaces, Entry::getKey, /* node */
        topologyInterfacesEntry -> {
            String hostname = topologyInterfacesEntry.getKey();
            Configuration c = _configurations.get(hostname);
            return topologyInterfacesEntry.getValue().stream().flatMap(ifaceName -> {
                Interface i = c.getInterfaces().get(ifaceName);
                ImmutableList.Builder<Pair<String, IpAccessList>> interfaceAcls = ImmutableList.builder();
                IpAccessList aclIn = i.getIncomingFilter();
                IpAccessList aclOut = i.getOutgoingFilter();
                if (aclIn != null) {
                    aclIn = _ipAclListSpecializer.specialize(aclIn);
                    interfaceAcls.add(new Pair<>(aclIn.getName(), aclIn));
                }
                if (aclOut != null) {
                    aclOut = _ipAclListSpecializer.specialize(aclOut);
                    interfaceAcls.add(new Pair<>(aclOut.getName(), aclOut));
                }
                i.getSourceNats().forEach(sourceNat -> {
                    IpAccessList sourceNatAcl = sourceNat.getAcl();
                    if (sourceNatAcl != null) {
                        interfaceAcls.add(new Pair<>(sourceNatAcl.getName(), sourceNatAcl));
                    } else {
                        interfaceAcls.add(new Pair<>(DEFAULT_SOURCE_NAT_ACL.getName(), DEFAULT_SOURCE_NAT_ACL));
                    }
                });
                return interfaceAcls.build().stream();
            }).collect(ImmutableSet.toImmutableSet()).stream().collect(ImmutableMap.toImmutableMap(Pair::getFirst, Pair::getSecond));
        });
    } else {
        return _configurations.entrySet().stream().filter(e -> !_disabledNodes.contains(e.getKey())).collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> {
            String hostname = e.getKey();
            Set<String> disabledAcls = _disabledAcls.get(hostname);
            return e.getValue().getIpAccessLists().entrySet().stream().filter(e2 -> disabledAcls == null || !disabledAcls.contains(e2.getKey())).collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
        }));
    }
}
Also used : HeaderSpace(org.batfish.datamodel.HeaderSpace) ForwardingAnalysis(org.batfish.datamodel.ForwardingAnalysis) CommonUtil.toImmutableMap(org.batfish.common.util.CommonUtil.toImmutableMap) HashMap(java.util.HashMap) BatfishException(org.batfish.common.BatfishException) IpAccessList(org.batfish.datamodel.IpAccessList) Function(java.util.function.Function) Edge(org.batfish.datamodel.Edge) Interface(org.batfish.datamodel.Interface) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Topology(org.batfish.datamodel.Topology) Map(java.util.Map) EmptyIpSpace(org.batfish.datamodel.EmptyIpSpace) Configuration(org.batfish.datamodel.Configuration) LineAction(org.batfish.datamodel.LineAction) HeaderSpaceMatchExpr(org.batfish.z3.expr.HeaderSpaceMatchExpr) Pair(org.batfish.common.Pair) Nullable(javax.annotation.Nullable) BooleanExpr(org.batfish.z3.expr.BooleanExpr) ImmutableSet(com.google.common.collect.ImmutableSet) NetworkFactory(org.batfish.datamodel.NetworkFactory) ImmutableMap(com.google.common.collect.ImmutableMap) IpSpaceMatchExpr(org.batfish.z3.expr.IpSpaceMatchExpr) CommonUtil.computeIpOwners(org.batfish.common.util.CommonUtil.computeIpOwners) Range(com.google.common.collect.Range) Set(java.util.Set) IpSpace(org.batfish.datamodel.IpSpace) AclPermit(org.batfish.z3.state.AclPermit) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) List(java.util.List) IpAccessListLine(org.batfish.datamodel.IpAccessListLine) Entry(java.util.Map.Entry) RangeMatchExpr(org.batfish.z3.expr.RangeMatchExpr) Type(org.batfish.z3.state.StateParameter.Type) Ip(org.batfish.datamodel.Ip) Entry(java.util.Map.Entry) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Configuration(org.batfish.datamodel.Configuration) IpAccessList(org.batfish.datamodel.IpAccessList) Interface(org.batfish.datamodel.Interface) Pair(org.batfish.common.Pair)

Aggregations

Pair (org.batfish.common.Pair)17 BatfishException (org.batfish.common.BatfishException)8 Configuration (org.batfish.datamodel.Configuration)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6 HeaderSpace (org.batfish.datamodel.HeaderSpace)6 Interface (org.batfish.datamodel.Interface)6 Ip (org.batfish.datamodel.Ip)6 NodeInterfacePair (org.batfish.datamodel.collections.NodeInterfacePair)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 ArrayList (java.util.ArrayList)5 TreeSet (java.util.TreeSet)5 ImmutableConfiguration (org.apache.commons.configuration2.ImmutableConfiguration)5 Settings (org.batfish.config.Settings)5 AwsConfiguration (org.batfish.representation.aws.AwsConfiguration)5 HostConfiguration (org.batfish.representation.host.HostConfiguration)5 IptablesVendorConfiguration (org.batfish.representation.iptables.IptablesVendorConfiguration)5