Merge "Fixing OF Multipart messages 1) So we have a MultipartRequestDesc message...
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PacketInMessageFactory.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
3
4 import io.netty.buffer.ByteBuf;
5
6 import java.math.BigInteger;
7
8 import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;
9 import org.opendaylight.openflowjava.protocol.impl.util.MatchDeserializer;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
13
14 /**
15  * Translates PacketIn messages
16  * @author michal.polkorab
17  * @author timotej.kubas
18  */
19 public class PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
20
21     private static PacketInMessageFactory instance;
22     private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
23     
24     private PacketInMessageFactory() {
25         // Singleton
26     }
27     
28     /**
29      * @return singleton factory
30      */
31     public static synchronized PacketInMessageFactory getInstance(){
32         if(instance == null){
33             instance = new PacketInMessageFactory();
34         }
35         return instance;
36     }
37
38     @Override
39     public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) {
40         PacketInMessageBuilder builder = new PacketInMessageBuilder();
41         builder.setVersion(version);
42         builder.setXid(rawMessage.readUnsignedInt());
43         builder.setBufferId(rawMessage.readUnsignedInt());
44         builder.setTotalLen(rawMessage.readUnsignedShort());
45         builder.setReason(rawMessage.readUnsignedByte());
46         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
47         byte[] cookie = new byte[Long.SIZE/Byte.SIZE];
48         rawMessage.readBytes(cookie);
49         builder.setCookie(new BigInteger(cookie));
50         builder.setMatch(MatchDeserializer.createMatch(rawMessage)); 
51         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
52         builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
53         return builder.build();
54     }
55 }