Add methods for modifying deserializer mapping
[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 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
17 import org.opendaylight.openflowjava.protocol.api.keys.TypeToClassKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20
21 /**
22  * @author michal.polkorab
23  * @author timotej.kubas
24  * @author giuseppex.petralia@intel.com
25  */
26 public class DeserializationFactory {
27
28     private final Map<TypeToClassKey, Class<?>> messageClassMap = new ConcurrentHashMap<>();
29     private DeserializerRegistry registry;
30
31     /**
32      * Constructor
33      */
34     public DeserializationFactory() {
35         TypeToClassMapInitializer.initializeTypeToClassMap(messageClassMap);
36
37         // Register type to class map for additional deserializers
38         TypeToClassMapInitializer.initializeAdditionalTypeToClassMap(messageClassMap);
39     }
40
41     /**
42      * Transforms ByteBuf into correct POJO message
43      *
44      * @param rawMessage
45      * @param version
46      *            version decoded from OpenFlow protocol message
47      * @return correct POJO as DataObject
48      */
49     public DataObject deserialize(final ByteBuf rawMessage, final short version) {
50         DataObject dataObject = null;
51         int type = rawMessage.readUnsignedByte();
52         Class<?> clazz = messageClassMap.get(new TypeToClassKey(version, type));
53         rawMessage.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
54         OFDeserializer<DataObject> deserializer = registry.getDeserializer(new MessageCodeKey(version, type, clazz));
55         dataObject = deserializer.deserialize(rawMessage);
56         return dataObject;
57     }
58
59     /**
60      * Register new type to class mapping used to assign return type when deserializing message
61      * @param key type to class key
62      * @param clazz return class
63      */
64     public void registerMapping(final TypeToClassKey key, final Class<?> clazz) {
65         messageClassMap.put(key, clazz);
66     }
67
68     /**
69      * Unregister type to class mapping used to assign return type when deserializing message
70      * @param key type to class key
71      * @return true if mapping was successfully removed
72      */
73     public boolean unregisterMapping(final TypeToClassKey key) {
74         if (key == null) {
75             throw new IllegalArgumentException("TypeToClassKey is null");
76         }
77
78         return messageClassMap.remove(key) != null;
79     }
80
81     /**
82      * @param registry
83      */
84     public void setRegistry(final DeserializerRegistry registry) {
85         this.registry = registry;
86     }
87
88 }