blob: e296b7b5d690365a036029c3c85160a13e1e13f1 [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 {
Pavlin Radoslavov3d322f42014-08-18 14:58:55 -070014 private final Dpid dpid; // The DPID of the switch
15 private final PortNumber port; // The port number on 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 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070065 public Dpid getDpid() {
66 return dpid;
67 }
68
69 /**
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070070 * Get the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070071 *
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070072 * @return the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070073 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070074 public PortNumber getPortNumber() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070075 return port;
76 }
77
78 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080079 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070080 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080081 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070082 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -080083 *
84 * @return the Switch-Port value as a string.
85 */
86 @Override
87 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080089 }
Ray Milkey269ffb92014-04-03 14:43:30 -070090
Jonathan Hart3edb1752013-11-14 13:28:17 -080091 @Override
92 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070093 if (!(other instanceof SwitchPort)) {
94 return false;
95 }
Jonathan Hartf0a81792013-11-14 11:36:06 -080096
Ray Milkey269ffb92014-04-03 14:43:30 -070097 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 return (dpid.equals(otherSwitchPort.dpid) &&
100 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -0800101 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800102
Jonathan Hart3edb1752013-11-14 13:28:17 -0800103 @Override
104 public int hashCode() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 int hash = 17;
106 hash += 31 * hash + dpid.hashCode();
107 hash += 31 * hash + port.hashCode();
108 return hash;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800109 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800110}