blob: b3ed98556727a09da47f61f389477fb7831926b8 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08003import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08004
5/**
6 * The class representing a Switch-Port.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -07007 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08008 */
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -07009public final class SwitchPort {
10 private final Dpid dpid; // The DPID of the switch
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070011 private final PortNumber port; // The port of the switch
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012
Jonathan Hart3edb1752013-11-14 13:28:17 -080013 /**
14 * Default constructor.
15 */
16 public SwitchPort() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070017 this.dpid = null;
18 this.port = null;
Jonathan Hart3edb1752013-11-14 13:28:17 -080019 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080020
Jonathan Hart3edb1752013-11-14 13:28:17 -080021 /**
22 * Constructor for a given DPID and a port.
23 *
24 * @param dpid the DPID to use.
25 * @param port the port to use.
26 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070027 public SwitchPort(Dpid dpid, PortNumber port) {
Ray Milkey269ffb92014-04-03 14:43:30 -070028 this.dpid = dpid;
29 this.port = port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080030 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080031
Jonathan Hart3edb1752013-11-14 13:28:17 -080032 /**
Jonathan Harte6e63732014-04-16 14:29:49 -070033 * Constructor for the specified primitive values of a DPID and port.
34 *
35 * @param dpid the long DPID to use
36 * @param port the short port number to use
37 */
38 public SwitchPort(long dpid, short port) {
39 this.dpid = new Dpid(dpid);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070040 this.port = new PortNumber(port);
Jonathan Harte6e63732014-04-16 14:29:49 -070041 }
42
43 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080044 * Get the DPID value of the Switch-Port.
45 *
46 * @return the DPID value of the Switch-Port.
47 */
48 @JsonProperty("dpid")
Ray Milkey269ffb92014-04-03 14:43:30 -070049 public Dpid dpid() {
50 return dpid;
51 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080052
Jonathan Hart3edb1752013-11-14 13:28:17 -080053 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080054 * Get the port value of the Switch-Port.
55 *
56 * @return the port value of the Switch-Port.
57 */
58 @JsonProperty("port")
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070059 public PortNumber port() {
Ray Milkey269ffb92014-04-03 14:43:30 -070060 return port;
61 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080062
Jonathan Hart3edb1752013-11-14 13:28:17 -080063 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080064 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070065 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080066 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070067 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -080068 *
69 * @return the Switch-Port value as a string.
70 */
71 @Override
72 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070073 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080074 }
Ray Milkey269ffb92014-04-03 14:43:30 -070075
Jonathan Hart3edb1752013-11-14 13:28:17 -080076 @Override
77 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 if (!(other instanceof SwitchPort)) {
79 return false;
80 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 return (dpid.equals(otherSwitchPort.dpid) &&
85 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -080086 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080087
Jonathan Hart3edb1752013-11-14 13:28:17 -080088 @Override
89 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -070090 int hash = 17;
91 hash += 31 * hash + dpid.hashCode();
92 hash += 31 * hash + port.hashCode();
93 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -080094 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080095}