blob: 26acea5017ee029bde19760e7411c51d8ca9a1ba [file] [log] [blame]
Simon Huntfb8ea1f2015-02-24 21:38:09 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Huntfb8ea1f2015-02-24 21:38:09 -08003 *
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/*
18 ONOS GUI -- Topology Link Module.
19 Functions for highlighting/selecting links
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
Simon Huntcaed0412017-08-12 13:49:17 -070026 var $log, fs, flash, tss;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080027
Simon Hunt0c6b2d32015-03-26 17:46:29 -070028 // internal state
Simon Huntfb8ea1f2015-02-24 21:38:09 -080029 var api,
Simon Hunt1a5301e2015-02-25 15:31:25 -080030 td3,
Simon Huntfb8ea1f2015-02-24 21:38:09 -080031 network,
Steven Burrows1c2a9682017-07-14 16:52:46 +010032 showPorts = true, // enable port highlighting by default
33 enhancedLink = null, // the link over which the mouse is hovering
34 selectedLinks = {}; // the links which are already selected
Simon Huntfb8ea1f2015-02-24 21:38:09 -080035
36 // SVG elements;
Simon Hunt9e2104c2015-02-26 10:48:59 -080037 var svg;
38
Simon Hunte2d9dc72017-08-10 15:21:04 -070039 // function to be replaced by the localization bundle function
40 var topoLion = function (x) {
41 return '#tlink#' + x + '#';
42 };
Simon Huntfb8ea1f2015-02-24 21:38:09 -080043
44 // ======== ALGORITHM TO FIND LINK CLOSEST TO MOUSE ========
45
Simon Hunt0c6b2d32015-03-26 17:46:29 -070046 function getLogicalMousePosition(container) {
47 var m = d3.mouse(container),
Simon Hunt9e2104c2015-02-26 10:48:59 -080048 sc = api.zoomer.scale(),
49 tr = api.zoomer.translate(),
50 mx = (m[0] - tr[0]) / sc,
51 my = (m[1] - tr[1]) / sc;
Steven Burrows1c2a9682017-07-14 16:52:46 +010052 return { x: mx, y: my };
Simon Huntfb8ea1f2015-02-24 21:38:09 -080053 }
54
Simon Hunt5aac2fc2015-06-09 12:34:07 -070055
56 function sq(x) { return x * x; }
57
58 function mdist(p, m) {
59 return Math.sqrt(sq(p.x - m.x) + sq(p.y - m.y));
60 }
61
62 function prox(dist) {
63 return dist / api.zoomer.scale();
64 }
65
66 function computeNearestNode(mouse) {
67 var proximity = prox(30),
Simon Hunt0c6b2d32015-03-26 17:46:29 -070068 nearest = null,
69 minDist;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080070
Simon Hunt5aac2fc2015-06-09 12:34:07 -070071 if (network.nodes.length) {
72 minDist = proximity * 2;
Simon Huntfb8ea1f2015-02-24 21:38:09 -080073
Simon Hunt5aac2fc2015-06-09 12:34:07 -070074 network.nodes.forEach(function (d) {
75 var dist;
76
77 if (!api.showHosts() && d.class === 'host') {
78 return; // skip hidden hosts
79 }
80
Steven Burrows1c2a9682017-07-14 16:52:46 +010081 dist = mdist({ x: d.x, y: d.y }, mouse);
Simon Hunt5aac2fc2015-06-09 12:34:07 -070082 if (dist < minDist && dist < proximity) {
83 minDist = dist;
84 nearest = d;
85 }
86 });
Simon Hunt9e2104c2015-02-26 10:48:59 -080087 }
Simon Hunt5aac2fc2015-06-09 12:34:07 -070088 return nearest;
89 }
90
91
92 function computeNearestLink(mouse) {
93 var proximity = prox(30),
94 nearest = null,
95 minDist;
Simon Hunt9e2104c2015-02-26 10:48:59 -080096
Simon Huntfb8ea1f2015-02-24 21:38:09 -080097 function pdrop(line, mouse) {
98 var x1 = line.x1,
99 y1 = line.y1,
100 x2 = line.x2,
101 y2 = line.y2,
102 x3 = mouse.x,
103 y3 = mouse.y,
104 k = ((y2-y1) * (x3-x1) - (x2-x1) * (y3-y1)) /
105 (sq(y2-y1) + sq(x2-x1)),
106 x4 = x3 - k * (y2-y1),
107 y4 = y3 + k * (x2-x1);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100108 return { x: x4, y: y4 };
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800109 }
110
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800111 function lineHit(line, p, m) {
112 if (p.x < line.x1 && p.x < line.x2) return false;
113 if (p.x > line.x1 && p.x > line.x2) return false;
114 if (p.y < line.y1 && p.y < line.y2) return false;
115 if (p.y > line.y1 && p.y > line.y2) return false;
Simon Hunt5f361082015-02-25 11:36:38 -0800116 // line intersects, but are we close enough?
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800117 return mdist(p, m) <= proximity;
118 }
119
120 if (network.links.length) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800121 minDist = proximity * 2;
122
123 network.links.forEach(function (d) {
Simon Huntf9761452016-11-19 09:06:17 -0800124 var line = d.position,
125 point,
126 hit,
127 dist;
128
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800129 if (!api.showHosts() && d.type() === 'hostLink') {
130 return; // skip hidden host links
131 }
132
Simon Huntf9761452016-11-19 09:06:17 -0800133 if (line) {
134 point = pdrop(line, mouse);
135 hit = lineHit(line, point, mouse);
136 if (hit) {
137 dist = mdist(point, mouse);
138 if (dist < minDist) {
139 minDist = dist;
140 nearest = d;
141 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800142 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800143 }
144 });
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800145 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700146 return nearest;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800147 }
148
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700149 function enhanceLink(ldata) {
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800150 // if the new link is same as old link, do nothing
151 if (enhancedLink && ldata && enhancedLink.key === ldata.key) return;
152
153 // first, unenhance the currently enhanced link
154 if (enhancedLink) {
155 unenhance(enhancedLink);
156 }
157 enhancedLink = ldata;
158 if (enhancedLink) {
159 enhance(enhancedLink);
160 }
161 }
162
163 function unenhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800164 // guard against link element not set
165 if (d.el) {
166 d.el.classed('enhanced', false);
167 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800168 api.portLabelG().selectAll('.portLabel').remove();
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800169 }
170
171 function enhance(d) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800172 var data = [],
173 point;
174
175 // guard against link element not set
176 if (!d.el) return;
177
Simon Huntd5264122015-02-25 10:17:43 -0800178 d.el.classed('enhanced', true);
Simon Hunt1a5301e2015-02-25 15:31:25 -0800179
Simon Hunt8ae474b2015-02-25 15:39:14 -0800180 // Define port label data objects.
181 // NOTE: src port is absent in the case of host-links.
182
Simon Hunt969b3c92015-02-25 18:11:31 -0800183 point = locatePortLabel(d);
184 angular.extend(point, {
Simon Hunt8ae474b2015-02-25 15:39:14 -0800185 id: 'topo-port-tgt',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100186 num: d.tgtPort,
Simon Hunt969b3c92015-02-25 18:11:31 -0800187 });
188 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800189
190 if (d.srcPort) {
Simon Hunt969b3c92015-02-25 18:11:31 -0800191 point = locatePortLabel(d, 1);
192 angular.extend(point, {
Simon Hunt1a5301e2015-02-25 15:31:25 -0800193 id: 'topo-port-src',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100194 num: d.srcPort,
Simon Hunt8ae474b2015-02-25 15:39:14 -0800195 });
Simon Hunt969b3c92015-02-25 18:11:31 -0800196 data.push(point);
Simon Hunt8ae474b2015-02-25 15:39:14 -0800197 }
Simon Hunt1a5301e2015-02-25 15:31:25 -0800198
199 td3.applyPortLabels(data, api.portLabelG());
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800200 }
201
Simon Hunt969b3c92015-02-25 18:11:31 -0800202 function locatePortLabel(link, src) {
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700203 var offset = 32,
204 pos = link.position,
205 nearX = src ? pos.x1 : pos.x2,
Steven Burrowsaefd5862016-10-19 14:20:46 -0500206 nearY = src ? pos.y1 : pos.y2,
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700207 farX = src ? pos.x2 : pos.x1,
Steven Burrowsaefd5862016-10-19 14:20:46 -0500208 farY = src ? pos.y2 : pos.y1;
Simon Hunt969b3c92015-02-25 18:11:31 -0800209
210 function dist(x, y) { return Math.sqrt(x*x + y*y); }
211
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700212 var dx = farX - nearX,
213 dy = farY - nearY,
Simon Hunt969b3c92015-02-25 18:11:31 -0800214 k = offset / dist(dx, dy);
215
Steven Burrows1c2a9682017-07-14 16:52:46 +0100216 return { x: k * dx + nearX, y: k * dy + nearY };
Simon Hunt969b3c92015-02-25 18:11:31 -0800217 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800218
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700219 function selectLink(ldata) {
220 // if the new link is same as old link, do nothing
Prince Pereira46c82d42016-09-19 13:30:50 +0530221 if (d3.event.shiftKey && ldata.el.classed('selected')) {
222 unselLink(ldata);
223 return;
224 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700225
Prince Pereira46c82d42016-09-19 13:30:50 +0530226 if (d3.event.shiftKey && !ldata.el.classed('selected')) {
227 selLink(ldata);
228 return;
229 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700230
Prince Pereira46c82d42016-09-19 13:30:50 +0530231 tss.deselectAll();
232
Simon Huntf9761452016-11-19 09:06:17 -0800233 if (ldata) {
Kavitha Alagesan21314f02016-11-17 11:59:11 +0530234 if (ldata.el.classed('selected')) {
235 unselLink(ldata);
Simon Huntf9761452016-11-19 09:06:17 -0800236 } else {
237 selLink(ldata);
Kavitha Alagesan21314f02016-11-17 11:59:11 +0530238 }
Prince Pereira46c82d42016-09-19 13:30:50 +0530239 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700240 }
241
242 function unselLink(d) {
243 // guard against link element not set
244 if (d.el) {
245 d.el.classed('selected', false);
Prince Pereira46c82d42016-09-19 13:30:50 +0530246 delete selectedLinks[d.key];
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700247 }
248 }
249
250 function selLink(d) {
251 // guard against link element not set
252 if (!d.el) return;
253
254 d.el.classed('selected', true);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100255 selectedLinks[d.key] = { key: d };
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700256
Simon Hunta58d8942017-08-11 12:51:14 -0700257 // TODO: deprecate tov.hooks.modifyLinkData
258 // tps.displayLink(d, tov.hooks.modifyLinkData);
259 // tps.displaySomething();
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700260 }
261
262 // ====== MOUSE EVENT HANDLERS ======
263
264 function mouseMoveHandler() {
265 var mp = getLogicalMousePosition(this),
266 link = computeNearestLink(mp);
267 enhanceLink(link);
268 }
269
270 function mouseClickHandler() {
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700271 var mp, link, node;
Prince Pereira46c82d42016-09-19 13:30:50 +0530272 if (!d3.event.shiftKey) {
273 deselectAllLinks();
274 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700275
276 if (!tss.clickConsumed()) {
277 mp = getLogicalMousePosition(this);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700278 node = computeNearestNode(mp);
279 if (node) {
280 $log.debug('found nearest node:', node.labels[1]);
281 tss.selectObject(node);
282 } else {
283 link = computeNearestLink(mp);
284 selectLink(link);
Prince Pereira46c82d42016-09-19 13:30:50 +0530285 tss.selectObject(link);
Simon Hunt5aac2fc2015-06-09 12:34:07 -0700286 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700287 }
288 }
289
290
291 // ======================
292
Simon Huntfcbde892015-04-16 12:05:28 -0700293 function togglePorts(x) {
294 var kev = (x === 'keyev'),
295 on = kev ? !showPorts : !!x,
Simon Hunte2d9dc72017-08-10 15:21:04 -0700296 what = on ? topoLion('enable') : topoLion('disable'),
297 philite = topoLion('fl_port_highlighting'),
Simon Huntfcbde892015-04-16 12:05:28 -0700298 handler = on ? mouseMoveHandler : null;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800299
Simon Huntfcbde892015-04-16 12:05:28 -0700300 showPorts = on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800301
Simon Huntfcbde892015-04-16 12:05:28 -0700302 if (!on) {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700303 enhanceLink(null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800304 }
305 svg.on('mousemove', handler);
Simon Hunte2d9dc72017-08-10 15:21:04 -0700306 flash.flash(what + ' ' + philite);
Simon Huntfcbde892015-04-16 12:05:28 -0700307 return on;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800308 }
309
Prince Pereira46c82d42016-09-19 13:30:50 +0530310 function deselectAllLinks() {
311
312 if (Object.keys(selectedLinks).length > 0) {
313 network.links.forEach(function (d) {
314 if (selectedLinks[d.key]) {
315 unselLink(d);
316 }
317 });
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700318 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700319 }
320
Simon Hunte2d9dc72017-08-10 15:21:04 -0700321 // invoked after the localization bundle has been received from the server
322 function setLionBundle(bundle) {
323 topoLion = bundle;
324 }
325
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800326 // ==========================
327 // Module definition
328
329 angular.module('ovTopo')
330 .factory('TopoLinkService',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100331 ['$log', 'FnService', 'FlashService', 'TopoSelectService',
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800332
Simon Huntcaed0412017-08-12 13:49:17 -0700333 function (_$log_, _fs_, _flash_, _tss_) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800334 $log = _$log_;
335 fs = _fs_;
Simon Hunt9e2104c2015-02-26 10:48:59 -0800336 flash = _flash_;
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700337 tss = _tss_;
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800338
Simon Hunt9e2104c2015-02-26 10:48:59 -0800339 function initLink(_api_, _td3_) {
340 api = _api_;
341 td3 = _td3_;
342 svg = api.svg;
343 network = api.network;
Bri Prebilic Coled8745462015-06-01 16:08:57 -0700344 if (showPorts && !fs.isMobile()) {
Simon Hunt9e2104c2015-02-26 10:48:59 -0800345 svg.on('mousemove', mouseMoveHandler);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800346 }
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700347 svg.on('click', mouseClickHandler);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800348 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800349
Simon Hunt9e2104c2015-02-26 10:48:59 -0800350 function destroyLink() {
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700351 // unconditionally remove any event handlers
Simon Hunt9e2104c2015-02-26 10:48:59 -0800352 svg.on('mousemove', null);
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700353 svg.on('click', null);
Simon Hunt9e2104c2015-02-26 10:48:59 -0800354 }
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800355
Simon Hunt9e2104c2015-02-26 10:48:59 -0800356 return {
357 initLink: initLink,
358 destroyLink: destroyLink,
Simon Hunt0c6b2d32015-03-26 17:46:29 -0700359 togglePorts: togglePorts,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100360 deselectAllLinks: deselectAllLinks,
Simon Hunte2d9dc72017-08-10 15:21:04 -0700361 setLionBundle: setLionBundle,
Simon Hunt9e2104c2015-02-26 10:48:59 -0800362 };
363 }]);
Simon Huntfb8ea1f2015-02-24 21:38:09 -0800364}());