blob: 74f0ed4ee5c912c90004066421a8cda9845db089 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Yuta HIGUCHIa860dff2014-08-20 21:16:30 -07003import static com.google.common.base.Preconditions.checkNotNull;
4
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07005import net.onrc.onos.core.util.serializers.SwitchPortSerializer;
6
7import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08008
9/**
10 * The class representing a Switch-Port.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070011 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070013@JsonSerialize(using = SwitchPortSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070014public final class SwitchPort {
Pavlin Radoslavov3d322f42014-08-18 14:58:55 -070015 private final Dpid dpid; // The DPID of the switch
16 private final PortNumber port; // The port number on the switch
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017
Jonathan Hart3edb1752013-11-14 13:28:17 -080018 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070019 * Default constructor for Serializer to use.
Jonathan Hart3edb1752013-11-14 13:28:17 -080020 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070021 protected SwitchPort() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070022 this.dpid = null;
23 this.port = null;
Jonathan Hart3edb1752013-11-14 13:28:17 -080024 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080025
Jonathan Hart3edb1752013-11-14 13:28:17 -080026 /**
27 * Constructor for a given DPID and a port.
28 *
29 * @param dpid the DPID to use.
30 * @param port the port to use.
31 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070032 public SwitchPort(Dpid dpid, PortNumber port) {
Yuta HIGUCHIa860dff2014-08-20 21:16:30 -070033 this.dpid = checkNotNull(dpid);
34 this.port = checkNotNull(port);
Jonathan Hart3edb1752013-11-14 13:28:17 -080035 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080036
Jonathan Hart3edb1752013-11-14 13:28:17 -080037 /**
Jonathan Harte6e63732014-04-16 14:29:49 -070038 * Constructor for the specified primitive values of a DPID and port.
39 *
40 * @param dpid the long DPID to use
41 * @param port the short port number to use
42 */
43 public SwitchPort(long dpid, short port) {
44 this.dpid = new Dpid(dpid);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070045 this.port = new PortNumber(port);
Jonathan Harte6e63732014-04-16 14:29:49 -070046 }
47
48 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070049 * Constructor for the specified primitive values of a DPID and port.
50 *
51 * @param dpid the DPID to use
52 * @param port the port number to use
53 */
54 public SwitchPort(Long dpid, Long port) {
55 this.dpid = new Dpid(dpid);
56 this.port = new PortNumber(port.shortValue());
57 }
58
59 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080060 * Get the DPID value of the Switch-Port.
61 *
62 * @return the DPID value of the Switch-Port.
63 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070064 public Dpid getDpid() {
65 return dpid;
66 }
67
68 /**
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070069 * Get the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070070 *
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070071 * @return the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070072 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070073 public PortNumber getPortNumber() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070074 return port;
75 }
76
77 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080078 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070079 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080080 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070081 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -080082 *
83 * @return the Switch-Port value as a string.
84 */
85 @Override
86 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070087 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080088 }
Ray Milkey269ffb92014-04-03 14:43:30 -070089
Jonathan Hart3edb1752013-11-14 13:28:17 -080090 @Override
91 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 if (!(other instanceof SwitchPort)) {
93 return false;
94 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 return (dpid.equals(otherSwitchPort.dpid) &&
99 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -0800100 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800101
Jonathan Hart3edb1752013-11-14 13:28:17 -0800102 @Override
103 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 int hash = 17;
105 hash += 31 * hash + dpid.hashCode();
106 hash += 31 * hash + port.hashCode();
107 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800108 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800109}