Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | |
| 17 | package org.onosproject.ui.model.topo; |
| 18 | |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 19 | import org.onlab.util.Identifier; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 22 | import org.onosproject.net.ElementId; |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 23 | import org.onosproject.net.HostId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Link; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.LinkKey; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 27 | import org.onosproject.net.region.RegionId; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A canonical representation of an identifier for {@link UiLink}s. |
| 36 | */ |
| 37 | public final class UiLinkId { |
| 38 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 39 | private static final String E_PORT_NULL = "Port number cannot be null"; |
| 40 | private static final String E_DEVICE_ID_NULL = "Device ID cannot be null"; |
| 41 | private static final String E_REGION_ID_NULL = "Region ID cannot be null"; |
| 42 | private static final String E_IDENTICAL = "Region IDs cannot be same"; |
| 43 | |
| 44 | private static final Comparator<RegionId> REGION_ID_COMPARATOR = |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 45 | Comparator.comparing(Identifier::toString); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 46 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Designates the directionality of an underlying (uni-directional) link. |
| 49 | */ |
| 50 | public enum Direction { |
| 51 | A_TO_B, |
| 52 | B_TO_A |
| 53 | } |
| 54 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 55 | static final String CP_DELIMITER = "~"; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 56 | static final String ID_PORT_DELIMITER = "/"; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 57 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 58 | private final RegionId regionA; |
| 59 | private final ElementId elementA; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 60 | private final PortNumber portA; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 61 | |
| 62 | private final RegionId regionB; |
| 63 | private final ElementId elementB; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 64 | private final PortNumber portB; |
| 65 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 66 | private final String idStr; |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 67 | private final String idA; |
| 68 | private final String idB; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Creates a UI link identifier. It is expected that A comes before B when |
| 72 | * the two identifiers are naturally sorted, thus providing a representation |
| 73 | * which is invariant to whether A or B is source or destination of the |
| 74 | * underlying link. |
| 75 | * |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 76 | * @param a first element ID |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 77 | * @param pa first element port |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 78 | * @param b second element ID |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 79 | * @param pb second element port |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 80 | */ |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 81 | private UiLinkId(ElementId a, PortNumber pa, ElementId b, PortNumber pb) { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 82 | elementA = a; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 83 | portA = pa; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 84 | elementB = b; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 85 | portB = pb; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 86 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 87 | regionA = null; |
| 88 | regionB = null; |
| 89 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 90 | // NOTE: for edgelinks, hosts are always element A |
| 91 | idA = (a instanceof HostId) ? a.toString() : a + ID_PORT_DELIMITER + pa; |
| 92 | idB = b + ID_PORT_DELIMITER + pb; |
| 93 | idStr = idA + CP_DELIMITER + idB; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Creates a UI link identifier. It is expected that A comes before B when |
| 98 | * the two identifiers are naturally sorted. |
| 99 | * |
| 100 | * @param a first region ID |
| 101 | * @param b second region ID |
| 102 | */ |
| 103 | private UiLinkId(RegionId a, RegionId b) { |
| 104 | regionA = a; |
| 105 | regionB = b; |
| 106 | |
| 107 | elementA = null; |
| 108 | elementB = null; |
| 109 | portA = null; |
| 110 | portB = null; |
| 111 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 112 | idA = a.toString(); |
| 113 | idB = b.toString(); |
| 114 | idStr = idA + CP_DELIMITER + idB; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates a UI link identifier, with region at one end and a device/port |
| 119 | * at the other. |
| 120 | * |
| 121 | * @param r region ID |
| 122 | * @param d device ID |
| 123 | * @param p port number |
| 124 | */ |
| 125 | private UiLinkId(RegionId r, DeviceId d, PortNumber p) { |
| 126 | regionA = r; |
| 127 | elementB = d; |
| 128 | portB = p; |
| 129 | |
| 130 | regionB = null; |
| 131 | elementA = null; |
| 132 | portA = null; |
| 133 | |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 134 | idA = r.toString(); |
| 135 | idB = elementB + ID_PORT_DELIMITER + portB; |
| 136 | idStr = idA + CP_DELIMITER + idB; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public String toString() { |
| 141 | return idStr; |
| 142 | } |
| 143 | |
| 144 | /** |
Simon Hunt | b7fd080 | 2016-10-27 12:21:40 -0700 | [diff] [blame] | 145 | * String representation of endpoint A. |
| 146 | * |
| 147 | * @return string rep of endpoint A |
| 148 | */ |
| 149 | public String idA() { |
| 150 | return idA; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * String representation of endpoint B. |
| 155 | * |
| 156 | * @return string rep of endpoint B |
| 157 | */ |
| 158 | public String idB() { |
| 159 | return idB; |
| 160 | } |
| 161 | |
| 162 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 163 | * Returns the identifier of the first element. Note that the returned |
| 164 | * value will be null if this identifier is for a region-region link. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 165 | * |
| 166 | * @return first element identity |
| 167 | */ |
| 168 | public ElementId elementA() { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 169 | return elementA; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 173 | * Returns the port of the first element. Note that the returned |
| 174 | * value will be null if this identifier is for a region-region link. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 175 | * |
| 176 | * @return first element port |
| 177 | */ |
| 178 | public PortNumber portA() { |
| 179 | return portA; |
| 180 | } |
| 181 | |
| 182 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 183 | * Returns the identifier of the second element. Note that the returned |
| 184 | * value will be null if this identifier is for a region-region link. |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 185 | * |
| 186 | * @return second element identity |
| 187 | */ |
| 188 | public ElementId elementB() { |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 189 | return elementB; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 192 | /** |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 193 | * Returns the port of the second element. Note that the returned |
| 194 | * value will be null if this identifier is for a region-region link. |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 195 | * |
| 196 | * @return second element port |
| 197 | */ |
| 198 | public PortNumber portB() { |
| 199 | return portB; |
| 200 | } |
| 201 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Returns the identity of the first region. Note that the returned value |
| 204 | * will be null if this identifier is for a device-device or device-host |
| 205 | * link. |
| 206 | * |
| 207 | * @return first region ID |
| 208 | */ |
| 209 | public RegionId regionA() { |
| 210 | return regionA; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns the identity of the second region. Note that the returned value |
| 215 | * will be null if this identifier is for a device-device or device-host |
| 216 | * link. |
| 217 | * |
| 218 | * @return second region ID |
| 219 | */ |
| 220 | public RegionId regionB() { |
| 221 | return regionB; |
| 222 | } |
| 223 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public boolean equals(Object o) { |
| 226 | if (this == o) { |
| 227 | return true; |
| 228 | } |
| 229 | if (o == null || getClass() != o.getClass()) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | UiLinkId uiLinkId = (UiLinkId) o; |
| 234 | return idStr.equals(uiLinkId.idStr); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int hashCode() { |
| 239 | return idStr.hashCode(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns the direction of the given link, or null if this link ID does |
| 244 | * not correspond to the given link. |
| 245 | * |
| 246 | * @param link the link to examine |
| 247 | * @return corresponding direction |
| 248 | */ |
| 249 | Direction directionOf(Link link) { |
| 250 | ConnectPoint src = link.src(); |
| 251 | ElementId srcId = src.elementId(); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 252 | return elementA.equals(srcId) ? Direction.A_TO_B |
| 253 | : elementB.equals(srcId) ? Direction.B_TO_A |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 254 | : null; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Generates the canonical link identifier for the given link. |
| 259 | * |
| 260 | * @param link link for which the identifier is required |
| 261 | * @return link identifier |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 262 | * @throws NullPointerException if src or dst connect point is null |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 263 | */ |
| 264 | public static UiLinkId uiLinkId(Link link) { |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 265 | return canonicalizeIdentifier(link.src(), link.dst()); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Creates the canonical link identifier from the given link key. |
| 270 | * |
| 271 | * @param lk link key |
| 272 | * @return equivalent link identifier |
| 273 | * @throws NullPointerException if src or dst connect point is null |
| 274 | */ |
| 275 | public static UiLinkId uiLinkId(LinkKey lk) { |
| 276 | return canonicalizeIdentifier(lk.src(), lk.dst()); |
| 277 | } |
| 278 | |
| 279 | private static UiLinkId canonicalizeIdentifier(ConnectPoint src, ConnectPoint dst) { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 280 | if (src == null || dst == null) { |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 281 | throw new NullPointerException( |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 282 | "null src or dst connect point (illegal for UiLinkId)"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 283 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 284 | ElementId srcId = src.elementId(); |
| 285 | ElementId dstId = dst.elementId(); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 286 | |
| 287 | // canonicalize |
| 288 | int comp = srcId.toString().compareTo(dstId.toString()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 289 | return comp <= 0 ? new UiLinkId(srcId, src.port(), dstId, dst.port()) |
| 290 | : new UiLinkId(dstId, dst.port(), srcId, src.port()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 291 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Generates the canonical link identifier for a link between the |
| 295 | * specified region nodes. |
| 296 | * |
| 297 | * @param one the first region ID |
| 298 | * @param two the second region ID |
| 299 | * @return link identifier |
| 300 | * @throws NullPointerException if any of the required fields are null |
| 301 | * @throws IllegalArgumentException if the identifiers are identical |
| 302 | */ |
| 303 | public static UiLinkId uiLinkId(RegionId one, RegionId two) { |
| 304 | checkNotNull(one, E_REGION_ID_NULL); |
| 305 | checkNotNull(two, E_REGION_ID_NULL); |
| 306 | checkArgument(!one.equals(two), E_IDENTICAL); |
| 307 | |
| 308 | boolean flip = REGION_ID_COMPARATOR.compare(one, two) > 0; |
| 309 | return flip ? new UiLinkId(two, one) : new UiLinkId(one, two); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Generates the canonical link identifier for a link between the specified |
| 314 | * region and device/port. |
| 315 | * |
| 316 | * @param regionId region ID |
| 317 | * @param deviceId device ID |
| 318 | * @param portNumber port number |
| 319 | * @return link identifier |
| 320 | * @throws NullPointerException if any of the required fields are null |
| 321 | */ |
| 322 | public static UiLinkId uiLinkId(RegionId regionId, DeviceId deviceId, |
| 323 | PortNumber portNumber) { |
| 324 | checkNotNull(regionId, E_REGION_ID_NULL); |
| 325 | checkNotNull(deviceId, E_DEVICE_ID_NULL); |
| 326 | checkNotNull(portNumber, E_PORT_NULL); |
| 327 | |
| 328 | return new UiLinkId(regionId, deviceId, portNumber); |
| 329 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 330 | } |