4daadffc7a42af656bc20af8fa14428487df81ef
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / DeserializationFactory.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.deserialization;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * @author michal.polkorab
20  * @author timotej.kubas
21  */
22 public abstract class DeserializationFactory {
23     
24     private static final Logger LOGGER = LoggerFactory
25             .getLogger(DeserializationFactory.class);
26
27     /**
28      * Transforms ByteBuf into correct POJO message
29      * @param rawMessage 
30      * @param version version decoded from OpenFlow protocol message
31      * @return correct POJO as DataObject
32      */
33     public static DataObject bufferToMessage(ByteBuf rawMessage, short version) {
34         DataObject dataObject = null;
35         short type = rawMessage.readUnsignedByte();
36         rawMessage.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
37
38         MessageTypeCodeKey msgTypeCodeKey = new MessageTypeCodeKey(version, type);
39         OFDeserializer<?> decoder = DecoderTable.getInstance().getDecoder(msgTypeCodeKey);
40         if (decoder != null) {
41             dataObject = decoder.bufferToMessage(rawMessage, version);
42         } else {
43             LOGGER.warn("No correct decoder found in DecoderTable for arguments: " + msgTypeCodeKey.toString());
44         }
45         return dataObject;
46     }
47 }