8a7b4ca9402a6b074bcffea252e8869b7d1de052
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketOutInputMessageFactory.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 \r
6 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
7 import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;\r
8 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;\r
10 \r
11 /**\r
12  * @author michal.polkorab\r
13  *\r
14  */\r
15 public class PacketOutInputMessageFactory implements OFSerializer<PacketOutInput>{\r
16 \r
17     /** Code type of PacketOut message */\r
18     public static final byte MESSAGE_TYPE = 13;\r
19     private static final int MESSAGE_LENGTH = 24;\r
20     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;\r
21     private static PacketOutInputMessageFactory instance;\r
22     \r
23     private PacketOutInputMessageFactory() {\r
24         // do nothing, just singleton\r
25     }\r
26     \r
27     /**\r
28      * @return singleton factory\r
29      */\r
30     public static synchronized PacketOutInputMessageFactory getInstance() {\r
31         if (instance == null) {\r
32             instance = new PacketOutInputMessageFactory();\r
33         }\r
34         return instance;\r
35     }\r
36     \r
37     @Override\r
38     public void messageToBuffer(short version, ByteBuf out,\r
39             PacketOutInput message) {\r
40         ByteBufUtils.writeOFHeader(instance, message, out);\r
41         out.writeInt(message.getBufferId().intValue());\r
42         out.writeInt(message.getInPort().getValue().intValue());\r
43         out.writeShort(ActionsSerializer.computeLengthOfActions(message.getActionsList()));\r
44         ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, out);\r
45         ActionsSerializer.encodeActions(message.getActionsList(), out);\r
46         byte[] data = message.getData();\r
47         if (data != null) {\r
48             out.writeBytes(data);\r
49         }\r
50     }\r
51 \r
52     @Override\r
53     public int computeLength(PacketOutInput message) {\r
54         int length = MESSAGE_LENGTH;\r
55         length += ActionsSerializer.computeLengthOfActions(message.getActionsList());\r
56         byte[] data = message.getData();\r
57         if (data != null) {\r
58             length += data.length;\r
59         }\r
60         return length;\r
61     }\r
62 \r
63     @Override\r
64     public byte getMessageType() {\r
65         return MESSAGE_TYPE;\r
66     }\r
67 \r
68 }\r