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