AclServiceOFFlowBuilder { public static Map<Integer,Integer> getLayer4MaskForRange(int portMin, int portMax) { final int[] offset = { 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 }; final int[] mask = { 0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0, 0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF }; int noOfPorts = portMax - portMin + 1; Map<Integer,Integer> portMap = new HashMap<>(); if (noOfPorts == 1) { portMap.put(portMin, mask[15]); return portMap; } else if (noOfPorts == AclConstants.ALL_LAYER4_PORT) { portMap.put(portMin, AclConstants.ALL_LAYER4_PORT_MASK); return portMap; } if (noOfPorts < 0) { LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin, portMax); return portMap; } String binaryNoOfPorts = Integer.toBinaryString(noOfPorts); if (binaryNoOfPorts.length() > 16) { LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin, portMax); return portMap; } int medianOffset = 16 - binaryNoOfPorts.length(); int medianLength = offset[medianOffset]; int median = 0; for (int tempMedian = 0;tempMedian < portMax;) { tempMedian = medianLength + tempMedian; if (portMin < tempMedian) { median = tempMedian; break; } } int tempMedian = 0; int currentMedain = median; for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) { tempMedian = currentMedain - offset[tempMedianOffset]; for (;portMin <= tempMedian;) { portMap.put(tempMedian, mask[tempMedianOffset]); currentMedain = tempMedian; tempMedian = tempMedian - offset[tempMedianOffset]; } } currentMedain = median; for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) { tempMedian = currentMedain + offset[tempMedianOffset]; for (;portMax >= tempMedian - 1;) { portMap.put(currentMedain, mask[tempMedianOffset]); currentMedain = tempMedian; tempMedian = tempMedian + offset[tempMedianOffset]; } } return portMap; } 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 testgetLayer4MaskForRange_SinglePort() { Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1111, 1111); assertEquals("port L4 mask missing", 1, layer4MaskForRange.size()); }
@Test public void testgetLayer4MaskForRange_MultiplePorts() { Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1024, 2048); assertEquals("port L4 mask missing", 2, layer4MaskForRange.size()); }
@Test public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMin() { Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(0, 1); assertEquals("port L4 mask missing", 1, layer4MaskForRange.size()); }
@Test public void testgetLayer4MaskForRange_IllegalPortRange_ExceedMax() { Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(1, 65536); assertEquals("Illegal ports range", 0, layer4MaskForRange.size()); }
@Test public void testgetLayer4MaskForRange_IllegalPortRange_MinGreaterThanMax() { Map<Integer, Integer> layer4MaskForRange = AclServiceOFFlowBuilder.getLayer4MaskForRange(8192, 4096); assertEquals("Illegal ports range", 0, layer4MaskForRange.size()); }