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 | |
| 19 | import org.onosproject.net.ConnectPoint; |
| 20 | import org.onosproject.net.ElementId; |
| 21 | import org.onosproject.net.Link; |
| 22 | |
| 23 | /** |
| 24 | * A canonical representation of an identifier for {@link UiLink}s. |
| 25 | */ |
| 26 | public final class UiLinkId { |
| 27 | |
| 28 | /** |
| 29 | * Designates the directionality of an underlying (uni-directional) link. |
| 30 | */ |
| 31 | public enum Direction { |
| 32 | A_TO_B, |
| 33 | B_TO_A |
| 34 | } |
| 35 | |
| 36 | private static final String ID_DELIMITER = "~"; |
| 37 | |
| 38 | private final ElementId idA; |
| 39 | private final ElementId idB; |
| 40 | private final String idStr; |
| 41 | |
| 42 | /** |
| 43 | * Creates a UI link identifier. It is expected that A comes before B when |
| 44 | * the two identifiers are naturally sorted, thus providing a representation |
| 45 | * which is invariant to whether A or B is source or destination of the |
| 46 | * underlying link. |
| 47 | * |
| 48 | * @param a first element ID |
| 49 | * @param b second element ID |
| 50 | */ |
| 51 | private UiLinkId(ElementId a, ElementId b) { |
| 52 | idA = a; |
| 53 | idB = b; |
| 54 | |
| 55 | idStr = a.toString() + ID_DELIMITER + b.toString(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return idStr; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the identifier of the first element. |
| 65 | * |
| 66 | * @return first element identity |
| 67 | */ |
| 68 | public ElementId elementA() { |
| 69 | return idA; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the identifier of the second element. |
| 74 | * |
| 75 | * @return second element identity |
| 76 | */ |
| 77 | public ElementId elementB() { |
| 78 | return idB; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean equals(Object o) { |
| 83 | if (this == o) { |
| 84 | return true; |
| 85 | } |
| 86 | if (o == null || getClass() != o.getClass()) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | UiLinkId uiLinkId = (UiLinkId) o; |
| 91 | return idStr.equals(uiLinkId.idStr); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return idStr.hashCode(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns the direction of the given link, or null if this link ID does |
| 101 | * not correspond to the given link. |
| 102 | * |
| 103 | * @param link the link to examine |
| 104 | * @return corresponding direction |
| 105 | */ |
| 106 | Direction directionOf(Link link) { |
| 107 | ConnectPoint src = link.src(); |
| 108 | ElementId srcId = src.elementId(); |
| 109 | return idA.equals(srcId) ? Direction.A_TO_B |
| 110 | : idB.equals(srcId) ? Direction.B_TO_A |
| 111 | : null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Generates the canonical link identifier for the given link. |
| 116 | * |
| 117 | * @param link link for which the identifier is required |
| 118 | * @return link identifier |
| 119 | * @throws NullPointerException if any of the required fields are null |
| 120 | */ |
| 121 | public static UiLinkId uiLinkId(Link link) { |
| 122 | ConnectPoint src = link.src(); |
| 123 | ConnectPoint dst = link.dst(); |
| 124 | if (src == null || dst == null) { |
| 125 | throw new NullPointerException("null src or dst connect point: " + link); |
| 126 | } |
| 127 | |
| 128 | ElementId srcId = src.elementId(); |
| 129 | ElementId dstId = dst.elementId(); |
| 130 | if (srcId == null || dstId == null) { |
| 131 | throw new NullPointerException("null element ID in connect point: " + link); |
| 132 | } |
| 133 | |
| 134 | // canonicalize |
| 135 | int comp = srcId.toString().compareTo(dstId.toString()); |
| 136 | return comp <= 0 ? new UiLinkId(srcId, dstId) |
| 137 | : new UiLinkId(dstId, srcId); |
| 138 | } |
| 139 | } |