class OspfMessageDecoder extends FrameDecoder {

    @Override
    protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
        log.debug("OspfMessageDecoder::Message received <:> length {}", buffer.readableBytes());
        if (!channel.isConnected()) {
            log.info("Channel is not connected.");
            return null;
        }
        OspfMessageReader messageReader = new OspfMessageReader();
        List<OspfMessage> ospfMessageList = new LinkedList<>();
        while (buffer.readableBytes() >= OspfUtil.MINIMUM_FRAME_LEN) {
            ChannelBuffer ospfDataBuffer = buffer.readBytes(OspfUtil.MINIMUM_FRAME_LEN);
            int readableBytes = ospfDataBuffer.readableBytes();
            OspfMessage message = messageReader.readFromBuffer(ospfDataBuffer);
            if (message != null) {
                if (ospfDataBuffer.readableBytes() >= OspfUtil.METADATA_LEN) {
                    ospfDataBuffer.readerIndex(readableBytes - OspfUtil.METADATA_LEN);
                    log.debug("IsisMessageDecoder::Reading metadata <:> length {}", ospfDataBuffer.readableBytes());


                    int interfaceIndex = ospfDataBuffer.readByte();
                    byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
                    ospfDataBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
                    Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);

                    message.setSourceIp(sourceIP);
                    message.setInterfaceIndex(interfaceIndex);
                }
                ospfMessageList.add(message);
            }
        }
        return (ospfMessageList.size() > 0) ? ospfMessageList : null;
    }

}