blob: 916944c7410f874e7bae36b76d81a77180770448 [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunt5f6dbf82016-03-30 08:53:33 -07003 *
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
17package org.onosproject.ui.model.topo;
18
Simon Huntc0f20c12016-05-09 09:30:20 -070019import org.onosproject.net.DeviceId;
Simon Hunt23fb1352016-04-11 12:15:19 -070020import org.onosproject.net.EdgeLink;
21import org.onosproject.net.Link;
22
23import java.util.Set;
24
Simon Huntc0f20c12016-05-09 09:30:20 -070025import static com.google.common.base.MoreObjects.toStringHelper;
26
Simon Hunt5f6dbf82016-03-30 08:53:33 -070027/**
Simon Huntc0f20c12016-05-09 09:30:20 -070028 * 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 Hunt5f6dbf82016-03-30 08:53:33 -070044 */
Simon Huntcda9c032016-04-11 10:32:54 -070045public class UiLink extends UiElement {
Simon Hunt23fb1352016-04-11 12:15:19 -070046
Simon Huntc0f20c12016-05-09 09:30:20 -070047 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 Hunt23fb1352016-04-11 12:15:19 -070064 // devices at either end of this link
Simon Huntc0f20c12016-05-09 09:30:20 -070065 private DeviceId deviceA;
66 private DeviceId deviceB;
Simon Hunt23fb1352016-04-11 12:15:19 -070067
68 // two unidirectional links underlying this link...
69 private Link linkAtoB;
70 private Link linkBtoA;
71
72 // ==OR== : private (synthetic) host link
Simon Huntc0f20c12016-05-09 09:30:20 -070073 private DeviceId edgeDevice;
Simon Hunt23fb1352016-04-11 12:15:19 -070074 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 Huntc0f20c12016-05-09 09:30:20 -070081 public String toString() {
82 return toStringHelper(this)
83 .add("id", id)
84 .toString();
85 }
86
87 @Override
Simon Hunt23fb1352016-04-11 12:15:19 -070088 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 Hunt642bc452016-05-04 19:34:45 -070099
Simon Huntc0f20c12016-05-09 09:30:20 -0700100 /**
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 Hunt642bc452016-05-04 19:34:45 -0700109 @Override
110 public String idAsString() {
Simon Huntc0f20c12016-05-09 09:30:20 -0700111 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 Hunt642bc452016-05-04 19:34:45 -0700179 }
Simon Hunt58a0dd02016-05-17 11:54:23 -0700180
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 Hunt5f6dbf82016-03-30 08:53:33 -0700218}