6b7b8a3cb29230b578bd6638244601f40c199392
[openflowplugin.git] / openflowjava / 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 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
9
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint16;
11 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
12
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder;
19
20 /**
21  * Translates PacketIn messages (OpenFlow v1.0).
22  *
23  * @author michal.polkorab
24  */
25 public class OF10PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
26
27     private static final byte PADDING_IN_PACKET_IN_HEADER = 1;
28
29     @Override
30     public PacketInMessage deserialize(final ByteBuf rawMessage) {
31         PacketInMessageBuilder builder = new PacketInMessageBuilder()
32                 .setVersion(EncodeConstants.OF_VERSION_1_0)
33                 .setXid(readUint32(rawMessage))
34                 .setBufferId(readUint32(rawMessage))
35                 .setTotalLen(readUint16(rawMessage))
36                 .setInPort(readUint16(rawMessage))
37                 .setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
38         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
39         int remainingBytes = rawMessage.readableBytes();
40         if (remainingBytes > 0) {
41             final byte[] buf = new byte[remainingBytes];
42             rawMessage.readBytes(buf);
43             builder.setData(buf);
44         }
45         return builder.build();
46     }
47 }