2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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
18 package org.openflow.protocol;
20 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.Charset;
22 import java.util.Arrays;
25 import org.jboss.netty.buffer.ChannelBuffer;
28 * Represents ofp_phy_port
29 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 25, 2010
31 public class OFPhysicalPort {
32 public static int MINIMUM_LENGTH = 48;
33 public static int OFP_ETH_ALEN = 6;
35 public enum OFPortConfig {
36 OFPPC_PORT_DOWN (1 << 0) {
37 public String toString() {
38 return "port-down (0x1)";
41 OFPPC_NO_STP (1 << 1) {
42 public String toString() {
43 return "no-stp (0x2)";
46 OFPPC_NO_RECV (1 << 2) {
47 public String toString() {
48 return "no-recv (0x4)";
51 OFPPC_NO_RECV_STP (1 << 3) {
52 public String toString() {
53 return "no-recv-stp (0x8)";
56 OFPPC_NO_FLOOD (1 << 4) {
57 public String toString() {
58 return "no-flood (0x10)";
61 OFPPC_NO_FWD (1 << 5) {
62 public String toString() {
63 return "no-fwd (0x20)";
66 OFPPC_NO_PACKET_IN (1 << 6) {
67 public String toString() {
68 return "no-pkt-in (0x40)";
74 private OFPortConfig(int value) {
81 public int getValue() {
86 public enum OFPortState {
87 OFPPS_LINK_DOWN (1 << 0) {
88 public String toString() {
89 return "link-down (0x1)";
92 OFPPS_STP_LISTEN (0 << 8) {
93 public String toString() {
94 return "listen (0x0)";
97 OFPPS_STP_LEARN (1 << 8) {
98 public String toString() {
99 return "learn-no-relay (0x100)";
102 OFPPS_STP_FORWARD (2 << 8) {
103 public String toString() {
104 return "forward (0x200)";
107 OFPPS_STP_BLOCK (3 << 8) {
108 public String toString() {
109 return "block-broadcast (0x300)";
112 OFPPS_STP_MASK (3 << 8) {
113 public String toString() {
114 return "block-broadcast (0x300)";
120 private OFPortState(int value) {
127 public int getValue() {
132 public enum OFPortFeatures {
133 OFPPF_10MB_HD (1 << 0) {
134 public String toString() {
135 return "10mb-hd (0x1)";
138 OFPPF_10MB_FD (1 << 1) {
139 public String toString() {
140 return "10mb-fd (0x2)";
143 OFPPF_100MB_HD (1 << 2) {
144 public String toString() {
145 return "100mb-hd (0x4)";
148 OFPPF_100MB_FD (1 << 3) {
149 public String toString() {
150 return "100mb-fd (0x8)";
153 OFPPF_1GB_HD (1 << 4) {
154 public String toString() {
155 return "1gb-hd (0x10)";
158 OFPPF_1GB_FD (1 << 5) {
159 public String toString() {
160 return "1gb-fd (0x20)";
163 OFPPF_10GB_FD (1 << 6) {
164 public String toString() {
165 return "10gb-fd (0x40)";
168 OFPPF_COPPER (1 << 7) {
169 public String toString() {
170 return "copper (0x80)";
173 OFPPF_FIBER (1 << 8) {
174 public String toString() {
175 return "fiber (0x100)";
178 OFPPF_AUTONEG (1 << 9) {
179 public String toString() {
180 return "autoneg (0x200)";
183 OFPPF_PAUSE (1 << 10) {
184 public String toString() {
185 return "pause (0x400)";
188 OFPPF_PAUSE_ASYM (1 << 11) {
189 public String toString() {
190 return "pause-asym (0x800)";
196 private OFPortFeatures(int value) {
203 public int getValue() {
208 protected short portNumber;
209 protected byte[] hardwareAddress;
210 protected String name;
211 protected int config;
213 protected int currentFeatures;
214 protected int advertisedFeatures;
215 protected int supportedFeatures;
216 protected int peerFeatures;
219 * @return the portNumber
221 public short getPortNumber() {
226 * @param portNumber the portNumber to set
228 public void setPortNumber(short portNumber) {
229 this.portNumber = portNumber;
233 * @return the hardwareAddress
235 public byte[] getHardwareAddress() {
236 return hardwareAddress;
240 * @param hardwareAddress the hardwareAddress to set
242 public void setHardwareAddress(byte[] hardwareAddress) {
243 if (hardwareAddress.length != OFP_ETH_ALEN)
244 throw new RuntimeException("Hardware address must have length "
246 this.hardwareAddress = hardwareAddress;
252 public String getName() {
257 * @param name the name to set
259 public void setName(String name) {
266 public int getConfig() {
271 * @param config the config to set
273 public void setConfig(int config) {
274 this.config = config;
280 public int getState() {
285 * @param state the state to set
287 public void setState(int state) {
292 * @return the currentFeatures
294 public int getCurrentFeatures() {
295 return currentFeatures;
299 * @param currentFeatures the currentFeatures to set
301 public void setCurrentFeatures(int currentFeatures) {
302 this.currentFeatures = currentFeatures;
306 * @return the advertisedFeatures
308 public int getAdvertisedFeatures() {
309 return advertisedFeatures;
313 * @param advertisedFeatures the advertisedFeatures to set
315 public void setAdvertisedFeatures(int advertisedFeatures) {
316 this.advertisedFeatures = advertisedFeatures;
320 * @return the supportedFeatures
322 public int getSupportedFeatures() {
323 return supportedFeatures;
327 * @param supportedFeatures the supportedFeatures to set
329 public void setSupportedFeatures(int supportedFeatures) {
330 this.supportedFeatures = supportedFeatures;
334 * @return the peerFeatures
336 public int getPeerFeatures() {
341 * @param peerFeatures the peerFeatures to set
343 public void setPeerFeatures(int peerFeatures) {
344 this.peerFeatures = peerFeatures;
348 * Read this message off the wire from the specified ByteBuffer
351 public void readFrom(ChannelBuffer data) {
352 this.portNumber = data.readShort();
353 if (this.hardwareAddress == null)
354 this.hardwareAddress = new byte[OFP_ETH_ALEN];
355 data.readBytes(this.hardwareAddress);
356 byte[] name = new byte[16];
357 data.readBytes(name);
358 // find the first index of 0
360 for (byte b : name) {
365 this.name = new String(Arrays.copyOf(name, index),
366 Charset.forName("ascii"));
367 this.config = data.readInt();
368 this.state = data.readInt();
369 this.currentFeatures = data.readInt();
370 this.advertisedFeatures = data.readInt();
371 this.supportedFeatures = data.readInt();
372 this.peerFeatures = data.readInt();
376 * Write this message's binary format to the specified ByteBuffer
379 public void writeTo(ChannelBuffer data) {
380 data.writeShort(this.portNumber);
381 data.writeBytes(hardwareAddress);
383 byte[] name = this.name.getBytes("ASCII");
384 if (name.length < 16) {
385 data.writeBytes(name);
386 for (int i = name.length; i < 16; ++i) {
387 data.writeByte((byte) 0);
390 data.writeBytes(name, 0, 15);
391 data.writeByte((byte) 0);
393 } catch (UnsupportedEncodingException e) {
394 throw new RuntimeException(e);
396 data.writeInt(this.config);
397 data.writeInt(this.state);
398 data.writeInt(this.currentFeatures);
399 data.writeInt(this.advertisedFeatures);
400 data.writeInt(this.supportedFeatures);
401 data.writeInt(this.peerFeatures);
405 public int hashCode() {
406 final int prime = 307;
408 result = prime * result + advertisedFeatures;
409 result = prime * result + config;
410 result = prime * result + currentFeatures;
411 result = prime * result + Arrays.hashCode(hardwareAddress);
412 result = prime * result + ((name == null) ? 0 : name.hashCode());
413 result = prime * result + peerFeatures;
414 result = prime * result + portNumber;
415 result = prime * result + state;
416 result = prime * result + supportedFeatures;
421 public boolean equals(Object obj) {
428 if (!(obj instanceof OFPhysicalPort)) {
431 OFPhysicalPort other = (OFPhysicalPort) obj;
432 if (advertisedFeatures != other.advertisedFeatures) {
435 if (config != other.config) {
438 if (currentFeatures != other.currentFeatures) {
441 if (!Arrays.equals(hardwareAddress, other.hardwareAddress)) {
445 if (other.name != null) {
448 } else if (!name.equals(other.name)) {
451 if (peerFeatures != other.peerFeatures) {
454 if (portNumber != other.portNumber) {
457 if (state != other.state) {
460 if (supportedFeatures != other.supportedFeatures) {