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