blob: 6adf1f3229462b1275bc309479c0ea540439af29 [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...
81 return true;
82 }
83
84 // else if we have [x,y] cached in meta data, use that...
Simon Hunta5487ad2016-06-16 13:10:41 -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;
89 return;
90 }
91
Simon Hunt3a6eec02015-02-09 21:16:43 -080092 // if this is a node update (not a node add).. skip randomizer
93 if (forUpdate) {
94 return;
95 }
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
123 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
124 angular.extend(node, xy);
125 }
126
Simon Huntfd7106c2016-02-09 15:05:26 -0800127 function setLongLat(node) {
128 var loc = node.location,
129 coord;
130
Thomas Vachuskac67a9912018-03-06 14:37:45 -0800131 if (loc) {
132 coord = loc.locType === 'geo' ? coordFromLngLat(loc) : coordFromXY(loc);
Simon Huntfd7106c2016-02-09 15:05:26 -0800133 node.fixed = true;
134 node.px = node.x = coord[0];
135 node.py = node.y = coord[1];
136 return true;
137 }
138 }
139
140 function resetAllLocations() {
141 nodes.forEach(function (d) {
142 setLongLat(d);
143 });
144 }
145
Simon Hunt3a6eec02015-02-09 21:16:43 -0800146 function mkSvgCls(dh, t, on) {
147 var ndh = 'node ' + dh,
148 ndht = t ? ndh + ' ' + t : ndh;
149 return on ? ndht + ' online' : ndht;
150 }
151
152 function createDeviceNode(device) {
153 var node = device;
154
155 // Augment as needed...
156 node.class = 'device';
157 node.svgClass = mkSvgCls('device', device.type, device.online);
158 positionNode(node);
159 return node;
160 }
161
162 function createHostNode(host) {
163 var node = host;
164
165 // Augment as needed...
166 node.class = 'host';
167 if (!node.type) {
168 node.type = 'endstation';
169 }
170 node.svgClass = mkSvgCls('host', node.type);
171 positionNode(node);
172 return node;
173 }
174
Simon Hunt12c79ed2017-09-12 11:58:44 -0700175 function createHostLink(hostId, devId, devPort) {
176 var linkKey = hostId + '/0-' + devId + '/' + devPort,
177 lnk = linkEndPoints(hostId, devId);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800178
179 if (!lnk) {
180 return null;
181 }
182
183 // Synthesize link ...
184 angular.extend(lnk, {
Simon Hunt12c79ed2017-09-12 11:58:44 -0700185 key: linkKey,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800186 class: 'link',
Simon Hunt5f361082015-02-25 11:36:38 -0800187 // NOTE: srcPort left undefined (host end of the link)
Simon Hunt12c79ed2017-09-12 11:58:44 -0700188 tgtPort: devPort,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800189
190 type: function () { return 'hostLink'; },
Simon Hunt219f0772016-02-09 10:58:49 -0800191 expected: function () { return true; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800192 online: function () {
193 // hostlink target is edge switch
194 return lnk.target.online;
195 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100196 linkWidth: function () { return 1; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800197 });
198 return lnk;
199 }
200
201 function createLink(link) {
202 var lnk = linkEndPoints(link.src, link.dst);
203
204 if (!lnk) {
205 return null;
206 }
207
208 angular.extend(lnk, {
209 key: link.id,
210 class: 'link',
211 fromSource: link,
Simon Hunt5f361082015-02-25 11:36:38 -0800212 srcPort: link.srcPort,
213 tgtPort: link.dstPort,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700214 position: {
215 x1: 0,
216 y1: 0,
217 x2: 0,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100218 y2: 0,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700219 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800220
221 // functions to aggregate dual link state
222 type: function () {
223 var s = lnk.fromSource,
224 t = lnk.fromTarget;
225 return (s && s.type) || (t && t.type) || defaultLinkType;
226 },
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800227 expected: function () {
228 var s = lnk.fromSource,
229 t = lnk.fromTarget;
230 return (s && s.expected) && (t && t.expected);
231 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800232 online: function () {
233 var s = lnk.fromSource,
234 t = lnk.fromTarget,
235 both = lnk.source.online && lnk.target.online;
Simon Hunt15813b22016-01-29 08:29:08 -0800236 return both && (s && s.online) && (t && t.online);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800237 },
238 linkWidth: function () {
239 var s = lnk.fromSource,
240 t = lnk.fromTarget,
241 ws = (s && s.linkWidth) || 0,
242 wt = (t && t.linkWidth) || 0;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700243 return lnk.position.multiLink ? 5 : Math.max(ws, wt);
Simon Hunt5c1a9382016-06-01 19:35:35 -0700244 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100245 extra: link.extra,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800246 });
247 return lnk;
248 }
249
250
251 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800252 var srcNode = lu[srcId],
253 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800254 sMiss = !srcNode ? missMsg('src', srcId) : '',
255 dMiss = !dstNode ? missMsg('dst', dstId) : '';
256
257 if (sMiss || dMiss) {
258 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100259 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800260 return null;
261 }
Steven Burrowsa3fca812016-10-14 15:11:04 -0500262
Simon Hunt3a6eec02015-02-09 21:16:43 -0800263 return {
264 source: srcNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100265 target: dstNode,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800266 };
267 }
268
269 function missMsg(what, id) {
270 return '\n[' + what + '] "' + id + '" missing';
271 }
272
Simon Huntdc6adea2015-02-09 22:29:36 -0800273
274 function makeNodeKey(d, what) {
275 var port = what + 'Port';
276 return d[what] + '/' + d[port];
277 }
278
279 function makeLinkKey(d, flipped) {
280 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
281 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
282 return one + '-' + two;
283 }
284
285 function findLinkById(id) {
286 // check to see if this is a reverse lookup, else default to given id
287 var key = rlk[id] || id;
288 return key && lu[key];
289 }
290
291 function findLink(linkData, op) {
292 var key = makeLinkKey(linkData),
293 keyrev = makeLinkKey(linkData, 1),
294 link = lu[key],
295 linkRev = lu[keyrev],
296 result = {},
297 ldata = link || linkRev,
298 rawLink;
299
300 if (op === 'add') {
301 if (link) {
302 // trying to add a link that we already know about
303 result.ldata = link;
304 result.badLogic = 'addLink: link already added';
305
306 } else if (linkRev) {
307 // we found the reverse of the link to be added
308 result.ldata = linkRev;
309 if (linkRev.fromTarget) {
310 result.badLogic = 'addLink: link already added';
311 }
312 }
313 } else if (op === 'update') {
314 if (!ldata) {
315 result.badLogic = 'updateLink: link not found';
316 } else {
317 rawLink = link ? ldata.fromSource : ldata.fromTarget;
318 result.updateWith = function (data) {
319 angular.extend(rawLink, data);
320 api.restyleLinkElement(ldata);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100321 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800322 }
323 } else if (op === 'remove') {
324 if (!ldata) {
325 result.badLogic = 'removeLink: link not found';
326 } else {
327 rawLink = link ? ldata.fromSource : ldata.fromTarget;
328
329 if (!rawLink) {
330 result.badLogic = 'removeLink: link not found';
331
332 } else {
333 result.removeRawLink = function () {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700334 // remove link out of aggregate linksByDevice list
335 var linksForDevPair = linksByDevice[ldata.devicePair],
336 rmvIdx = fs.find(ldata.key, linksForDevPair, 'key');
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700337 if (rmvIdx >= 0) {
338 linksForDevPair.splice(rmvIdx, 1);
339 }
340 ldata.position.multilink = linksForDevPair.length >= 5;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700341
Simon Huntdc6adea2015-02-09 22:29:36 -0800342 if (link) {
343 // remove fromSource
344 ldata.fromSource = null;
345 if (ldata.fromTarget) {
346 // promote target into source position
347 ldata.fromSource = ldata.fromTarget;
348 ldata.fromTarget = null;
349 ldata.key = keyrev;
350 delete lu[key];
351 lu[keyrev] = ldata;
352 delete rlk[keyrev];
353 }
354 } else {
355 // remove fromTarget
356 ldata.fromTarget = null;
357 delete rlk[keyrev];
358 }
359 if (ldata.fromSource) {
360 api.restyleLinkElement(ldata);
361 } else {
362 api.removeLinkElement(ldata);
363 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100364 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800365 }
366 }
367 }
368 return result;
369 }
370
371 function findDevices(offlineOnly) {
372 var a = [];
373 nodes.forEach(function (d) {
374 if (d.class === 'device' && !(offlineOnly && d.online)) {
375 a.push(d);
376 }
377 });
378 return a;
379 }
380
Simon Hunt10618f62017-06-15 19:30:52 -0700381 function findHosts() {
382 var hosts = [];
383 nodes.forEach(function (d) {
384 if (d.class === 'host') {
385 hosts.push(d);
386 }
387 });
388 return hosts;
389 }
390
Simon Huntdc6adea2015-02-09 22:29:36 -0800391 function findAttachedHosts(devId) {
392 var hosts = [];
393 nodes.forEach(function (d) {
394 if (d.class === 'host' && d.cp.device === devId) {
395 hosts.push(d);
396 }
397 });
398 return hosts;
399 }
400
401 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700402 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800403 links.forEach(function (d) {
404 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700405 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800406 }
407 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700408 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800409 }
410
Simon Hunt86b7c882015-04-02 23:06:08 -0700411 // returns one-way links or where the internal link types differ
412 function findBadLinks() {
413 var lnks = [],
414 src, tgt;
415 links.forEach(function (d) {
416 // NOTE: skip edge links, which are synthesized
417 if (d.type() !== 'hostLink') {
418 delete d.bad;
419 src = d.fromSource;
420 tgt = d.fromTarget;
421 if (src && !tgt) {
422 d.bad = 'missing link';
423 } else if (src.type !== tgt.type) {
424 d.bad = 'type mismatch';
425 }
426 if (d.bad) {
427 lnks.push(d);
428 }
429 }
430 });
431 return lnks;
432 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800433
Simon Hunt3a6eec02015-02-09 21:16:43 -0800434 // ==========================
435 // Module definition
436
437 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800438 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800439 ['$log', 'FnService', 'RandomService',
440
441 function (_$log_, _fs_, _rnd_) {
442 $log = _$log_;
443 fs = _fs_;
444 rnd = _rnd_;
445
446 function initModel(_api_, _dim_) {
447 api = _api_;
448 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800449 lu = api.network.lookup;
450 rlk = api.network.revLinkToKey;
451 nodes = api.network.nodes;
452 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700453 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800454 }
455
456 function newDim(_dim_) {
457 dim = _dim_;
458 }
459
Simon Huntf542d842015-02-11 16:20:33 -0800460 function destroyModel() { }
461
Simon Hunt3a6eec02015-02-09 21:16:43 -0800462 return {
463 initModel: initModel,
464 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800465 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800466
467 positionNode: positionNode,
Simon Huntfd7106c2016-02-09 15:05:26 -0800468 resetAllLocations: resetAllLocations,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800469 createDeviceNode: createDeviceNode,
470 createHostNode: createHostNode,
471 createHostLink: createHostLink,
472 createLink: createLink,
473 coordFromLngLat: coordFromLngLat,
474 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800475 findLink: findLink,
476 findLinkById: findLinkById,
477 findDevices: findDevices,
Simon Hunt10618f62017-06-15 19:30:52 -0700478 findHosts: findHosts,
Simon Huntdc6adea2015-02-09 22:29:36 -0800479 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700480 findAttachedLinks: findAttachedLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100481 findBadLinks: findBadLinks,
482 };
Simon Hunt3a6eec02015-02-09 21:16:43 -0800483 }]);
484}());