Remove redundant exception declarations
[openflowplugin.git] / samples / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientFramer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9
10 package org.opendaylight.openflowjava.protocol.impl.clients;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.util.List;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Class for decoding incoming messages into message frames.
21  *
22  * @author michal.polkorab
23  */
24 public class SimpleClientFramer extends ByteToMessageDecoder {
25
26     /** Length of OpenFlow 1.3 header. */
27     public static final byte LENGTH_OF_HEADER = 8;
28     private static final byte LENGTH_INDEX_IN_HEADER = 2;
29     private static final Logger LOG = LoggerFactory.getLogger(SimpleClientFramer.class);
30
31     /**
32      * Constructor of class.
33      */
34     public SimpleClientFramer() {
35         LOG.trace("Creating OFFrameDecoder");
36     }
37
38     @Override
39     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
40         LOG.warn("Unexpected exception from downstream.", cause);
41         ctx.close();
42     }
43
44     @Override
45     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) {
46         if (bb.readableBytes() < LENGTH_OF_HEADER) {
47             LOG.debug("skipping bb - too few data for header: {}", bb.readableBytes());
48             return;
49         }
50
51         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
52         if (bb.readableBytes() < length) {
53             LOG.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length);
54             return;
55         }
56         LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
57
58         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
59         list.add(messageBuffer);
60         messageBuffer.retain();
61         bb.skipBytes(length);
62     }
63
64 }