b72752542a12506b5dbd115a687ab2afa3ae5e56
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / queue / OFPQueuePropertyType.java
1 /**
2  *
3  */
4 package org.openflow.codec.protocol.queue;
5
6 import java.lang.reflect.Constructor;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import org.openflow.codec.protocol.Instantiable;
11 import org.openflow.codec.util.U16;
12
13 /**
14  * List of OpenFlow Queue Property types and mappings to wire protocol value and
15  * derived classes
16  *
17  * @author David Erickson (daviderickson@cs.stanford.edu)
18  */
19 public class OFPQueuePropertyType {
20
21     public static OFPQueuePropertyType MIN_RATE = new OFPQueuePropertyType(1, "MIN_RATE",
22             OFPQueuePropertyMinRate.class, new Instantiable<OFPQueueProperty>() {
23                 @Override
24                 public OFPQueueProperty instantiate() {
25                     return new OFPQueuePropertyMinRate();
26                 }
27             });
28     public static OFPQueuePropertyType MAX_RATE = new OFPQueuePropertyType(2, "MAX_RATE",
29             OFPQueuePropertyMaxRate.class, new Instantiable<OFPQueueProperty>() {
30                 @Override
31                 public OFPQueueProperty instantiate() {
32                     return new OFPQueuePropertyMaxRate();
33                 }
34             });
35     public static OFPQueuePropertyType EXPERIMENTER = new OFPQueuePropertyType(0xffff, "EXPERIMENTER",
36             OFPQueuePropertyExperimenter.class, new Instantiable<OFPQueueProperty>() {
37                 @Override
38                 public OFPQueueProperty instantiate() {
39                     return new OFPQueuePropertyExperimenter();
40                 }
41             });
42
43     protected static Map<Integer, OFPQueuePropertyType> mapping;
44
45     protected Class<? extends OFPQueueProperty> clazz;
46     protected Constructor<? extends OFPQueueProperty> constructor;
47     protected Instantiable<OFPQueueProperty> instantiable;
48     protected int minLen;
49     protected String name;
50     protected short type;
51
52     /**
53      * Store some information about the OpenFlow Queue Property type, including
54      * wire protocol type number, length, and derived class
55      *
56      * @param type
57      *            Wire protocol number associated with this OFPQueuePropertyType
58      * @param name
59      *            The name of this type
60      * @param clazz
61      *            The Java class corresponding to this type of OpenFlow Queue
62      *            Property
63      * @param instantiable
64      *            the instantiable for the OFPQueueProperty this type represents
65      */
66     OFPQueuePropertyType(int type, String name, Class<? extends OFPQueueProperty> clazz,
67             Instantiable<OFPQueueProperty> instantiable) {
68         this.type = (short) type;
69         this.name = name;
70         this.clazz = clazz;
71         this.instantiable = instantiable;
72         try {
73             this.constructor = clazz.getConstructor(new Class[] {});
74         } catch (Exception e) {
75             throw new RuntimeException("Failure getting constructor for class: " + clazz, e);
76         }
77         OFPQueuePropertyType.addMapping(type, this);
78     }
79
80     /**
81      * Adds a mapping from type value to OFPQueuePropertyType enum
82      *
83      * @param i
84      *            OpenFlow wire protocol Action type value
85      * @param t
86      *            type
87      */
88     static public void addMapping(int i, OFPQueuePropertyType t) {
89         if (mapping == null)
90             mapping = new HashMap<Integer, OFPQueuePropertyType>();
91         OFPQueuePropertyType.mapping.put(i, t);
92     }
93
94     /**
95      * Given a wire protocol OpenFlow type number, return the OFPType associated
96      * with it
97      *
98      * @param i
99      *            wire protocol number
100      * @return OFPType enum type
101      */
102
103     static public OFPQueuePropertyType valueOf(short i) {
104         return OFPQueuePropertyType.mapping.get(U16.f(i));
105     }
106
107     /**
108      * @return Returns the wire protocol value corresponding to this
109      *         OFPQueuePropertyType
110      */
111     public short getTypeValue() {
112         return this.type;
113     }
114
115     /**
116      * @return return the OFPQueueProperty subclass corresponding to this
117      *         OFPQueuePropertyType
118      */
119     public Class<? extends OFPQueueProperty> toClass() {
120         return clazz;
121     }
122
123     /**
124      * Returns the no-argument Constructor of the implementation class for this
125      * OFPQueuePropertyType
126      *
127      * @return the constructor
128      */
129     public Constructor<? extends OFPQueueProperty> getConstructor() {
130         return constructor;
131     }
132
133     /**
134      * Returns a new instance of the OFPQueueProperty represented by this
135      * OFPQueuePropertyType
136      *
137      * @return the new object
138      */
139     public OFPQueueProperty newInstance() {
140         return instantiable.instantiate();
141     }
142
143     /**
144      * @return the instantiable
145      */
146     public Instantiable<OFPQueueProperty> getInstantiable() {
147         return instantiable;
148     }
149
150     /**
151      * @param instantiable
152      *            the instantiable to set
153      */
154     public void setInstantiable(Instantiable<OFPQueueProperty> instantiable) {
155         this.instantiable = instantiable;
156     }
157
158     public String getName() {
159         return this.name;
160     }
161
162     @Override
163     public String toString() {
164         return this.name;
165     }
166 }