Read data directly into a local array
[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 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
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     @Override
27     public PacketInMessage deserialize(final ByteBuf rawMessage) {
28         PacketInMessageBuilder builder = new PacketInMessageBuilder();
29         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
30         builder.setXid(rawMessage.readUnsignedInt());
31         builder.setBufferId(rawMessage.readUnsignedInt());
32         builder.setTotalLen(rawMessage.readUnsignedShort());
33         builder.setInPort(rawMessage.readUnsignedShort());
34         builder.setReason(PacketInReason.forValue(rawMessage.readUnsignedByte()));
35         rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER);
36         int remainingBytes = rawMessage.readableBytes();
37         if (remainingBytes > 0) {
38             final byte[] buf = new byte[remainingBytes];
39             rawMessage.readBytes(buf);
40             builder.setData(buf);
41         }
42         return builder.build();
43     }
44 }