DhcpLeaseParser { public static List<DhcpLease> parseLeaseFile( final String dhcpLeaseFile ) { if ( StringUtils.isBlank( dhcpLeaseFile ) ) { logger.warn( "In parseLeaseFile, DHCP lease file is either null or blank." ); return null; } if ( !FileUtil.isFileExists( dhcpLeaseFile ) ) { logger.warn( "In parseLeaseFile, DHCP lease file '{}' does not exist.", dhcpLeaseFile ); return null; } final String dhcpLeaseFileContent = FileUtil.readFileToString( dhcpLeaseFile ); if ( StringUtils.isBlank( dhcpLeaseFileContent ) ) { logger.warn( "In parseLeaseFile, DHCP lease file '{}' content is either null or blank." ); return null; } Map<String, DhcpLease> dhcpLeasesMap = new HashMap<String, DhcpLease>(); Pattern pattern = Pattern.compile( DHCP_LEASE_REGEX_PATTERN, Pattern.DOTALL ); Matcher matcher = pattern.matcher( dhcpLeaseFileContent ); DhcpLease dhcpLease = null; String macAddress = null; while ( matcher.find() ) { dhcpLease = parseLeaseContent( matcher.group() ); if ( dhcpLease != null ) { macAddress = dhcpLease.getMacAddress(); if ( !dhcpLeasesMap.containsKey( macAddress ) ) { dhcpLeasesMap.put( macAddress, dhcpLease ); } else { if ( isLatestLease( dhcpLeasesMap.get( macAddress ), dhcpLease ) ) { dhcpLeasesMap.put( macAddress, dhcpLease ); } } } } return new ArrayList<DhcpLease>( dhcpLeasesMap.values() ); } private  DhcpLeaseParser(); static List<DhcpLease> parseLeaseFile( final String dhcpLeaseFile ); static DhcpLease parseLeaseContent( final String leaseContent ); static boolean isLatestLease( DhcpLease existingLease, DhcpLease newLease );  }
@Test public void testParseLeaseFile() { final String dhcpLeaseFile = "src/test/resources/test-dhcpd.lease"; List<DhcpLease> dhcpLeases = DhcpLeaseParser.parseLeaseFile( dhcpLeaseFile ); assertNotNull( dhcpLeases ); assertTrue( dhcpLeases.size() > 0 ); assertTrue( dhcpLeases.size() == 8 ); DhcpLease dhcpLease = dhcpLeases.get( 0 ); assertNotNull( dhcpLease ); assertTrue( StringUtils.equals( dhcpLease.getStarts(), "2016/06/19 09:55:11" ) ); assertTrue( StringUtils.equals( dhcpLease.getEnds(), "2016/06/20 09:55:11" ) ); }