Integration test with hadshake
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 \r
3 package org.opendaylight.openflowjava.protocol.impl.clients;\r
4 \r
5 import io.netty.buffer.ByteBuf;\r
6 import io.netty.buffer.UnpooledByteBufAllocator;\r
7 import io.netty.channel.ChannelHandlerContext;\r
8 import io.netty.channel.ChannelInboundHandlerAdapter;\r
9 \r
10 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
11 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
12 import org.slf4j.Logger;\r
13 import org.slf4j.LoggerFactory;\r
14 \r
15 import com.google.common.util.concurrent.SettableFuture;\r
16 \r
17 /**\r
18  *\r
19  * @author michal.polkorab\r
20  */\r
21 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {\r
22 \r
23     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);\r
24     private SettableFuture<Boolean> isOnlineFuture;\r
25     private int messagesReceived;\r
26 \r
27     /**\r
28      * @param isOnlineFuture future notifier of connected channel\r
29      */\r
30     public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture) {\r
31         this.isOnlineFuture = isOnlineFuture;\r
32     }\r
33     \r
34     @Override\r
35     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {\r
36         LOGGER.info("SimpleClientHandler - start of read");\r
37         ByteBuf bb = (ByteBuf) msg;\r
38         if (LOGGER.isDebugEnabled()) {\r
39             LOGGER.debug(ByteBufUtils.byteBufToHexString(bb));\r
40         }\r
41         messagesReceived += readHeaders(bb);\r
42         LOGGER.debug("Messages received: " + messagesReceived);\r
43         switch (messagesReceived) {\r
44         case 2:\r
45             LOGGER.debug("FeaturesReply case");\r
46             ByteBuf featuresReply = createFeaturesReplyBytebuf();\r
47             ctx.write(featuresReply);\r
48             LOGGER.debug("FeaturesReply sent");\r
49             break;\r
50         default:\r
51             LOGGER.debug("Default case");\r
52             break;\r
53         }\r
54 \r
55         ctx.flush();\r
56         LOGGER.info("end of read");\r
57     }\r
58 \r
59     private static ByteBuf createFeaturesReplyBytebuf() {\r
60         ByteBuf featuresReply = UnpooledByteBufAllocator.DEFAULT.buffer();\r
61         featuresReply.writeByte(4);\r
62         featuresReply.writeByte(6);\r
63         featuresReply.writeShort(32);\r
64         ByteBuf featuresReplyBody = BufferHelper\r
65                 .buildBuffer("00 01 02 03 04 05 06 07 00 01 02 03 01 01 00 00 00"\r
66                         + " 01 02 03 00 01 02 03");\r
67         featuresReply.writeBytes(featuresReplyBody);\r
68         return featuresReply;\r
69     }\r
70     \r
71     @Override\r
72     public void channelActive(ChannelHandlerContext ctx) throws Exception {\r
73         System.out.println("Client is active");\r
74         if (isOnlineFuture != null) {\r
75             isOnlineFuture.set(true);\r
76             isOnlineFuture = null;\r
77         }\r
78     }\r
79 \r
80     private static int readHeaders(ByteBuf bb) {\r
81         int messages = 0;\r
82         int length = 0;\r
83         while (bb.readableBytes() > 0) {\r
84             length = bb.getShort(2);\r
85             bb.skipBytes(length);\r
86             messages++;\r
87         }\r
88         return messages;\r
89     }\r
90 \r
91 }\r