blob: 75e2560f875a33bfc9ee2c5c52180e2902d591e9 [file] [log] [blame]
Simon Huntc13082f2016-08-03 21:20:23 -07001/*
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
17package org.onosproject.ui.model.topo;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.Link;
21import org.onosproject.net.PortNumber;
22
23/**
24 * Represents a link between two devices; that is, an infrastructure link.
25 */
26public class UiDeviceLink extends UiLink {
27
28 private static final String E_UNASSOC =
29 "backing link not associated with this UI device link: ";
30
31 // devices and ports at either end of this link
32 private DeviceId deviceA;
33 private DeviceId deviceB;
34 private PortNumber portA;
35 private PortNumber portB;
36
37 // two unidirectional links underlying this link...
38 private Link linkAtoB;
39 private Link linkBtoA;
40
41
42 /**
43 * Creates a device to device UI link.
44 *
45 * @param topology parent topology
46 * @param id canonicalized link identifier
47 */
48 public UiDeviceLink(UiTopology topology, UiLinkId id) {
49 super(topology, id);
50 }
51
52 @Override
53 public String endPointA() {
54 return deviceA + UiLinkId.ID_PORT_DELIMITER + portA;
55 }
56
57 @Override
58 public String endPointB() {
59 return deviceB + UiLinkId.ID_PORT_DELIMITER + portB;
60 }
61
Simon Hunt3d712522016-08-11 11:20:44 -070062 @Override
63 public String endPortA() {
Simon Hunt8eac4ae2017-01-20 12:56:45 -080064 return portA == null ? null : portA.toString();
Simon Hunt3d712522016-08-11 11:20:44 -070065 }
66
67 @Override
68 public String endPortB() {
Simon Hunt8eac4ae2017-01-20 12:56:45 -080069 return portB == null ? null : portB.toString();
Simon Hunt3d712522016-08-11 11:20:44 -070070 }
71
Simon Huntc13082f2016-08-03 21:20:23 -070072
73 @Override
74 protected void destroy() {
75 deviceA = null;
76 deviceB = null;
77 portA = null;
78 portB = null;
79 linkAtoB = null;
80 linkBtoA = null;
81 }
82
83
84 /**
85 * Attaches the given backing link to this UI link. This method will
86 * throw an exception if this UI link is not representative of the
87 * supplied link.
88 *
89 * @param link backing link to attach
90 * @throws IllegalArgumentException if the link is not appropriate
91 */
92 public void attachBackingLink(Link link) {
93 UiLinkId.Direction d = id.directionOf(link);
94
95 if (d == UiLinkId.Direction.A_TO_B) {
96 linkAtoB = link;
97 deviceA = link.src().deviceId();
98 portA = link.src().port();
99 deviceB = link.dst().deviceId();
100 portB = link.dst().port();
101
102 } else if (d == UiLinkId.Direction.B_TO_A) {
103 linkBtoA = link;
104 deviceB = link.src().deviceId();
105 portB = link.src().port();
106 deviceA = link.dst().deviceId();
107 portA = link.dst().port();
108
109 } else {
110 throw new IllegalArgumentException(E_UNASSOC + link);
111 }
112 }
113
114 /**
115 * Detaches the given backing link from this UI link, returning true if the
116 * reverse link is still attached, or false otherwise.
117 *
118 * @param link the backing link to detach
119 * @return true if other link still attached, false otherwise
120 * @throws IllegalArgumentException if the link is not appropriate
121 */
122 public boolean detachBackingLink(Link link) {
123 UiLinkId.Direction d = id.directionOf(link);
124 if (d == UiLinkId.Direction.A_TO_B) {
125 linkAtoB = null;
126 return linkBtoA != null;
127 }
128 if (d == UiLinkId.Direction.B_TO_A) {
129 linkBtoA = null;
130 return linkAtoB != null;
131 }
132 throw new IllegalArgumentException(E_UNASSOC + link);
133 }
134
135
136 /**
137 * Returns the identity of device A.
138 *
139 * @return device A ID
140 */
141 public DeviceId deviceA() {
142 return deviceA;
143 }
144
145 /**
146 * Returns the port number of device A.
147 *
148 * @return port A
149 */
150 public PortNumber portA() {
151 return portA;
152 }
153
154 /**
155 * Returns the identity of device B.
156 *
157 * @return device B ID
158 */
159 public DeviceId deviceB() {
160 return deviceB;
161 }
162
163 /**
164 * Returns the port number of device B.
165 *
166 * @return port B
167 */
168 public PortNumber portB() {
169 return portB;
170 }
171
172 /**
173 * Returns backing link from A to B.
174 *
175 * @return backing link A to B
176 */
177 public Link linkAtoB() {
178 return linkAtoB;
179 }
180
181 /**
182 * Returns backing link from B to A.
183 *
184 * @return backing link B to A
185 */
186 public Link linkBtoA() {
187 return linkBtoA;
188 }
189
190}