1 package org.openflow.codec.protocol.statistics;
3 import java.lang.reflect.Constructor;
4 import java.util.HashMap;
7 import org.openflow.codec.protocol.Instantiable;
8 import org.openflow.codec.protocol.OFPType;
9 import org.openflow.codec.util.U16;
12 * Represents an ofp_multipart_types enum
17 public enum OFPMultipartTypes {
18 DESC(0, OFPDescriptionStatistics.class, OFPDescriptionStatistics.class, new Instantiable<OFPStatistics>() {
20 public OFPStatistics instantiate() {
21 return new OFPDescriptionStatistics();
23 }, new Instantiable<OFPStatistics>() {
25 public OFPStatistics instantiate() {
26 return new OFPDescriptionStatistics();
28 }), FLOW(1, OFPFlowStatisticsRequest.class, OFPFlowStatisticsReply.class, new Instantiable<OFPStatistics>() {
30 public OFPStatistics instantiate() {
31 return new OFPFlowStatisticsRequest();
33 }, new Instantiable<OFPStatistics>() {
35 public OFPStatistics instantiate() {
36 return new OFPFlowStatisticsReply();
38 }), AGGREGATE(2, OFPAggregateStatisticsRequest.class, OFPAggregateStatisticsReply.class,
39 new Instantiable<OFPStatistics>() {
41 public OFPStatistics instantiate() {
42 return new OFPAggregateStatisticsRequest();
44 }, new Instantiable<OFPStatistics>() {
46 public OFPStatistics instantiate() {
47 return new OFPAggregateStatisticsReply();
49 }), TABLE(3, OFPTableStatistics.class, OFPTableStatistics.class, new Instantiable<OFPStatistics>() {
51 public OFPStatistics instantiate() {
52 return new OFPTableStatistics();
54 }, new Instantiable<OFPStatistics>() {
56 public OFPStatistics instantiate() {
57 return new OFPTableStatistics();
59 }), PORT_STATS(4, OFPPortStatisticsRequest.class, OFPPortStatisticsReply.class, new Instantiable<OFPStatistics>() {
61 public OFPStatistics instantiate() {
62 return new OFPPortStatisticsRequest();
64 }, new Instantiable<OFPStatistics>() {
66 public OFPStatistics instantiate() {
67 return new OFPPortStatisticsReply();
69 }), QUEUE(5, OFPQueueStatisticsRequest.class, OFPQueueStatisticsReply.class, new Instantiable<OFPStatistics>() {
71 public OFPStatistics instantiate() {
72 return new OFPQueueStatisticsRequest();
74 }, new Instantiable<OFPStatistics>() {
76 public OFPStatistics instantiate() {
77 return new OFPQueueStatisticsReply();
79 }), GROUP_DESC(7, OFPGroupDescription.class, OFPGroupDescription.class, new Instantiable<OFPStatistics>() {
81 public OFPStatistics instantiate() {
82 return new OFPGroupDescription();
84 }, new Instantiable<OFPStatistics>() {
86 public OFPStatistics instantiate() {
87 return new OFPGroupDescription();
89 }), GROUP_FEATURES(8, OFPGroupFeatures.class, OFPGroupFeatures.class, new Instantiable<OFPStatistics>() {
91 public OFPStatistics instantiate() {
92 return new OFPGroupFeatures();
94 }, new Instantiable<OFPStatistics>() {
96 public OFPStatistics instantiate() {
97 return new OFPGroupFeatures();
99 }), TABLE_FEATURES(12, OFPTableFeatures.class, OFPTableFeatures.class, new Instantiable<OFPStatistics>() {
101 public OFPStatistics instantiate() {
102 return new OFPTableFeatures();
104 }, new Instantiable<OFPStatistics>() {
106 public OFPStatistics instantiate() {
107 return new OFPTableFeatures();
109 }), PORT_DESC(13, OFPPortDescriptionStatistics.class, OFPPortDescriptionStatistics.class,
110 new Instantiable<OFPStatistics>() {
112 public OFPStatistics instantiate() {
113 return new OFPPortDescriptionStatistics();
115 }, new Instantiable<OFPStatistics>() {
117 public OFPStatistics instantiate() {
118 return new OFPPortDescriptionStatistics();
122 EXPERIMENTER(0xffff, OFPExperimenterMultipartHeader.class, OFPExperimenterMultipartHeader.class,
123 new Instantiable<OFPStatistics>() {
125 public OFPStatistics instantiate() {
126 return new OFPExperimenterMultipartHeader();
128 }, new Instantiable<OFPStatistics>() {
130 public OFPStatistics instantiate() {
131 return new OFPExperimenterMultipartHeader();
135 private static Map<Integer, OFPMultipartTypes> requestMapping;
136 private static Map<Integer, OFPMultipartTypes> replyMapping;
138 private Class<? extends OFPStatistics> requestClass;
139 private Constructor<? extends OFPStatistics> requestConstructor;
140 private Instantiable<OFPStatistics> requestInstantiable;
141 private Class<? extends OFPStatistics> replyClass;
142 private Constructor<? extends OFPStatistics> replyConstructor;
143 private Instantiable<OFPStatistics> replyInstantiable;
147 * Store some information about the OpenFlow Statistic type, including wire
148 * protocol type number, and derived class
151 * Wire protocol number associated with this OFPMultipartType
152 * @param requestClass
153 * The Multipart Java class to return when the containing OFPType
154 * is MULTIPART_REQUEST
156 * The Multipart Java class to return when the containing OFPType
159 OFPMultipartTypes(int type, Class<? extends OFPStatistics> requestClass, Class<? extends OFPStatistics> replyClass,
160 Instantiable<OFPStatistics> requestInstantiable, Instantiable<OFPStatistics> replyInstantiable) {
162 this.requestClass = requestClass;
164 this.requestConstructor = requestClass.getConstructor(new Class[] {});
165 } catch (Exception e) {
166 throw new RuntimeException("Failure getting constructor for class: " + requestClass, e);
169 this.replyClass = replyClass;
171 this.replyConstructor = replyClass.getConstructor(new Class[] {});
172 } catch (Exception e) {
173 throw new RuntimeException("Failure getting constructor for class: " + replyClass, e);
175 this.requestInstantiable = requestInstantiable;
176 this.replyInstantiable = replyInstantiable;
177 OFPMultipartTypes.addMapping(this.type, OFPType.MULTIPART_REQUEST, this);
178 OFPMultipartTypes.addMapping(this.type, OFPType.MULTIPART_REPLY, this);
182 * Adds a mapping from type value to OFStatisticsType enum
185 * OpenFlow wire protocol type
187 * type of containing OFPMessage, only accepts MULTIPART_REQUEST
192 public static void addMapping(int i, OFPType t, OFPMultipartTypes st) {
194 if (t == OFPType.MULTIPART_REQUEST) {
195 if (requestMapping == null)
196 requestMapping = new HashMap<Integer, OFPMultipartTypes>();
197 OFPMultipartTypes.requestMapping.put(i, st);
198 } else if (t == OFPType.MULTIPART_REPLY) {
199 if (replyMapping == null)
200 replyMapping = new HashMap<Integer, OFPMultipartTypes>();
201 OFPMultipartTypes.replyMapping.put(i, st);
203 throw new RuntimeException(t.toString() + " is an invalid OFPType");
208 * Remove a mapping from type value to OFPMultipartType enum
211 * OpenFlow wire protocol type
213 * type of containing OFPMessage, only accepts MULTIPART_REQUEST
216 public static void removeMapping(int i, OFPType t) {
217 if (t == OFPType.MULTIPART_REQUEST) {
218 requestMapping.remove(i);
219 } else if (t == OFPType.MULTIPART_REPLY) {
220 replyMapping.remove(i);
222 throw new RuntimeException(t.toString() + " is an invalid OFPType");
227 * Given a wire protocol OpenFlow type number, return the OFPMultipartType
231 * wire protocol number
233 * type of containing OFPMessage, only accepts MULTIPART_REQUEST
235 * @return OFPMultipartType enum type
237 public static OFPMultipartTypes valueOf(short i, OFPType t) {
238 if (t == OFPType.MULTIPART_REQUEST) {
239 return requestMapping.get(U16.f(i));
240 } else if (t == OFPType.MULTIPART_REPLY) {
241 return replyMapping.get(U16.f(i));
243 throw new RuntimeException(t.toString() + " is an invalid OFPType");
248 * @return Returns the wire protocol value corresponding to this
251 public short getTypeValue() {
252 return U16.t(this.type);
256 * @return Returns the wire protocol unsigned value corresponding to this
259 public int getTypeValueU() {
265 * type of containing OFPMessage, only accepts MULTIPART_REQUEST
267 * @return return the OFPMessage subclass corresponding to this
270 public Class<? extends OFPStatistics> toClass(OFPType t) {
271 if (t == OFPType.MULTIPART_REQUEST) {
273 } else if (t == OFPType.MULTIPART_REPLY) {
276 throw new RuntimeException(t.toString() + " is an invalid OFPType");
281 * Returns the no-argument Constructor of the implementation class for this
282 * OFPMultipartType, either request or reply based on the supplied OFPType
287 public Constructor<? extends OFPStatistics> getConstructor(OFPType t) {
288 if (t == OFPType.MULTIPART_REQUEST) {
289 return requestConstructor;
290 } else if (t == OFPType.MULTIPART_REPLY) {
291 return replyConstructor;
293 throw new RuntimeException(t.toString() + " is an invalid OFPType");
298 * @return the requestInstantiable
300 public Instantiable<OFPStatistics> getRequestInstantiable() {
301 return requestInstantiable;
305 * @param requestInstantiable
306 * the requestInstantiable to set
308 public void setRequestInstantiable(Instantiable<OFPStatistics> requestInstantiable) {
309 this.requestInstantiable = requestInstantiable;
313 * @return the replyInstantiable
315 public Instantiable<OFPStatistics> getReplyInstantiable() {
316 return replyInstantiable;
320 * @param replyInstantiable
321 * the replyInstantiable to set
323 public void setReplyInstantiable(Instantiable<OFPStatistics> replyInstantiable) {
324 this.replyInstantiable = replyInstantiable;
328 * Returns a new instance of the implementation class for this
329 * OFPMultipartType, either request or reply based on the supplied OFPType
334 public OFPStatistics newInstance(OFPType t) {
335 if (t == OFPType.MULTIPART_REQUEST) {
336 return requestInstantiable.instantiate();
337 } else if (t == OFPType.MULTIPART_REPLY) {
338 return replyInstantiable.instantiate();
340 throw new RuntimeException(t.toString() + " is an invalid OFPType");