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 | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.serializers.SwitchPortSerializer; |
| 4 | |
Yuta HIGUCHI | a16a823 | 2014-07-03 19:45:40 -0700 | [diff] [blame] | 5 | import org.apache.commons.lang.Validate; |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 6 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 7 | |
| 8 | /** |
| 9 | * The class representing a Switch-Port. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 10 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 11 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 12 | @JsonSerialize(using = SwitchPortSerializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 13 | public final class SwitchPort { |
| 14 | private final Dpid dpid; // The DPID of the switch |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 15 | private final PortNumber port; // The port of the switch |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 17 | /** |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 18 | * Default constructor for Serializer to use. |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 19 | */ |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 20 | protected SwitchPort() { |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 21 | this.dpid = null; |
| 22 | this.port = null; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 23 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 24 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 25 | /** |
| 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 HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 31 | public SwitchPort(Dpid dpid, PortNumber port) { |
Yuta HIGUCHI | a16a823 | 2014-07-03 19:45:40 -0700 | [diff] [blame] | 32 | Validate.notNull(dpid); |
| 33 | Validate.notNull(port); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | this.dpid = dpid; |
| 35 | this.port = port; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 36 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 37 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 38 | /** |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 39 | * 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 HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 46 | this.port = new PortNumber(port); |
Jonathan Hart | e6e6373 | 2014-04-16 14:29:49 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 50 | * 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 Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 61 | * Get the DPID value of the Switch-Port. |
| 62 | * |
| 63 | * @return the DPID value of the Switch-Port. |
| 64 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | public Dpid dpid() { |
| 66 | return dpid; |
| 67 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 68 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 69 | /** |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 70 | * 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 Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 79 | * Get the port value of the Switch-Port. |
| 80 | * |
| 81 | * @return the port value of the Switch-Port. |
| 82 | */ |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 83 | public PortNumber port() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | return port; |
| 85 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 86 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 87 | /** |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 88 | * Get the port number of the Switch-Port. |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 89 | * |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 90 | * @return the port number of the Switch-Port. |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 91 | */ |
Yuta HIGUCHI | b1e2ab7 | 2014-06-30 11:01:31 -0700 | [diff] [blame] | 92 | public PortNumber getPortNumber() { |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 93 | return port; |
| 94 | } |
| 95 | |
| 96 | /** |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 97 | * Convert the Switch-Port value to a string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | * <p/> |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 99 | * The string has the following form: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | * 01:02:03:04:05:06:07:08/1234 |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 101 | * |
| 102 | * @return the Switch-Port value as a string. |
| 103 | */ |
| 104 | @Override |
| 105 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | return this.dpid.toString() + "/" + this.port; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 107 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 109 | @Override |
| 110 | public boolean equals(Object other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | if (!(other instanceof SwitchPort)) { |
| 112 | return false; |
| 113 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 114 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | SwitchPort otherSwitchPort = (SwitchPort) other; |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | return (dpid.equals(otherSwitchPort.dpid) && |
| 118 | port.equals(otherSwitchPort.port)); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 119 | } |
Jonathan Hart | f0a8179 | 2013-11-14 11:36:06 -0800 | [diff] [blame] | 120 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 121 | @Override |
| 122 | public int hashCode() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | int hash = 17; |
| 124 | hash += 31 * hash + dpid.hashCode(); |
| 125 | hash += 31 * hash + port.hashCode(); |
| 126 | return hash; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 127 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 128 | } |