blob: 4a84674ef6c629aec68f719fdf488710aa8413cc [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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
18package org.openflow.protocol.statistics;
19
20import java.lang.reflect.Constructor;
21
22import org.openflow.protocol.Instantiable;
23import org.openflow.protocol.OFType;
24
Ray Milkeyff735142014-05-22 19:06:02 -070025@SuppressWarnings("rawtypes")
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026public enum OFStatisticsType {
27 DESC (0, OFDescriptionStatistics.class, OFDescriptionStatistics.class,
28 new Instantiable<OFStatistics>() {
29 @Override
30 public OFStatistics instantiate() {
31 return new OFDescriptionStatistics();
32 }
33 },
34 new Instantiable<OFStatistics>() {
35 @Override
36 public OFStatistics instantiate() {
37 return new OFDescriptionStatistics();
38 }
39 }),
40 FLOW (1, OFFlowStatisticsRequest.class, OFFlowStatisticsReply.class,
41 new Instantiable<OFStatistics>() {
42 @Override
43 public OFStatistics instantiate() {
44 return new OFFlowStatisticsRequest();
45 }
46 },
47 new Instantiable<OFStatistics>() {
48 @Override
49 public OFStatistics instantiate() {
50 return new OFFlowStatisticsReply();
51 }
52 }),
53 AGGREGATE (2, OFAggregateStatisticsRequest.class, OFAggregateStatisticsReply.class,
54 new Instantiable<OFStatistics>() {
55 @Override
56 public OFStatistics instantiate() {
57 return new OFAggregateStatisticsRequest();
58 }
59 },
60 new Instantiable<OFStatistics>() {
61 @Override
62 public OFStatistics instantiate() {
63 return new OFAggregateStatisticsReply();
64 }
65 }),
66 TABLE (3, OFTableStatistics.class, OFTableStatistics.class,
67 new Instantiable<OFStatistics>() {
68 @Override
69 public OFStatistics instantiate() {
70 return new OFTableStatistics();
71 }
72 },
73 new Instantiable<OFStatistics>() {
74 @Override
75 public OFStatistics instantiate() {
76 return new OFTableStatistics();
77 }
78 }),
79 PORT (4, OFPortStatisticsRequest.class, OFPortStatisticsReply.class,
80 new Instantiable<OFStatistics>() {
81 @Override
82 public OFStatistics instantiate() {
83 return new OFPortStatisticsRequest();
84 }
85 },
86 new Instantiable<OFStatistics>() {
87 @Override
88 public OFStatistics instantiate() {
89 return new OFPortStatisticsReply();
90 }
91 }),
92 QUEUE (5, OFQueueStatisticsRequest.class, OFQueueStatisticsReply.class,
93 new Instantiable<OFStatistics>() {
94 @Override
95 public OFStatistics instantiate() {
96 return new OFQueueStatisticsRequest();
97 }
98 },
99 new Instantiable<OFStatistics>() {
100 @Override
101 public OFStatistics instantiate() {
102 return new OFQueueStatisticsReply();
103 }
104 }),
105 VENDOR (0xffff, OFVendorStatistics.class, OFVendorStatistics.class,
106 new Instantiable<OFStatistics>() {
107 @Override
108 public OFStatistics instantiate() {
109 return new OFVendorStatistics();
110 }
111 },
112 new Instantiable<OFStatistics>() {
113 @Override
114 public OFStatistics instantiate() {
115 return new OFVendorStatistics();
116 }
117 });
118
119 static OFStatisticsType[] requestMapping;
120 static OFStatisticsType[] replyMapping;
121
122 protected Class<? extends OFStatistics> requestClass;
123 protected Constructor<? extends OFStatistics> requestConstructor;
124 protected Instantiable<OFStatistics> requestInstantiable;
125 protected Class<? extends OFStatistics> replyClass;
126 protected Constructor<? extends OFStatistics> replyConstructor;
127 protected Instantiable<OFStatistics> replyInstantiable;
128 protected short type;
129
130 /**
131 * Store some information about the OpenFlow Statistic type, including wire
132 * protocol type number, and derived class
133 *
134 * @param type Wire protocol number associated with this OFStatisticsType
135 * @param requestClass The Statistics Java class to return when the
136 * containing OFType is STATS_REQUEST
137 * @param replyClass The Statistics Java class to return when the
138 * containing OFType is STATS_REPLY
139 */
140 OFStatisticsType(int type, Class<? extends OFStatistics> requestClass,
141 Class<? extends OFStatistics> replyClass,
142 Instantiable<OFStatistics> requestInstantiable,
143 Instantiable<OFStatistics> replyInstantiable) {
144 this.type = (short) type;
145 this.requestClass = requestClass;
146 try {
147 this.requestConstructor = requestClass.getConstructor(new Class[]{});
148 } catch (Exception e) {
149 throw new RuntimeException(
150 "Failure getting constructor for class: " + requestClass, e);
151 }
152
153 this.replyClass = replyClass;
154 try {
155 this.replyConstructor = replyClass.getConstructor(new Class[]{});
156 } catch (Exception e) {
157 throw new RuntimeException(
158 "Failure getting constructor for class: " + replyClass, e);
159 }
160 this.requestInstantiable = requestInstantiable;
161 this.replyInstantiable = replyInstantiable;
162 OFStatisticsType.addMapping(this.type, OFType.STATS_REQUEST, this);
163 OFStatisticsType.addMapping(this.type, OFType.STATS_REPLY, this);
164 }
165
166 /**
167 * Adds a mapping from type value to OFStatisticsType enum
168 *
169 * @param i OpenFlow wire protocol type
170 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
171 * STATS_REPLY
172 * @param st type
173 */
174 static public void addMapping(short i, OFType t, OFStatisticsType st) {
175 if (i < 0)
176 i = (short) (16+i);
177 if (t == OFType.STATS_REQUEST) {
178 if (requestMapping == null)
179 requestMapping = new OFStatisticsType[16];
180 OFStatisticsType.requestMapping[i] = st;
181 } else if (t == OFType.STATS_REPLY){
182 if (replyMapping == null)
183 replyMapping = new OFStatisticsType[16];
184 OFStatisticsType.replyMapping[i] = st;
185 } else {
186 throw new RuntimeException(t.toString() + " is an invalid OFType");
187 }
188 }
189
190 /**
191 * Remove a mapping from type value to OFStatisticsType enum
192 *
193 * @param i OpenFlow wire protocol type
194 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
195 * STATS_REPLY
196 */
197 static public void removeMapping(short i, OFType t) {
198 if (i < 0)
199 i = (short) (16+i);
200 if (t == OFType.STATS_REQUEST) {
201 requestMapping[i] = null;
202 } else if (t == OFType.STATS_REPLY){
203 replyMapping[i] = null;
204 } else {
205 throw new RuntimeException(t.toString() + " is an invalid OFType");
206 }
207 }
208
209 /**
210 * Given a wire protocol OpenFlow type number, return the OFStatisticsType
211 * associated with it
212 *
213 * @param i wire protocol number
214 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
215 * STATS_REPLY
216 * @return OFStatisticsType enum type
217 */
218 static public OFStatisticsType valueOf(short i, OFType t) {
219 if (i < 0)
220 i = (short) (16+i);
221 if (t == OFType.STATS_REQUEST) {
222 return requestMapping[i];
223 } else if (t == OFType.STATS_REPLY){
224 return replyMapping[i];
225 } else {
226 throw new RuntimeException(t.toString() + " is an invalid OFType");
227 }
228 }
229
230 /**
231 * @return Returns the wire protocol value corresponding to this
232 * OFStatisticsType
233 */
234 public short getTypeValue() {
235 return this.type;
236 }
237
238 /**
239 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
240 * STATS_REPLY
241 * @return return the OFMessage subclass corresponding to this
242 * OFStatisticsType
243 */
244 public Class<? extends OFStatistics> toClass(OFType t) {
245 if (t == OFType.STATS_REQUEST) {
246 return requestClass;
247 } else if (t == OFType.STATS_REPLY){
248 return replyClass;
249 } else {
250 throw new RuntimeException(t.toString() + " is an invalid OFType");
251 }
252 }
253
254 /**
255 * Returns the no-argument Constructor of the implementation class for
256 * this OFStatisticsType, either request or reply based on the supplied
257 * OFType
258 *
259 * @param t
260 * @return
261 */
262 public Constructor<? extends OFStatistics> getConstructor(OFType t) {
263 if (t == OFType.STATS_REQUEST) {
264 return requestConstructor;
265 } else if (t == OFType.STATS_REPLY) {
266 return replyConstructor;
267 } else {
268 throw new RuntimeException(t.toString() + " is an invalid OFType");
269 }
270 }
271
272 /**
273 * @return the requestInstantiable
274 */
275 public Instantiable<OFStatistics> getRequestInstantiable() {
276 return requestInstantiable;
277 }
278
279 /**
280 * @param requestInstantiable the requestInstantiable to set
281 */
282 public void setRequestInstantiable(
283 Instantiable<OFStatistics> requestInstantiable) {
284 this.requestInstantiable = requestInstantiable;
285 }
286
287 /**
288 * @return the replyInstantiable
289 */
290 public Instantiable<OFStatistics> getReplyInstantiable() {
291 return replyInstantiable;
292 }
293
294 /**
295 * @param replyInstantiable the replyInstantiable to set
296 */
297 public void setReplyInstantiable(Instantiable<OFStatistics> replyInstantiable) {
298 this.replyInstantiable = replyInstantiable;
299 }
300
301 /**
302 * Returns a new instance of the implementation class for
303 * this OFStatisticsType, either request or reply based on the supplied
304 * OFType
305 *
306 * @param t
307 * @return
308 */
309 public OFStatistics newInstance(OFType t) {
310 if (t == OFType.STATS_REQUEST) {
311 return requestInstantiable.instantiate();
312 } else if (t == OFType.STATS_REPLY) {
313 return replyInstantiable.instantiate();
314 } else {
315 throw new RuntimeException(t.toString() + " is an invalid OFType");
316 }
317 }
318}