09f4fd1db7d402f0954cda8c47fbb1f0afca0362
[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 java.util.HashMap;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.util.TypeToClassKey;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22
23 /**
24  * @author michal.polkorab
25  * @author timotej.kubas
26  */
27 public class DeserializationFactory {
28
29     private DeserializerRegistry registry;
30     private Map<TypeToClassKey, Class<?>> messageClassMap;
31
32     /**
33      * Constructor
34      */
35     public DeserializationFactory() {
36         messageClassMap = new HashMap<>();
37         TypeToClassMapInitializer.initializeTypeToClassMap(messageClassMap);
38     }
39
40     /**
41      * Transforms ByteBuf into correct POJO message
42      * @param rawMessage
43      * @param version version decoded from OpenFlow protocol message
44      * @return correct POJO as DataObject
45      */
46     public DataObject deserialize(ByteBuf rawMessage, short version) {
47         DataObject dataObject = null;
48         int type = rawMessage.readUnsignedByte();
49         Class<?> clazz = messageClassMap.get(new TypeToClassKey(version, type));
50         rawMessage.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
51         OFDeserializer<DataObject> deserializer = registry.getDeserializer(
52                 new MessageCodeKey(version, type, clazz));
53         dataObject = deserializer.deserialize(rawMessage);
54         return dataObject;
55     }
56
57     /**
58      * @param registry
59      */
60     public void setRegistry(DeserializerRegistry registry) {
61         this.registry = registry;
62     }
63
64 }