Merge "Bug 1277 - Move ByteBuffUtils to separate bundle"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoder.java
index c342e9d56bcf74ec323348822db5b95d2b4c73aa..7e62347524b0a6f714d436b756d9564ae7311dc9 100644 (file)
@@ -1,4 +1,11 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
+/*
+ * Copyright (c) 2013 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;
 
@@ -8,6 +15,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
 
 import java.util.List;
 
+import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionFacade;
 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,25 +30,39 @@ public class OFFrameDecoder extends ByteToMessageDecoder {
     public static final byte LENGTH_OF_HEADER = 8;
     private static final byte LENGTH_INDEX_IN_HEADER = 2;
     private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
+    private ConnectionFacade connectionFacade;
+    private boolean started = false;
 
     /**
      * Constructor of class.
+     * @param connectionFacade 
      */
-    public OFFrameDecoder() {
-        LOGGER.debug("Creating OFFrameDecoder");
+    public OFFrameDecoder(ConnectionFacade connectionFacade) {
+        LOGGER.trace("Creating OFFrameDecoder");
+        this.connectionFacade = connectionFacade;
     }
 
     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-        LOGGER.warn("Unexpected exception from downstream.", cause);
+        if (cause instanceof io.netty.handler.ssl.NotSslRecordException) {
+            LOGGER.warn("Not an TLS record exception - please verify TLS configuration.");
+        } else {
+            LOGGER.warn("Unexpected exception from downstream.", cause);
+        }
+        LOGGER.warn("Closing connection.");
         ctx.close();
     }
 
     @Override
     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
+        if (!started) {
+            connectionFacade.fireConnectionReadyNotification();
+            started = true;
+        }
         int readableBytes = bb.readableBytes();
         if (readableBytes < LENGTH_OF_HEADER) {
-            LOGGER.debug("skipping bb - too few data for header: " + readableBytes);
+            LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );
+            LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
             return;
         }
         
@@ -48,18 +70,12 @@ public class OFFrameDecoder extends ByteToMessageDecoder {
         LOGGER.debug("length of actual message: {}", length);
         
         if (readableBytes < length) {
-            if (LOGGER.isDebugEnabled()) {
-                LOGGER.debug("skipping bb - too few data for msg: " +
+                LOGGER.debug("skipping bytebuf - too few bytes for msg: " +
                         readableBytes + " < " + length);
-                LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
-                LOGGER.debug("readableBytes: " + readableBytes);
-            }
-            
+                LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));
             return;
-        } else {
-            LOGGER.debug("[enough bytes] readableBytes: " + readableBytes);
         }
-        LOGGER.info("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
+        LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
         
         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
         list.add(messageBuffer);