class DhcpLeaseParser {

    public static DhcpLease parseLeaseContent( final String leaseContent )
    {

        // check that leaseContent is not null or blank
        if ( StringUtils.isBlank( leaseContent ) )
        {
            logger.warn( "In parseLeaseContent, lease content is either null or blank." );
            return null;
        }

        // split leaseContent after each line break
        String[] lines = leaseContent.split( "\\r|\\n" );

        // first line contains IP address.
        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;
        }

        // parse the remaining lines for mac address
        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();

            // skip if line is blank
            if ( StringUtils.isBlank( line ) )
            {
                continue;
            }

            // replace line ending characters with blank string.
            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 0 2016/06/19 10:06:00;
                starts = DhcpLeaseParser.getLeaseDate( tokens );
            }
            else if ( StringUtils.equals( singleKey, "ends" ) )
            {
                // ends 1 2016/06/20 10:06:00;
                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" ) )
            {
                // client-hostname "QCT2C600CFC788A"
                if ( tokens.length == 2 )
                {
                    // strip off quote
                    if ( tokens[1].startsWith( "\"" ) && tokens[1].endsWith( "\"" ) )
                    {
                        clientHostname = tokens[1].substring( 1, tokens[1].length() - 1 );
                    }
                }
            }
            else if ( StringUtils.equals( dualKey, "hardware ethernet" ) )
            {
                // hardware ethernet 00:00:00:00:00:00;
                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();

}