2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.socket.DatagramPacket;
14 import io.netty.handler.codec.MessageToMessageDecoder;
16 import java.util.List;
18 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
21 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
23 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;
24 import org.opendaylight.openflowjava.util.ByteBufUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * @author michal.polkorab
32 public class OFDatagramPacketHandler extends MessageToMessageDecoder<DatagramPacket> {
34 private static final Logger LOGGER = LoggerFactory.getLogger(OFDatagramPacketHandler.class);
36 /** Length of OpenFlow 1.3 header */
37 public static final byte LENGTH_OF_HEADER = 8;
38 private static final byte LENGTH_INDEX_IN_HEADER = 2;
39 private ConnectionAdapterFactory adapterFactory = new ConnectionAdapterFactoryImpl();
40 private SwitchConnectionHandler connectionHandler;
44 * @param sch the switchConnectionHandler that decides
45 * what to do with incomming message / channel
47 public OFDatagramPacketHandler(SwitchConnectionHandler sch) {
48 this.connectionHandler = sch;
52 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
53 LOGGER.warn("Unexpected exception from downstream.", cause);
54 LOGGER.warn("Closing connection.");
59 protected void decode(ChannelHandlerContext ctx, DatagramPacket msg,
60 List<Object> out) throws Exception {
61 LOGGER.debug("OFDatagramPacketFramer");
62 MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender());
63 if (consumer == null) {
64 ConnectionFacade connectionFacade =
65 adapterFactory.createConnectionFacade(ctx.channel(), msg.sender());
66 connectionHandler.onSwitchConnected(connectionFacade);
67 connectionFacade.checkListeners();
68 UdpConnectionMap.addConnection(msg.sender(), connectionFacade);
70 ByteBuf bb = msg.content();
71 int readableBytes = bb.readableBytes();
72 if (readableBytes < LENGTH_OF_HEADER) {
73 if (LOGGER.isDebugEnabled()) {
74 LOGGER.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
75 LOGGER.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
80 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
81 LOGGER.debug("length of actual message: {}", length);
83 if (readableBytes < length) {
84 if (LOGGER.isDebugEnabled()) {
85 LOGGER.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
86 LOGGER.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
90 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
93 byte version = bb.readByte();
94 if ((version == EncodeConstants.OF13_VERSION_ID) || (version == EncodeConstants.OF10_VERSION_ID)) {
95 LOGGER.debug("detected version: {}", version);
96 ByteBuf messageBuffer = bb.slice();
97 out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));
98 messageBuffer.retain();
100 LOGGER.warn("detected version: {} - currently not supported", version);
102 bb.skipBytes(bb.readableBytes());