blob: d5087713b796a156e144438e4aca355e0a5d16cb [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom0eb04ca2014-08-25 14:34:51 -070017
tomca20e0c2014-09-03 23:22:24 -070018import java.util.Objects;
19
alshabib4680bb62014-09-04 17:15:08 -070020import com.google.common.primitives.UnsignedLongs;
tomca20e0c2014-09-03 23:22:24 -070021
tom0eb04ca2014-08-25 14:34:51 -070022/**
23 * Representation of a port number.
24 */
tomca20e0c2014-09-03 23:22:24 -070025public final class PortNumber {
26
toma1d16b62014-10-02 23:45:11 -070027 public static final PortNumber P0 = portNumber(0);
28
tom613d8142014-09-11 15:09:37 -070029 // TODO: revisit the max and the logical port value assignments
30
tomca20e0c2014-09-03 23:22:24 -070031 private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
32
alshabib63d5afe2014-09-15 09:40:24 -070033
34 public static final PortNumber IN_PORT = new PortNumber(-8);
35 public static final PortNumber TABLE = new PortNumber(-7);
36 public static final PortNumber NORMAL = new PortNumber(-6);
37 public static final PortNumber FLOOD = new PortNumber(-5);
tom613d8142014-09-11 15:09:37 -070038 public static final PortNumber ALL = new PortNumber(-4);
alshabib63d5afe2014-09-15 09:40:24 -070039 public static final PortNumber LOCAL = new PortNumber(-2);
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080040 public static final PortNumber CONTROLLER = new PortNumber(-3);
tom613d8142014-09-11 15:09:37 -070041
tomca20e0c2014-09-03 23:22:24 -070042 private final long number;
43
44 // Public creation is prohibited
45 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070046 this.number = number;
47 }
48
49 /**
50 * Returns the port number representing the specified long value.
51 *
52 * @param number port number as long value
53 * @return port number
54 */
55 public static PortNumber portNumber(long number) {
56 return new PortNumber(number);
57 }
58
59 /**
60 * Returns the port number representing the specified string value.
61 *
62 * @param string port number as string value
63 * @return port number
64 */
65 public static PortNumber portNumber(String string) {
66 return new PortNumber(UnsignedLongs.decode(string));
67 }
68
69 /**
tom613d8142014-09-11 15:09:37 -070070 * Indicates whether or not this port number is a reserved logical one or
71 * whether it corresponds to a normal physical port of a device or NIC.
72 *
73 * @return true if logical port number
74 */
75 public boolean isLogical() {
76 return number < 0 || number > MAX_NUMBER;
77 }
78
79 /**
tomca20e0c2014-09-03 23:22:24 -070080 * Returns the backing long value.
81 *
82 * @return port number as long
83 */
84 public long toLong() {
85 return number;
86 }
87
88 @Override
89 public String toString() {
90 return UnsignedLongs.toString(number);
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(number);
96 }
97
98 @Override
99 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700100 if (this == obj) {
101 return true;
102 }
tomca20e0c2014-09-03 23:22:24 -0700103 if (obj instanceof PortNumber) {
104 final PortNumber other = (PortNumber) obj;
105 return this.number == other.number;
106 }
107 return false;
108 }
tom0eb04ca2014-08-25 14:34:51 -0700109}