d83eb2029b277b1097bea1e6838456c6c1c07497
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / FeaturesReplyMessageFactory.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 \r
6 import java.math.BigInteger;\r
7 \r
8 import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;\r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;\r
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;\r
12 \r
13 /**\r
14  * @author michal.polkorab\r
15  *\r
16  */\r
17 public class FeaturesReplyMessageFactory implements OFDeserializer<GetFeaturesOutput>{\r
18     \r
19     private static final byte PADDING_IN_FEATURES_REPLY_HEADER = 2;\r
20     \r
21     private static FeaturesReplyMessageFactory instance;\r
22 \r
23     private FeaturesReplyMessageFactory() {\r
24         // do nothing, just singleton\r
25     }\r
26     \r
27     /**\r
28      * @return singleton factory\r
29      */\r
30     public static synchronized FeaturesReplyMessageFactory getInstance() {\r
31         if (instance == null) {\r
32             instance = new FeaturesReplyMessageFactory();\r
33         }\r
34         return instance;\r
35     }\r
36     \r
37     @Override\r
38     public GetFeaturesOutput bufferToMessage(ByteBuf rawMessage, short version) {\r
39         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();\r
40         builder.setVersion(version);\r
41         builder.setXid(rawMessage.readUnsignedInt());\r
42         byte[] datapathId = new byte[Long.SIZE/Byte.SIZE];\r
43         rawMessage.readBytes(datapathId);\r
44         builder.setDatapathId(new BigInteger(datapathId));\r
45         builder.setBuffers(rawMessage.readUnsignedInt());\r
46         builder.setTables(rawMessage.readUnsignedByte());\r
47         builder.setAuxiliaryId(rawMessage.readUnsignedByte());\r
48         rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER);\r
49         builder.setCapabilities(createCapabilities(rawMessage.readUnsignedInt()));\r
50         builder.setReserved(rawMessage.readUnsignedInt());\r
51         return builder.build();\r
52     }\r
53 \r
54     private static Capabilities createCapabilities(long input) {\r
55         final Boolean FLOW_STATS = (input & (1 << 0)) != 0;\r
56         final Boolean TABLE_STATS = (input & (1 << 1)) != 0;\r
57         final Boolean PORT_STATS = (input & (1 << 2)) != 0;\r
58         final Boolean GROUP_STATS = (input & (1 << 3)) != 0;\r
59         final Boolean IP_REASM = (input & (1 << 5)) != 0;\r
60         final Boolean QUEUE_STATS = (input & (1 << 6)) != 0;\r
61         final Boolean PORT_BLOCKED = (input & (1 << 8)) != 0;\r
62         return new Capabilities(FLOW_STATS, GROUP_STATS, IP_REASM,\r
63                 PORT_BLOCKED, PORT_STATS, QUEUE_STATS, TABLE_STATS);\r
64     }\r
65 \r
66 }\r