blob: 58838718696e1765a75df416471d7f8023e22685 [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 */
tom0eb04ca2014-08-25 14:34:51 -070016package org.onlab.onos.net;
17
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);
tom613d8142014-09-11 15:09:37 -070040
tomca20e0c2014-09-03 23:22:24 -070041 private final long number;
42
43 // Public creation is prohibited
44 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070045 this.number = number;
46 }
47
48 /**
49 * Returns the port number representing the specified long value.
50 *
51 * @param number port number as long value
52 * @return port number
53 */
54 public static PortNumber portNumber(long number) {
55 return new PortNumber(number);
56 }
57
58 /**
59 * Returns the port number representing the specified string value.
60 *
61 * @param string port number as string value
62 * @return port number
63 */
64 public static PortNumber portNumber(String string) {
65 return new PortNumber(UnsignedLongs.decode(string));
66 }
67
68 /**
tom613d8142014-09-11 15:09:37 -070069 * Indicates whether or not this port number is a reserved logical one or
70 * whether it corresponds to a normal physical port of a device or NIC.
71 *
72 * @return true if logical port number
73 */
74 public boolean isLogical() {
75 return number < 0 || number > MAX_NUMBER;
76 }
77
78 /**
tomca20e0c2014-09-03 23:22:24 -070079 * Returns the backing long value.
80 *
81 * @return port number as long
82 */
83 public long toLong() {
84 return number;
85 }
86
87 @Override
88 public String toString() {
89 return UnsignedLongs.toString(number);
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hash(number);
95 }
96
97 @Override
98 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070099 if (this == obj) {
100 return true;
101 }
tomca20e0c2014-09-03 23:22:24 -0700102 if (obj instanceof PortNumber) {
103 final PortNumber other = (PortNumber) obj;
104 return this.number == other.number;
105 }
106 return false;
107 }
tom0eb04ca2014-08-25 14:34:51 -0700108}