759cb136133e32d44add9af9bb3d847a4bf8db84
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / DecoderTable.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 java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.BarrierReplyMessageFactory;
15 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.EchoReplyMessageFactory;
16 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.EchoRequestMessageFactory;
17 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ErrorMessageFactory;
18 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ExperimenterMessageFactory;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FeaturesReplyMessageFactory;
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FlowRemovedMessageFactory;
21 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.GetAsyncReplyMessageFactory;
22 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.GetConfigReplyMessageFactory;
23 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactory;
24 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory;
25 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10ErrorMessageFactory;
26 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10FeaturesReplyMessageFactory;
27 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10FlowRemovedMessageFactory;
28 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10HelloMessageFactory;
29 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10PacketInMessageFactory;
30 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10PortStatusMessageFactory;
31 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10QueueGetConfigReplyMessageFactory;
32 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10StatsReplyMessageFactory;
33 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.OF10VendorMessageFactory;
34 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.PacketInMessageFactory;
35 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.PortStatusMessageFactory;
36 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.QueueGetConfigReplyMessageFactory;
37 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.RoleReplyMessageFactory;
38 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
39
40 /**
41  * Stores and provides correct decoders for received messages
42  * @author michal.polkorab
43  * 
44  *  <pre>         
45  *  Type   Message
46  *   0: HELLO
47  *   1: ERROR
48  *   2: ECHO_REQUEST
49  *   3: ECHO_REPLY
50  *   4: EXPERIMENTER
51  *   5: FEATURES_REQUEST
52  *   6: FEATURES_REPLY
53  *   7: GET_CONFIG_REQUEST
54  *   8: GET_CONFIG_REPLY
55  *   9: SET_CONFIG
56  *   10: PACKET_IN
57  *   11: FLOW_REMOVED
58  *   12: PORT_STATUS
59  *   13: PACKET_OUT
60  *   14: FLOW_MOD
61  *   15: GROUP_MOD
62  *   16: PORT_MOD
63  *   17: TABLE_MOD
64  *   18: MULTIPART_REQUEST
65  *   19: MULTIPART_REPLY
66  *   20: BARRIER_REQUEST
67  *   21: BARRIER_REPLY
68  *   22: QUEUE_GET_CONFIG_REQUEST
69  *   23: QUEUE_GET_CONFIG_REPLY
70  *   24: ROLE_REQUEST
71  *   25: ROLE_REPLY    
72  *   26: GET_ASYNC_REQUEST
73  *   27: GET_ASYNC_REPLY
74  *   28: SET_ASYNC
75  *   29: METER_MOD
76  *   </pre>
77  */
78 public class DecoderTable {
79     
80     private static final short OF10 = EncodeConstants.OF10_VERSION_ID;
81     private static final short OF13 = EncodeConstants.OF13_VERSION_ID;
82     private Map<MessageTypeCodeKey, OFDeserializer<?>> table;
83     private static DecoderTable instance;
84     
85     
86     private DecoderTable() {
87         // do nothing
88     }
89     
90     /**
91      * @return singleton instance
92      */
93     public static synchronized DecoderTable getInstance() {
94         if (instance == null) {
95             instance = new DecoderTable();
96             instance.init();
97         }
98         return instance;
99     }
100     
101     /**
102      * Decoder table provisioning
103      */
104     public void init() {
105         table = new HashMap<>();
106         table.put(new MessageTypeCodeKey(OF10, (short) 0), OF10HelloMessageFactory.getInstance());
107         table.put(new MessageTypeCodeKey(OF10, (short) 1), OF10ErrorMessageFactory.getInstance());
108         table.put(new MessageTypeCodeKey(OF10, (short) 2), EchoRequestMessageFactory.getInstance());
109         table.put(new MessageTypeCodeKey(OF10, (short) 3), EchoReplyMessageFactory.getInstance());
110         table.put(new MessageTypeCodeKey(OF10, (short) 4), OF10VendorMessageFactory.getInstance());
111         table.put(new MessageTypeCodeKey(OF10, (short) 6), OF10FeaturesReplyMessageFactory.getInstance());
112         table.put(new MessageTypeCodeKey(OF10, (short) 8), GetConfigReplyMessageFactory.getInstance());
113         table.put(new MessageTypeCodeKey(OF10, (short) 10), OF10PacketInMessageFactory.getInstance());
114         table.put(new MessageTypeCodeKey(OF10, (short) 11), OF10FlowRemovedMessageFactory.getInstance());
115         table.put(new MessageTypeCodeKey(OF10, (short) 12), OF10PortStatusMessageFactory.getInstance());
116         table.put(new MessageTypeCodeKey(OF10, (short) 17), OF10StatsReplyMessageFactory.getInstance());
117         table.put(new MessageTypeCodeKey(OF10, (short) 19), BarrierReplyMessageFactory.getInstance());
118         table.put(new MessageTypeCodeKey(OF10, (short) 21), OF10QueueGetConfigReplyMessageFactory.getInstance());
119         table.put(new MessageTypeCodeKey(OF13, (short) 0), HelloMessageFactory.getInstance());
120         table.put(new MessageTypeCodeKey(OF13, (short) 1), ErrorMessageFactory.getInstance());
121         table.put(new MessageTypeCodeKey(OF13, (short) 2), EchoRequestMessageFactory.getInstance());
122         table.put(new MessageTypeCodeKey(OF13, (short) 3), EchoReplyMessageFactory.getInstance());
123         table.put(new MessageTypeCodeKey(OF13, (short) 4), ExperimenterMessageFactory.getInstance());
124         table.put(new MessageTypeCodeKey(OF13, (short) 6), FeaturesReplyMessageFactory.getInstance());
125         table.put(new MessageTypeCodeKey(OF13, (short) 8), GetConfigReplyMessageFactory.getInstance());
126         table.put(new MessageTypeCodeKey(OF13, (short) 10), PacketInMessageFactory.getInstance());
127         table.put(new MessageTypeCodeKey(OF13, (short) 11), FlowRemovedMessageFactory.getInstance());
128         table.put(new MessageTypeCodeKey(OF13, (short) 12), PortStatusMessageFactory.getInstance());
129         table.put(new MessageTypeCodeKey(OF13, (short) 19), MultipartReplyMessageFactory.getInstance());
130         table.put(new MessageTypeCodeKey(OF13, (short) 21), BarrierReplyMessageFactory.getInstance());
131         table.put(new MessageTypeCodeKey(OF13, (short) 23), QueueGetConfigReplyMessageFactory.getInstance());
132         table.put(new MessageTypeCodeKey(OF13, (short) 25), RoleReplyMessageFactory.getInstance());
133         table.put(new MessageTypeCodeKey(OF13, (short) 27), GetAsyncReplyMessageFactory.getInstance());
134     }
135     
136     /**
137      * @param msgTypeKey
138      * @return decoder for given message types
139      */
140     public OFDeserializer<?> getDecoder(MessageTypeCodeKey msgTypeKey) {
141         return table.get(msgTypeKey);
142     }
143
144 }