From d9eb4595415e33a2730aed6931829b5839ed5fb6 Mon Sep 17 00:00:00 2001 From: Michal Polkorab Date: Thu, 14 Nov 2013 16:13:11 +0100 Subject: [PATCH] SimpleClient secure mode fixed Signed-off-by: Michal Polkorab --- .../protocol/impl/integration/MockPlugin.java | 9 ++- .../impl/clients/SimpleClientFramer.java | 60 +++++++++++++++++++ .../impl/clients/SimpleClientHandler.java | 13 ++-- .../impl/clients/SimpleClientInitializer.java | 1 + 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientFramer.java diff --git a/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java b/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java index e98b48ed..8c5f093f 100644 --- a/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java +++ b/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java @@ -184,7 +184,14 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan @Override public void onPacketInMessage(PacketInMessage notification) { LOGGER.debug("PacketIn message received"); - + LOGGER.debug("BufferId: " + notification.getBufferId()); + LOGGER.debug("TotalLength: " + notification.getTotalLen()); + LOGGER.debug("Reason: " + notification.getReason()); + LOGGER.debug("TableId: " + notification.getTableId()); + LOGGER.debug("Cookie: " + notification.getCookie()); + LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass()); + LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField()); + LOGGER.debug("Datasize: " + notification.getData().length); } @Override diff --git a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientFramer.java b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientFramer.java new file mode 100644 index 00000000..95eb08c0 --- /dev/null +++ b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientFramer.java @@ -0,0 +1,60 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ + +package org.opendaylight.openflowjava.protocol.impl.clients; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class for decoding incoming messages into message frames. + * + * @author michal.polkorab + */ +public class SimpleClientFramer extends ByteToMessageDecoder { + + /** Length of OpenFlow 1.3 header */ + public static final byte LENGTH_OF_HEADER = 8; + private static final byte LENGTH_INDEX_IN_HEADER = 2; + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientFramer.class); + + /** + * Constructor of class. + */ + public SimpleClientFramer() { + LOGGER.debug("Creating OFFrameDecoder"); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + LOGGER.warn("Unexpected exception from downstream.", cause); + ctx.close(); + } + + @Override + protected void decode(ChannelHandlerContext chc, ByteBuf bb, List list) throws Exception { + if (bb.readableBytes() < LENGTH_OF_HEADER) { + LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes()); + return; + } + + int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER); + if (bb.readableBytes() < length) { + LOGGER.debug("skipping bb - too few data for msg: " + + bb.readableBytes() + " < " + length); + return; + } + LOGGER.info("OF Protocol message received, type:{}", bb.getByte(1)); + + ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length); + list.add(messageBuffer); + messageBuffer.retain(); + bb.skipBytes(length); + } + +} diff --git a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java index 04147af0..e9453b58 100644 --- a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java +++ b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java @@ -19,7 +19,7 @@ import com.google.common.util.concurrent.SettableFuture; public class SimpleClientHandler extends ChannelInboundHandlerAdapter { protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class); - private static final int OFP_HEADER_LENGTH = 8; + private static final int LENGTH_INDEX_IN_HEADER = 2; private SettableFuture isOnlineFuture; protected ScenarioHandler scenarioHandler; @@ -34,18 +34,13 @@ public class SimpleClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - LOGGER.info("SimpleClientHandler - start of read"); ByteBuf bb = (ByteBuf) msg; if (LOGGER.isDebugEnabled()) { LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(bb)); } - - if (bb.readableBytes() < OFP_HEADER_LENGTH) { - LOGGER.debug("too few bytes received: "+bb.readableBytes()+" - wait for next data portion"); - return; - } - int msgSize = bb.getUnsignedShort(2); - byte[] message = new byte[msgSize]; + int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER); + LOGGER.info("SimpleClientHandler - start of read"); + byte[] message = new byte[length]; bb.readBytes(message); scenarioHandler.addOfMsg(message); LOGGER.info("end of read"); diff --git a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientInitializer.java b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientInitializer.java index 8ca01e8e..eed60970 100644 --- a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientInitializer.java +++ b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientInitializer.java @@ -40,6 +40,7 @@ public class SimpleClientInitializer extends ChannelInitializer { .createSSLEngine(); engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); + pipeline.addLast("framer", new SimpleClientFramer()); } SimpleClientHandler simpleClientHandler = new SimpleClientHandler(isOnlineFuture, scenarioHandler); simpleClientHandler.setScenario(scenarioHandler); -- 2.36.6