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