2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.math.BigInteger;
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;
24 * Translates PacketIn messages
25 * @author michal.polkorab
26 * @author timotej.kubas
28 public class PacketInMessageFactory implements OFDeserializer<PacketInMessage> {
30 private static PacketInMessageFactory instance;
31 private static final byte PADDING_IN_PACKET_IN_HEADER = 2;
33 private PacketInMessageFactory() {
38 * @return singleton factory
40 public static synchronized PacketInMessageFactory getInstance(){
42 instance = new PacketInMessageFactory();
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();