Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 19 | import com.google.common.base.Supplier; |
| 20 | import com.google.common.base.Suppliers; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableMap.Builder; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 23 | import com.google.common.primitives.UnsignedLongs; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 24 | |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 25 | import java.util.Map; |
| 26 | import java.util.regex.Matcher; |
| 27 | import java.util.regex.Pattern; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Representation of a port number. |
| 34 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 35 | public final class PortNumber { |
| 36 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 37 | public static final PortNumber P0 = portNumber(0); |
| 38 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 39 | // TODO: revisit the max and the logical port value assignments |
| 40 | |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 41 | static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1; |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 42 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 43 | static final long IN_PORT_NUMBER = -8L; |
| 44 | static final long TABLE_NUMBER = -7L; |
| 45 | static final long NORMAL_NUMBER = -6L; |
| 46 | static final long FLOOD_NUMBER = -5L; |
| 47 | static final long ALL_NUMBER = -4L; |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 48 | static final long CONTROLLER_NUMBER = -3L; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 49 | static final long LOCAL_NUMBER = -2L; |
| 50 | static final long ANY_NUMBER = -1L; |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 51 | |
DongRyeol Cha | da008e3 | 2020-01-09 10:55:08 +0900 | [diff] [blame] | 52 | public static final long NO_DISPLAY_NUMBER = -1L; |
| 53 | |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Logical PortNumbers. |
| 56 | */ |
Sho SHIMIZU | 3aa0b18 | 2016-08-12 15:03:00 -0700 | [diff] [blame] | 57 | public enum Logical { |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 58 | IN_PORT(IN_PORT_NUMBER), |
| 59 | TABLE(TABLE_NUMBER), |
| 60 | NORMAL(NORMAL_NUMBER), |
| 61 | FLOOD(FLOOD_NUMBER), |
| 62 | ALL(ALL_NUMBER), |
| 63 | LOCAL(LOCAL_NUMBER), |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 64 | CONTROLLER(CONTROLLER_NUMBER), |
| 65 | ANY(ANY_NUMBER); |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 66 | |
| 67 | private final long number; |
| 68 | private final PortNumber instance; |
| 69 | |
| 70 | public long number() { |
| 71 | return number; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * PortNumber instance for the logical port. |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 76 | * |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 77 | * @return {@link PortNumber} |
| 78 | */ |
| 79 | public PortNumber instance() { |
| 80 | return instance; |
| 81 | } |
| 82 | |
| 83 | Logical(long number) { |
| 84 | this.number = number; |
| 85 | this.instance = new PortNumber(number); |
| 86 | } |
| 87 | } |
| 88 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 89 | public static final PortNumber IN_PORT = new PortNumber(IN_PORT_NUMBER); |
| 90 | public static final PortNumber TABLE = new PortNumber(TABLE_NUMBER); |
| 91 | public static final PortNumber NORMAL = new PortNumber(NORMAL_NUMBER); |
| 92 | public static final PortNumber FLOOD = new PortNumber(FLOOD_NUMBER); |
| 93 | public static final PortNumber ALL = new PortNumber(ALL_NUMBER); |
| 94 | public static final PortNumber LOCAL = new PortNumber(LOCAL_NUMBER); |
| 95 | public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 96 | public static final PortNumber ANY = new PortNumber(ANY_NUMBER); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 97 | |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 98 | // lazily populated Logical port number to PortNumber |
| 99 | static final Supplier<Map<Long, Logical>> LOGICAL = Suppliers.memoize(() -> { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 100 | Builder<Long, Logical> builder = ImmutableMap.builder(); |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 101 | for (Logical lp : Logical.values()) { |
| 102 | builder.put(lp.number(), lp); |
| 103 | } |
| 104 | return builder.build(); |
| 105 | }); |
| 106 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 107 | private final long number; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 108 | private final String name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 109 | private final boolean hasName; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 110 | |
| 111 | // Public creation is prohibited |
| 112 | private PortNumber(long number) { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 113 | this.number = number; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 114 | this.name = UnsignedLongs.toString(number); |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 115 | this.hasName = false; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private PortNumber(long number, String name) { |
| 119 | this.number = number; |
| 120 | this.name = name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 121 | this.hasName = true; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the port number representing the specified long value. |
| 126 | * |
| 127 | * @param number port number as long value |
| 128 | * @return port number |
| 129 | */ |
| 130 | public static PortNumber portNumber(long number) { |
| 131 | return new PortNumber(number); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the port number representing the specified string value. |
| 136 | * |
Yuta HIGUCHI | a8a1375 | 2017-03-31 10:29:15 -0700 | [diff] [blame] | 137 | * @param string port number as decimal, hexadecimal, or octal number string |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 138 | * @return port number |
| 139 | */ |
| 140 | public static PortNumber portNumber(String string) { |
| 141 | return new PortNumber(UnsignedLongs.decode(string)); |
| 142 | } |
| 143 | |
| 144 | /** |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 145 | * Returns the port number representing the specified long value and name. |
| 146 | * |
| 147 | * @param number port number as long value |
| 148 | * @param name port name as string value |
| 149 | * @return port number |
| 150 | */ |
| 151 | public static PortNumber portNumber(long number, String name) { |
| 152 | return new PortNumber(number, name); |
| 153 | } |
| 154 | |
| 155 | /** |
Andrea Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 156 | * Returns PortNumber instance from String representation. |
| 157 | * |
| 158 | * @param s String representation equivalent to {@link PortNumber#toString()} |
| 159 | * @return {@link PortNumber} instance |
| 160 | * @throws IllegalArgumentException if given String was malformed |
| 161 | */ |
| 162 | public static PortNumber fromString(String s) { |
| 163 | checkNotNull(s); |
| 164 | checkArgument(!s.isEmpty(), "cannot be empty"); |
| 165 | |
| 166 | if (isAsciiDecimal(s.charAt(0))) { |
| 167 | // unsigned decimal string |
| 168 | return portNumber(s); |
| 169 | } else if (s.startsWith("[")) { |
| 170 | // named PortNumber |
| 171 | Matcher matcher = NAMED.matcher(s); |
| 172 | checkArgument(matcher.matches(), "Invalid named PortNumber %s", s); |
| 173 | |
| 174 | String name = matcher.group("name"); |
| 175 | String num = matcher.group("num"); |
| 176 | return portNumber(UnsignedLongs.parseUnsignedLong(num), name); |
| 177 | } |
| 178 | |
| 179 | // Logical |
| 180 | if (s.startsWith("UNKNOWN(") && s.endsWith(")")) { |
| 181 | return portNumber(s.substring("UNKNOWN(".length(), s.length() - 1)); |
| 182 | } else { |
| 183 | return Logical.valueOf(s).instance; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 188 | * Indicates whether or not this port number is a reserved logical one or |
| 189 | * whether it corresponds to a normal physical port of a device or NIC. |
| 190 | * |
| 191 | * @return true if logical port number |
| 192 | */ |
| 193 | public boolean isLogical() { |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 194 | if (hasName) { |
| 195 | return false; |
| 196 | } else { |
| 197 | return (number < 0 || number > MAX_NUMBER); |
| 198 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 202 | * Returns the backing long value. |
| 203 | * |
| 204 | * @return port number as long |
| 205 | */ |
| 206 | public long toLong() { |
| 207 | return number; |
| 208 | } |
| 209 | |
Marc De Leenheer | c0e37ec | 2015-04-14 22:53:23 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Returns the backing string value. |
| 212 | * |
| 213 | * @return port name as string value |
| 214 | */ |
| 215 | public String name() { |
| 216 | return name; |
| 217 | } |
| 218 | |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Indicates whether this port number was created with a port name, |
| 221 | * or only with a number. |
| 222 | * |
| 223 | * @return true if port was created with name |
| 224 | */ |
| 225 | public boolean hasName() { |
| 226 | return hasName; |
| 227 | } |
| 228 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 229 | private String decodeLogicalPort() { |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 230 | Logical logical = LOGICAL.get().get(number); |
| 231 | if (logical != null) { |
| 232 | // enum name |
| 233 | return logical.toString(); |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 234 | } |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 235 | return String.format("UNKNOWN(%s)", UnsignedLongs.toString(number)); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * Regular expression to match String representation of named PortNumber. |
| 241 | * |
| 242 | * Format: "[name](num:unsigned decimal string)" |
| 243 | */ |
| 244 | private static final Pattern NAMED = Pattern.compile("^\\[(?<name>.*)\\]\\((?<num>\\d+)\\)$"); |
| 245 | |
| 246 | private static boolean isAsciiDecimal(char c) { |
| 247 | return '0' <= c && c <= '9'; |
| 248 | } |
| 249 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 250 | @Override |
| 251 | public String toString() { |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 252 | if (isLogical()) { |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 253 | return decodeLogicalPort(); |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 254 | } else if (hasName()) { |
| 255 | // named port |
DongRyeol Cha | da008e3 | 2020-01-09 10:55:08 +0900 | [diff] [blame] | 256 | if (number == NO_DISPLAY_NUMBER) { |
| 257 | return String.format("[%s]", name); |
| 258 | } else { |
| 259 | return String.format("[%s](%d)", name, number); |
| 260 | } |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 261 | } else { |
| 262 | // unsigned decimal string |
| 263 | return name; |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 264 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public int hashCode() { |
Sho SHIMIZU | d5c66af | 2015-06-30 09:43:11 -0700 | [diff] [blame] | 269 | return Long.hashCode(number); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 274 | if (this == obj) { |
| 275 | return true; |
| 276 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 277 | if (obj instanceof PortNumber) { |
| 278 | final PortNumber other = (PortNumber) obj; |
| 279 | return this.number == other.number; |
| 280 | } |
| 281 | return false; |
| 282 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Indicates whether some other PortNumber object is equal to this one |
| 286 | * including it's name. |
| 287 | * |
| 288 | * @param that other {@link PortNumber} instance to compare |
| 289 | * @return true if equal, false otherwise |
| 290 | */ |
| 291 | public boolean exactlyEquals(PortNumber that) { |
| 292 | if (this == that) { |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | return this.equals(that) && |
| 297 | this.hasName == that.hasName && |
| 298 | Objects.equal(this.name, that.name); |
| 299 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 300 | } |