blob: 60c3305e5d2b9a0ddc17aa50122a21e947236490 [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
toma1d16b62014-10-02 23:45:11 -070012 public static final PortNumber P0 = portNumber(0);
13
tom613d8142014-09-11 15:09:37 -070014 // TODO: revisit the max and the logical port value assignments
15
tomca20e0c2014-09-03 23:22:24 -070016 private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
17
alshabib63d5afe2014-09-15 09:40:24 -070018
19 public static final PortNumber IN_PORT = new PortNumber(-8);
20 public static final PortNumber TABLE = new PortNumber(-7);
21 public static final PortNumber NORMAL = new PortNumber(-6);
22 public static final PortNumber FLOOD = new PortNumber(-5);
tom613d8142014-09-11 15:09:37 -070023 public static final PortNumber ALL = new PortNumber(-4);
alshabib63d5afe2014-09-15 09:40:24 -070024 public static final PortNumber LOCAL = new PortNumber(-2);
tom613d8142014-09-11 15:09:37 -070025
tomca20e0c2014-09-03 23:22:24 -070026 private final long number;
27
28 // Public creation is prohibited
29 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070030 this.number = number;
31 }
32
33 /**
34 * Returns the port number representing the specified long value.
35 *
36 * @param number port number as long value
37 * @return port number
38 */
39 public static PortNumber portNumber(long number) {
40 return new PortNumber(number);
41 }
42
43 /**
44 * Returns the port number representing the specified string value.
45 *
46 * @param string port number as string value
47 * @return port number
48 */
49 public static PortNumber portNumber(String string) {
50 return new PortNumber(UnsignedLongs.decode(string));
51 }
52
53 /**
tom613d8142014-09-11 15:09:37 -070054 * Indicates whether or not this port number is a reserved logical one or
55 * whether it corresponds to a normal physical port of a device or NIC.
56 *
57 * @return true if logical port number
58 */
59 public boolean isLogical() {
60 return number < 0 || number > MAX_NUMBER;
61 }
62
63 /**
tomca20e0c2014-09-03 23:22:24 -070064 * Returns the backing long value.
65 *
66 * @return port number as long
67 */
68 public long toLong() {
69 return number;
70 }
71
72 @Override
73 public String toString() {
74 return UnsignedLongs.toString(number);
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hash(number);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070084 if (this == obj) {
85 return true;
86 }
tomca20e0c2014-09-03 23:22:24 -070087 if (obj instanceof PortNumber) {
88 final PortNumber other = (PortNumber) obj;
89 return this.number == other.number;
90 }
91 return false;
92 }
tom0eb04ca2014-08-25 14:34:51 -070093}