AclServiceOFFlowBuilder { public static Map<String,List<MatchInfoBase>> programTcpFlow(AceIp acl) { Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>(); SourcePortRange sourcePortRange = acl.getSourcePortRange(); DestinationPortRange destinationPortRange = acl.getDestinationPortRange(); if (sourcePortRange == null && destinationPortRange == null) { List<MatchInfoBase> flowMatches = new ArrayList<>(); flowMatches.addAll(addSrcIpMatches(acl)); flowMatches.addAll(addDstIpMatches(acl)); flowMatches.add(new MatchIpProtocol(acl.getProtocol().toJava())); String flowId = "TCP_SOURCE_ALL_"; flowMatchesMap.put(flowId,flowMatches); return flowMatchesMap; } if (sourcePortRange != null) { Map<Integer, Integer> portMaskMap = getLayer4MaskForRange( sourcePortRange.getLowerPort().getValue().toJava(), sourcePortRange.getUpperPort().getValue().toJava()); updateFlowMatches(acl, portMaskMap, flowMatchesMap, NxMatchTcpSourcePort::new, "TCP_SOURCE_"); } if (destinationPortRange != null) { Map<Integer, Integer> portMaskMap = getLayer4MaskForRange( destinationPortRange.getLowerPort().getValue().toJava(), destinationPortRange.getUpperPort().getValue().toJava()); updateFlowMatches(acl, portMaskMap, flowMatchesMap, NxMatchTcpDestinationPort::new, "TCP_DESTINATION_"); } return flowMatchesMap; } private  AclServiceOFFlowBuilder(); @Nullable static Map<String, List<MatchInfoBase>> programIpFlow(Matches matches); static Map<String,List<MatchInfoBase>> programEtherFlow(AceIp acl); static Map<String,List<MatchInfoBase>> programOtherProtocolFlow(AceIp acl); static Map<String,List<MatchInfoBase>> programIcmpFlow(AceIp acl); static Map<String,List<MatchInfoBase>> programTcpFlow(AceIp acl); static Map<String,List<MatchInfoBase>> programUdpFlow(AceIp acl); static List<MatchInfoBase> addSrcIpMatches(AceIp acl); static List<MatchInfoBase> addDstIpMatches(AceIp acl); static List<MatchInfoBase> addLPortTagMatches(int lportTag, int conntrackState, int conntrackMask,
            Class<? extends ServiceModeBase> serviceMode); static List<InstructionInfo> getDropInstructionInfo(); static List<InstructionInfo> getResubmitInstructionInfo(short tableId); static List<InstructionInfo> getGotoInstructionInfo(short gotoTableId); static Map<Integer,Integer> getLayer4MaskForRange(int portMin, int portMax);  }
@Test public void testprogramTcpFlow_NoSrcDstPortRange() { AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", null, null, (short) 1); Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build()); List<MatchInfoBase> flowMatches = flowMatchesMap.get("TCP_SOURCE_ALL_"); AclServiceTestUtils.verifyGeneralFlows(flowMatches, "1", "10.1.1.1", "20.1.1.1", "24"); AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchTcpSourcePort.class); AclServiceTestUtils.verifyMatchFieldTypeDontExist(flowMatches, NxMatchTcpDestinationPort.class); }
@Test public void testprogramTcpFlow_WithSrcDstPortRange() { AceIpBuilder builder = AclServiceTestUtils.prepareAceIpBuilder("10.1.1.1/24", "20.1.1.1/24", "1024", "1024", (short) 1); Map<String, List<MatchInfoBase>> flowMatchesMap = AclServiceOFFlowBuilder.programTcpFlow(builder.build()); List<MatchInfoBase> srcFlowMatches = new ArrayList<>(); List<MatchInfoBase> dstFlowMatches = new ArrayList<>(); for (String flowId : flowMatchesMap.keySet()) { if (flowId.startsWith("TCP_SOURCE_")) { srcFlowMatches.addAll(flowMatchesMap.get(flowId)); } if (flowId.startsWith("TCP_DESTINATION_")) { dstFlowMatches.addAll(flowMatchesMap.get(flowId)); } } AclServiceTestUtils.verifyGeneralFlows(srcFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24"); List<MatchInfoBase> tcpSrcMatches = srcFlowMatches.stream().filter( item -> item instanceof NxMatchTcpSourcePort).collect(Collectors.toList()); assertEquals(new NxMatchTcpSourcePort(1024, 65535), tcpSrcMatches.get(0)); AclServiceTestUtils.verifyGeneralFlows(dstFlowMatches, "1", "10.1.1.1", "20.1.1.1", "24"); List<MatchInfoBase> tcpDstMatches = dstFlowMatches.stream().filter( item -> item instanceof NxMatchTcpDestinationPort).collect(Collectors.toList()); assertEquals(new NxMatchTcpDestinationPort(1024, 65535), tcpDstMatches.get(0)); }