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