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
10 package org.opendaylight.openflowjava.protocol.impl.clients;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.socket.DatagramPacket;
15 import io.netty.handler.codec.MessageToMessageDecoder;
17 import java.util.List;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Class for decoding incoming udp messages into message frames.
25 * @author michal.polkorab
27 public class UdpSimpleClientFramer extends MessageToMessageDecoder<DatagramPacket> {
29 /** Length of OpenFlow 1.3 header */
30 public static final byte LENGTH_OF_HEADER = 8;
31 private static final byte LENGTH_INDEX_IN_HEADER = 2;
32 private static final Logger LOGGER = LoggerFactory.getLogger(UdpSimpleClientFramer.class);
35 * Constructor of class.
37 public UdpSimpleClientFramer() {
38 LOGGER.trace("Creating OFFrameDecoder");
42 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
43 LOGGER.warn("Unexpected exception from downstream.", cause);
48 protected void decode(ChannelHandlerContext chc, DatagramPacket msg, List<Object> list) throws Exception {
49 ByteBuf bb = msg.content();
50 if (bb.readableBytes() < LENGTH_OF_HEADER) {
51 LOGGER.debug("skipping bb - too few data for header: {}", bb.readableBytes());
55 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
56 if (bb.readableBytes() < length) {
57 LOGGER.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length);
60 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
62 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
63 list.add(messageBuffer);
64 messageBuffer.retain();