blob: c4c7eedbb7919d6942fa1d33d3e723e360d6382e [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 HIGUCHI9da3a6e2014-06-10 22:11:58 -070042 * 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 Hart3edb1752013-11-14 13:28:17 -080093 *
94 * @return the value of the port.
95 */
96 @JsonProperty("value")
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070097 public long value() {
98 // TODO Will require masking when we start storing 32bit port number.
Ray Milkey269ffb92014-04-03 14:43:30 -070099 return value;
100 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800101
Jonathan Hart3edb1752013-11-14 13:28:17 -0800102 /**
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700103 * Convert the port value as unsigned integer to a string.
Jonathan Hart3edb1752013-11-14 13:28:17 -0800104 *
105 * @return the port value as a string.
106 */
107 @Override
108 public String toString() {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700109 return UnsignedInts.toString(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -0800110 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700111
Jonathan Hart3edb1752013-11-14 13:28:17 -0800112 @Override
113 public boolean equals(Object other) {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700114 if (!(other instanceof PortNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 return false;
116 }
Jonathan Hart3edb1752013-11-14 13:28:17 -0800117
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700118 PortNumber otherPort = (PortNumber) other;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800119
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 return value == otherPort.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800121 }
122
123 @Override
124 public int hashCode() {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700125 return value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800126 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800127}