blob: da60cbfefcf34a435761fa1a266902dfe7af2749 [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 HIGUCHI6d7ee9f2014-08-22 09:56:50 -07005import java.util.Objects;
6
7import javax.annotation.concurrent.Immutable;
8
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -07009import net.onrc.onos.core.util.serializers.SwitchPortSerializer;
10
11import org.codehaus.jackson.map.annotate.JsonSerialize;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012
13/**
14 * The class representing a Switch-Port.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070015 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080016 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070017@JsonSerialize(using = SwitchPortSerializer.class)
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -070018@Immutable
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070019public final class SwitchPort {
Pavlin Radoslavov3d322f42014-08-18 14:58:55 -070020 private final Dpid dpid; // The DPID of the switch
21 private final PortNumber port; // The port number on the switch
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080022
Jonathan Hart3edb1752013-11-14 13:28:17 -080023 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070024 * Default constructor for Serializer to use.
Jonathan Hart3edb1752013-11-14 13:28:17 -080025 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070026 protected SwitchPort() {
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070027 this.dpid = null;
28 this.port = null;
Jonathan Hart3edb1752013-11-14 13:28:17 -080029 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080030
Jonathan Hart3edb1752013-11-14 13:28:17 -080031 /**
32 * Constructor for a given DPID and a port.
33 *
34 * @param dpid the DPID to use.
35 * @param port the port to use.
36 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070037 public SwitchPort(Dpid dpid, PortNumber port) {
Yuta HIGUCHIa860dff2014-08-20 21:16:30 -070038 this.dpid = checkNotNull(dpid);
39 this.port = checkNotNull(port);
Jonathan Hart3edb1752013-11-14 13:28:17 -080040 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080041
Jonathan Hart3edb1752013-11-14 13:28:17 -080042 /**
Jonathan Harte6e63732014-04-16 14:29:49 -070043 * Constructor for the specified primitive values of a DPID and port.
44 *
45 * @param dpid the long DPID to use
46 * @param port the short port number to use
47 */
48 public SwitchPort(long dpid, short port) {
49 this.dpid = new Dpid(dpid);
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070050 this.port = new PortNumber(port);
Jonathan Harte6e63732014-04-16 14:29:49 -070051 }
52
53 /**
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070054 * Constructor for the specified primitive values of a DPID and port.
55 *
56 * @param dpid the DPID to use
57 * @param port the port number to use
58 */
59 public SwitchPort(Long dpid, Long port) {
60 this.dpid = new Dpid(dpid);
61 this.port = new PortNumber(port.shortValue());
62 }
63
64 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080065 * Get the DPID value of the Switch-Port.
66 *
67 * @return the DPID value of the Switch-Port.
68 */
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070069 public Dpid getDpid() {
70 return dpid;
71 }
72
73 /**
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070074 * Get the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070075 *
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070076 * @return the port number of the Switch-Port.
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070077 */
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070078 public PortNumber getPortNumber() {
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -070079 return port;
80 }
81
82 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080083 * Convert the Switch-Port value to a string.
Ray Milkey269ffb92014-04-03 14:43:30 -070084 * <p/>
Jonathan Hart3edb1752013-11-14 13:28:17 -080085 * The string has the following form:
Ray Milkey269ffb92014-04-03 14:43:30 -070086 * 01:02:03:04:05:06:07:08/1234
Jonathan Hart3edb1752013-11-14 13:28:17 -080087 *
88 * @return the Switch-Port value as a string.
89 */
90 @Override
91 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 return this.dpid.toString() + "/" + this.port;
Jonathan Hart3edb1752013-11-14 13:28:17 -080093 }
Ray Milkey269ffb92014-04-03 14:43:30 -070094
Jonathan Hart3edb1752013-11-14 13:28:17 -080095 @Override
96 public boolean equals(Object other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070097 if (!(other instanceof SwitchPort)) {
98 return false;
99 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 SwitchPort otherSwitchPort = (SwitchPort) other;
Jonathan Hartf0a81792013-11-14 11:36:06 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 return (dpid.equals(otherSwitchPort.dpid) &&
104 port.equals(otherSwitchPort.port));
Jonathan Hart3edb1752013-11-14 13:28:17 -0800105 }
Jonathan Hartf0a81792013-11-14 11:36:06 -0800106
Jonathan Hart3edb1752013-11-14 13:28:17 -0800107 @Override
108 public int hashCode() {
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -0700109 return Objects.hash(dpid, port);
Jonathan Hart3edb1752013-11-14 13:28:17 -0800110 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800111}