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