-/*\r
- * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.openflowjava.protocol.impl.core;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-import io.netty.channel.ChannelHandlerContext;\r
-import io.netty.channel.socket.DatagramPacket;\r
-import io.netty.handler.codec.MessageToMessageDecoder;\r
-\r
-import java.util.List;\r
-\r
-import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
-import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;\r
-import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;\r
-import org.opendaylight.openflowjava.util.ByteBufUtils;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * @author michal.polkorab\r
- *\r
- */\r
-public class OFDatagramPacketHandler extends MessageToMessageDecoder<DatagramPacket> {\r
-\r
- private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketHandler.class);\r
-\r
- /** Length of OpenFlow 1.3 header */\r
- public static final byte LENGTH_OF_HEADER = 8;\r
- private static final byte LENGTH_INDEX_IN_HEADER = 2;\r
- private ConnectionAdapterFactory adapterFactory = new ConnectionAdapterFactoryImpl();\r
- private SwitchConnectionHandler connectionHandler;\r
-\r
- /**\r
- * Default constructor\r
- * @param sch the switchConnectionHandler that decides\r
- * what to do with incomming message / channel\r
- */\r
- public OFDatagramPacketHandler(SwitchConnectionHandler sch) {\r
- this.connectionHandler = sch;\r
- }\r
-\r
- @Override\r
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {\r
- LOGGER.warn("Unexpected exception from downstream.", cause);\r
- LOGGER.warn("Closing connection.");\r
- ctx.close();\r
- }\r
-\r
- @Override\r
- protected void decode(ChannelHandlerContext ctx, DatagramPacket msg,\r
- List<Object> out) throws Exception {\r
- LOGGER.debug("OFDatagramPacketFramer");\r
- MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender());\r
- if (consumer == null) {\r
- ConnectionFacade connectionFacade =\r
- adapterFactory.createConnectionFacade(ctx.channel(), msg.sender());\r
- connectionHandler.onSwitchConnected(connectionFacade);\r
- connectionFacade.checkListeners();\r
- UdpConnectionMap.addConnection(msg.sender(), connectionFacade);\r
- }\r
- ByteBuf bb = msg.content();\r
- int readableBytes = bb.readableBytes();\r
- if (readableBytes < LENGTH_OF_HEADER) {\r
- if (LOGGER.isDebugEnabled()) {\r
- LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );\r
- LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));\r
- }\r
- return;\r
- }\r
-\r
- int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);\r
- LOGGER.debug("length of actual message: {}", length);\r
- \r
- if (readableBytes < length) {\r
- if (LOGGER.isDebugEnabled()) {\r
- LOGGER.debug("skipping bytebuf - too few bytes for msg: " +\r
- readableBytes + " < " + length);\r
- LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));\r
- }\r
- return;\r
- }\r
- LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));\r
-\r
- \r
- byte version = bb.readByte();\r
- if ((version == EncodeConstants.OF13_VERSION_ID) || (version == EncodeConstants.OF10_VERSION_ID)) {\r
- LOGGER.debug("detected version: " + version);\r
- ByteBuf messageBuffer = bb.slice();\r
- out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));\r
- messageBuffer.retain();\r
- } else {\r
- LOGGER.warn("detected version: " + version + " - currently not supported");\r
- }\r
- bb.skipBytes(bb.readableBytes());\r
- }\r
-}
\ No newline at end of file
+/*
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.core;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.socket.DatagramPacket;
+import io.netty.handler.codec.MessageToMessageDecoder;
+
+import java.util.List;
+
+import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author michal.polkorab
+ *
+ */
+public class OFDatagramPacketHandler extends MessageToMessageDecoder<DatagramPacket> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketHandler.class);
+
+ /** Length of OpenFlow 1.3 header */
+ public static final byte LENGTH_OF_HEADER = 8;
+ private static final byte LENGTH_INDEX_IN_HEADER = 2;
+ private ConnectionAdapterFactory adapterFactory = new ConnectionAdapterFactoryImpl();
+ private SwitchConnectionHandler connectionHandler;
+
+ /**
+ * Default constructor
+ * @param sch the switchConnectionHandler that decides
+ * what to do with incomming message / channel
+ */
+ public OFDatagramPacketHandler(SwitchConnectionHandler sch) {
+ this.connectionHandler = sch;
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+ LOGGER.warn("Unexpected exception from downstream.", cause);
+ LOGGER.warn("Closing connection.");
+ ctx.close();
+ }
+
+ @Override
+ protected void decode(ChannelHandlerContext ctx, DatagramPacket msg,
+ List<Object> out) throws Exception {
+ LOGGER.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 (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
+ LOGGER.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
+ }
+ return;
+ }
+
+ int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
+ LOGGER.debug("length of actual message: {}", length);
+
+ if (readableBytes < length) {
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
+ LOGGER.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
+ }
+ return;
+ }
+ LOGGER.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)) {
+ LOGGER.debug("detected version: {}", version);
+ ByteBuf messageBuffer = bb.slice();
+ out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));
+ messageBuffer.retain();
+ } else {
+ LOGGER.warn("detected version: {} - currently not supported", version);
+ }
+ bb.skipBytes(bb.readableBytes());
+ }
+}