Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -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 | */ |
| 16 | |
| 17 | package org.onosproject.ui.model.topo; |
| 18 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 20 | import org.onosproject.net.EdgeLink; |
| 21 | import org.onosproject.net.Link; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 25 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 26 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 27 | /** |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 28 | * Represents a link (line between two elements). This may have one of |
| 29 | * several forms: |
| 30 | * <ul> |
| 31 | * <li> |
| 32 | * An infrastructure link: |
| 33 | * two backing unidirectional links between two devices. |
| 34 | * </li> |
| 35 | * <li> |
| 36 | * An edge link: |
| 37 | * representing the connection between a host and a device. |
| 38 | * </li> |
| 39 | * <li> |
| 40 | * An aggregation link: |
| 41 | * representing multiple underlying UI link instances. |
| 42 | * </li> |
| 43 | * </ul> |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 44 | */ |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 45 | public class UiLink extends UiElement { |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 46 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 47 | private static final String E_UNASSOC = |
| 48 | "backing link not associated with this UI link: "; |
| 49 | |
| 50 | private final UiTopology topology; |
| 51 | private final UiLinkId id; |
| 52 | |
| 53 | /** |
| 54 | * Creates a UI link. |
| 55 | * |
| 56 | * @param topology parent topology |
| 57 | * @param id canonicalized link identifier |
| 58 | */ |
| 59 | public UiLink(UiTopology topology, UiLinkId id) { |
| 60 | this.topology = topology; |
| 61 | this.id = id; |
| 62 | } |
| 63 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 64 | // devices at either end of this link |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 65 | private DeviceId deviceA; |
| 66 | private DeviceId deviceB; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 67 | |
| 68 | // two unidirectional links underlying this link... |
| 69 | private Link linkAtoB; |
| 70 | private Link linkBtoA; |
| 71 | |
| 72 | // ==OR== : private (synthetic) host link |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 73 | private DeviceId edgeDevice; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 74 | private EdgeLink edgeLink; |
| 75 | |
| 76 | // ==OR== : set of underlying UI links that this link aggregates |
| 77 | private Set<UiLink> children; |
| 78 | |
| 79 | |
| 80 | @Override |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 81 | public String toString() { |
| 82 | return toStringHelper(this) |
| 83 | .add("id", id) |
| 84 | .toString(); |
| 85 | } |
| 86 | |
| 87 | @Override |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 88 | protected void destroy() { |
| 89 | deviceA = null; |
| 90 | deviceB = null; |
| 91 | linkAtoB = null; |
| 92 | linkBtoA = null; |
| 93 | edgeLink = null; |
| 94 | if (children != null) { |
| 95 | children.clear(); |
| 96 | children = null; |
| 97 | } |
| 98 | } |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 99 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Returns the canonicalized link identifier for this link. |
| 102 | * |
| 103 | * @return the link identifier |
| 104 | */ |
| 105 | public UiLinkId id() { |
| 106 | return id; |
| 107 | } |
| 108 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public String idAsString() { |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 111 | return id.toString(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Attaches the given backing link to this UI link. This method will |
| 116 | * throw an exception if this UI link is not representative of the |
| 117 | * supplied link. |
| 118 | * |
| 119 | * @param link backing link to attach |
| 120 | * @throws IllegalArgumentException if the link is not appropriate |
| 121 | */ |
| 122 | public void attachBackingLink(Link link) { |
| 123 | UiLinkId.Direction d = id.directionOf(link); |
| 124 | |
| 125 | if (d == UiLinkId.Direction.A_TO_B) { |
| 126 | linkAtoB = link; |
| 127 | deviceA = link.src().deviceId(); |
| 128 | deviceB = link.dst().deviceId(); |
| 129 | |
| 130 | } else if (d == UiLinkId.Direction.B_TO_A) { |
| 131 | linkBtoA = link; |
| 132 | deviceB = link.src().deviceId(); |
| 133 | deviceA = link.dst().deviceId(); |
| 134 | |
| 135 | } else { |
| 136 | throw new IllegalArgumentException(E_UNASSOC + link); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Detaches the given backing link from this UI link, returning true if the |
| 142 | * reverse link is still attached, or false otherwise. |
| 143 | * |
| 144 | * @param link the backing link to detach |
| 145 | * @return true if other link still attached, false otherwise |
| 146 | * @throws IllegalArgumentException if the link is not appropriate |
| 147 | */ |
| 148 | public boolean detachBackingLink(Link link) { |
| 149 | UiLinkId.Direction d = id.directionOf(link); |
| 150 | if (d == UiLinkId.Direction.A_TO_B) { |
| 151 | linkAtoB = null; |
| 152 | return linkBtoA != null; |
| 153 | } |
| 154 | if (d == UiLinkId.Direction.B_TO_A) { |
| 155 | linkBtoA = null; |
| 156 | return linkAtoB != null; |
| 157 | } |
| 158 | throw new IllegalArgumentException(E_UNASSOC + link); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Attaches the given edge link to this UI link. This method will |
| 163 | * throw an exception if this UI link is not representative of the |
| 164 | * supplied link. |
| 165 | * |
| 166 | * @param elink edge link to attach |
| 167 | * @throws IllegalArgumentException if the link is not appropriate |
| 168 | */ |
| 169 | public void attachEdgeLink(EdgeLink elink) { |
| 170 | UiLinkId.Direction d = id.directionOf(elink); |
| 171 | // Expected direction of edge links is A-to-B (Host to device) |
| 172 | // but checking not null is sufficient |
| 173 | if (d == null) { |
| 174 | throw new IllegalArgumentException(E_UNASSOC + elink); |
| 175 | } |
| 176 | |
| 177 | edgeLink = elink; |
| 178 | edgeDevice = elink.hostLocation().deviceId(); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 179 | } |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 180 | |
| 181 | |
| 182 | /** |
| 183 | * Returns the identity of device A. |
| 184 | * |
| 185 | * @return device A ID |
| 186 | */ |
| 187 | public DeviceId deviceA() { |
| 188 | return deviceA; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the identity of device B. |
| 193 | * |
| 194 | * @return device B ID |
| 195 | */ |
| 196 | public DeviceId deviceB() { |
| 197 | return deviceB; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns backing link from A to B. |
| 202 | * |
| 203 | * @return backing link A to B |
| 204 | */ |
| 205 | public Link linkAtoB() { |
| 206 | return linkAtoB; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns backing link from B to A. |
| 211 | * |
| 212 | * @return backing link B to A |
| 213 | */ |
| 214 | public Link linkBtoA() { |
| 215 | return linkBtoA; |
| 216 | } |
| 217 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 218 | } |