blob: 457567873eb956b3e3940ca86e02d66e0e4aabbb [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
62
63 @Override
64 protected void destroy() {
65 deviceA = null;
66 deviceB = null;
67 portA = null;
68 portB = null;
69 linkAtoB = null;
70 linkBtoA = null;
71 }
72
73
74 /**
75 * Attaches the given backing link to this UI link. This method will
76 * throw an exception if this UI link is not representative of the
77 * supplied link.
78 *
79 * @param link backing link to attach
80 * @throws IllegalArgumentException if the link is not appropriate
81 */
82 public void attachBackingLink(Link link) {
83 UiLinkId.Direction d = id.directionOf(link);
84
85 if (d == UiLinkId.Direction.A_TO_B) {
86 linkAtoB = link;
87 deviceA = link.src().deviceId();
88 portA = link.src().port();
89 deviceB = link.dst().deviceId();
90 portB = link.dst().port();
91
92 } else if (d == UiLinkId.Direction.B_TO_A) {
93 linkBtoA = link;
94 deviceB = link.src().deviceId();
95 portB = link.src().port();
96 deviceA = link.dst().deviceId();
97 portA = link.dst().port();
98
99 } else {
100 throw new IllegalArgumentException(E_UNASSOC + link);
101 }
102 }
103
104 /**
105 * Detaches the given backing link from this UI link, returning true if the
106 * reverse link is still attached, or false otherwise.
107 *
108 * @param link the backing link to detach
109 * @return true if other link still attached, false otherwise
110 * @throws IllegalArgumentException if the link is not appropriate
111 */
112 public boolean detachBackingLink(Link link) {
113 UiLinkId.Direction d = id.directionOf(link);
114 if (d == UiLinkId.Direction.A_TO_B) {
115 linkAtoB = null;
116 return linkBtoA != null;
117 }
118 if (d == UiLinkId.Direction.B_TO_A) {
119 linkBtoA = null;
120 return linkAtoB != null;
121 }
122 throw new IllegalArgumentException(E_UNASSOC + link);
123 }
124
125
126 /**
127 * Returns the identity of device A.
128 *
129 * @return device A ID
130 */
131 public DeviceId deviceA() {
132 return deviceA;
133 }
134
135 /**
136 * Returns the port number of device A.
137 *
138 * @return port A
139 */
140 public PortNumber portA() {
141 return portA;
142 }
143
144 /**
145 * Returns the identity of device B.
146 *
147 * @return device B ID
148 */
149 public DeviceId deviceB() {
150 return deviceB;
151 }
152
153 /**
154 * Returns the port number of device B.
155 *
156 * @return port B
157 */
158 public PortNumber portB() {
159 return portB;
160 }
161
162 /**
163 * Returns backing link from A to B.
164 *
165 * @return backing link A to B
166 */
167 public Link linkAtoB() {
168 return linkAtoB;
169 }
170
171 /**
172 * Returns backing link from B to A.
173 *
174 * @return backing link B to A
175 */
176 public Link linkBtoA() {
177 return linkBtoA;
178 }
179
180}