blob: da34944a370630d7e0246ee7e026207386cc8043 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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
HIGUCHI Yuta14c04362015-11-12 10:12:04 -080018import com.google.common.base.Supplier;
19import com.google.common.base.Suppliers;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.ImmutableMap.Builder;
alshabib4680bb62014-09-04 17:15:08 -070022import com.google.common.primitives.UnsignedLongs;
tomca20e0c2014-09-03 23:22:24 -070023
Andrea Campanella5df35952015-12-08 15:46:49 -080024import java.util.Map;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
tom0eb04ca2014-08-25 14:34:51 -070031/**
32 * Representation of a port number.
33 */
tomca20e0c2014-09-03 23:22:24 -070034public final class PortNumber {
35
toma1d16b62014-10-02 23:45:11 -070036 public static final PortNumber P0 = portNumber(0);
37
tom613d8142014-09-11 15:09:37 -070038 // TODO: revisit the max and the logical port value assignments
39
HIGUCHI Yuta14c04362015-11-12 10:12:04 -080040 static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
alshabib63d5afe2014-09-15 09:40:24 -070041
Ray Milkey8717be22015-01-12 14:16:27 -080042 static final long IN_PORT_NUMBER = -8L;
43 static final long TABLE_NUMBER = -7L;
44 static final long NORMAL_NUMBER = -6L;
45 static final long FLOOD_NUMBER = -5L;
46 static final long ALL_NUMBER = -4L;
47 static final long LOCAL_NUMBER = -2L;
48 static final long CONTROLLER_NUMBER = -3L;
49
HIGUCHI Yuta14c04362015-11-12 10:12:04 -080050 /**
51 * Logical PortNumbers.
52 */
53 public static enum Logical {
54 IN_PORT(IN_PORT_NUMBER),
55 TABLE(TABLE_NUMBER),
56 NORMAL(NORMAL_NUMBER),
57 FLOOD(FLOOD_NUMBER),
58 ALL(ALL_NUMBER),
59 LOCAL(LOCAL_NUMBER),
60 CONTROLLER(CONTROLLER_NUMBER);
61
62 private final long number;
63 private final PortNumber instance;
64
65 public long number() {
66 return number;
67 }
68
69 /**
70 * PortNumber instance for the logical port.
Andrea Campanella5df35952015-12-08 15:46:49 -080071 *
HIGUCHI Yuta14c04362015-11-12 10:12:04 -080072 * @return {@link PortNumber}
73 */
74 public PortNumber instance() {
75 return instance;
76 }
77
78 Logical(long number) {
79 this.number = number;
80 this.instance = new PortNumber(number);
81 }
82 }
83
Ray Milkey8717be22015-01-12 14:16:27 -080084 public static final PortNumber IN_PORT = new PortNumber(IN_PORT_NUMBER);
85 public static final PortNumber TABLE = new PortNumber(TABLE_NUMBER);
86 public static final PortNumber NORMAL = new PortNumber(NORMAL_NUMBER);
87 public static final PortNumber FLOOD = new PortNumber(FLOOD_NUMBER);
88 public static final PortNumber ALL = new PortNumber(ALL_NUMBER);
89 public static final PortNumber LOCAL = new PortNumber(LOCAL_NUMBER);
90 public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER);
tom613d8142014-09-11 15:09:37 -070091
HIGUCHI Yuta14c04362015-11-12 10:12:04 -080092 // lazily populated Logical port number to PortNumber
93 static final Supplier<Map<Long, Logical>> LOGICAL = Suppliers.memoize(() -> {
94 Builder<Long, Logical> builder = ImmutableMap.<Long, Logical>builder();
95 for (Logical lp : Logical.values()) {
96 builder.put(lp.number(), lp);
97 }
98 return builder.build();
99 });
100
tomca20e0c2014-09-03 23:22:24 -0700101 private final long number;
Marc De Leenheer171c2e82015-04-13 17:20:37 -0700102 private final String name;
Marc De Leenheer76d89742015-04-16 15:03:03 -0700103 private final boolean hasName;
tomca20e0c2014-09-03 23:22:24 -0700104
105 // Public creation is prohibited
106 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -0700107 this.number = number;
Marc De Leenheer171c2e82015-04-13 17:20:37 -0700108 this.name = UnsignedLongs.toString(number);
Marc De Leenheer76d89742015-04-16 15:03:03 -0700109 this.hasName = false;
Marc De Leenheer171c2e82015-04-13 17:20:37 -0700110 }
111
112 private PortNumber(long number, String name) {
113 this.number = number;
114 this.name = name;
Marc De Leenheer76d89742015-04-16 15:03:03 -0700115 this.hasName = true;
tomca20e0c2014-09-03 23:22:24 -0700116 }
117
118 /**
119 * Returns the port number representing the specified long value.
120 *
121 * @param number port number as long value
122 * @return port number
123 */
124 public static PortNumber portNumber(long number) {
125 return new PortNumber(number);
126 }
127
128 /**
129 * Returns the port number representing the specified string value.
130 *
131 * @param string port number as string value
132 * @return port number
133 */
134 public static PortNumber portNumber(String string) {
135 return new PortNumber(UnsignedLongs.decode(string));
136 }
137
138 /**
Marc De Leenheer171c2e82015-04-13 17:20:37 -0700139 * Returns the port number representing the specified long value and name.
140 *
141 * @param number port number as long value
142 * @param name port name as string value
143 * @return port number
144 */
145 public static PortNumber portNumber(long number, String name) {
146 return new PortNumber(number, name);
147 }
148
149 /**
Andrea Campanella5df35952015-12-08 15:46:49 -0800150 * Returns PortNumber instance from String representation.
151 *
152 * @param s String representation equivalent to {@link PortNumber#toString()}
153 * @return {@link PortNumber} instance
154 * @throws IllegalArgumentException if given String was malformed
155 */
156 public static PortNumber fromString(String s) {
157 checkNotNull(s);
158 checkArgument(!s.isEmpty(), "cannot be empty");
159
160 if (isAsciiDecimal(s.charAt(0))) {
161 // unsigned decimal string
162 return portNumber(s);
163 } else if (s.startsWith("[")) {
164 // named PortNumber
165 Matcher matcher = NAMED.matcher(s);
166 checkArgument(matcher.matches(), "Invalid named PortNumber %s", s);
167
168 String name = matcher.group("name");
169 String num = matcher.group("num");
170 return portNumber(UnsignedLongs.parseUnsignedLong(num), name);
171 }
172
173 // Logical
174 if (s.startsWith("UNKNOWN(") && s.endsWith(")")) {
175 return portNumber(s.substring("UNKNOWN(".length(), s.length() - 1));
176 } else {
177 return Logical.valueOf(s).instance;
178 }
179 }
180
181 /**
tom613d8142014-09-11 15:09:37 -0700182 * Indicates whether or not this port number is a reserved logical one or
183 * whether it corresponds to a normal physical port of a device or NIC.
184 *
185 * @return true if logical port number
186 */
187 public boolean isLogical() {
Marc De Leenheer76d89742015-04-16 15:03:03 -0700188 if (hasName) {
189 return false;
190 } else {
191 return (number < 0 || number > MAX_NUMBER);
192 }
tom613d8142014-09-11 15:09:37 -0700193 }
194
195 /**
tomca20e0c2014-09-03 23:22:24 -0700196 * Returns the backing long value.
197 *
198 * @return port number as long
199 */
200 public long toLong() {
201 return number;
202 }
203
Marc De Leenheerc0e37ec2015-04-14 22:53:23 -0700204 /**
205 * Returns the backing string value.
206 *
207 * @return port name as string value
208 */
209 public String name() {
210 return name;
211 }
212
Marc De Leenheer76d89742015-04-16 15:03:03 -0700213 /**
214 * Indicates whether this port number was created with a port name,
215 * or only with a number.
216 *
217 * @return true if port was created with name
218 */
219 public boolean hasName() {
220 return hasName;
221 }
222
Ray Milkey8717be22015-01-12 14:16:27 -0800223 private String decodeLogicalPort() {
HIGUCHI Yuta14c04362015-11-12 10:12:04 -0800224 Logical logical = LOGICAL.get().get(number);
225 if (logical != null) {
226 // enum name
227 return logical.toString();
Ray Milkey8717be22015-01-12 14:16:27 -0800228 }
HIGUCHI Yuta14c04362015-11-12 10:12:04 -0800229 return String.format("UNKNOWN(%s)", UnsignedLongs.toString(number));
230 }
231
232
233 /**
234 * Regular expression to match String representation of named PortNumber.
235 *
236 * Format: "[name](num:unsigned decimal string)"
237 */
238 private static final Pattern NAMED = Pattern.compile("^\\[(?<name>.*)\\]\\((?<num>\\d+)\\)$");
239
240 private static boolean isAsciiDecimal(char c) {
241 return '0' <= c && c <= '9';
242 }
243
tomca20e0c2014-09-03 23:22:24 -0700244 @Override
245 public String toString() {
HIGUCHI Yuta14c04362015-11-12 10:12:04 -0800246 if (isLogical()) {
Ray Milkey8717be22015-01-12 14:16:27 -0800247 return decodeLogicalPort();
HIGUCHI Yuta14c04362015-11-12 10:12:04 -0800248 } else if (hasName()) {
249 // named port
250 return String.format("[%s](%d)", name, number);
251 } else {
252 // unsigned decimal string
253 return name;
Ray Milkey8717be22015-01-12 14:16:27 -0800254 }
tomca20e0c2014-09-03 23:22:24 -0700255 }
256
257 @Override
258 public int hashCode() {
Sho SHIMIZUd5c66af2015-06-30 09:43:11 -0700259 return Long.hashCode(number);
tomca20e0c2014-09-03 23:22:24 -0700260 }
261
262 @Override
263 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700264 if (this == obj) {
265 return true;
266 }
tomca20e0c2014-09-03 23:22:24 -0700267 if (obj instanceof PortNumber) {
268 final PortNumber other = (PortNumber) obj;
269 return this.number == other.number;
270 }
271 return false;
272 }
tom0eb04ca2014-08-25 14:34:51 -0700273}