UI support for multiple host per port
[controller.git] / third-party / openflowj / src / main / java / org / openflow / protocol / OFType.java
1 package org.openflow.protocol;
2
3 import java.lang.reflect.Constructor;
4
5 /**
6  * List of OpenFlow types and mappings to wire protocol value and derived
7  * classes
8  *
9  * @author Rob Sherwood (rob.sherwood@stanford.edu)
10  * @author David Erickson (daviderickson@cs.stanford.edu)
11  *
12  */
13 public enum OFType {
14     HELLO               (0, OFHello.class, new Instantiable<OFMessage>() {
15                             @Override
16                             public OFMessage instantiate() {
17                                 return new OFHello();
18                             }}),
19     ERROR               (1, OFError.class, new Instantiable<OFMessage>() {
20                             @Override
21                             public OFMessage instantiate() {
22                                 return new OFError();
23                             }}),
24     ECHO_REQUEST        (2, OFEchoRequest.class, new Instantiable<OFMessage>() {
25                             @Override
26                             public OFMessage instantiate() {
27                                 return new OFEchoRequest();
28                             }}),
29     ECHO_REPLY          (3, OFEchoReply.class, new Instantiable<OFMessage>() {
30                             @Override
31                             public OFMessage instantiate() {
32                                 return new OFEchoReply();
33                             }}),
34     VENDOR              (4, OFVendor.class, new Instantiable<OFMessage>() {
35                             @Override
36                             public OFMessage instantiate() {
37                                 return new OFVendor();
38                             }}),
39     FEATURES_REQUEST    (5, OFFeaturesRequest.class, new Instantiable<OFMessage>() {
40                             @Override
41                             public OFMessage instantiate() {
42                                 return new OFFeaturesRequest();
43                             }}),
44     FEATURES_REPLY      (6, OFFeaturesReply.class, new Instantiable<OFMessage>() {
45                             @Override
46                             public OFMessage instantiate() {
47                                 return new OFFeaturesReply();
48                             }}),
49     GET_CONFIG_REQUEST  (7, OFGetConfigRequest.class, new Instantiable<OFMessage>() {
50                             @Override
51                             public OFMessage instantiate() {
52                                 return new OFGetConfigRequest();
53                             }}),
54     GET_CONFIG_REPLY    (8, OFGetConfigReply.class, new Instantiable<OFMessage>() {
55                             @Override
56                             public OFMessage instantiate() {
57                                 return new OFGetConfigReply();
58                             }}),
59     SET_CONFIG          (9, OFSetConfig.class, new Instantiable<OFMessage>() {
60                             @Override
61                             public OFMessage instantiate() {
62                                 return new OFSetConfig();
63                             }}),
64     PACKET_IN           (10, OFPacketIn.class, new Instantiable<OFMessage>() {
65                             @Override
66                             public OFMessage instantiate() {
67                                 return new OFPacketIn();
68                             }}),
69     FLOW_REMOVED        (11, OFFlowRemoved.class, new Instantiable<OFMessage>() {
70                             @Override
71                             public OFMessage instantiate() {
72                                 return new OFFlowRemoved();
73                             }}),
74     PORT_STATUS         (12, OFPortStatus.class, new Instantiable<OFMessage>() {
75                             @Override
76                             public OFMessage instantiate() {
77                                 return new OFPortStatus();
78                             }}),
79     PACKET_OUT          (13, OFPacketOut.class, new Instantiable<OFMessage>() {
80                             @Override
81                             public OFMessage instantiate() {
82                                 return new OFPacketOut();
83                             }}),
84     FLOW_MOD            (14, OFFlowMod.class, new Instantiable<OFMessage>() {
85                             @Override
86                             public OFMessage instantiate() {
87                                 return new OFFlowMod();
88                             }}),
89     PORT_MOD            (15, OFPortMod.class, new Instantiable<OFMessage>() {
90                             @Override
91                             public OFMessage instantiate() {
92                                 return new OFPortMod();
93                             }}),
94     STATS_REQUEST       (16, OFStatisticsRequest.class, new Instantiable<OFMessage>() {
95                             @Override
96                             public OFMessage instantiate() {
97                                 return new OFStatisticsRequest();
98                             }}),
99     STATS_REPLY         (17, OFStatisticsReply.class, new Instantiable<OFMessage>() {
100                             @Override
101                             public OFMessage instantiate() {
102                                 return new OFStatisticsReply();
103                             }}),
104     BARRIER_REQUEST     (18, OFBarrierRequest.class, new Instantiable<OFMessage>() {
105                             @Override
106                             public OFMessage instantiate() {
107                                 return new OFBarrierRequest();
108                             }}),
109     BARRIER_REPLY       (19, OFBarrierReply.class, new Instantiable<OFMessage>() {
110                             @Override
111                             public OFMessage instantiate() {
112                                 return new OFBarrierReply();
113                             }}),
114     QUEUE_CONFIG_REQUEST    (20, OFMessage.class, new Instantiable<OFMessage>() {
115                             @Override
116                             public OFMessage instantiate() {
117                                 return new OFQueueConfigRequest();
118                             }}),
119     QUEUE_CONFIG_REPLY  (21, OFMessage.class, new Instantiable<OFMessage>() {
120                             @Override
121                             public OFMessage instantiate() {
122                                 return new OFQueueConfigReply();
123                             }});
124
125     static OFType[] mapping;
126
127     protected Class<? extends OFMessage> clazz;
128     protected Constructor<? extends OFMessage> constructor;
129     protected Instantiable<OFMessage> instantiable;
130     protected byte type;
131
132     /**
133      * Store some information about the OpenFlow type, including wire protocol
134      * type number, length, and derived class
135      *
136      * @param type Wire protocol number associated with this OFType
137      * @param requestClass The Java class corresponding to this type of OpenFlow
138      *              message
139      * @param instantiator An Instantiator<OFMessage> implementation that creates an
140      *          instance of the specified OFMessage
141      */
142     OFType(int type, Class<? extends OFMessage> clazz, Instantiable<OFMessage> instantiator) {
143         this.type = (byte) type;
144         this.clazz = clazz;
145         this.instantiable = instantiator;
146         try {
147             this.constructor = clazz.getConstructor(new Class[]{});
148         } catch (Exception e) {
149             throw new RuntimeException(
150                     "Failure getting constructor for class: " + clazz, e);
151         }
152         OFType.addMapping(this.type, this);
153     }
154
155     /**
156      * Adds a mapping from type value to OFType enum
157      *
158      * @param i OpenFlow wire protocol type
159      * @param t type
160      */
161     static public void addMapping(byte i, OFType t) {
162         if (mapping == null)
163             mapping = new OFType[32];
164         OFType.mapping[i] = t;
165     }
166
167     /**
168      * Remove a mapping from type value to OFType enum
169      *
170      * @param i OpenFlow wire protocol type
171      */
172     static public void removeMapping(byte i) {
173         OFType.mapping[i] = null;
174     }
175
176     /**
177      * Given a wire protocol OpenFlow type number, return the OFType associated
178      * with it
179      *
180      * @param i wire protocol number
181      * @return OFType enum type
182      */
183
184     static public OFType valueOf(Byte i) {
185         return OFType.mapping[i];
186     }
187
188     /**
189      * @return Returns the wire protocol value corresponding to this OFType
190      */
191     public byte getTypeValue() {
192         return this.type;
193     }
194
195     /**
196      * @return return the OFMessage subclass corresponding to this OFType
197      */
198     public Class<? extends OFMessage> toClass() {
199         return clazz;
200     }
201
202     /**
203      * Returns the no-argument Constructor of the implementation class for
204      * this OFType
205      * @return the constructor
206      */
207     public Constructor<? extends OFMessage> getConstructor() {
208         return constructor;
209     }
210
211     /**
212      * Returns a new instance of the OFMessage represented by this OFType
213      * @return the new object
214      */
215     public OFMessage newInstance() {
216         return instantiable.instantiate();
217     }
218
219     /**
220      * @return the instantiable
221      */
222     public Instantiable<OFMessage> getInstantiable() {
223         return instantiable;
224     }
225
226     /**
227      * @param instantiable the instantiable to set
228      */
229     public void setInstantiable(Instantiable<OFMessage> instantiable) {
230         this.instantiable = instantiable;
231     }
232 }