tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 3 | import java.util.Objects; |
| 4 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 5 | import com.google.common.primitives.UnsignedLongs; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 6 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 7 | /** |
| 8 | * Representation of a port number. |
| 9 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 10 | public final class PortNumber { |
| 11 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 12 | // TODO: revisit the max and the logical port value assignments |
| 13 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 14 | private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1; |
| 15 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 16 | |
| 17 | public static final PortNumber IN_PORT = new PortNumber(-8); |
| 18 | public static final PortNumber TABLE = new PortNumber(-7); |
| 19 | public static final PortNumber NORMAL = new PortNumber(-6); |
| 20 | public static final PortNumber FLOOD = new PortNumber(-5); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 21 | public static final PortNumber ALL = new PortNumber(-4); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 22 | public static final PortNumber LOCAL = new PortNumber(-2); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 23 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 24 | private final long number; |
| 25 | |
| 26 | // Public creation is prohibited |
| 27 | private PortNumber(long number) { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 28 | this.number = number; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns the port number representing the specified long value. |
| 33 | * |
| 34 | * @param number port number as long value |
| 35 | * @return port number |
| 36 | */ |
| 37 | public static PortNumber portNumber(long number) { |
| 38 | return new PortNumber(number); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the port number representing the specified string value. |
| 43 | * |
| 44 | * @param string port number as string value |
| 45 | * @return port number |
| 46 | */ |
| 47 | public static PortNumber portNumber(String string) { |
| 48 | return new PortNumber(UnsignedLongs.decode(string)); |
| 49 | } |
| 50 | |
| 51 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 52 | * Indicates whether or not this port number is a reserved logical one or |
| 53 | * whether it corresponds to a normal physical port of a device or NIC. |
| 54 | * |
| 55 | * @return true if logical port number |
| 56 | */ |
| 57 | public boolean isLogical() { |
| 58 | return number < 0 || number > MAX_NUMBER; |
| 59 | } |
| 60 | |
| 61 | /** |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 62 | * Returns the backing long value. |
| 63 | * |
| 64 | * @return port number as long |
| 65 | */ |
| 66 | public long toLong() { |
| 67 | return number; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return UnsignedLongs.toString(number); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
| 77 | return Objects.hash(number); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 82 | if (this == obj) { |
| 83 | return true; |
| 84 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 85 | if (obj instanceof PortNumber) { |
| 86 | final PortNumber other = (PortNumber) obj; |
| 87 | return this.number == other.number; |
| 88 | } |
| 89 | return false; |
| 90 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 91 | } |