Search in sources :

Example 31 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr in project batfish by batfish.

the class SimplifierTest method testSimplifyAndExprNestedConjuncts.

/**
 * Test that nested ANDs get flattened into one AND.
 */
@Test
public void testSimplifyAndExprNestedConjuncts() {
    BooleanExpr p1 = newAtom();
    BooleanExpr p2 = newAtom();
    BooleanExpr p3 = newAtom();
    AndExpr leftNested = new AndExpr(of(new AndExpr(of(p1, p2)), p3));
    AndExpr rightNested = new AndExpr(of(p1, new AndExpr(of(p2, p3))));
    assertThat(leftNested, not(equalTo(rightNested)));
    assertThat(simplifyBooleanExpr(leftNested), equalTo(new AndExpr(of(p1, p2, p3))));
    assertThat(simplifyBooleanExpr(rightNested), equalTo(new AndExpr(of(p1, p2, p3))));
}
Also used : AndExpr(org.batfish.z3.expr.AndExpr) BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) Test(org.junit.Test)

Example 32 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr in project batfish by batfish.

the class SimplifierTest method testSimplifyOrTrue.

/**
 * Test that an OR node with a TRUE child simplifies to TRUE.
 */
@Test
public void testSimplifyOrTrue() {
    BooleanExpr p1 = newAtom();
    OrExpr or = new OrExpr(of(p1, TrueExpr.INSTANCE));
    assertThat(simplifyBooleanExpr(or), equalTo(TrueExpr.INSTANCE));
}
Also used : BooleanExpr(org.batfish.z3.expr.BooleanExpr) Simplifier.simplifyBooleanExpr(org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr) OrExpr(org.batfish.z3.expr.OrExpr) Test(org.junit.Test)

Example 33 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr in project batfish by batfish.

the class DefaultTransitionGeneratorTest method testPreOutEdgePostNat.

@Test
public void testPreOutEdgePostNat() {
    SynthesizerInput input = MockSynthesizerInput.builder().setEnabledEdges(ImmutableSet.of(new Edge(NODE1, INTERFACE1, NODE2, INTERFACE1), new Edge(NODE1, INTERFACE1, NODE2, INTERFACE2), new Edge(NODE1, INTERFACE2, NODE2, INTERFACE1), new Edge(NODE1, INTERFACE2, NODE2, INTERFACE2), new Edge(NODE2, INTERFACE1, NODE1, INTERFACE1), new Edge(NODE2, INTERFACE1, NODE1, INTERFACE2), new Edge(NODE2, INTERFACE2, NODE1, INTERFACE1), new Edge(NODE2, INTERFACE2, NODE1, INTERFACE2))).build();
    Set<RuleStatement> rules = ImmutableSet.copyOf(DefaultTransitionGenerator.generateTransitions(input, ImmutableSet.of(PreOutEdgePostNat.State.INSTANCE)));
    BooleanExpr newSrcIpEqualsCurrentSrcIp = new EqExpr(new VarIntExpr(TransformationHeaderField.NEW_SRC_IP), new VarIntExpr(TransformationHeaderField.NEW_SRC_IP.getCurrent()));
    // PreOutEdgePostNatForTopologyEdges
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE1, INTERFACE1, NODE2, INTERFACE1)), ImmutableSet.of(), new PreOutEdgePostNat(NODE1, INTERFACE1, NODE2, INTERFACE1))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE1, INTERFACE1, NODE2, INTERFACE2)), ImmutableSet.of(), new PreOutEdgePostNat(NODE1, INTERFACE1, NODE2, INTERFACE2))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE1, INTERFACE2, NODE2, INTERFACE1)), ImmutableSet.of(), new PreOutEdgePostNat(NODE1, INTERFACE2, NODE2, INTERFACE1))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE1, INTERFACE2, NODE2, INTERFACE2)), ImmutableSet.of(), new PreOutEdgePostNat(NODE1, INTERFACE2, NODE2, INTERFACE2))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE2, INTERFACE1, NODE1, INTERFACE1)), ImmutableSet.of(), new PreOutEdgePostNat(NODE2, INTERFACE1, NODE1, INTERFACE1))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE2, INTERFACE1, NODE1, INTERFACE2)), ImmutableSet.of(), new PreOutEdgePostNat(NODE2, INTERFACE1, NODE1, INTERFACE2))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE2, INTERFACE2, NODE1, INTERFACE1)), ImmutableSet.of(), new PreOutEdgePostNat(NODE2, INTERFACE2, NODE1, INTERFACE1))));
    assertThat(rules, hasItem(new TransformationRuleStatement(newSrcIpEqualsCurrentSrcIp, ImmutableSet.of(new PreOutEdge(NODE2, INTERFACE2, NODE1, INTERFACE2)), ImmutableSet.of(), new PreOutEdgePostNat(NODE2, INTERFACE2, NODE1, INTERFACE2))));
}
Also used : VarIntExpr(org.batfish.z3.expr.VarIntExpr) PreOutEdge(org.batfish.z3.state.PreOutEdge) MockSynthesizerInput(org.batfish.z3.MockSynthesizerInput) SynthesizerInput(org.batfish.z3.SynthesizerInput) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) RuleStatement(org.batfish.z3.expr.RuleStatement) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) EqExpr(org.batfish.z3.expr.EqExpr) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) Edge(org.batfish.datamodel.Edge) PostOutEdge(org.batfish.z3.state.PostOutEdge) PreOutEdge(org.batfish.z3.state.PreOutEdge) BooleanExpr(org.batfish.z3.expr.BooleanExpr) PreOutEdgePostNat(org.batfish.z3.state.PreOutEdgePostNat) Test(org.junit.Test)

Example 34 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr in project batfish by batfish.

the class SynthesizerInputImpl method computeSourceNats.

private Map<String, Map<String, List<Entry<AclPermit, BooleanExpr>>>> computeSourceNats() {
    return toImmutableMap(_topologyInterfaces, Entry::getKey, topologyInterfacesEntryByHostname -> {
        String hostname = topologyInterfacesEntryByHostname.getKey();
        Set<String> ifaces = topologyInterfacesEntryByHostname.getValue();
        Configuration c = _configurations.get(hostname);
        return toImmutableMap(ifaces, Function.identity(), ifaceName -> c.getInterfaces().get(ifaceName).getSourceNats().stream().map(sourceNat -> {
            IpAccessList acl = sourceNat.getAcl();
            String aclName = acl == null ? DEFAULT_SOURCE_NAT_ACL.getName() : acl.getName();
            AclPermit preconditionPreTransformationState = new AclPermit(hostname, aclName);
            BooleanExpr transformationConstraint = new RangeMatchExpr(TransformationHeaderField.NEW_SRC_IP, TransformationHeaderField.NEW_SRC_IP.getSize(), ImmutableSet.of(Range.closed(sourceNat.getPoolIpFirst().asLong(), sourceNat.getPoolIpLast().asLong())));
            return Maps.immutableEntry(preconditionPreTransformationState, transformationConstraint);
        }).collect(ImmutableList.toImmutableList()));
    });
}
Also used : Entry(java.util.Map.Entry) Configuration(org.batfish.datamodel.Configuration) AclPermit(org.batfish.z3.state.AclPermit) RangeMatchExpr(org.batfish.z3.expr.RangeMatchExpr) IpAccessList(org.batfish.datamodel.IpAccessList) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Example 35 with BooleanExpr

use of org.batfish.z3.expr.BooleanExpr in project batfish by batfish.

the class SynthesizerInputImpl method computeArpTrueEdge.

private Map<String, Map<String, Map<String, Map<String, Map<String, BooleanExpr>>>>> computeArpTrueEdge(Map<Edge, IpSpace> arpTrueEdge) {
    Map<String, Map<String, Map<String, Map<String, Map<String, BooleanExpr>>>>> output = new HashMap<>();
    arpTrueEdge.forEach((edge, ipSpace) -> {
        ipSpace = _ipSpaceSpecializer.specialize(ipSpace);
        if (ipSpace instanceof EmptyIpSpace) {
            return;
        }
        String hostname = edge.getNode1();
        String outInterface = edge.getInt1();
        String vrf = _configurations.get(hostname).getInterfaces().get(outInterface).getVrfName();
        String recvNode = edge.getNode2();
        String recvInterface = edge.getInt2();
        output.computeIfAbsent(hostname, n -> new HashMap<>()).computeIfAbsent(vrf, n -> new HashMap<>()).computeIfAbsent(outInterface, n -> new HashMap<>()).computeIfAbsent(recvNode, n -> new HashMap<>()).put(recvInterface, new IpSpaceMatchExpr(ipSpace, false, true));
    });
    // freeze
    return toImmutableMap(output, Entry::getKey, /* node */
    outputByHostnameEntry -> toImmutableMap(outputByHostnameEntry.getValue(), Entry::getKey, /* vrf */
    outputByVrfEntry -> toImmutableMap(outputByVrfEntry.getValue(), Entry::getKey, /* outInterface */
    outputByOutInterfaceEntry -> toImmutableMap(outputByOutInterfaceEntry.getValue(), Entry::getKey, /* recvNode */
    outputByRecvNodeEntry -> toImmutableMap(outputByRecvNodeEntry.getValue(), Entry::getKey, /* recvInterface */
    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) HashMap(java.util.HashMap) IpSpaceMatchExpr(org.batfish.z3.expr.IpSpaceMatchExpr) CommonUtil.toImmutableMap(org.batfish.common.util.CommonUtil.toImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EmptyIpSpace(org.batfish.datamodel.EmptyIpSpace) BooleanExpr(org.batfish.z3.expr.BooleanExpr)

Aggregations

BooleanExpr (org.batfish.z3.expr.BooleanExpr)43 Test (org.junit.Test)27 AndExpr (org.batfish.z3.expr.AndExpr)14 NotExpr (org.batfish.z3.expr.NotExpr)13 Simplifier.simplifyBooleanExpr (org.batfish.z3.expr.visitors.Simplifier.simplifyBooleanExpr)13 BasicRuleStatement (org.batfish.z3.expr.BasicRuleStatement)10 ImmutableList (com.google.common.collect.ImmutableList)9 HeaderSpaceMatchExpr (org.batfish.z3.expr.HeaderSpaceMatchExpr)8 BoolExpr (com.microsoft.z3.BoolExpr)7 IfExpr (org.batfish.z3.expr.IfExpr)7 VarIntExpr (org.batfish.z3.expr.VarIntExpr)7 BoolExprTransformer.toBoolExpr (org.batfish.z3.expr.visitors.BoolExprTransformer.toBoolExpr)7 List (java.util.List)6 IpWildcard (org.batfish.datamodel.IpWildcard)6 EqExpr (org.batfish.z3.expr.EqExpr)6 OrExpr (org.batfish.z3.expr.OrExpr)6 QueryStatement (org.batfish.z3.expr.QueryStatement)6 RuleStatement (org.batfish.z3.expr.RuleStatement)6 TransformationRuleStatement (org.batfish.z3.expr.TransformationRuleStatement)6 Ip (org.batfish.datamodel.Ip)5