18229170b77dc1a01984cfc9f156272f5ad858c5
[openflowjava.git] / third-party / openflowj_netty / src / main / java / org / openflow / protocol / action / OFActionType.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 /**
19  *
20  */
21 package org.openflow.protocol.action;
22
23 import java.lang.reflect.Constructor;
24
25 import org.openflow.protocol.Instantiable;
26
27 /**
28  * List of OpenFlow Action types and mappings to wire protocol value and
29  * derived classes
30  *
31  * @author David Erickson (daviderickson@cs.stanford.edu)
32  */
33 public enum OFActionType {
34     OUTPUT              (0, OFActionOutput.class, new Instantiable<OFAction>() {
35                             @Override
36                             public OFAction instantiate() {
37                                 return new OFActionOutput();
38                             }}),
39     SET_VLAN_ID        (1, OFActionVirtualLanIdentifier.class, new Instantiable<OFAction>() {
40                             @Override
41                             public OFAction instantiate() {
42                                 return new OFActionVirtualLanIdentifier();
43                             }}),
44     SET_VLAN_PCP        (2, OFActionVirtualLanPriorityCodePoint.class, new Instantiable<OFAction>() {
45                             @Override
46                             public OFAction instantiate() {
47                                 return new OFActionVirtualLanPriorityCodePoint();
48                             }}),
49     STRIP_VLAN          (3, OFActionStripVirtualLan.class, new Instantiable<OFAction>() {
50                             @Override
51                             public OFAction instantiate() {
52                                 return new OFActionStripVirtualLan();
53                             }}),
54     SET_DL_SRC          (4, OFActionDataLayerSource.class, new Instantiable<OFAction>() {
55                             @Override
56                             public OFAction instantiate() {
57                                 return new OFActionDataLayerSource();
58                             }}),
59     SET_DL_DST          (5, OFActionDataLayerDestination.class, new Instantiable<OFAction>() {
60                             @Override
61                             public OFAction instantiate() {
62                                 return new OFActionDataLayerDestination();
63                             }}),
64     SET_NW_SRC          (6, OFActionNetworkLayerSource.class, new Instantiable<OFAction>() {
65                             @Override
66                             public OFAction instantiate() {
67                                 return new OFActionNetworkLayerSource();
68                             }}),
69     SET_NW_DST          (7, OFActionNetworkLayerDestination.class, new Instantiable<OFAction>() {
70                             @Override
71                             public OFAction instantiate() {
72                                 return new OFActionNetworkLayerDestination();
73                             }}),
74     SET_NW_TOS          (8, OFActionNetworkTypeOfService.class, new Instantiable<OFAction>() {
75                             @Override
76                             public OFAction instantiate() {
77                                 return new OFActionNetworkTypeOfService();
78                             }}),
79     SET_TP_SRC          (9, OFActionTransportLayerSource.class, new Instantiable<OFAction>() {
80                             @Override
81                             public OFAction instantiate() {
82                                 return new OFActionTransportLayerSource();
83                             }}),
84     SET_TP_DST          (10, OFActionTransportLayerDestination.class, new Instantiable<OFAction>() {
85                             @Override
86                             public OFAction instantiate() {
87                                 return new OFActionTransportLayerDestination();
88                             }}),
89     OPAQUE_ENQUEUE      (11, OFActionEnqueue.class, new Instantiable<OFAction>() {
90                             @Override
91                             public OFAction instantiate() {
92                                 return new OFActionEnqueue();
93                             }}),
94     VENDOR              (0xffff, OFActionVendor.class, new Instantiable<OFAction>() {
95                             @Override
96                             public OFAction instantiate() {
97                                 return new OFActionVendorGeneric();
98                             }});
99
100     protected static OFActionType[] mapping;
101
102     protected Class<? extends OFAction> clazz;
103     protected Constructor<? extends OFAction> constructor;
104     protected Instantiable<OFAction> instantiable;
105     protected int minLen;
106     protected short type;
107
108     /**
109      * Store some information about the OpenFlow Action type, including wire
110      * protocol type number, length, and derrived class
111      *
112      * @param type Wire protocol number associated with this OFType
113      * @param clazz The Java class corresponding to this type of OpenFlow Action
114      * @param instantiable the instantiable for the OFAction this type represents
115      */
116     OFActionType(int type, Class<? extends OFAction> clazz, Instantiable<OFAction> instantiable) {
117         this.type = (short) type;
118         this.clazz = clazz;
119         this.instantiable = instantiable;
120         try {
121             this.constructor = clazz.getConstructor(new Class[]{});
122         } catch (Exception e) {
123             throw new RuntimeException(
124                     "Failure getting constructor for class: " + clazz, e);
125         }
126         OFActionType.addMapping(this.type, this);
127     }
128
129     /**
130      * Adds a mapping from type value to OFActionType enum
131      *
132      * @param i OpenFlow wire protocol Action type value
133      * @param t type
134      */
135     static public void addMapping(short i, OFActionType t) {
136         if (mapping == null)
137             mapping = new OFActionType[16];
138         // bring higher mappings down to the edge of our array
139         if (i < 0)
140             i = (short) (16 + i);
141         OFActionType.mapping[i] = t;
142     }
143
144     /**
145      * Given a wire protocol OpenFlow type number, return the OFType associated
146      * with it
147      *
148      * @param i wire protocol number
149      * @return OFType enum type
150      */
151
152     static public OFActionType valueOf(short i) {
153         if (i < 0)
154             i = (short) (16+i);
155         return OFActionType.mapping[i];
156     }
157
158     /**
159      * @return Returns the wire protocol value corresponding to this
160      *         OFActionType
161      */
162     public short getTypeValue() {
163         return this.type;
164     }
165
166     /**
167      * @return return the OFAction subclass corresponding to this OFActionType
168      */
169     public Class<? extends OFAction> toClass() {
170         return clazz;
171     }
172
173     /**
174      * Returns the no-argument Constructor of the implementation class for
175      * this OFActionType
176      * @return the constructor
177      */
178     public Constructor<? extends OFAction> getConstructor() {
179         return constructor;
180     }
181
182     /**
183      * Returns a new instance of the OFAction represented by this OFActionType
184      * @return the new object
185      */
186     public OFAction newInstance() {
187         return instantiable.instantiate();
188     }
189
190     /**
191      * @return the instantiable
192      */
193     public Instantiable<OFAction> getInstantiable() {
194         return instantiable;
195     }
196
197     /**
198      * @param instantiable the instantiable to set
199      */
200     public void setInstantiable(Instantiable<OFAction> instantiable) {
201         this.instantiable = instantiable;
202     }
203 }