Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | a860dff | 2014-08-20 21:16:30 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 5 | import java.util.Objects; |
| 6 | |
| 7 | import javax.annotation.concurrent.Immutable; |
| 8 | |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.serializers.SwitchPortSerializer; |
| 10 | |
| 11 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * The class representing a Switch-Port. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 15 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 16 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 17 | @JsonSerialize(using = SwitchPortSerializer.class) |
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 18 | @Immutable |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 19 | public final class SwitchPort { |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 20 | private final Dpid dpid; // The DPID of the switch |
| 21 | private final PortNumber port; // The port number on the switch |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 22 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 23 | /** |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 24 | * Default constructor for Serializer to use. |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 25 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 26 | protected SwitchPort() { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 27 | this.dpid = null; |
| 28 | this.port = null; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 29 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 30 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 31 | /** |
| 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 HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 37 | public SwitchPort(Dpid dpid, PortNumber port) { |
Yuta HIGUCHI | a860dff | 2014-08-20 21:16:30 -0700 | [diff] [blame] | 38 | this.dpid = checkNotNull(dpid); |
| 39 | this.port = checkNotNull(port); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 40 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 41 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 42 | /** |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 43 | * 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 HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 50 | this.port = new PortNumber(port); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 54 | * 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 Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 65 | * Get the DPID value of the Switch-Port. |
| 66 | * |
| 67 | * @return the DPID value of the Switch-Port. |
| 68 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 69 | public Dpid getDpid() { |
| 70 | return dpid; |
| 71 | } |
| 72 | |
| 73 | /** |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 74 | * Get the port number of the Switch-Port. |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 75 | * |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 76 | * @return the port number of the Switch-Port. |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 77 | */ |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 78 | public PortNumber getPortNumber() { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 79 | return port; |
| 80 | } |
| 81 | |
| 82 | /** |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 83 | * Convert the Switch-Port value to a string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | * <p/> |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 85 | * The string has the following form: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | * 01:02:03:04:05:06:07:08/1234 |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 87 | * |
| 88 | * @return the Switch-Port value as a string. |
| 89 | */ |
| 90 | @Override |
| 91 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | return this.dpid.toString() + "/" + this.port; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 93 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public boolean equals(Object other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | if (!(other instanceof SwitchPort)) { |
| 98 | return false; |
| 99 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 100 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | SwitchPort otherSwitchPort = (SwitchPort) other; |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 102 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | return (dpid.equals(otherSwitchPort.dpid) && |
| 104 | port.equals(otherSwitchPort.port)); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 105 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 106 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 107 | @Override |
| 108 | public int hashCode() { |
Yuta HIGUCHI | 6d7ee9f | 2014-08-22 09:56:50 -0700 | [diff] [blame] | 109 | return Objects.hash(dpid, port); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 110 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 111 | } |