blob: caaf38bc3ad88da543c9eaf30cd60e3d77aa3464 [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
58 function positionNode(node, forUpdate) {
59 var meta = node.metaUi,
60 x = meta && meta.x,
61 y = meta && meta.y,
62 xy;
63
Simon Huntfd7106c2016-02-09 15:05:26 -080064 // if the device contains explicit LONG/LAT data, use that to position
65 if (setLongLat(node)) {
66 // indicate we want to update cached meta data...
67 return true;
68 }
69
70 // else if we have [x,y] cached in meta data, use that...
Simon Hunta5487ad2016-06-16 13:10:41 -070071 if (x !== undefined && y !== undefined) {
Simon Hunt3a6eec02015-02-09 21:16:43 -080072 node.fixed = true;
73 node.px = node.x = x;
74 node.py = node.y = y;
75 return;
76 }
77
Simon Hunt3a6eec02015-02-09 21:16:43 -080078 // if this is a node update (not a node add).. skip randomizer
79 if (forUpdate) {
80 return;
81 }
82
83 // Note: Placing incoming unpinned nodes at exactly the same point
84 // (center of the view) causes them to explode outwards when
85 // the force layout kicks in. So, we spread them out a bit
86 // initially, to provide a more serene layout convergence.
87 // Additionally, if the node is a host, we place it near
88 // the device it is connected to.
89
90 function rand() {
91 return {
92 x: rnd.randDim(dim[0]),
Steven Burrows1c2a9682017-07-14 16:52:46 +010093 y: rnd.randDim(dim[1]),
Simon Hunt3a6eec02015-02-09 21:16:43 -080094 };
95 }
96
97 function near(node) {
98 return {
99 x: node.x + nearDist + rnd.spread(nearDist),
Steven Burrows1c2a9682017-07-14 16:52:46 +0100100 y: node.y + nearDist + rnd.spread(nearDist),
Simon Hunt3a6eec02015-02-09 21:16:43 -0800101 };
102 }
103
104 function getDevice(cp) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800105 var d = lu[cp.device];
Simon Hunt3a6eec02015-02-09 21:16:43 -0800106 return d || rand();
107 }
108
109 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
110 angular.extend(node, xy);
111 }
112
Simon Huntfd7106c2016-02-09 15:05:26 -0800113 function setLongLat(node) {
114 var loc = node.location,
115 coord;
116
Simon Huntffbad3b2017-05-16 15:37:51 -0700117 if (loc && loc.locType === 'geo') {
Simon Huntfd7106c2016-02-09 15:05:26 -0800118 coord = coordFromLngLat(loc);
119 node.fixed = true;
120 node.px = node.x = coord[0];
121 node.py = node.y = coord[1];
122 return true;
123 }
124 }
125
126 function resetAllLocations() {
127 nodes.forEach(function (d) {
128 setLongLat(d);
129 });
130 }
131
Simon Hunt3a6eec02015-02-09 21:16:43 -0800132 function mkSvgCls(dh, t, on) {
133 var ndh = 'node ' + dh,
134 ndht = t ? ndh + ' ' + t : ndh;
135 return on ? ndht + ' online' : ndht;
136 }
137
138 function createDeviceNode(device) {
139 var node = device;
140
141 // Augment as needed...
142 node.class = 'device';
143 node.svgClass = mkSvgCls('device', device.type, device.online);
144 positionNode(node);
145 return node;
146 }
147
148 function createHostNode(host) {
149 var node = host;
150
151 // Augment as needed...
152 node.class = 'host';
153 if (!node.type) {
154 node.type = 'endstation';
155 }
156 node.svgClass = mkSvgCls('host', node.type);
157 positionNode(node);
158 return node;
159 }
160
161 function createHostLink(host) {
162 var src = host.id,
163 dst = host.cp.device,
164 id = host.ingress,
165 lnk = linkEndPoints(src, dst);
166
167 if (!lnk) {
168 return null;
169 }
170
171 // Synthesize link ...
172 angular.extend(lnk, {
173 key: id,
174 class: 'link',
Simon Hunt5f361082015-02-25 11:36:38 -0800175 // NOTE: srcPort left undefined (host end of the link)
176 tgtPort: host.cp.port,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800177
178 type: function () { return 'hostLink'; },
Simon Hunt219f0772016-02-09 10:58:49 -0800179 expected: function () { return true; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800180 online: function () {
181 // hostlink target is edge switch
182 return lnk.target.online;
183 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100184 linkWidth: function () { return 1; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800185 });
186 return lnk;
187 }
188
189 function createLink(link) {
190 var lnk = linkEndPoints(link.src, link.dst);
191
192 if (!lnk) {
193 return null;
194 }
195
196 angular.extend(lnk, {
197 key: link.id,
198 class: 'link',
199 fromSource: link,
Simon Hunt5f361082015-02-25 11:36:38 -0800200 srcPort: link.srcPort,
201 tgtPort: link.dstPort,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700202 position: {
203 x1: 0,
204 y1: 0,
205 x2: 0,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100206 y2: 0,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700207 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800208
209 // functions to aggregate dual link state
210 type: function () {
211 var s = lnk.fromSource,
212 t = lnk.fromTarget;
213 return (s && s.type) || (t && t.type) || defaultLinkType;
214 },
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800215 expected: function () {
216 var s = lnk.fromSource,
217 t = lnk.fromTarget;
218 return (s && s.expected) && (t && t.expected);
219 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800220 online: function () {
221 var s = lnk.fromSource,
222 t = lnk.fromTarget,
223 both = lnk.source.online && lnk.target.online;
Simon Hunt15813b22016-01-29 08:29:08 -0800224 return both && (s && s.online) && (t && t.online);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800225 },
226 linkWidth: function () {
227 var s = lnk.fromSource,
228 t = lnk.fromTarget,
229 ws = (s && s.linkWidth) || 0,
230 wt = (t && t.linkWidth) || 0;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700231 return lnk.position.multiLink ? 5 : Math.max(ws, wt);
Simon Hunt5c1a9382016-06-01 19:35:35 -0700232 },
Steven Burrows1c2a9682017-07-14 16:52:46 +0100233 extra: link.extra,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800234 });
235 return lnk;
236 }
237
238
239 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800240 var srcNode = lu[srcId],
241 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800242 sMiss = !srcNode ? missMsg('src', srcId) : '',
243 dMiss = !dstNode ? missMsg('dst', dstId) : '';
244
245 if (sMiss || dMiss) {
246 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100247 // logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800248 return null;
249 }
Steven Burrowsa3fca812016-10-14 15:11:04 -0500250
Simon Hunt3a6eec02015-02-09 21:16:43 -0800251 return {
252 source: srcNode,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100253 target: dstNode,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800254 };
255 }
256
257 function missMsg(what, id) {
258 return '\n[' + what + '] "' + id + '" missing';
259 }
260
Simon Huntdc6adea2015-02-09 22:29:36 -0800261
262 function makeNodeKey(d, what) {
263 var port = what + 'Port';
264 return d[what] + '/' + d[port];
265 }
266
267 function makeLinkKey(d, flipped) {
268 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
269 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
270 return one + '-' + two;
271 }
272
273 function findLinkById(id) {
274 // check to see if this is a reverse lookup, else default to given id
275 var key = rlk[id] || id;
276 return key && lu[key];
277 }
278
279 function findLink(linkData, op) {
280 var key = makeLinkKey(linkData),
281 keyrev = makeLinkKey(linkData, 1),
282 link = lu[key],
283 linkRev = lu[keyrev],
284 result = {},
285 ldata = link || linkRev,
286 rawLink;
287
288 if (op === 'add') {
289 if (link) {
290 // trying to add a link that we already know about
291 result.ldata = link;
292 result.badLogic = 'addLink: link already added';
293
294 } else if (linkRev) {
295 // we found the reverse of the link to be added
296 result.ldata = linkRev;
297 if (linkRev.fromTarget) {
298 result.badLogic = 'addLink: link already added';
299 }
300 }
301 } else if (op === 'update') {
302 if (!ldata) {
303 result.badLogic = 'updateLink: link not found';
304 } else {
305 rawLink = link ? ldata.fromSource : ldata.fromTarget;
306 result.updateWith = function (data) {
307 angular.extend(rawLink, data);
308 api.restyleLinkElement(ldata);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100309 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800310 }
311 } else if (op === 'remove') {
312 if (!ldata) {
313 result.badLogic = 'removeLink: link not found';
314 } else {
315 rawLink = link ? ldata.fromSource : ldata.fromTarget;
316
317 if (!rawLink) {
318 result.badLogic = 'removeLink: link not found';
319
320 } else {
321 result.removeRawLink = function () {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700322 // remove link out of aggregate linksByDevice list
323 var linksForDevPair = linksByDevice[ldata.devicePair],
324 rmvIdx = fs.find(ldata.key, linksForDevPair, 'key');
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700325 if (rmvIdx >= 0) {
326 linksForDevPair.splice(rmvIdx, 1);
327 }
328 ldata.position.multilink = linksForDevPair.length >= 5;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700329
Simon Huntdc6adea2015-02-09 22:29:36 -0800330 if (link) {
331 // remove fromSource
332 ldata.fromSource = null;
333 if (ldata.fromTarget) {
334 // promote target into source position
335 ldata.fromSource = ldata.fromTarget;
336 ldata.fromTarget = null;
337 ldata.key = keyrev;
338 delete lu[key];
339 lu[keyrev] = ldata;
340 delete rlk[keyrev];
341 }
342 } else {
343 // remove fromTarget
344 ldata.fromTarget = null;
345 delete rlk[keyrev];
346 }
347 if (ldata.fromSource) {
348 api.restyleLinkElement(ldata);
349 } else {
350 api.removeLinkElement(ldata);
351 }
Steven Burrows1c2a9682017-07-14 16:52:46 +0100352 };
Simon Huntdc6adea2015-02-09 22:29:36 -0800353 }
354 }
355 }
356 return result;
357 }
358
359 function findDevices(offlineOnly) {
360 var a = [];
361 nodes.forEach(function (d) {
362 if (d.class === 'device' && !(offlineOnly && d.online)) {
363 a.push(d);
364 }
365 });
366 return a;
367 }
368
Simon Hunt10618f62017-06-15 19:30:52 -0700369 function findHosts() {
370 var hosts = [];
371 nodes.forEach(function (d) {
372 if (d.class === 'host') {
373 hosts.push(d);
374 }
375 });
376 return hosts;
377 }
378
Simon Huntdc6adea2015-02-09 22:29:36 -0800379 function findAttachedHosts(devId) {
380 var hosts = [];
381 nodes.forEach(function (d) {
382 if (d.class === 'host' && d.cp.device === devId) {
383 hosts.push(d);
384 }
385 });
386 return hosts;
387 }
388
389 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700390 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800391 links.forEach(function (d) {
392 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700393 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800394 }
395 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700396 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800397 }
398
Simon Hunt86b7c882015-04-02 23:06:08 -0700399 // returns one-way links or where the internal link types differ
400 function findBadLinks() {
401 var lnks = [],
402 src, tgt;
403 links.forEach(function (d) {
404 // NOTE: skip edge links, which are synthesized
405 if (d.type() !== 'hostLink') {
406 delete d.bad;
407 src = d.fromSource;
408 tgt = d.fromTarget;
409 if (src && !tgt) {
410 d.bad = 'missing link';
411 } else if (src.type !== tgt.type) {
412 d.bad = 'type mismatch';
413 }
414 if (d.bad) {
415 lnks.push(d);
416 }
417 }
418 });
419 return lnks;
420 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800421
Simon Hunt3a6eec02015-02-09 21:16:43 -0800422 // ==========================
423 // Module definition
424
425 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800426 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800427 ['$log', 'FnService', 'RandomService',
428
429 function (_$log_, _fs_, _rnd_) {
430 $log = _$log_;
431 fs = _fs_;
432 rnd = _rnd_;
433
434 function initModel(_api_, _dim_) {
435 api = _api_;
436 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800437 lu = api.network.lookup;
438 rlk = api.network.revLinkToKey;
439 nodes = api.network.nodes;
440 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700441 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800442 }
443
444 function newDim(_dim_) {
445 dim = _dim_;
446 }
447
Simon Huntf542d842015-02-11 16:20:33 -0800448 function destroyModel() { }
449
Simon Hunt3a6eec02015-02-09 21:16:43 -0800450 return {
451 initModel: initModel,
452 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800453 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800454
455 positionNode: positionNode,
Simon Huntfd7106c2016-02-09 15:05:26 -0800456 resetAllLocations: resetAllLocations,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800457 createDeviceNode: createDeviceNode,
458 createHostNode: createHostNode,
459 createHostLink: createHostLink,
460 createLink: createLink,
461 coordFromLngLat: coordFromLngLat,
462 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800463 findLink: findLink,
464 findLinkById: findLinkById,
465 findDevices: findDevices,
Simon Hunt10618f62017-06-15 19:30:52 -0700466 findHosts: findHosts,
Simon Huntdc6adea2015-02-09 22:29:36 -0800467 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700468 findAttachedLinks: findAttachedLinks,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100469 findBadLinks: findBadLinks,
470 };
Simon Hunt3a6eec02015-02-09 21:16:43 -0800471 }]);
472}());