Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / ExperimenterInputMessageFactory.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.ByteBufUtils;
15 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
17
18 /**
19  * Translates Experimenter messages
20  * @author michal.polkorab
21  * @author timotej.kubas
22  */
23 public class ExperimenterInputMessageFactory implements OFSerializer<ExperimenterInput>{
24
25     /** Code type of Experimenter message */
26     public static final byte MESSAGE_TYPE = 4;
27     private static final byte MESSAGE_LENGTH = 8;
28     private static ExperimenterInputMessageFactory instance;
29     
30     private ExperimenterInputMessageFactory() {
31         // do nothing, just singleton
32     }
33     
34     /**
35      * @return singleton factory
36      */
37     public static synchronized ExperimenterInputMessageFactory getInstance() {
38         if (instance == null) {
39             instance = new ExperimenterInputMessageFactory();
40         }
41         return instance;
42     }
43     
44     @Override
45     public void messageToBuffer(short version, ByteBuf out,
46             ExperimenterInput message) {
47         ByteBufUtils.writeOFHeader(instance, message, out);
48         out.writeInt(message.getExperimenter().intValue());
49         out.writeInt(message.getExpType().intValue());
50         byte[] data = message.getData();
51         if (data != null) {
52             out.writeBytes(data);
53         }
54     }
55
56     @Override
57     public int computeLength(ExperimenterInput message) {
58         int length = MESSAGE_LENGTH + 2 * (EncodeConstants.SIZE_OF_INT_IN_BYTES);
59         byte[] data = message.getData();
60         if (data != null) {
61             length += data.length;
62         }
63         return length;
64     }
65
66     @Override
67     public byte getMessageType() {
68         return MESSAGE_TYPE;
69     }
70     
71 }