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