blob: 16a4193f9195168c535530a78c4f51c095cb1e82 [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
Ray Milkey8717be22015-01-12 14:16:27 -080034 static final long IN_PORT_NUMBER = -8L;
35 static final long TABLE_NUMBER = -7L;
36 static final long NORMAL_NUMBER = -6L;
37 static final long FLOOD_NUMBER = -5L;
38 static final long ALL_NUMBER = -4L;
39 static final long LOCAL_NUMBER = -2L;
40 static final long CONTROLLER_NUMBER = -3L;
41
42 public static final PortNumber IN_PORT = new PortNumber(IN_PORT_NUMBER);
43 public static final PortNumber TABLE = new PortNumber(TABLE_NUMBER);
44 public static final PortNumber NORMAL = new PortNumber(NORMAL_NUMBER);
45 public static final PortNumber FLOOD = new PortNumber(FLOOD_NUMBER);
46 public static final PortNumber ALL = new PortNumber(ALL_NUMBER);
47 public static final PortNumber LOCAL = new PortNumber(LOCAL_NUMBER);
48 public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER);
tom613d8142014-09-11 15:09:37 -070049
tomca20e0c2014-09-03 23:22:24 -070050 private final long number;
51
52 // Public creation is prohibited
53 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070054 this.number = number;
55 }
56
57 /**
58 * Returns the port number representing the specified long value.
59 *
60 * @param number port number as long value
61 * @return port number
62 */
63 public static PortNumber portNumber(long number) {
64 return new PortNumber(number);
65 }
66
67 /**
68 * Returns the port number representing the specified string value.
69 *
70 * @param string port number as string value
71 * @return port number
72 */
73 public static PortNumber portNumber(String string) {
74 return new PortNumber(UnsignedLongs.decode(string));
75 }
76
77 /**
tom613d8142014-09-11 15:09:37 -070078 * Indicates whether or not this port number is a reserved logical one or
79 * whether it corresponds to a normal physical port of a device or NIC.
80 *
81 * @return true if logical port number
82 */
83 public boolean isLogical() {
84 return number < 0 || number > MAX_NUMBER;
85 }
86
87 /**
tomca20e0c2014-09-03 23:22:24 -070088 * Returns the backing long value.
89 *
90 * @return port number as long
91 */
92 public long toLong() {
93 return number;
94 }
95
Ray Milkey8717be22015-01-12 14:16:27 -080096 private String decodeLogicalPort() {
97 if (number == CONTROLLER_NUMBER) {
98 return "CONTROLLER";
99 } else if (number == LOCAL_NUMBER) {
100 return "LOCAL";
101 } else if (number == ALL_NUMBER) {
102 return "ALL";
103 } else if (number == FLOOD_NUMBER) {
104 return "FLOOD";
105 } else if (number == NORMAL_NUMBER) {
106 return "NORMAL";
107 } else if (number == TABLE_NUMBER) {
108 return "TABLE";
109 } else if (number == IN_PORT_NUMBER) {
110 return "IN_PORT";
111 }
112 return "UNKNOWN";
113 }
114
tomca20e0c2014-09-03 23:22:24 -0700115 @Override
116 public String toString() {
Ray Milkey8717be22015-01-12 14:16:27 -0800117 if (!isLogical()) {
118 return UnsignedLongs.toString(number);
119 } else {
120 return decodeLogicalPort();
121 }
tomca20e0c2014-09-03 23:22:24 -0700122 }
123
124 @Override
125 public int hashCode() {
126 return Objects.hash(number);
127 }
128
129 @Override
130 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700131 if (this == obj) {
132 return true;
133 }
tomca20e0c2014-09-03 23:22:24 -0700134 if (obj instanceof PortNumber) {
135 final PortNumber other = (PortNumber) obj;
136 return this.number == other.number;
137 }
138 return false;
139 }
tom0eb04ca2014-08-25 14:34:51 -0700140}