Minor model refactor
[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.impl.serialization.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
15 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
17
18 /**
19  * Translates PacketOut messages
20  * @author michal.polkorab
21  * @author timotej.kubas
22  */
23 public class PacketOutInputMessageFactory implements OFSerializer<PacketOutInput>{
24
25     /** Code type of PacketOut message */
26     public static final byte MESSAGE_TYPE = 13;
27     private static final int MESSAGE_LENGTH = 24;
28     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
29     private static PacketOutInputMessageFactory instance;
30     
31     private PacketOutInputMessageFactory() {
32         // do nothing, just singleton
33     }
34     
35     /**
36      * @return singleton factory
37      */
38     public static synchronized PacketOutInputMessageFactory getInstance() {
39         if (instance == null) {
40             instance = new PacketOutInputMessageFactory();
41         }
42         return instance;
43     }
44     
45     @Override
46     public void messageToBuffer(short version, ByteBuf out,
47             PacketOutInput message) {
48         ByteBufUtils.writeOFHeader(instance, message, out);
49         out.writeInt(message.getBufferId().intValue());
50         out.writeInt(message.getInPort().getValue().intValue());
51         out.writeShort(ActionsSerializer.computeLengthOfActions(message.getAction()));
52         ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, out);
53         ActionsSerializer.encodeActions(message.getAction(), out);
54         byte[] data = message.getData();
55         if (data != null) {
56             out.writeBytes(data);
57         }
58     }
59
60     @Override
61     public int computeLength(PacketOutInput message) {
62         int length = MESSAGE_LENGTH;
63         length += ActionsSerializer.computeLengthOfActions(message.getAction());
64         byte[] data = message.getData();
65         if (data != null) {
66             length += data.length;
67         }
68         return length;
69     }
70
71     @Override
72     public byte getMessageType() {
73         return MESSAGE_TYPE;
74     }
75
76 }