blob: cfb11d5646be5bce54309ffe2645bc7366b39ff1 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.onlab.onos.net;
2
tomca20e0c2014-09-03 23:22:24 -07003import java.util.Objects;
4
alshabib4680bb62014-09-04 17:15:08 -07005import com.google.common.primitives.UnsignedLongs;
tomca20e0c2014-09-03 23:22:24 -07006
tom0eb04ca2014-08-25 14:34:51 -07007/**
8 * Representation of a port number.
9 */
tomca20e0c2014-09-03 23:22:24 -070010public final class PortNumber {
11
tom613d8142014-09-11 15:09:37 -070012 // TODO: revisit the max and the logical port value assignments
13
tomca20e0c2014-09-03 23:22:24 -070014 private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
15
alshabib63d5afe2014-09-15 09:40:24 -070016
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);
tom613d8142014-09-11 15:09:37 -070021 public static final PortNumber ALL = new PortNumber(-4);
alshabib63d5afe2014-09-15 09:40:24 -070022 public static final PortNumber LOCAL = new PortNumber(-2);
tom613d8142014-09-11 15:09:37 -070023
tomca20e0c2014-09-03 23:22:24 -070024 private final long number;
25
26 // Public creation is prohibited
27 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070028 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 /**
tom613d8142014-09-11 15:09:37 -070052 * 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 /**
tomca20e0c2014-09-03 23:22:24 -070062 * 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) {
tomfc9a4ff2014-09-22 18:22:47 -070082 if (this == obj) {
83 return true;
84 }
tomca20e0c2014-09-03 23:22:24 -070085 if (obj instanceof PortNumber) {
86 final PortNumber other = (PortNumber) obj;
87 return this.number == other.number;
88 }
89 return false;
90 }
tom0eb04ca2014-08-25 14:34:51 -070091}