Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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; |
| 47 | static final long LOCAL_NUMBER = -2L; |
| 48 | static final long CONTROLLER_NUMBER = -3L; |
| 49 | |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 50 | /** |
| 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 Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 71 | * |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 72 | * @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 Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 84 | 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); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 91 | |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 92 | // 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 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 101 | private final long number; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 102 | private final String name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 103 | private final boolean hasName; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 104 | |
| 105 | // Public creation is prohibited |
| 106 | private PortNumber(long number) { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 107 | this.number = number; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 108 | this.name = UnsignedLongs.toString(number); |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 109 | this.hasName = false; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private PortNumber(long number, String name) { |
| 113 | this.number = number; |
| 114 | this.name = name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 115 | this.hasName = true; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 116 | } |
| 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 Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 139 | * 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 Campanella | 5df3595 | 2015-12-08 15:46:49 -0800 | [diff] [blame] | 150 | * 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 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 182 | * 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 Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 188 | if (hasName) { |
| 189 | return false; |
| 190 | } else { |
| 191 | return (number < 0 || number > MAX_NUMBER); |
| 192 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 196 | * Returns the backing long value. |
| 197 | * |
| 198 | * @return port number as long |
| 199 | */ |
| 200 | public long toLong() { |
| 201 | return number; |
| 202 | } |
| 203 | |
Marc De Leenheer | c0e37ec | 2015-04-14 22:53:23 -0700 | [diff] [blame] | 204 | /** |
| 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 Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 213 | /** |
| 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 Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 223 | private String decodeLogicalPort() { |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 224 | Logical logical = LOGICAL.get().get(number); |
| 225 | if (logical != null) { |
| 226 | // enum name |
| 227 | return logical.toString(); |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 228 | } |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 229 | 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 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 244 | @Override |
| 245 | public String toString() { |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 246 | if (isLogical()) { |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 247 | return decodeLogicalPort(); |
HIGUCHI Yuta | 14c0436 | 2015-11-12 10:12:04 -0800 | [diff] [blame] | 248 | } else if (hasName()) { |
| 249 | // named port |
| 250 | return String.format("[%s](%d)", name, number); |
| 251 | } else { |
| 252 | // unsigned decimal string |
| 253 | return name; |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 254 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public int hashCode() { |
Sho SHIMIZU | d5c66af | 2015-06-30 09:43:11 -0700 | [diff] [blame] | 259 | return Long.hashCode(number); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 264 | if (this == obj) { |
| 265 | return true; |
| 266 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 267 | if (obj instanceof PortNumber) { |
| 268 | final PortNumber other = (PortNumber) obj; |
| 269 | return this.number == other.number; |
| 270 | } |
| 271 | return false; |
| 272 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 273 | } |