blob: 1084b528be78b5293bb67bf1916c5be533367f2a [file] [log] [blame]
Simon Hunt3a6eec02015-02-09 21:16:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt3a6eec02015-02-09 21:16:43 -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 Model Module.
19 Auxiliary functions for the model of the topology; that is, our internal
20 representations of devices, hosts, links, etc.
21 */
22
23(function () {
24 'use strict';
25
26 // injected refs
Simon Hunt08f841d02015-02-10 14:39:20 -080027 var $log, fs, rnd;
28
29 // api to topoForce
30 var api;
31 /*
32 projection()
33 network {...}
34 restyleLinkElement( ldata )
35 removeLinkElement( ldata )
36 */
Simon Hunt3a6eec02015-02-09 21:16:43 -080037
Simon Huntdc6adea2015-02-09 22:29:36 -080038 // shorthand
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -070039 var lu, rlk, nodes, links, linksByDevice;
Simon Huntdc6adea2015-02-09 22:29:36 -080040
Steven Burrows1c2a9682017-07-14 16:52:46 +010041 var dim; // dimensions of layout [w,h]
Simon Hunt3a6eec02015-02-09 21:16:43 -080042
43 // configuration 'constants'
44 var defaultLinkType = 'direct',
45 nearDist = 15;
46
47
48 function coordFromLngLat(loc) {
49 var p = api.projection();
Simon Huntffbad3b2017-05-16 15:37:51 -070050 return p ? p([loc.longOrX, loc.latOrY]) : [0, 0];
Simon Hunt3a6eec02015-02-09 21:16:43 -080051 }
52
53 function lngLatFromCoord(coord) {
54 var p = api.projection();
55 return p ? p.invert(coord) : [0, 0];
56 }
57
Thomas Vachuskac67a9912018-03-06 14:37:45 -080058 function coordFromXY(loc) {
59 var bgWidth = 1000,
60 bgHeight = 1000;
61
62 var scale = 1000 / bgWidth,
63 yOffset = (1000 - (bgHeight * scale)) / 2;
64
65 // 1000 is a hardcoded HTML value of the SVG element (topo2.html)
66 var x = scale * loc.longOrX,
67 y = (scale * loc.latOrY) + yOffset;
68
69 return [x, y];
70 }
71
Simon Hunt3a6eec02015-02-09 21:16:43 -080072 function positionNode(node, forUpdate) {
73 var meta = node.metaUi,
74 x = meta && meta.x,
75 y = meta && meta.y,
76 xy;
77
Simon Huntfd7106c2016-02-09 15:05:26 -080078 // if the device contains explicit LONG/LAT data, use that to position
79 if (setLongLat(node)) {
80 // indicate we want to update cached meta data...
Thomas Vachuskac616e172018-04-17 16:57:12 -070081 return false;
Simon Huntfd7106c2016-02-09 15:05:26 -080082 }
83
84 // else if we have [x,y] cached in meta data, use that...
Thomas Vachuskac616e172018-04-17 16:57:12 -070085 if (x != undefined && y != undefined) {
Simon Hunt3a6eec02015-02-09 21:16:43 -080086 node.fixed = true;
87 node.px = node.x = x;
88 node.py = node.y = y;
Thomas Vachuskac616e172018-04-17 16:57:12 -070089 return true;
Simon Hunt3a6eec02015-02-09 21:16:43 -080090 }
91
Simon Hunt3a6eec02015-02-09 21:16:43 -080092 // if this is a node update (not a node add).. skip randomizer
Thomas Vachuskac616e172018-04-17 16:57:12 -070093 if (forUpdate && node.x != undefined && node.y != undefined) {
94 return false;
Simon Hunt3a6eec02015-02-09 21:16:43 -080095 }
96
97 // Note: Placing incoming unpinned nodes at exactly the same point
98 // (center of the view) causes them to explode outwards when
99 // the force layout kicks in. So, we spread them out a bit
100 // initially, to provide a more serene layout convergence.
101 // Additionally, if the node is a host, we place it near
102 // the device it is connected to.
103
104 function rand() {
105 return {
106 x: rnd.randDim(dim[0]),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100107 y: rnd.randDim(dim[1]),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800108 };
109 }
110
111 function near(node) {
112 return {
113 x: node.x + nearDist + rnd.spread(nearDist),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100114 y: node.y + nearDist + rnd.spread(nearDist),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800115 };
116 }
117
118 function getDevice(cp) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800119 var d = lu[cp.device];
Simon Hunt3a6eec02015-02-09 21:16:43 -0800120 return d || rand();
121 }
122
Thomas Vachuskac616e172018-04-17 16:57:12 -0700123 node.fixed = false;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800124 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
125 angular.extend(node, xy);
Thomas Vachuskac616e172018-04-17 16:57:12 -0700126 return false;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800127 }
128
Simon Huntfd7106c2016-02-09 15:05:26 -0800129 function setLongLat(node) {
130 var loc = node.location,
131 coord;
132
Thomas Vachuskac616e172018-04-17 16:57:12 -0700133 if (!loc || loc.locType === 'none') {
134 node.fixed = false;
135
136 } else if (loc) {
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800137 coord = loc.locType === 'geo' ? coordFromLngLat(loc) : coordFromXY(loc);
Simon Huntfd7106c2016-02-09 15:05:26 -0800138 node.fixed = true;
139 node.px = node.x = coord[0];
140 node.py = node.y = coord[1];
141 return true;
142 }
Thomas Vachuskac616e172018-04-17 16:57:12 -0700143 return false;
Simon Huntfd7106c2016-02-09 15:05:26 -0800144 }
145
146 function resetAllLocations() {
147 nodes.forEach(function (d) {
148 setLongLat(d);
149 });
150 }
151
Simon Hunt3a6eec02015-02-09 21:16:43 -0800152 function mkSvgCls(dh, t, on) {
153 var ndh = 'node ' + dh,
154 ndht = t ? ndh + ' ' + t : ndh;
155 return on ? ndht + ' online' : ndht;
156 }
157
158 function createDeviceNode(device) {
159 var node = device;
160
161 // Augment as needed...
162 node.class = 'device';
163 node.svgClass = mkSvgCls('device', device.type, device.online);
164 positionNode(node);
165 return node;
166 }
167
168 function createHostNode(host) {
169 var node = host;
170
171 // Augment as needed...
172 node.class = 'host';
173 if (!node.type) {
174 node.type = 'endstation';
175 }
176 node.svgClass = mkSvgCls('host', node.type);
177 positionNode(node);
178 return node;
179 }
180
Simon Hunt12c79ed2017-09-12 11:58:44 -0700181 function createHostLink(hostId, devId, devPort) {
182 var linkKey = hostId + '/0-' + devId + '/' + devPort,
183 lnk = linkEndPoints(hostId, devId);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800184
185 if (!lnk) {
186 return null;
187 }
188
189 // Synthesize link ...
190 angular.extend(lnk, {
Simon Hunt12c79ed2017-09-12 11:58:44 -0700191 key: linkKey,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800192 class: 'link',
Simon Hunt5f361082015-02-25 11:36:38 -0800193 // NOTE: srcPort left undefined (host end of the link)
Simon Hunt12c79ed2017-09-12 11:58:44 -0700194 tgtPort: devPort,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800195
196 type: function () { return 'hostLink'; },
Simon Hunt219f0772016-02-09 10:58:49 -0800197 expected: function () { return true; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800198 online: function () {
199 // hostlink target is edge switch
200 return lnk.target.online;
201 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100202 linkWidth: function () { return 1; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800203 });
204 return lnk;
205 }
206
207 function createLink(link) {
208 var lnk = linkEndPoints(link.src, link.dst);
209
210 if (!lnk) {
211 return null;
212 }
213
214 angular.extend(lnk, {
215 key: link.id,
216 class: 'link',
217 fromSource: link,
Simon Hunt5f361082015-02-25 11:36:38 -0800218 srcPort: link.srcPort,
219 tgtPort: link.dstPort,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700220 position: {
221 x1: 0,
222 y1: 0,
223 x2: 0,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100224 y2: 0,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700225 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800226
227 // functions to aggregate dual link state
228 type: function () {
229 var s = lnk.fromSource,
230 t = lnk.fromTarget;
231 return (s && s.type) || (t && t.type) || defaultLinkType;
232 },
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800233 expected: function () {
234 var s = lnk.fromSource,
235 t = lnk.fromTarget;
236 return (s && s.expected) && (t && t.expected);
237 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800238 online: function () {
239 var s = lnk.fromSource,
240 t = lnk.fromTarget,
241 both = lnk.source.online && lnk.target.online;
Simon Hunt15813b22016-01-29 08:29:08 -0800242 return both && (s && s.online) && (t && t.online);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800243 },
244 linkWidth: function () {
245 var s = lnk.fromSource,
246 t = lnk.fromTarget,
247 ws = (s && s.linkWidth) || 0,
248 wt = (t && t.linkWidth) || 0;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700249 return lnk.position.multiLink ? 5 : Math.max(ws, wt);
Simon Hunt5c1a9382016-06-01 19:35:35 -0700250 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100251 extra: link.extra,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800252 });
253 return lnk;
254 }
255
256
257 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800258 var srcNode = lu[srcId],
259 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800260 sMiss = !srcNode ? missMsg('src', srcId) : '',
261 dMiss = !dstNode ? missMsg('dst', dstId) : '';
262
263 if (sMiss || dMiss) {
264 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100265 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800266 return null;
267 }
Steven Burrowsa3fca812016-10-14 15:11:04 -0500268
Simon Hunt3a6eec02015-02-09 21:16:43 -0800269 return {
270 source: srcNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100271 target: dstNode,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800272 };
273 }
274
275 function missMsg(what, id) {
276 return '\n[' + what + '] "' + id + '" missing';
277 }
278
Simon Huntdc6adea2015-02-09 22:29:36 -0800279
280 function makeNodeKey(d, what) {
281 var port = what + 'Port';
282 return d[what] + '/' + d[port];
283 }
284
285 function makeLinkKey(d, flipped) {
286 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
287 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
288 return one + '-' + two;
289 }
290
291 function findLinkById(id) {
292 // check to see if this is a reverse lookup, else default to given id
293 var key = rlk[id] || id;
294 return key && lu[key];
295 }
296
297 function findLink(linkData, op) {
298 var key = makeLinkKey(linkData),
299 keyrev = makeLinkKey(linkData, 1),
300 link = lu[key],
301 linkRev = lu[keyrev],
302 result = {},
303 ldata = link || linkRev,
304 rawLink;
305
306 if (op === 'add') {
307 if (link) {
308 // trying to add a link that we already know about
309 result.ldata = link;
310 result.badLogic = 'addLink: link already added';
311
312 } else if (linkRev) {
313 // we found the reverse of the link to be added
314 result.ldata = linkRev;
315 if (linkRev.fromTarget) {
316 result.badLogic = 'addLink: link already added';
317 }
318 }
319 } else if (op === 'update') {
320 if (!ldata) {
321 result.badLogic = 'updateLink: link not found';
322 } else {
323 rawLink = link ? ldata.fromSource : ldata.fromTarget;
324 result.updateWith = function (data) {
325 angular.extend(rawLink, data);
326 api.restyleLinkElement(ldata);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100327 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800328 }
329 } else if (op === 'remove') {
330 if (!ldata) {
331 result.badLogic = 'removeLink: link not found';
332 } else {
333 rawLink = link ? ldata.fromSource : ldata.fromTarget;
334
335 if (!rawLink) {
336 result.badLogic = 'removeLink: link not found';
337
338 } else {
339 result.removeRawLink = function () {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700340 // remove link out of aggregate linksByDevice list
341 var linksForDevPair = linksByDevice[ldata.devicePair],
342 rmvIdx = fs.find(ldata.key, linksForDevPair, 'key');
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700343 if (rmvIdx >= 0) {
344 linksForDevPair.splice(rmvIdx, 1);
345 }
346 ldata.position.multilink = linksForDevPair.length >= 5;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700347
Simon Huntdc6adea2015-02-09 22:29:36 -0800348 if (link) {
349 // remove fromSource
350 ldata.fromSource = null;
351 if (ldata.fromTarget) {
352 // promote target into source position
353 ldata.fromSource = ldata.fromTarget;
354 ldata.fromTarget = null;
355 ldata.key = keyrev;
356 delete lu[key];
357 lu[keyrev] = ldata;
358 delete rlk[keyrev];
359 }
360 } else {
361 // remove fromTarget
362 ldata.fromTarget = null;
363 delete rlk[keyrev];
364 }
365 if (ldata.fromSource) {
366 api.restyleLinkElement(ldata);
367 } else {
368 api.removeLinkElement(ldata);
369 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100370 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800371 }
372 }
373 }
374 return result;
375 }
376
377 function findDevices(offlineOnly) {
378 var a = [];
379 nodes.forEach(function (d) {
380 if (d.class === 'device' && !(offlineOnly && d.online)) {
381 a.push(d);
382 }
383 });
384 return a;
385 }
386
Simon Hunt10618f62017-06-15 19:30:52 -0700387 function findHosts() {
388 var hosts = [];
389 nodes.forEach(function (d) {
390 if (d.class === 'host') {
391 hosts.push(d);
392 }
393 });
394 return hosts;
395 }
396
Simon Huntdc6adea2015-02-09 22:29:36 -0800397 function findAttachedHosts(devId) {
398 var hosts = [];
399 nodes.forEach(function (d) {
400 if (d.class === 'host' && d.cp.device === devId) {
401 hosts.push(d);
402 }
403 });
404 return hosts;
405 }
406
407 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700408 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800409 links.forEach(function (d) {
410 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700411 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800412 }
413 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700414 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800415 }
416
Simon Hunt86b7c882015-04-02 23:06:08 -0700417 // returns one-way links or where the internal link types differ
418 function findBadLinks() {
419 var lnks = [],
420 src, tgt;
421 links.forEach(function (d) {
422 // NOTE: skip edge links, which are synthesized
423 if (d.type() !== 'hostLink') {
424 delete d.bad;
425 src = d.fromSource;
426 tgt = d.fromTarget;
427 if (src && !tgt) {
428 d.bad = 'missing link';
429 } else if (src.type !== tgt.type) {
430 d.bad = 'type mismatch';
431 }
432 if (d.bad) {
433 lnks.push(d);
434 }
435 }
436 });
437 return lnks;
438 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800439
Simon Hunt3a6eec02015-02-09 21:16:43 -0800440 // ==========================
441 // Module definition
442
443 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800444 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800445 ['$log', 'FnService', 'RandomService',
446
447 function (_$log_, _fs_, _rnd_) {
448 $log = _$log_;
449 fs = _fs_;
450 rnd = _rnd_;
451
452 function initModel(_api_, _dim_) {
453 api = _api_;
454 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800455 lu = api.network.lookup;
456 rlk = api.network.revLinkToKey;
457 nodes = api.network.nodes;
458 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700459 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800460 }
461
462 function newDim(_dim_) {
463 dim = _dim_;
464 }
465
Simon Huntf542d842015-02-11 16:20:33 -0800466 function destroyModel() { }
467
Simon Hunt3a6eec02015-02-09 21:16:43 -0800468 return {
469 initModel: initModel,
470 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800471 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800472
473 positionNode: positionNode,
Simon Huntfd7106c2016-02-09 15:05:26 -0800474 resetAllLocations: resetAllLocations,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800475 createDeviceNode: createDeviceNode,
476 createHostNode: createHostNode,
477 createHostLink: createHostLink,
478 createLink: createLink,
479 coordFromLngLat: coordFromLngLat,
480 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800481 findLink: findLink,
482 findLinkById: findLinkById,
483 findDevices: findDevices,
Simon Hunt10618f62017-06-15 19:30:52 -0700484 findHosts: findHosts,
Simon Huntdc6adea2015-02-09 22:29:36 -0800485 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700486 findAttachedLinks: findAttachedLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100487 findBadLinks: findBadLinks,
488 };
Simon Hunt3a6eec02015-02-09 21:16:43 -0800489 }]);
490}());