blob: 09df2474a6e47c2260ef0f4ab213a9186d373c96 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08003import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08004
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -07005import com.google.common.primitives.UnsignedInts;
6
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08007/**
Yuta HIGUCHIfb564502014-06-16 21:29:00 -07008 * Immutable class representing a port number.
9 * <p/>
10 * Current implementation supports only OpenFlow 1.0 (16 bit unsigned) port number.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080011 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070012public final class PortNumber {
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080013
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070014 private final int value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080015
Jonathan Hart3edb1752013-11-14 13:28:17 -080016 /**
17 * Default constructor.
18 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070019 protected PortNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -070020 this.value = 0;
Jonathan Hart3edb1752013-11-14 13:28:17 -080021 }
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -080022
Jonathan Hart3edb1752013-11-14 13:28:17 -080023 /**
24 * Copy constructor.
25 *
26 * @param other the object to copy from.
27 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070028 public PortNumber(PortNumber other) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070029 this.value = other.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080030 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080031
Jonathan Hart3edb1752013-11-14 13:28:17 -080032 /**
33 * Constructor from a short integer value.
34 *
35 * @param value the value to use.
36 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070037 public PortNumber(short value) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070038 this.value = (int) shortToUnsignedLong(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080039 }
Pavlin Radoslavovede97582013-03-08 18:57:28 -080040
Jonathan Hart3edb1752013-11-14 13:28:17 -080041 /**
Yuta HIGUCHI5865bdb2014-07-30 09:32:13 -070042 * Creates the unsigned 16 bit port number.
43 *
44 * @param number unsigned 16 bit port number.
45 * @return PortNumber instance
46 */
47 public static PortNumber uint16(final short number) {
48 return new PortNumber(number);
49 }
50
51 /**
52 * Creates the unsigned 32 bit port number.
53 *
54 * @param number unsigned 32 bit port number.
55 * @return PortNumber instance
56 */
57 public static PortNumber uint32(final int number) {
58 return new PortNumber(number);
59 }
60
61 /**
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070062 * Constructor from an int.
63 *
64 * @param value the value to use. (Value will not be validated in any way.)
65 */
66 PortNumber(int value) {
67 this.value = value;
68 }
69
70 // TODO We may want a factory method version
71 // which does the range validation of parsed value.
72 /**
73 * Constructor from decimal string.
74 *
75 * @param decStr decimal string representation of a port number
76 */
77 public PortNumber(String decStr) {
78 this(decStr, 10);
79 }
80
81 /**
82 * Constructor from string.
83 *
84 * @param s string representation of a port number
85 * @param radix the radix to use while parsing {@code s}
86 */
87 public PortNumber(String s, int radix) {
88 this(UnsignedInts.parseUnsignedInt(s, radix));
89 }
90
91 /**
92 * Convert unsigned short to unsigned long.
93 *
94 * @param portno unsigned integer representing port number
95 * @return port number as unsigned long
96 */
97 public static long shortToUnsignedLong(short portno) {
98 return UnsignedInts.toLong(0xffff & portno);
99 }
100
101 /**
102 * Gets the port number as short.
103 * <p/>
104 * Note: User of this method needs to be careful, handling unsigned value.
105 * @return number as short
106 */
107 public short shortValue() {
108 return (short) value;
109 }
110
111 /**
112 * Gets the value of the port as unsigned integer.
Jonathan Hart3edb1752013-11-14 13:28:17 -0800113 *
114 * @return the value of the port.
115 */
116 @JsonProperty("value")
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700117 public long value() {
Yuta HIGUCHI5865bdb2014-07-30 09:32:13 -0700118 return 0xffffffffL & value;
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800120
Jonathan Hart3edb1752013-11-14 13:28:17 -0800121 /**
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700122 * Convert the port value as unsigned integer to a string.
Jonathan Hart3edb1752013-11-14 13:28:17 -0800123 *
124 * @return the port value as a string.
125 */
126 @Override
127 public String toString() {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700128 return UnsignedInts.toString(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -0800129 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700130
Jonathan Hart3edb1752013-11-14 13:28:17 -0800131 @Override
132 public boolean equals(Object other) {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700133 if (!(other instanceof PortNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 return false;
135 }
Jonathan Hart3edb1752013-11-14 13:28:17 -0800136
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700137 PortNumber otherPort = (PortNumber) other;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 return value == otherPort.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800140 }
141
142 @Override
143 public int hashCode() {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700144 return value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800145 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800146}