Merge "add basic lib - plugin communication"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / openflow / lib / deserialization / DecoderTable.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.openflow.lib.deserialization;\r
3 \r
4 import java.util.HashMap;\r
5 import java.util.Map;\r
6 \r
7 import org.openflow.deserialization.factories.EchoReplyMessageFactory;\r
8 import org.openflow.deserialization.factories.EchoRequestMessageFactory;\r
9 import org.openflow.deserialization.factories.FeaturesReplyMessageFactory;\r
10 import org.openflow.deserialization.factories.HelloMessageFactory;\r
11 import org.openflow.lib.OfVersionDetector;\r
12 \r
13 /**\r
14  * @author michal.polkorab\r
15  *\r
16  */\r
17 public class DecoderTable {\r
18     \r
19     private static final short OF13 = OfVersionDetector.OF13_VERSION_ID;\r
20     private Map<MessageTypeCodeKey, OfDeserializer<?>> table;\r
21     private static DecoderTable instance;\r
22     \r
23     \r
24     private DecoderTable() {\r
25         // do nothing\r
26     }\r
27     \r
28     /**\r
29      * @return singleton instance\r
30      */\r
31     public static DecoderTable getInstance() {\r
32         if (instance == null) {\r
33             synchronized (DecoderTable.class) {\r
34                 instance = new DecoderTable();\r
35                 instance.init();\r
36             }\r
37         }\r
38         return instance;\r
39     }\r
40     \r
41     /**\r
42      * Decoder table provisioning\r
43      */\r
44     public void init() {\r
45         table = new HashMap<>();\r
46         table.put(new MessageTypeCodeKey(OF13, (short) 0), HelloMessageFactory.getInstance());\r
47         table.put(new MessageTypeCodeKey(OF13, (short) 2), EchoRequestMessageFactory.getInstance());\r
48         table.put(new MessageTypeCodeKey(OF13, (short) 3), EchoReplyMessageFactory.getInstance());\r
49         table.put(new MessageTypeCodeKey(OF13, (short) 6), FeaturesReplyMessageFactory.getInstance());\r
50     }\r
51     \r
52     /**\r
53      * @param msgTypeKey\r
54      * @return decoder for given message types\r
55      */\r
56     public OfDeserializer<?> getDecoder(MessageTypeCodeKey msgTypeKey) {\r
57         return table.get(msgTypeKey);\r
58     }\r
59 \r
60 }\r