1 package org.openflow.protocol;
3 import java.io.Serializable;
4 import java.io.UnsupportedEncodingException;
5 import java.nio.ByteBuffer;
6 import java.nio.charset.Charset;
7 import java.util.Arrays;
8 import java.util.HashMap;
13 * Represents ofp_phy_port
14 * @author David Erickson (daviderickson@cs.stanford.edu) - Mar 25, 2010
16 public class OFPhysicalPort implements Cloneable, Serializable {
17 public static int MINIMUM_LENGTH = 48;
18 public static int OFP_ETH_ALEN = 6;
20 public enum OFPortConfig {
21 OFPPC_PORT_DOWN (1 << 0),
22 OFPPC_NO_STP (1 << 1),
23 OFPPC_NO_RECV (1 << 2),
24 OFPPC_NO_RECV_STP (1 << 3),
25 OFPPC_NO_FLOOD (1 << 4),
26 OFPPC_NO_FWD (1 << 5),
27 OFPPC_NO_PACKET_IN (1 << 6);
31 private OFPortConfig(int value) {
38 public int getValue() {
43 public enum OFPortState {
44 OFPPS_LINK_DOWN (1 << 0),
45 OFPPS_STP_LISTEN (0 << 8),
46 OFPPS_STP_LEARN (1 << 8),
47 OFPPS_STP_FORWARD (2 << 8),
48 OFPPS_STP_BLOCK (3 << 8),
49 OFPPS_STP_MASK (3 << 8);
53 private OFPortState(int value) {
60 public int getValue() {
65 public enum OFPortFeatures {
66 OFPPF_10MB_HD (1 << 0),
67 OFPPF_10MB_FD (1 << 1),
68 OFPPF_100MB_HD (1 << 2),
69 OFPPF_100MB_FD (1 << 3),
70 OFPPF_1GB_HD (1 << 4),
71 OFPPF_1GB_FD (1 << 5),
72 OFPPF_10GB_FD (1 << 6),
73 OFPPF_COPPER (1 << 7),
75 OFPPF_AUTONEG (1 << 9),
76 OFPPF_PAUSE (1 << 10),
77 OFPPF_PAUSE_ASYM (1 << 11);
81 private OFPortFeatures(int value) {
88 public int getValue() {
93 protected short portNumber;
94 protected byte[] hardwareAddress;
95 protected String name;
98 protected int currentFeatures;
99 protected int advertisedFeatures;
100 protected int supportedFeatures;
101 protected int peerFeatures;
104 * @return the portNumber
106 public short getPortNumber() {
111 * @param portNumber the portNumber to set
113 public void setPortNumber(short portNumber) {
114 this.portNumber = portNumber;
118 * @return the hardwareAddress
120 public byte[] getHardwareAddress() {
121 return hardwareAddress;
125 * @param hardwareAddress the hardwareAddress to set
127 public void setHardwareAddress(byte[] hardwareAddress) {
128 if (hardwareAddress.length != OFP_ETH_ALEN)
129 throw new RuntimeException("Hardware address must have length "
131 this.hardwareAddress = hardwareAddress;
137 public String getName() {
142 * @param name the name to set
144 public void setName(String name) {
151 public int getConfig() {
156 * @param config the config to set
158 public void setConfig(int config) {
159 this.config = config;
165 public int getState() {
170 * @param state the state to set
172 public void setState(int state) {
177 * @return the currentFeatures
179 public int getCurrentFeatures() {
180 return currentFeatures;
184 * @param currentFeatures the currentFeatures to set
186 public void setCurrentFeatures(int currentFeatures) {
187 this.currentFeatures = currentFeatures;
191 * @return the advertisedFeatures
193 public int getAdvertisedFeatures() {
194 return advertisedFeatures;
198 * @param advertisedFeatures the advertisedFeatures to set
200 public void setAdvertisedFeatures(int advertisedFeatures) {
201 this.advertisedFeatures = advertisedFeatures;
205 * @return the supportedFeatures
207 public int getSupportedFeatures() {
208 return supportedFeatures;
212 * @param supportedFeatures the supportedFeatures to set
214 public void setSupportedFeatures(int supportedFeatures) {
215 this.supportedFeatures = supportedFeatures;
219 * @return the peerFeatures
221 public int getPeerFeatures() {
226 * @param peerFeatures the peerFeatures to set
228 public void setPeerFeatures(int peerFeatures) {
229 this.peerFeatures = peerFeatures;
233 * Read this message off the wire from the specified ByteBuffer
236 public void readFrom(ByteBuffer data) {
237 this.portNumber = data.getShort();
238 if (this.hardwareAddress == null)
239 this.hardwareAddress = new byte[OFP_ETH_ALEN];
240 data.get(this.hardwareAddress);
241 byte[] name = new byte[16];
243 // find the first index of 0
245 for (byte b : name) {
250 this.name = new String(Arrays.copyOf(name, index),
251 Charset.forName("ascii"));
252 this.config = data.getInt();
253 this.state = data.getInt();
254 this.currentFeatures = data.getInt();
255 this.advertisedFeatures = data.getInt();
256 this.supportedFeatures = data.getInt();
257 this.peerFeatures = data.getInt();
261 * Write this message's binary format to the specified ByteBuffer
264 public void writeTo(ByteBuffer data) {
265 data.putShort(this.portNumber);
266 data.put(hardwareAddress);
268 byte[] name = this.name.getBytes("ASCII");
269 if (name.length < 16) {
271 for (int i = name.length; i < 16; ++i) {
275 data.put(name, 0, 15);
278 } catch (UnsupportedEncodingException e) {
279 throw new RuntimeException(e);
281 data.putInt(this.config);
282 data.putInt(this.state);
283 data.putInt(this.currentFeatures);
284 data.putInt(this.advertisedFeatures);
285 data.putInt(this.supportedFeatures);
286 data.putInt(this.peerFeatures);
290 public int hashCode() {
291 final int prime = 307;
293 result = prime * result + advertisedFeatures;
294 result = prime * result + config;
295 result = prime * result + currentFeatures;
296 result = prime * result + Arrays.hashCode(hardwareAddress);
297 result = prime * result + ((name == null) ? 0 : name.hashCode());
298 result = prime * result + peerFeatures;
299 result = prime * result + portNumber;
300 result = prime * result + state;
301 result = prime * result + supportedFeatures;
306 public boolean equals(Object obj) {
313 if (!(obj instanceof OFPhysicalPort)) {
316 OFPhysicalPort other = (OFPhysicalPort) obj;
317 if (advertisedFeatures != other.advertisedFeatures) {
320 if (config != other.config) {
323 if (currentFeatures != other.currentFeatures) {
326 if (!Arrays.equals(hardwareAddress, other.hardwareAddress)) {
330 if (other.name != null) {
333 } else if (!name.equals(other.name)) {
336 if (peerFeatures != other.peerFeatures) {
339 if (portNumber != other.portNumber) {
342 if (state != other.state) {
345 if (supportedFeatures != other.supportedFeatures) {
351 public OFPhysicalPort cloneOFPhysicalPort() {
355 p = (OFPhysicalPort) this.clone();
358 catch (CloneNotSupportedException e)
360 throw new AssertionError();