Fix - bug 270
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PacketInMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.math.BigInteger;
14
15 import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.MatchDeserializer;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
22
23 /**
24  * Translates PacketIn messages
25  * @author michal.polkorab
26  * @author timotej.kubas
27  */
28 public class PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
29
30     private static PacketInMessageFactory instance;
31     private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
32     
33     private PacketInMessageFactory() {
34         // Singleton
35     }
36     
37     /**
38      * @return singleton factory
39      */
40     public static synchronized PacketInMessageFactory getInstance(){
41         if(instance == null){
42             instance = new PacketInMessageFactory();
43         }
44         return instance;
45     }
46
47     @Override
48     public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) {
49         PacketInMessageBuilder builder = new PacketInMessageBuilder();
50         builder.setVersion(version);
51         builder.setXid(rawMessage.readUnsignedInt());
52         builder.setBufferId(rawMessage.readUnsignedInt());
53         builder.setTotalLen(rawMessage.readUnsignedShort());
54         builder.setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
55         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
56         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
57         rawMessage.readBytes(cookie);
58         builder.setCookie(new BigInteger(1, cookie));
59         builder.setMatch(MatchDeserializer.createMatch(rawMessage)); 
60         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
61         builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
62         return builder.build();
63     }
64 }