Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / SerializationFactory.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;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author michal.polkorab
19  * @author timotej.kubas
20  */
21 public class SerializationFactory {
22
23     private static final Logger LOGGER = LoggerFactory
24             .getLogger(SerializationFactory.class);
25     /**
26      * Transforms POJO message into ByteBuf
27      * @param version version used for encoding received message
28      * @param out ByteBuf for storing and sending transformed message
29      * @param message POJO message
30      */
31     public static <E extends DataObject> void messageToBuffer(short version, ByteBuf out, E message) {
32         @SuppressWarnings("unchecked")
33         MessageTypeKey<E> msgTypeKey = new MessageTypeKey<>(version, (Class<E>) message.getClass());
34         OFSerializer<E> encoder = EncoderTable.getInstance().getEncoder(msgTypeKey);
35         if (encoder != null) {
36             encoder.messageToBuffer(version, out, message);
37         } else {
38             LOGGER.warn("No correct encoder found in EncoderTable for arguments: " + msgTypeKey.toString());
39         }
40     }
41 }