Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 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; |
| 19 | |
| 20 | import java.lang.reflect.Constructor; |
| 21 | |
| 22 | /** |
| 23 | * List of OpenFlow types and mappings to wire protocol value and derived |
| 24 | * classes |
| 25 | * |
| 26 | * @author Rob Sherwood (rob.sherwood@stanford.edu) |
| 27 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 28 | * |
| 29 | */ |
| 30 | public enum OFType { |
| 31 | HELLO (0, OFHello.class, new Instantiable<OFMessage>() { |
| 32 | @Override |
| 33 | public OFMessage instantiate() { |
| 34 | return new OFHello(); |
| 35 | }}), |
| 36 | ERROR (1, OFError.class, new Instantiable<OFMessage>() { |
| 37 | @Override |
| 38 | public OFMessage instantiate() { |
| 39 | return new OFError(); |
| 40 | }}), |
| 41 | ECHO_REQUEST (2, OFEchoRequest.class, new Instantiable<OFMessage>() { |
| 42 | @Override |
| 43 | public OFMessage instantiate() { |
| 44 | return new OFEchoRequest(); |
| 45 | }}), |
| 46 | ECHO_REPLY (3, OFEchoReply.class, new Instantiable<OFMessage>() { |
| 47 | @Override |
| 48 | public OFMessage instantiate() { |
| 49 | return new OFEchoReply(); |
| 50 | }}), |
| 51 | VENDOR (4, OFVendor.class, new Instantiable<OFMessage>() { |
| 52 | @Override |
| 53 | public OFMessage instantiate() { |
| 54 | return new OFVendor(); |
| 55 | }}), |
| 56 | FEATURES_REQUEST (5, OFFeaturesRequest.class, new Instantiable<OFMessage>() { |
| 57 | @Override |
| 58 | public OFMessage instantiate() { |
| 59 | return new OFFeaturesRequest(); |
| 60 | }}), |
| 61 | FEATURES_REPLY (6, OFFeaturesReply.class, new Instantiable<OFMessage>() { |
| 62 | @Override |
| 63 | public OFMessage instantiate() { |
| 64 | return new OFFeaturesReply(); |
| 65 | }}), |
| 66 | GET_CONFIG_REQUEST (7, OFGetConfigRequest.class, new Instantiable<OFMessage>() { |
| 67 | @Override |
| 68 | public OFMessage instantiate() { |
| 69 | return new OFGetConfigRequest(); |
| 70 | }}), |
| 71 | GET_CONFIG_REPLY (8, OFGetConfigReply.class, new Instantiable<OFMessage>() { |
| 72 | @Override |
| 73 | public OFMessage instantiate() { |
| 74 | return new OFGetConfigReply(); |
| 75 | }}), |
| 76 | SET_CONFIG (9, OFSetConfig.class, new Instantiable<OFMessage>() { |
| 77 | @Override |
| 78 | public OFMessage instantiate() { |
| 79 | return new OFSetConfig(); |
| 80 | }}), |
| 81 | PACKET_IN (10, OFPacketIn.class, new Instantiable<OFMessage>() { |
| 82 | @Override |
| 83 | public OFMessage instantiate() { |
| 84 | return new OFPacketIn(); |
| 85 | }}), |
| 86 | FLOW_REMOVED (11, OFFlowRemoved.class, new Instantiable<OFMessage>() { |
| 87 | @Override |
| 88 | public OFMessage instantiate() { |
| 89 | return new OFFlowRemoved(); |
| 90 | }}), |
| 91 | PORT_STATUS (12, OFPortStatus.class, new Instantiable<OFMessage>() { |
| 92 | @Override |
| 93 | public OFMessage instantiate() { |
| 94 | return new OFPortStatus(); |
| 95 | }}), |
| 96 | PACKET_OUT (13, OFPacketOut.class, new Instantiable<OFMessage>() { |
| 97 | @Override |
| 98 | public OFMessage instantiate() { |
| 99 | return new OFPacketOut(); |
| 100 | }}), |
| 101 | FLOW_MOD (14, OFFlowMod.class, new Instantiable<OFMessage>() { |
| 102 | @Override |
| 103 | public OFMessage instantiate() { |
| 104 | return new OFFlowMod(); |
| 105 | }}), |
| 106 | PORT_MOD (15, OFPortMod.class, new Instantiable<OFMessage>() { |
| 107 | @Override |
| 108 | public OFMessage instantiate() { |
| 109 | return new OFPortMod(); |
| 110 | }}), |
| 111 | STATS_REQUEST (16, OFStatisticsRequest.class, new Instantiable<OFMessage>() { |
| 112 | @Override |
| 113 | public OFMessage instantiate() { |
| 114 | return new OFStatisticsRequest(); |
| 115 | }}), |
| 116 | STATS_REPLY (17, OFStatisticsReply.class, new Instantiable<OFMessage>() { |
| 117 | @Override |
| 118 | public OFMessage instantiate() { |
| 119 | return new OFStatisticsReply(); |
| 120 | }}), |
| 121 | BARRIER_REQUEST (18, OFBarrierRequest.class, new Instantiable<OFMessage>() { |
| 122 | @Override |
| 123 | public OFMessage instantiate() { |
| 124 | return new OFBarrierRequest(); |
| 125 | }}), |
| 126 | BARRIER_REPLY (19, OFBarrierReply.class, new Instantiable<OFMessage>() { |
| 127 | @Override |
| 128 | public OFMessage instantiate() { |
| 129 | return new OFBarrierReply(); |
| 130 | }}); |
| 131 | |
| 132 | static OFType[] mapping; |
| 133 | |
| 134 | protected Class<? extends OFMessage> clazz; |
| 135 | protected Constructor<? extends OFMessage> constructor; |
| 136 | protected Instantiable<OFMessage> instantiable; |
| 137 | protected byte type; |
| 138 | |
| 139 | /** |
| 140 | * Store some information about the OpenFlow type, including wire protocol |
| 141 | * type number, length, and derived class |
| 142 | * |
| 143 | * @param type Wire protocol number associated with this OFType |
| 144 | * @param clazz The Java class corresponding to this type of OpenFlow |
| 145 | * message |
| 146 | * @param instantiator An Instantiator<OFMessage> implementation that creates an |
| 147 | * instance of the specified OFMessage |
| 148 | */ |
| 149 | OFType(int type, Class<? extends OFMessage> clazz, Instantiable<OFMessage> instantiator) { |
| 150 | this.type = (byte) type; |
| 151 | this.clazz = clazz; |
| 152 | this.instantiable = instantiator; |
| 153 | try { |
| 154 | this.constructor = clazz.getConstructor(new Class[]{}); |
| 155 | } catch (Exception e) { |
| 156 | throw new RuntimeException( |
| 157 | "Failure getting constructor for class: " + clazz, e); |
| 158 | } |
| 159 | OFType.addMapping(this.type, this); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Adds a mapping from type value to OFType enum |
| 164 | * |
| 165 | * @param i OpenFlow wire protocol type |
| 166 | * @param t type |
| 167 | */ |
| 168 | static public void addMapping(byte i, OFType t) { |
| 169 | if (mapping == null) |
| 170 | mapping = new OFType[32]; |
| 171 | OFType.mapping[i] = t; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Remove a mapping from type value to OFType enum |
| 176 | * |
| 177 | * @param i OpenFlow wire protocol type |
| 178 | */ |
| 179 | static public void removeMapping(byte i) { |
| 180 | OFType.mapping[i] = null; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Given a wire protocol OpenFlow type number, return the OFType associated |
| 185 | * with it |
| 186 | * |
| 187 | * @param i wire protocol number |
| 188 | * @return OFType enum type |
| 189 | */ |
| 190 | |
| 191 | static public OFType valueOf(Byte i) { |
| 192 | return OFType.mapping[i]; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @return Returns the wire protocol value corresponding to this OFType |
| 197 | */ |
| 198 | public byte getTypeValue() { |
| 199 | return this.type; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return return the OFMessage subclass corresponding to this OFType |
| 204 | */ |
| 205 | public Class<? extends OFMessage> toClass() { |
| 206 | return clazz; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the no-argument Constructor of the implementation class for |
| 211 | * this OFType |
| 212 | * @return the constructor |
| 213 | */ |
| 214 | public Constructor<? extends OFMessage> getConstructor() { |
| 215 | return constructor; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns a new instance of the OFMessage represented by this OFType |
| 220 | * @return the new object |
| 221 | */ |
| 222 | public OFMessage newInstance() { |
| 223 | return instantiable.instantiate(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return the instantiable |
| 228 | */ |
| 229 | public Instantiable<OFMessage> getInstantiable() { |
| 230 | return instantiable; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @param instantiable the instantiable to set |
| 235 | */ |
| 236 | public void setInstantiable(Instantiable<OFMessage> instantiable) { |
| 237 | this.instantiable = instantiable; |
| 238 | } |
| 239 | } |