blob: b9e1964209c5e963757d8e3634c66ee1df8ae0a3 [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 HIGUCHI5c8cbeb2014-06-27 11:13:48 -07003import net.onrc.onos.core.util.serializers.SwitchPortSerializer;
4
5import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08006
7/**
8 * The class representing a Switch-Port.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -07009 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080010 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070011@JsonSerialize(using = SwitchPortSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070012public final class SwitchPort {
13 private final Dpid dpid; // The DPID of the switch
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070014 private final PortNumber port; // The port of the switch
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015
Jonathan Hart3edb1752013-11-14 13:28:17 -080016 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070017 * Default constructor for Serializer to use.
Jonathan Hart3edb1752013-11-14 13:28:17 -080018 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070019 protected SwitchPort() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070020 this.dpid = null;
21 this.port = null;
Jonathan Hart3edb1752013-11-14 13:28:17 -080022 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080023
Jonathan Hart3edb1752013-11-14 13:28:17 -080024 /**
25 * Constructor for a given DPID and a port.
26 *
27 * @param dpid the DPID to use.
28 * @param port the port to use.
29 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070030 public SwitchPort(Dpid dpid, PortNumber port) {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 this.dpid = dpid;
32 this.port = port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080033 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080034
Jonathan Hart3edb1752013-11-14 13:28:17 -080035 /**
Jonathan Harte6e63732014-04-16 14:29:49 -070036 * Constructor for the specified primitive values of a DPID and port.
37 *
38 * @param dpid the long DPID to use
39 * @param port the short port number to use
40 */
41 public SwitchPort(long dpid, short port) {
42 this.dpid = new Dpid(dpid);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070043 this.port = new PortNumber(port);
Jonathan Harte6e63732014-04-16 14:29:49 -070044 }
45
46 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070047 * Constructor for the specified primitive values of a DPID and port.
48 *
49 * @param dpid the DPID to use
50 * @param port the port number to use
51 */
52 public SwitchPort(Long dpid, Long port) {
53 this.dpid = new Dpid(dpid);
54 this.port = new PortNumber(port.shortValue());
55 }
56
57 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080058 * Get the DPID value of the Switch-Port.
59 *
60 * @return the DPID value of the Switch-Port.
61 */
Ray Milkey269ffb92014-04-03 14:43:30 -070062 public Dpid dpid() {
63 return dpid;
64 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080065
Jonathan Hart3edb1752013-11-14 13:28:17 -080066 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070067 * Get the DPID value of the Switch-Port.
68 *
69 * @return the DPID value of the Switch-Port.
70 */
71 public Dpid getDpid() {
72 return dpid;
73 }
74
75 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080076 * Get the port value of the Switch-Port.
77 *
78 * @return the port value of the Switch-Port.
79 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070080 public PortNumber port() {
Ray Milkey269ffb92014-04-03 14:43:30 -070081 return port;
82 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080083
Jonathan Hart3edb1752013-11-14 13:28:17 -080084 /**
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070085 * Get the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070086 *
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070087 * @return the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070088 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070089 public PortNumber getPortNumber() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070090 return port;
91 }
92
93 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080094 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070095 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080096 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070097 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -080098 *
99 * @return the Switch-Port value as a string.
100 */
101 @Override
102 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800104 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700105
Jonathan Hart3edb1752013-11-14 13:28:17 -0800106 @Override
107 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 if (!(other instanceof SwitchPort)) {
109 return false;
110 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800111
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -0800113
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 return (dpid.equals(otherSwitchPort.dpid) &&
115 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -0800116 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800117
Jonathan Hart3edb1752013-11-14 13:28:17 -0800118 @Override
119 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 int hash = 17;
121 hash += 31 * hash + dpid.hashCode();
122 hash += 31 * hash + port.hashCode();
123 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800124 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800125}