### Question:
OFDatagramPacketHandler extends MessageToMessageDecoder<DatagramPacket> { @Override protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception { LOG.debug("OFDatagramPacketFramer"); MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender()); if (consumer == null) { ConnectionFacade connectionFacade = adapterFactory.createConnectionFacade(ctx.channel(), msg.sender(), false); connectionHandler.onSwitchConnected(connectionFacade); connectionFacade.checkListeners(); UdpConnectionMap.addConnection(msg.sender(), connectionFacade); } ByteBuf bb = msg.content(); int readableBytes = bb.readableBytes(); if (readableBytes < LENGTH_OF_HEADER) { if (LOG.isDebugEnabled()) { LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER); LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb)); } return; } int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER); LOG.debug("length of actual message: {}", length); if (readableBytes < length) { if (LOG.isDebugEnabled()) { LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length); LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb)); } return; } LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1)); byte version = bb.readByte(); if ((version == EncodeConstants.OF13_VERSION_ID) || (version == EncodeConstants.OF10_VERSION_ID)) { LOG.debug("detected version: {}", version); ByteBuf messageBuffer = bb.slice(); out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender())); messageBuffer.retain(); } else { LOG.warn("detected version: {} - currently not supported", version); } bb.skipBytes(bb.readableBytes()); } OFDatagramPacketHandler(SwitchConnectionHandler sch); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); static final byte LENGTH_OF_HEADER; }### Answer:
@Test public void test(){ OFDatagramPacketHandler handler = new OFDatagramPacketHandler(switchConnHandler); byte version = EncodeConstants.OF13_VERSION_ID; ByteBuf messageBuffer = ByteBufUtils.hexStringToByteBuf("04 02 00 08 01 02 03 04"); InetSocketAddress recipientISA = InetSocketAddress.createUnresolved("localhost", 9876); InetSocketAddress senderISA = InetSocketAddress.createUnresolved("192.168.15.24", 21021); DatagramPacket datagramPacket = new DatagramPacket(messageBuffer, recipientISA, senderISA); UdpConnectionMap.addConnection(datagramPacket.sender(), consumerMock); List<Object> outList = new ArrayList<>(); try { handler.decode(ctxMock, datagramPacket, outList); } catch (Exception e) { Assert.fail("Wrong - Unexcepted exception occurred"); } VersionMessageUdpWrapper versionUdpWrapper = (VersionMessageUdpWrapper) outList.get(0); Assert.assertEquals("Wrong - incorrect version has been decoded",version, versionUdpWrapper.getVersion()); Assert.assertEquals("Wrong - sender addresses are different", senderISA, versionUdpWrapper.getAddress()); messageBuffer.readerIndex(1); Assert.assertEquals("Wrong - undecoded part of input ByteBuff is differnt to output",0, messageBuffer.compareTo(versionUdpWrapper.getMessageBuffer())); }