574987ffbc32c584cd62797ed06b33e65a763f55
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketOutInputMessageFactory.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.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
17 import org.opendaylight.openflowjava.protocol.impl.util.CodingUtils;
18 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
20
21 /**
22  * Translates PacketOut messages
23  * @author michal.polkorab
24  * @author timotej.kubas
25  */
26 public class PacketOutInputMessageFactory implements OFSerializer<PacketOutInput>, SerializerRegistryInjector {
27
28     /** Code type of PacketOut message */
29     private static final byte MESSAGE_TYPE = 13;
30     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
31     private SerializerRegistry registry;
32
33     @Override
34     public void serialize(PacketOutInput message, ByteBuf outBuffer) {
35         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
36         outBuffer.writeInt(message.getBufferId().intValue());
37         outBuffer.writeInt(message.getInPort().getValue().intValue());
38         int actionsLengthIndex = outBuffer.writerIndex();
39         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
40         ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, outBuffer);
41         int actionsStartIndex = outBuffer.writerIndex();
42         CodingUtils.serializeActions(message.getAction(), registry,
43                 outBuffer, EncodeConstants.OF13_VERSION_ID);
44         outBuffer.setShort(actionsLengthIndex, outBuffer.writerIndex() - actionsStartIndex);
45         byte[] data = message.getData();
46         if (data != null) {
47             outBuffer.writeBytes(data);
48         }
49         ByteBufUtils.updateOFHeaderLength(outBuffer);
50     }
51
52     @Override
53     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
54         this.registry = serializerRegistry;
55     }
56
57 }