Copyright update
[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.MatchDeserializer;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
21
22 /**
23  * Translates PacketIn messages
24  * @author michal.polkorab
25  * @author timotej.kubas
26  */
27 public class PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
28
29     private static PacketInMessageFactory instance;
30     private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
31     
32     private PacketInMessageFactory() {
33         // Singleton
34     }
35     
36     /**
37      * @return singleton factory
38      */
39     public static synchronized PacketInMessageFactory getInstance(){
40         if(instance == null){
41             instance = new PacketInMessageFactory();
42         }
43         return instance;
44     }
45
46     @Override
47     public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) {
48         PacketInMessageBuilder builder = new PacketInMessageBuilder();
49         builder.setVersion(version);
50         builder.setXid(rawMessage.readUnsignedInt());
51         builder.setBufferId(rawMessage.readUnsignedInt());
52         builder.setTotalLen(rawMessage.readUnsignedShort());
53         builder.setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
54         builder.setTableId(new TableId((long)rawMessage.readUnsignedByte()));
55         byte[] cookie = new byte[Long.SIZE/Byte.SIZE];
56         rawMessage.readBytes(cookie);
57         builder.setCookie(new BigInteger(cookie));
58         builder.setMatch(MatchDeserializer.createMatch(rawMessage)); 
59         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
60         builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
61         return builder.build();
62     }
63 }