0a45ef73fa7310c02755c432698fe987199cbbd9
[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.PacketInReason;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
14
15 /**
16  * Translates PacketIn messages
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 PacketInMessageFactory() {
26         // Singleton
27     }
28     
29     /**
30      * @return singleton factory
31      */
32     public static synchronized PacketInMessageFactory getInstance(){
33         if(instance == null){
34             instance = new PacketInMessageFactory();
35         }
36         return instance;
37     }
38
39     @Override
40     public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) {
41         PacketInMessageBuilder builder = new PacketInMessageBuilder();
42         builder.setVersion(version);
43         builder.setXid(rawMessage.readUnsignedInt());
44         builder.setBufferId(rawMessage.readUnsignedInt());
45         builder.setTotalLen(rawMessage.readUnsignedShort());
46         builder.setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
47         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
48         byte[] cookie = new byte[Long.SIZE/Byte.SIZE];
49         rawMessage.readBytes(cookie);
50         builder.setCookie(new BigInteger(cookie));
51         builder.setMatch(MatchDeserializer.createMatch(rawMessage)); 
52         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
53         builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
54         return builder.build();
55     }
56 }