Added openflow-codec and openflowj_netty from openflowplugin
[openflowjava.git] / third-party / openflow-codec / src / main / java / org / openflow / codec / protocol / action / OFPActionType.java
1 /**
2  *
3  */
4 package org.openflow.codec.protocol.action;
5
6 import java.io.Serializable;
7 import java.lang.reflect.Constructor;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import org.openflow.codec.protocol.Instantiable;
12
13 /**
14  * List of OpenFlow Action types and mappings to wire protocol value and derived
15  * classes
16  *
17  * @author David Erickson (daviderickson@cs.stanford.edu)
18  * @author Yugandhar Sarraju (ysarraju@in.ibm.com)
19  */
20 public enum OFPActionType implements Serializable {
21     OUTPUT(0, OFPActionOutput.class, new Instantiable<OFPAction>() {
22         @Override
23         public OFPAction instantiate() {
24             return new OFPActionOutput();
25         }
26     }), COPY_TTL_OUT(11, OFPActionCopyTimeToLiveOut.class, new Instantiable<OFPAction>() {
27         @Override
28         public OFPAction instantiate() {
29             return new OFPActionCopyTimeToLiveOut();
30         }
31     }), COPY_TTL_IN(12, OFPActionCopyTimeToLiveIn.class, new Instantiable<OFPAction>() {
32         @Override
33         public OFPAction instantiate() {
34             return new OFPActionCopyTimeToLiveIn();
35         }
36     }), SET_MPLS_TTL(15, OFPActionMplsTimeToLive.class, new Instantiable<OFPAction>() {
37         @Override
38         public OFPAction instantiate() {
39             return new OFPActionMplsTimeToLive();
40         }
41     }), DEC_MPLS_TTL(16, OFPActionDecMplsTimeToLive.class, new Instantiable<OFPAction>() {
42         @Override
43         public OFPAction instantiate() {
44             return new OFPActionDecMplsTimeToLive();
45         }
46     }), PUSH_VLAN(17, OFPActionPushVLAN.class, new Instantiable<OFPAction>() {
47         @Override
48         public OFPAction instantiate() {
49             return new OFPActionPushVLAN();
50         }
51     }), POP_VLAN(18, OFPActionPopVLAN.class, new Instantiable<OFPAction>() {
52         @Override
53         public OFPAction instantiate() {
54             return new OFPActionPopVLAN();
55         }
56     }), PUSH_MPLS(19, OFPActionPushMpls.class, new Instantiable<OFPAction>() {
57         @Override
58         public OFPAction instantiate() {
59             return new OFPActionPushMpls();
60         }
61     }), POP_MPLS(20, OFPActionPopMpls.class, new Instantiable<OFPAction>() {
62         @Override
63         public OFPAction instantiate() {
64             return new OFPActionPopMpls();
65         }
66     }), SET_QUEUE(21, OFPActionSetQueue.class, new Instantiable<OFPAction>() {
67         @Override
68         public OFPAction instantiate() {
69             return new OFPActionSetQueue();
70         }
71     }), GROUP(22, OFPActionGroup.class, new Instantiable<OFPAction>() {
72         @Override
73         public OFPAction instantiate() {
74             return new OFPActionGroup();
75         }
76     }), SET_NW_TTL(23, OFPActionNetworkTimeToLive.class, new Instantiable<OFPAction>() {
77         @Override
78         public OFPAction instantiate() {
79             return new OFPActionNetworkTimeToLive();
80         }
81     }), DEC_NW_TTL(24, OFPActionDecNetworkTimeToLive.class, new Instantiable<OFPAction>() {
82         @Override
83         public OFPAction instantiate() {
84             return new OFPActionDecNetworkTimeToLive();
85         }
86     }), SET_FIELD(25, OFPActionSetField.class, new Instantiable<OFPAction>() {
87         @Override
88         public OFPAction instantiate() {
89             return new OFPActionSetField();
90         }
91     }), PUSH_PBB(26, OFPActionPushPbb.class, new Instantiable<OFPAction>() {
92         @Override
93         public OFPAction instantiate() {
94             return new OFPActionPushPbb();
95         }
96     }), POP_PBB(27, OFPActionPopPbb.class, new Instantiable<OFPAction>() {
97         @Override
98         public OFPAction instantiate() {
99             return new OFPActionPopPbb();
100         }
101     }),
102
103     EXPERIMENTER(0xffff, OFPActionExperimenterHeader.class, new Instantiable<OFPAction>() {
104         @Override
105         public OFPAction instantiate() {
106             return new OFPActionExperimenterHeader();
107         }
108     });
109
110     protected static HashMap<Short, OFPActionType> mapping;
111
112     protected Class<? extends OFPAction> clazz;
113     protected Constructor<? extends OFPAction> constructor;
114     protected Instantiable<OFPAction> instantiable;
115     protected int minLen;
116     protected short type;
117
118     /**
119      * Store some information about the OpenFlow Action type, including wire
120      * protocol type number, length, and derrived class
121      *
122      * @param type
123      *            Wire protocol number associated with this OFPType
124      * @param clazz
125      *            The Java class corresponding to this type of OpenFlow Action
126      * @param instantiable
127      *            the instantiable for the OFPAction this type represents
128      */
129     OFPActionType(int type, Class<? extends OFPAction> clazz, Instantiable<OFPAction> instantiable) {
130         this.type = (short) type;
131         this.clazz = clazz;
132         this.instantiable = instantiable;
133         try {
134             this.constructor = clazz.getConstructor(new Class[] {});
135         } catch (Exception e) {
136             throw new RuntimeException("Failure getting constructor for class: " + clazz, e);
137         }
138         OFPActionType.addMapping(this.type, this);
139     }
140
141     /**
142      * Adds a mapping from type value to OFPActionType enum
143      *
144      * @param i
145      *            OpenFlow wire protocol Action type value
146      * @param t
147      *            type
148      */
149     static public void addMapping(short i, OFPActionType t) {
150         if (mapping == null)
151             mapping = new HashMap<Short, OFPActionType>();
152         // bring higher mappings down to the edge of our map
153         if (i < 0)
154             i = (short) (30 + i);
155         mapping.put(i, t);
156     }
157
158     /**
159      * Given a wire protocol OpenFlow type number, return the OFPType associated
160      * with it
161      *
162      * @param i
163      *            wire protocol number
164      * @return OFPType enum type
165      */
166
167     static public OFPActionType valueOf(short i) {
168         if (i < 0)
169             i = (short) (30 + i);
170         return OFPActionType.mapping.get(i);
171     }
172
173     /**
174      * @return Returns the wire protocol value corresponding to this
175      *         OFPActionType
176      */
177     public short getTypeValue() {
178         return this.type;
179     }
180
181     /**
182      * @return return the OFPAction subclass corresponding to this OFPActionType
183      */
184     public Class<? extends OFPAction> toClass() {
185         return clazz;
186     }
187
188     /**
189      * Returns the no-argument Constructor of the implementation class for this
190      * OFPActionType
191      *
192      * @return the constructor
193      */
194     public Constructor<? extends OFPAction> getConstructor() {
195         return constructor;
196     }
197
198     /**
199      * Returns a new instance of the OFPAction represented by this OFPActionType
200      *
201      * @return the new object
202      */
203     public OFPAction newInstance() {
204         return instantiable.instantiate();
205     }
206
207     /**
208      * @return the instantiable
209      */
210     public Instantiable<OFPAction> getInstantiable() {
211         return instantiable;
212     }
213
214     /**
215      * @param instantiable
216      *            the instantiable to set
217      */
218     public void setInstantiable(Instantiable<OFPAction> instantiable) {
219         this.instantiable = instantiable;
220     }
221 }