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.io.UnsupportedEncodingException; |
| 21 | import java.nio.charset.Charset; |
| 22 | import java.util.Arrays; |
| 23 | |
| 24 | import net.floodlightcontroller.core.web.serializers.ByteArrayMACSerializer; |
| 25 | import net.floodlightcontroller.core.web.serializers.UShortSerializer; |
| 26 | |
| 27 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 28 | import org.jboss.netty.buffer.ChannelBuffer; |
| 29 | |
| 30 | /** |
| 31 | * Represents ofp_phy_port |
| 32 | * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 25, 2010 |
| 33 | */ |
| 34 | public class OFPhysicalPort { |
| 35 | public static int MINIMUM_LENGTH = 48; |
| 36 | public static int OFP_ETH_ALEN = 6; |
| 37 | |
| 38 | public enum OFPortConfig { |
| 39 | OFPPC_PORT_DOWN (1 << 0) { |
| 40 | public String toString() { |
| 41 | return "port-down (0x1)"; |
| 42 | } |
| 43 | }, |
| 44 | OFPPC_NO_STP (1 << 1) { |
| 45 | public String toString() { |
| 46 | return "no-stp (0x2)"; |
| 47 | } |
| 48 | }, |
| 49 | OFPPC_NO_RECV (1 << 2) { |
| 50 | public String toString() { |
| 51 | return "no-recv (0x4)"; |
| 52 | } |
| 53 | }, |
| 54 | OFPPC_NO_RECV_STP (1 << 3) { |
| 55 | public String toString() { |
| 56 | return "no-recv-stp (0x8)"; |
| 57 | } |
| 58 | }, |
| 59 | OFPPC_NO_FLOOD (1 << 4) { |
| 60 | public String toString() { |
| 61 | return "no-flood (0x10)"; |
| 62 | } |
| 63 | }, |
| 64 | OFPPC_NO_FWD (1 << 5) { |
| 65 | public String toString() { |
| 66 | return "no-fwd (0x20)"; |
| 67 | } |
| 68 | }, |
| 69 | OFPPC_NO_PACKET_IN (1 << 6) { |
| 70 | public String toString() { |
| 71 | return "no-pkt-in (0x40)"; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | protected int value; |
| 76 | |
| 77 | private OFPortConfig(int value) { |
| 78 | this.value = value; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return the value |
| 83 | */ |
| 84 | public int getValue() { |
| 85 | return value; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public enum OFPortState { |
| 90 | OFPPS_LINK_DOWN (1 << 0) { |
| 91 | public String toString() { |
| 92 | return "link-down (0x1)"; |
| 93 | } |
| 94 | }, |
| 95 | OFPPS_STP_LISTEN (0 << 8) { |
| 96 | public String toString() { |
| 97 | return "listen (0x0)"; |
| 98 | } |
| 99 | }, |
| 100 | OFPPS_STP_LEARN (1 << 8) { |
| 101 | public String toString() { |
| 102 | return "learn-no-relay (0x100)"; |
| 103 | } |
| 104 | }, |
| 105 | OFPPS_STP_FORWARD (2 << 8) { |
| 106 | public String toString() { |
| 107 | return "forward (0x200)"; |
| 108 | } |
| 109 | }, |
| 110 | OFPPS_STP_BLOCK (3 << 8) { |
| 111 | public String toString() { |
| 112 | return "block-broadcast (0x300)"; |
| 113 | } |
| 114 | }, |
| 115 | OFPPS_STP_MASK (3 << 8) { |
| 116 | public String toString() { |
| 117 | return "block-broadcast (0x300)"; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | protected int value; |
| 122 | |
| 123 | private OFPortState(int value) { |
| 124 | this.value = value; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return the value |
| 129 | */ |
| 130 | public int getValue() { |
| 131 | return value; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public enum OFPortFeatures { |
| 136 | OFPPF_10MB_HD (1 << 0) { |
| 137 | public String toString() { |
| 138 | return "10mb-hd (0x1)"; |
| 139 | } |
| 140 | }, |
| 141 | OFPPF_10MB_FD (1 << 1) { |
| 142 | public String toString() { |
| 143 | return "10mb-fd (0x2)"; |
| 144 | } |
| 145 | }, |
| 146 | OFPPF_100MB_HD (1 << 2) { |
| 147 | public String toString() { |
| 148 | return "100mb-hd (0x4)"; |
| 149 | } |
| 150 | }, |
| 151 | OFPPF_100MB_FD (1 << 3) { |
| 152 | public String toString() { |
| 153 | return "100mb-fd (0x8)"; |
| 154 | } |
| 155 | }, |
| 156 | OFPPF_1GB_HD (1 << 4) { |
| 157 | public String toString() { |
| 158 | return "1gb-hd (0x10)"; |
| 159 | } |
| 160 | }, |
| 161 | OFPPF_1GB_FD (1 << 5) { |
| 162 | public String toString() { |
| 163 | return "1gb-fd (0x20)"; |
| 164 | } |
| 165 | }, |
| 166 | OFPPF_10GB_FD (1 << 6) { |
| 167 | public String toString() { |
| 168 | return "10gb-fd (0x40)"; |
| 169 | } |
| 170 | }, |
| 171 | OFPPF_COPPER (1 << 7) { |
| 172 | public String toString() { |
| 173 | return "copper (0x80)"; |
| 174 | } |
| 175 | }, |
| 176 | OFPPF_FIBER (1 << 8) { |
| 177 | public String toString() { |
| 178 | return "fiber (0x100)"; |
| 179 | } |
| 180 | }, |
| 181 | OFPPF_AUTONEG (1 << 9) { |
| 182 | public String toString() { |
| 183 | return "autoneg (0x200)"; |
| 184 | } |
| 185 | }, |
| 186 | OFPPF_PAUSE (1 << 10) { |
| 187 | public String toString() { |
| 188 | return "pause (0x400)"; |
| 189 | } |
| 190 | }, |
| 191 | OFPPF_PAUSE_ASYM (1 << 11) { |
| 192 | public String toString() { |
| 193 | return "pause-asym (0x800)"; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | protected int value; |
| 198 | |
| 199 | private OFPortFeatures(int value) { |
| 200 | this.value = value; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * @return the value |
| 205 | */ |
| 206 | public int getValue() { |
| 207 | return value; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | protected short portNumber; |
| 212 | protected byte[] hardwareAddress; |
| 213 | protected String name; |
| 214 | protected int config; |
| 215 | protected int state; |
| 216 | protected int currentFeatures; |
| 217 | protected int advertisedFeatures; |
| 218 | protected int supportedFeatures; |
| 219 | protected int peerFeatures; |
| 220 | |
| 221 | /** |
| 222 | * @return the portNumber |
| 223 | */ |
| 224 | @JsonSerialize(using=UShortSerializer.class) |
| 225 | public short getPortNumber() { |
| 226 | return portNumber; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @param portNumber the portNumber to set |
| 231 | */ |
| 232 | public void setPortNumber(short portNumber) { |
| 233 | this.portNumber = portNumber; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @return the hardwareAddress |
| 238 | */ |
| 239 | @JsonSerialize(using=ByteArrayMACSerializer.class) |
| 240 | public byte[] getHardwareAddress() { |
| 241 | return hardwareAddress; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @param hardwareAddress the hardwareAddress to set |
| 246 | */ |
| 247 | public void setHardwareAddress(byte[] hardwareAddress) { |
| 248 | if (hardwareAddress.length != OFP_ETH_ALEN) |
| 249 | throw new RuntimeException("Hardware address must have length " |
| 250 | + OFP_ETH_ALEN); |
| 251 | this.hardwareAddress = hardwareAddress; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @return the name |
| 256 | */ |
| 257 | public String getName() { |
| 258 | return name; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @param name the name to set |
| 263 | */ |
| 264 | public void setName(String name) { |
| 265 | this.name = name; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @return the config |
| 270 | */ |
| 271 | public int getConfig() { |
| 272 | return config; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @param config the config to set |
| 277 | */ |
| 278 | public void setConfig(int config) { |
| 279 | this.config = config; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @return the state |
| 284 | */ |
| 285 | public int getState() { |
| 286 | return state; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @param state the state to set |
| 291 | */ |
| 292 | public void setState(int state) { |
| 293 | this.state = state; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @return the currentFeatures |
| 298 | */ |
| 299 | public int getCurrentFeatures() { |
| 300 | return currentFeatures; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @param currentFeatures the currentFeatures to set |
| 305 | */ |
| 306 | public void setCurrentFeatures(int currentFeatures) { |
| 307 | this.currentFeatures = currentFeatures; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @return the advertisedFeatures |
| 312 | */ |
| 313 | public int getAdvertisedFeatures() { |
| 314 | return advertisedFeatures; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @param advertisedFeatures the advertisedFeatures to set |
| 319 | */ |
| 320 | public void setAdvertisedFeatures(int advertisedFeatures) { |
| 321 | this.advertisedFeatures = advertisedFeatures; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @return the supportedFeatures |
| 326 | */ |
| 327 | public int getSupportedFeatures() { |
| 328 | return supportedFeatures; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * @param supportedFeatures the supportedFeatures to set |
| 333 | */ |
| 334 | public void setSupportedFeatures(int supportedFeatures) { |
| 335 | this.supportedFeatures = supportedFeatures; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * @return the peerFeatures |
| 340 | */ |
| 341 | public int getPeerFeatures() { |
| 342 | return peerFeatures; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @param peerFeatures the peerFeatures to set |
| 347 | */ |
| 348 | public void setPeerFeatures(int peerFeatures) { |
| 349 | this.peerFeatures = peerFeatures; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Read this message off the wire from the specified ByteBuffer |
| 354 | * @param data |
| 355 | */ |
| 356 | public void readFrom(ChannelBuffer data) { |
| 357 | this.portNumber = data.readShort(); |
| 358 | if (this.hardwareAddress == null) |
| 359 | this.hardwareAddress = new byte[OFP_ETH_ALEN]; |
| 360 | data.readBytes(this.hardwareAddress); |
| 361 | byte[] name = new byte[16]; |
| 362 | data.readBytes(name); |
| 363 | // find the first index of 0 |
| 364 | int index = 0; |
| 365 | for (byte b : name) { |
| 366 | if (0 == b) |
| 367 | break; |
| 368 | ++index; |
| 369 | } |
| 370 | this.name = new String(Arrays.copyOf(name, index), |
| 371 | Charset.forName("ascii")); |
| 372 | this.config = data.readInt(); |
| 373 | this.state = data.readInt(); |
| 374 | this.currentFeatures = data.readInt(); |
| 375 | this.advertisedFeatures = data.readInt(); |
| 376 | this.supportedFeatures = data.readInt(); |
| 377 | this.peerFeatures = data.readInt(); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Write this message's binary format to the specified ByteBuffer |
| 382 | * @param data |
| 383 | */ |
| 384 | public void writeTo(ChannelBuffer data) { |
| 385 | data.writeShort(this.portNumber); |
| 386 | data.writeBytes(hardwareAddress); |
| 387 | try { |
| 388 | byte[] name = this.name.getBytes("ASCII"); |
| 389 | if (name.length < 16) { |
| 390 | data.writeBytes(name); |
| 391 | for (int i = name.length; i < 16; ++i) { |
| 392 | data.writeByte((byte) 0); |
| 393 | } |
| 394 | } else { |
| 395 | data.writeBytes(name, 0, 15); |
| 396 | data.writeByte((byte) 0); |
| 397 | } |
| 398 | } catch (UnsupportedEncodingException e) { |
| 399 | throw new RuntimeException(e); |
| 400 | } |
| 401 | data.writeInt(this.config); |
| 402 | data.writeInt(this.state); |
| 403 | data.writeInt(this.currentFeatures); |
| 404 | data.writeInt(this.advertisedFeatures); |
| 405 | data.writeInt(this.supportedFeatures); |
| 406 | data.writeInt(this.peerFeatures); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public int hashCode() { |
| 411 | final int prime = 307; |
| 412 | int result = 1; |
| 413 | result = prime * result + advertisedFeatures; |
| 414 | result = prime * result + config; |
| 415 | result = prime * result + currentFeatures; |
| 416 | result = prime * result + Arrays.hashCode(hardwareAddress); |
| 417 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 418 | result = prime * result + peerFeatures; |
| 419 | result = prime * result + portNumber; |
| 420 | result = prime * result + state; |
| 421 | result = prime * result + supportedFeatures; |
| 422 | return result; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean equals(Object obj) { |
| 427 | if (this == obj) { |
| 428 | return true; |
| 429 | } |
| 430 | if (obj == null) { |
| 431 | return false; |
| 432 | } |
| 433 | if (!(obj instanceof OFPhysicalPort)) { |
| 434 | return false; |
| 435 | } |
| 436 | OFPhysicalPort other = (OFPhysicalPort) obj; |
| 437 | if (advertisedFeatures != other.advertisedFeatures) { |
| 438 | return false; |
| 439 | } |
| 440 | if (config != other.config) { |
| 441 | return false; |
| 442 | } |
| 443 | if (currentFeatures != other.currentFeatures) { |
| 444 | return false; |
| 445 | } |
| 446 | if (!Arrays.equals(hardwareAddress, other.hardwareAddress)) { |
| 447 | return false; |
| 448 | } |
| 449 | if (name == null) { |
| 450 | if (other.name != null) { |
| 451 | return false; |
| 452 | } |
| 453 | } else if (!name.equals(other.name)) { |
| 454 | return false; |
| 455 | } |
| 456 | if (peerFeatures != other.peerFeatures) { |
| 457 | return false; |
| 458 | } |
| 459 | if (portNumber != other.portNumber) { |
| 460 | return false; |
| 461 | } |
| 462 | if (state != other.state) { |
| 463 | return false; |
| 464 | } |
| 465 | if (supportedFeatures != other.supportedFeatures) { |
| 466 | return false; |
| 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | } |