Renamed packages to org.opendaylight.openflowjava.protocol.impl.*
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OF13Codec.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.core;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.channel.ChannelHandlerContext;\r
6 import io.netty.channel.ChannelInboundHandlerAdapter;\r
7 \r
8 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;\r
9 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
10 import org.slf4j.Logger;\r
11 import org.slf4j.LoggerFactory;\r
12 \r
13 /**\r
14  * Transforms OpenFlow Protocol messages to Java messages / objects and takes appropriate\r
15  * actions\r
16  *\r
17  * @author michal.polkorab\r
18  */\r
19 public class OF13Codec extends ChannelInboundHandlerAdapter {\r
20 \r
21     // TODO - fix with enum in API\r
22     private static final int MESSAGE_TYPES = 29;\r
23     private static final Logger LOGGER = LoggerFactory.getLogger(OF13Codec.class);\r
24 \r
25     /**\r
26      * Constructor of class\r
27      */\r
28     public OF13Codec() {\r
29         LOGGER.info("Creating OF 1.3 Codec");\r
30         \r
31     }\r
32 \r
33     @Override\r
34     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {\r
35         LOGGER.info("Reading frame");\r
36         LOGGER.debug("Received msg is of type: " + msg.getClass().getName());\r
37         ByteBuf bb = (ByteBuf) msg;\r
38         if (LOGGER.isDebugEnabled()) {\r
39             LOGGER.debug(ByteBufUtils.byteBufToHexString(bb));\r
40         }\r
41         \r
42         bb.markReaderIndex();\r
43         short type = bb.readUnsignedByte();\r
44         int length = bb.readUnsignedShort();\r
45         long xid = bb.readUnsignedInt();\r
46         bb.resetReaderIndex();\r
47 \r
48         if (!checkOFHeader(type, length)) {\r
49             bb.discardReadBytes();\r
50             LOGGER.info("Non-OF Protocol message received (discarding)");\r
51             return;\r
52         }\r
53 \r
54         // TODO - change hardcoded version to constant, enum, ...\r
55         DeserializationFactory.bufferToMessage(bb, (short) 0x04);\r
56         \r
57         // TODO - delete this switch case after completing deserialization factory\r
58         ByteBuf out = ctx.alloc().buffer();\r
59         switch (type) {\r
60             case 0: {\r
61                 LOGGER.info("OFPT_HELLO received");\r
62                 byte[] hello = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0, 0x0, 0x0, 0x01};\r
63                 out.writeBytes(hello);\r
64                 break;\r
65             }\r
66             case 1:\r
67                 LOGGER.info("OFPT_ERROR received");\r
68                 break;\r
69             case 2: {\r
70                 LOGGER.info("OFPT_ECHO_REQUEST received");\r
71                 byte[] echoReply = new byte[]{0x04, 0x03, 0x00, 0x08};\r
72                 out.writeBytes(echoReply);\r
73                 out.writeInt((int) xid);\r
74                 // TODO - append original data field\r
75                 break;\r
76             }\r
77             case 3:\r
78                 LOGGER.info("OFPT_ECHO_REPLY received");\r
79                 break;\r
80             case 5:\r
81                 LOGGER.info("OFPT_FEATURES_REQUEST received");\r
82                 break;\r
83             case 6:\r
84                 LOGGER.info("OFPT_FEATURES_REPLY received");\r
85                 break;\r
86             default:\r
87                 LOGGER.info("Received message type: " + type);\r
88                 break;\r
89         }\r
90         if (LOGGER.isDebugEnabled()) {\r
91             LOGGER.debug(ByteBufUtils.byteBufToHexString(out));\r
92         }\r
93 \r
94         ctx.writeAndFlush(out);\r
95         LOGGER.info("Flushed");\r
96 \r
97         int bytesRemaining = bb.readableBytes();\r
98         LOGGER.debug("Skipping unread bytes");\r
99         bb.skipBytes(bytesRemaining);\r
100         LOGGER.debug("Discarding read bytes");\r
101         LOGGER.debug("RI: " + bb.readerIndex());\r
102         LOGGER.debug("WI: " + bb.writerIndex());\r
103 \r
104     }\r
105 \r
106     private static boolean checkOFHeader(short type, int length) {\r
107         return !((type > MESSAGE_TYPES) || (length < OFFrameDecoder.LENGTH_OF_HEADER));\r
108     }\r
109 }\r