DhcpLeaseParser { public static DhcpLease parseLeaseContent( final String leaseContent ) { if ( StringUtils.isBlank( leaseContent ) ) { logger.warn( "In parseLeaseContent, lease content is either null or blank." ); return null; } String[] lines = leaseContent.split( "\\r|\\n" ); String ipAddress = lines[0].split( "\\s" )[1]; if ( StringUtils.isBlank( ipAddress ) ) { logger.debug( "In parseLeaseContent, IP Address is null or blank of the lease: {}.", leaseContent ); return null; } if ( !Pattern.matches( IPV4_ADDRESS_REGEX_PATTERN, ipAddress ) ) { logger.warn( "In parseLeaseContent, IP Address '{}' is not a valid IPv4 Address.", ipAddress ); return null; } String line = null; String singleKey = null; String dualKey = null; String[] tokens = null; String macAddress = null; String starts = null; String ends = null; String bindingState = null; String clientHostname = null; for ( int index = 1; index < lines.length - 1; index++ ) { line = lines[index].trim(); if ( StringUtils.isBlank( line ) ) { continue; } line = line.replaceAll( "\\n", "" ); line = line.replaceAll( "\\r", "" ); lines[index] = line.substring( 0, line.indexOf( ';' ) ); tokens = lines[index].trim().split( "\\s" ); singleKey = tokens[0]; dualKey = String.format( "%s %s", tokens[0], tokens[1] ); if ( StringUtils.equals( singleKey, "starts" ) ) { starts = DhcpLeaseParser.getLeaseDate( tokens ); } else if ( StringUtils.equals( singleKey, "ends" ) ) { ends = DhcpLeaseParser.getLeaseDate( tokens ); } else if ( StringUtils.equals( dualKey, "binding state" ) ) { if ( tokens.length == 3 ) { bindingState = tokens[2]; } } else if ( StringUtils.equals( singleKey, "client-hostname" ) ) { if ( tokens.length == 2 ) { if ( tokens[1].startsWith( "\"" ) && tokens[1].endsWith( "\"" ) ) { clientHostname = tokens[1].substring( 1, tokens[1].length() - 1 ); } } } else if ( StringUtils.equals( dualKey, "hardware ethernet" ) ) { if ( StringUtils.isNotBlank( tokens[2] ) ) { if ( Pattern.matches( MAC_ADDRESS_REGEX_PATTERN, tokens[2] ) ) { macAddress = tokens[2]; } } } } if ( StringUtils.isNotBlank( ipAddress ) && StringUtils.isNotBlank( macAddress ) && StringUtils.isNotBlank( ends ) && StringUtils.isNotBlank( bindingState ) ) { DhcpLease dhcpLease = new DhcpLease(); dhcpLease.setIpAddress( ipAddress ); dhcpLease.setMacAddress( macAddress ); dhcpLease.setEnds( ends ); dhcpLease.setBindingState( bindingState ); if ( StringUtils.isNotBlank( starts ) ) { dhcpLease.setStarts( starts ); } if ( StringUtils.isNotBlank( clientHostname ) ) { dhcpLease.setClientHostname( clientHostname ); } logger.debug( "In parseLeaseContent, DHCP Lease: {}", dhcpLease.toString() ); return dhcpLease; } return null; } 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 testParseLeaseContent() { final String leaseContent = "lease 192.168.0.50 {\n" + " starts 4 2011/09/22 20:27:28;\n" + " ends 1 2011/09/26 20:27:28;\n" + " tstp 1 2011/09/26 20:27:28;\n" + " binding state free;\n" + " hardware ethernet 00:00:00:00:00:00;\n" + " uid \"\\001\\000\\033w\\223\\241i\";\n client-hostname \"QCT2C600CFC761D\";\n}"; DhcpLease dhcpLease = DhcpLeaseParser.parseLeaseContent( leaseContent ); assertNotNull( dhcpLease ); assertNotNull( dhcpLease.getIpAddress() ); assertTrue( StringUtils.equals( dhcpLease.getIpAddress(), "192.168.0.50" ) ); assertNotNull( dhcpLease.getMacAddress() ); assertTrue( StringUtils.equals( dhcpLease.getMacAddress(), "00:00:00:00:00:00" ) ); }