e318c683069cf39bc0938c9ae834c94b7be83ab9
[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.openflowjava.protocol.api.extensibility.MessageTypeKey;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17
18 /**
19  * @author michal.polkorab
20  * @author timotej.kubas
21  */
22 public class SerializationFactory {
23
24     private SerializerRegistry registry;
25     
26     /**
27      * Transforms POJO message into ByteBuf
28      * @param version version used for encoding received message
29      * @param out ByteBuf for storing and sending transformed message
30      * @param message POJO message
31      */
32     public void messageToBuffer(short version, ByteBuf out, DataObject message) {
33         OFSerializer<DataObject> serializer = registry.getSerializer(
34                 new MessageTypeKey<>(version, message.getImplementedInterface()));
35         serializer.serialize(message, out);
36     }
37
38     /**
39      * @param serializerRegistry registry with serializers
40      */
41     public void setSerializerTable(SerializerRegistry serializerRegistry) {
42         this.registry = serializerRegistry;
43     }
44
45 }