private TransformationEvaluator( Flow flow, String srcInterface, Map<String, IpAccessList> namedAcls, Map<String, IpSpace> namedIpSpaces) { _currentFlow = flow; _flowBuilder = flow.toBuilder(); _srcInterface = srcInterface; _namedAcls = ImmutableMap.copyOf(namedAcls); _namedIpSpaces = ImmutableMap.copyOf(namedIpSpaces); _traceSteps = ImmutableList.builder(); initializeAclEvaluator(); }
/** * Creates a return {@link Flow} for the input {@param forwardFlow}. Swaps the source/destination * IPs/ports, and sets the ingress node/vrf/interface. */ static Flow returnFlow( Flow forwardFlow, String returnIngressNode, @Nullable String returnIngressVrf, @Nullable String returnIngressIface) { checkArgument( returnIngressVrf == null ^ returnIngressIface == null, "Either returnIngressVrf or returnIngressIface required, but not both"); return forwardFlow .toBuilder() .setDstIp(forwardFlow.getSrcIp()) .setDstPort(forwardFlow.getSrcPort()) .setSrcIp(forwardFlow.getDstIp()) .setSrcPort(forwardFlow.getDstPort()) .setIngressNode(returnIngressNode) .setIngressVrf(returnIngressVrf) .setIngressInterface(returnIngressIface) .build(); }
@Test public void testFlowDiffs() { Ip dst1 = Ip.parse("1.1.1.1"); Ip src1 = Ip.parse("2.2.2.2"); Ip dst2 = Ip.parse("3.3.3.3"); Ip src2 = Ip.parse("4.4.4.4"); Flow orig = Flow.builder() .setIngressNode("ingressNode") .setTag("tag") .setDstIp(dst1) .setSrcIp(src1) .build(); Flow newDst = orig.toBuilder().setDstIp(dst2).build(); Flow newSrc = orig.toBuilder().setSrcIp(src2).build(); Flow newDstAndSrc = newDst.toBuilder().setSrcIp(src2).build(); assertThat(flowDiffs(null, orig), empty()); assertThat(flowDiffs(orig, null), empty()); assertThat(flowDiffs(orig, orig), empty()); FlowDiff dstDiff = new FlowDiff(PROP_DST_IP, dst1.toString(), dst2.toString()); FlowDiff srcDiff = new FlowDiff(PROP_SRC_IP, src1.toString(), src2.toString()); assertThat(flowDiffs(orig, newDst), contains(dstDiff)); assertThat(flowDiffs(orig, newSrc), contains(srcDiff)); assertThat(flowDiffs(orig, newDstAndSrc), containsInAnyOrder(dstDiff, srcDiff)); }
/** Compute the differences between two flows */ public static SortedSet<FlowDiff> flowDiffs(@Nullable Flow flow1, @Nullable Flow flow2) { if (flow1 == null || flow2 == null || flow1.equals(flow2)) { return ImmutableSortedSet.of(); } checkArgument( flow1 .toBuilder() .setDstIp(flow2.getDstIp()) .setSrcIp(flow2.getSrcIp()) .build() .equals(flow2), "flowDiff only supports differences of src or dst Ip"); ImmutableSortedSet.Builder<FlowDiff> diffs = ImmutableSortedSet.naturalOrder(); if (!flow1.getDstIp().equals(flow2.getDstIp())) { diffs.add( new FlowDiff(PROP_DST_IP, flow1.getDstIp().toString(), flow2.getDstIp().toString())); } if (!flow1.getSrcIp().equals(flow2.getSrcIp())) { diffs.add( new FlowDiff(PROP_SRC_IP, flow1.getSrcIp().toString(), flow2.getSrcIp().toString())); } return diffs.build(); }