2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 package org.openflow.protocol.statistics;
20 import java.lang.reflect.Constructor;
22 import org.openflow.protocol.Instantiable;
23 import org.openflow.protocol.OFType;
25 public enum OFStatisticsType {
26 DESC (0, OFDescriptionStatistics.class, OFDescriptionStatistics.class,
27 new Instantiable<OFStatistics>() {
29 public OFStatistics instantiate() {
30 return new OFDescriptionStatistics();
33 new Instantiable<OFStatistics>() {
35 public OFStatistics instantiate() {
36 return new OFDescriptionStatistics();
39 FLOW (1, OFFlowStatisticsRequest.class, OFFlowStatisticsReply.class,
40 new Instantiable<OFStatistics>() {
42 public OFStatistics instantiate() {
43 return new OFFlowStatisticsRequest();
46 new Instantiable<OFStatistics>() {
48 public OFStatistics instantiate() {
49 return new OFFlowStatisticsReply();
52 AGGREGATE (2, OFAggregateStatisticsRequest.class, OFAggregateStatisticsReply.class,
53 new Instantiable<OFStatistics>() {
55 public OFStatistics instantiate() {
56 return new OFAggregateStatisticsRequest();
59 new Instantiable<OFStatistics>() {
61 public OFStatistics instantiate() {
62 return new OFAggregateStatisticsReply();
65 TABLE (3, OFTableStatistics.class, OFTableStatistics.class,
66 new Instantiable<OFStatistics>() {
68 public OFStatistics instantiate() {
69 return new OFTableStatistics();
72 new Instantiable<OFStatistics>() {
74 public OFStatistics instantiate() {
75 return new OFTableStatistics();
78 PORT (4, OFPortStatisticsRequest.class, OFPortStatisticsReply.class,
79 new Instantiable<OFStatistics>() {
81 public OFStatistics instantiate() {
82 return new OFPortStatisticsRequest();
85 new Instantiable<OFStatistics>() {
87 public OFStatistics instantiate() {
88 return new OFPortStatisticsReply();
91 QUEUE (5, OFQueueStatisticsRequest.class, OFQueueStatisticsReply.class,
92 new Instantiable<OFStatistics>() {
94 public OFStatistics instantiate() {
95 return new OFQueueStatisticsRequest();
98 new Instantiable<OFStatistics>() {
100 public OFStatistics instantiate() {
101 return new OFQueueStatisticsReply();
104 VENDOR (0xffff, OFVendorStatistics.class, OFVendorStatistics.class,
105 new Instantiable<OFStatistics>() {
107 public OFStatistics instantiate() {
108 return new OFVendorStatistics();
111 new Instantiable<OFStatistics>() {
113 public OFStatistics instantiate() {
114 return new OFVendorStatistics();
118 static OFStatisticsType[] requestMapping;
119 static OFStatisticsType[] replyMapping;
121 protected Class<? extends OFStatistics> requestClass;
122 protected Constructor<? extends OFStatistics> requestConstructor;
123 protected Instantiable<OFStatistics> requestInstantiable;
124 protected Class<? extends OFStatistics> replyClass;
125 protected Constructor<? extends OFStatistics> replyConstructor;
126 protected Instantiable<OFStatistics> replyInstantiable;
127 protected short type;
130 * Store some information about the OpenFlow Statistic type, including wire
131 * protocol type number, and derived class
133 * @param type Wire protocol number associated with this OFStatisticsType
134 * @param requestClass The Statistics Java class to return when the
135 * containing OFType is STATS_REQUEST
136 * @param replyClass The Statistics Java class to return when the
137 * containing OFType is STATS_REPLY
139 OFStatisticsType(int type, Class<? extends OFStatistics> requestClass,
140 Class<? extends OFStatistics> replyClass,
141 Instantiable<OFStatistics> requestInstantiable,
142 Instantiable<OFStatistics> replyInstantiable) {
143 this.type = (short) type;
144 this.requestClass = requestClass;
146 this.requestConstructor = requestClass.getConstructor(new Class[]{});
147 } catch (Exception e) {
148 throw new RuntimeException(
149 "Failure getting constructor for class: " + requestClass, e);
152 this.replyClass = replyClass;
154 this.replyConstructor = replyClass.getConstructor(new Class[]{});
155 } catch (Exception e) {
156 throw new RuntimeException(
157 "Failure getting constructor for class: " + replyClass, e);
159 this.requestInstantiable = requestInstantiable;
160 this.replyInstantiable = replyInstantiable;
161 OFStatisticsType.addMapping(this.type, OFType.STATS_REQUEST, this);
162 OFStatisticsType.addMapping(this.type, OFType.STATS_REPLY, this);
166 * Adds a mapping from type value to OFStatisticsType enum
168 * @param i OpenFlow wire protocol type
169 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
173 static public void addMapping(short i, OFType t, OFStatisticsType st) {
176 if (t == OFType.STATS_REQUEST) {
177 if (requestMapping == null)
178 requestMapping = new OFStatisticsType[16];
179 OFStatisticsType.requestMapping[i] = st;
180 } else if (t == OFType.STATS_REPLY){
181 if (replyMapping == null)
182 replyMapping = new OFStatisticsType[16];
183 OFStatisticsType.replyMapping[i] = st;
185 throw new RuntimeException(t.toString() + " is an invalid OFType");
190 * Remove a mapping from type value to OFStatisticsType enum
192 * @param i OpenFlow wire protocol type
193 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
196 static public void removeMapping(short i, OFType t) {
199 if (t == OFType.STATS_REQUEST) {
200 requestMapping[i] = null;
201 } else if (t == OFType.STATS_REPLY){
202 replyMapping[i] = null;
204 throw new RuntimeException(t.toString() + " is an invalid OFType");
209 * Given a wire protocol OpenFlow type number, return the OFStatisticsType
212 * @param i wire protocol number
213 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
215 * @return OFStatisticsType enum type
217 static public OFStatisticsType valueOf(short i, OFType t) {
220 if (t == OFType.STATS_REQUEST) {
221 return requestMapping[i];
222 } else if (t == OFType.STATS_REPLY){
223 return replyMapping[i];
225 throw new RuntimeException(t.toString() + " is an invalid OFType");
230 * @return Returns the wire protocol value corresponding to this
233 public short getTypeValue() {
238 * @param t type of containing OFMessage, only accepts STATS_REQUEST or
240 * @return return the OFMessage subclass corresponding to this
243 public Class<? extends OFStatistics> toClass(OFType t) {
244 if (t == OFType.STATS_REQUEST) {
246 } else if (t == OFType.STATS_REPLY){
249 throw new RuntimeException(t.toString() + " is an invalid OFType");
254 * Returns the no-argument Constructor of the implementation class for
255 * this OFStatisticsType, either request or reply based on the supplied
261 public Constructor<? extends OFStatistics> getConstructor(OFType t) {
262 if (t == OFType.STATS_REQUEST) {
263 return requestConstructor;
264 } else if (t == OFType.STATS_REPLY) {
265 return replyConstructor;
267 throw new RuntimeException(t.toString() + " is an invalid OFType");
272 * @return the requestInstantiable
274 public Instantiable<OFStatistics> getRequestInstantiable() {
275 return requestInstantiable;
279 * @param requestInstantiable the requestInstantiable to set
281 public void setRequestInstantiable(
282 Instantiable<OFStatistics> requestInstantiable) {
283 this.requestInstantiable = requestInstantiable;
287 * @return the replyInstantiable
289 public Instantiable<OFStatistics> getReplyInstantiable() {
290 return replyInstantiable;
294 * @param replyInstantiable the replyInstantiable to set
296 public void setReplyInstantiable(Instantiable<OFStatistics> replyInstantiable) {
297 this.replyInstantiable = replyInstantiable;
301 * Returns a new instance of the implementation class for
302 * this OFStatisticsType, either request or reply based on the supplied
308 public OFStatistics newInstance(OFType t) {
309 if (t == OFType.STATS_REQUEST) {
310 return requestInstantiable.instantiate();
311 } else if (t == OFType.STATS_REPLY) {
312 return replyInstantiable.instantiate();
314 throw new RuntimeException(t.toString() + " is an invalid OFType");