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