blob: cbe4bcb2bea36c6d0d36cd85d48ceb57e998fb72 [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
Yuta HIGUCHIa16a8232014-07-03 19:45:40 -07005import org.apache.commons.lang.Validate;
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07006import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08007
8/**
9 * The class representing a Switch-Port.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070010 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080011 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070012@JsonSerialize(using = SwitchPortSerializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070013public final class SwitchPort {
14 private final Dpid dpid; // The DPID of the switch
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070015 private final PortNumber port; // The port of the switch
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080016
Jonathan Hart3edb1752013-11-14 13:28:17 -080017 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070018 * Default constructor for Serializer to use.
Jonathan Hart3edb1752013-11-14 13:28:17 -080019 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070020 protected SwitchPort() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070021 this.dpid = null;
22 this.port = null;
Jonathan Hart3edb1752013-11-14 13:28:17 -080023 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080024
Jonathan Hart3edb1752013-11-14 13:28:17 -080025 /**
26 * Constructor for a given DPID and a port.
27 *
28 * @param dpid the DPID to use.
29 * @param port the port to use.
30 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070031 public SwitchPort(Dpid dpid, PortNumber port) {
Yuta HIGUCHIa16a8232014-07-03 19:45:40 -070032 Validate.notNull(dpid);
33 Validate.notNull(port);
Ray Milkey269ffb92014-04-03 14:43:30 -070034 this.dpid = dpid;
35 this.port = port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080036 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080037
Jonathan Hart3edb1752013-11-14 13:28:17 -080038 /**
Jonathan Harte6e63732014-04-16 14:29:49 -070039 * Constructor for the specified primitive values of a DPID and port.
40 *
41 * @param dpid the long DPID to use
42 * @param port the short port number to use
43 */
44 public SwitchPort(long dpid, short port) {
45 this.dpid = new Dpid(dpid);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070046 this.port = new PortNumber(port);
Jonathan Harte6e63732014-04-16 14:29:49 -070047 }
48
49 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070050 * Constructor for the specified primitive values of a DPID and port.
51 *
52 * @param dpid the DPID to use
53 * @param port the port number to use
54 */
55 public SwitchPort(Long dpid, Long port) {
56 this.dpid = new Dpid(dpid);
57 this.port = new PortNumber(port.shortValue());
58 }
59
60 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080061 * Get the DPID value of the Switch-Port.
62 *
63 * @return the DPID value of the Switch-Port.
64 */
Ray Milkey269ffb92014-04-03 14:43:30 -070065 public Dpid dpid() {
66 return dpid;
67 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080068
Jonathan Hart3edb1752013-11-14 13:28:17 -080069 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070070 * Get the DPID value of the Switch-Port.
71 *
72 * @return the DPID value of the Switch-Port.
73 */
74 public Dpid getDpid() {
75 return dpid;
76 }
77
78 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080079 * Get the port value of the Switch-Port.
80 *
81 * @return the port value of the Switch-Port.
82 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070083 public PortNumber port() {
Ray Milkey269ffb92014-04-03 14:43:30 -070084 return port;
85 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080086
Jonathan Hart3edb1752013-11-14 13:28:17 -080087 /**
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070088 * Get the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070089 *
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070090 * @return the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070091 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070092 public PortNumber getPortNumber() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070093 return port;
94 }
95
96 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080097 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070098 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080099 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -0800101 *
102 * @return the Switch-Port value as a string.
103 */
104 @Override
105 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800107 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700108
Jonathan Hart3edb1752013-11-14 13:28:17 -0800109 @Override
110 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 if (!(other instanceof SwitchPort)) {
112 return false;
113 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800114
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 return (dpid.equals(otherSwitchPort.dpid) &&
118 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -0800119 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800120
Jonathan Hart3edb1752013-11-14 13:28:17 -0800121 @Override
122 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 int hash = 17;
124 hash += 31 * hash + dpid.hashCode();
125 hash += 31 * hash + port.hashCode();
126 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800127 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800128}