814bd502045cf48be689dc52592a6362bf8ba033
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10PacketInMessageFactory.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 org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
17
18 /**
19  * Translates PacketIn messages (OpenFlow v1.0)
20  * @author michal.polkorab
21  */
22 public class OF10PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
23
24     private static final byte PADDING_IN_PACKET_IN_HEADER = 1;
25
26     private static OF10PacketInMessageFactory instance;
27     
28     private OF10PacketInMessageFactory() {
29         // Singleton
30     }
31     
32     /**
33      * @return singleton factory
34      */
35     public static synchronized OF10PacketInMessageFactory getInstance(){
36         if(instance == null){
37             instance = new OF10PacketInMessageFactory();
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.setInPort(rawMessage.readUnsignedShort());
50         builder.setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
51         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
52         int remainingBytes = rawMessage.readableBytes();
53         if (remainingBytes > 0) {
54             builder.setData(rawMessage.readBytes(remainingBytes).array());
55         }
56         return builder.build();
57     }
58 }