Factory tests back to stable
[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  * @author michal.polkorab
16  * @author timotej.kubas
17  */
18 public class PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
19
20     private static PacketInMessageFactory instance;
21     private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
22     
23     private PacketInMessageFactory() {
24         // Singleton
25     }
26     
27     /**
28      * @return singleton factory
29      */
30     public static synchronized PacketInMessageFactory getInstance(){
31         if(instance == null){
32             instance = new PacketInMessageFactory();
33         }
34         return instance;
35     }
36
37     @Override
38     public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) {
39         PacketInMessageBuilder builder = new PacketInMessageBuilder();
40         builder.setVersion(version);
41         builder.setXid(rawMessage.readUnsignedInt());
42         builder.setBufferId(rawMessage.readUnsignedInt());
43         builder.setTotalLen(rawMessage.readUnsignedShort());
44         builder.setReason(rawMessage.readUnsignedByte());
45         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
46         byte[] cookie = new byte[Long.SIZE/Byte.SIZE];
47         rawMessage.readBytes(cookie);
48         builder.setCookie(new BigInteger(cookie));
49         builder.setMatch(MatchDeserializer.createMatch(rawMessage)); 
50         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
51         builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
52         return builder.build();
53     }
54 }