Add method to register listener for unknown msg
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoder.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.core;
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.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Decodes incoming messages into message frames.
23  * @author michal.polkorab
24  */
25 public class OFFrameDecoder extends ByteToMessageDecoder {
26
27     /** Length of OpenFlow header */
28     public static final byte LENGTH_OF_HEADER = 8;
29     private static final byte LENGTH_INDEX_IN_HEADER = 2;
30     private static final Logger LOG = LoggerFactory.getLogger(OFFrameDecoder.class);
31     private ConnectionFacade connectionFacade;
32     private boolean firstTlsPass = false;
33
34     /**
35      * Constructor of class.
36      * @param connectionFacade  ConnectionFacade that will be notified
37      *                          with ConnectionReadyNotification after TLS has been successfully set up.
38      * @param tlsPresent true is TLS is required, false otherwise
39      */
40     public OFFrameDecoder(ConnectionFacade connectionFacade, boolean tlsPresent) {
41         LOG.trace("Creating OFFrameDecoder");
42         if (tlsPresent) {
43             firstTlsPass = true;
44         }
45         this.connectionFacade = connectionFacade;
46     }
47
48     @Override
49     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
50         if (cause instanceof io.netty.handler.ssl.NotSslRecordException) {
51             LOG.warn("Not an TLS record exception - please verify TLS configuration.");
52         } else {
53             LOG.warn("Unexpected exception from downstream.", cause);
54         }
55         LOG.warn("Closing connection.");
56         ctx.close();
57     }
58
59     @Override
60     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
61         if (firstTlsPass) {
62             connectionFacade.fireConnectionReadyNotification();
63             firstTlsPass = false;
64         }
65         int readableBytes = bb.readableBytes();
66         if (readableBytes < LENGTH_OF_HEADER) {
67             if (LOG.isDebugEnabled()) {
68                 LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
69                 LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
70             }
71             return;
72         }
73
74         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
75         LOG.debug("length of actual message: {}", length);
76
77         if (readableBytes < length) {
78             if (LOG.isDebugEnabled()) {
79                 LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
80                 LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
81             }
82             return;
83         }
84         LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
85
86         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
87         list.add(messageBuffer);
88         messageBuffer.retain();
89         bb.skipBytes(length);
90     }
91
92 }