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 | |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 3 | import org.codehaus.jackson.annotate.JsonProperty; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 4 | |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 5 | import com.google.common.primitives.UnsignedInts; |
| 6 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 7 | /** |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 8 | * Immutable class representing a port number. |
| 9 | * <p/> |
| 10 | * Current implementation supports only OpenFlow 1.0 (16 bit unsigned) port number. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 11 | */ |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 12 | public final class PortNumber { |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 13 | |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 14 | private final int value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 15 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 16 | /** |
| 17 | * Default constructor. |
| 18 | */ |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 19 | protected PortNumber() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 20 | this.value = 0; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 21 | } |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 22 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Copy constructor. |
| 25 | * |
| 26 | * @param other the object to copy from. |
| 27 | */ |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 28 | public PortNumber(PortNumber other) { |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 29 | this.value = other.value; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 30 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 31 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Constructor from a short integer value. |
| 34 | * |
| 35 | * @param value the value to use. |
| 36 | */ |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 37 | public PortNumber(short value) { |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 38 | this.value = (int) shortToUnsignedLong(value); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 39 | } |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 40 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 41 | /** |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 42 | * Constructor from an int. |
| 43 | * |
| 44 | * @param value the value to use. (Value will not be validated in any way.) |
| 45 | */ |
| 46 | PortNumber(int value) { |
| 47 | this.value = value; |
| 48 | } |
| 49 | |
| 50 | // TODO We may want a factory method version |
| 51 | // which does the range validation of parsed value. |
| 52 | /** |
| 53 | * Constructor from decimal string. |
| 54 | * |
| 55 | * @param decStr decimal string representation of a port number |
| 56 | */ |
| 57 | public PortNumber(String decStr) { |
| 58 | this(decStr, 10); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructor from string. |
| 63 | * |
| 64 | * @param s string representation of a port number |
| 65 | * @param radix the radix to use while parsing {@code s} |
| 66 | */ |
| 67 | public PortNumber(String s, int radix) { |
| 68 | this(UnsignedInts.parseUnsignedInt(s, radix)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Convert unsigned short to unsigned long. |
| 73 | * |
| 74 | * @param portno unsigned integer representing port number |
| 75 | * @return port number as unsigned long |
| 76 | */ |
| 77 | public static long shortToUnsignedLong(short portno) { |
| 78 | return UnsignedInts.toLong(0xffff & portno); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Gets the port number as short. |
| 83 | * <p/> |
| 84 | * Note: User of this method needs to be careful, handling unsigned value. |
| 85 | * @return number as short |
| 86 | */ |
| 87 | public short shortValue() { |
| 88 | return (short) value; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Gets the value of the port as unsigned integer. |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 93 | * |
| 94 | * @return the value of the port. |
| 95 | */ |
| 96 | @JsonProperty("value") |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 97 | public long value() { |
| 98 | // TODO Will require masking when we start storing 32bit port number. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | return value; |
| 100 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 101 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 102 | /** |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 103 | * Convert the port value as unsigned integer to a string. |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 104 | * |
| 105 | * @return the port value as a string. |
| 106 | */ |
| 107 | @Override |
| 108 | public String toString() { |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 109 | return UnsignedInts.toString(value); |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 110 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 112 | @Override |
| 113 | public boolean equals(Object other) { |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 114 | if (!(other instanceof PortNumber)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 117 | |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 118 | PortNumber otherPort = (PortNumber) other; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 119 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | return value == otherPort.value; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int hashCode() { |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 125 | return value; |
Jonathan Hart | 3edb175 | 2013-11-14 13:28:17 -0800 | [diff] [blame] | 126 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 127 | } |