blob: 7679fe1d725639a3f47118eaa8f387ca83064e11 [file] [log] [blame]
Simon Hunt3a6eec02015-02-09 21:16:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Simon Hunt08f841d02015-02-10 14:39:20 -080041 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();
Bri Prebilic Cole6b95a3f2015-06-04 09:15:00 -070050 // suspected cause of ONOS-2109
Simon Hunt3a6eec02015-02-09 21:16:43 -080051 return p ? p([loc.lng, loc.lat]) : [0, 0];
52 }
53
54 function lngLatFromCoord(coord) {
55 var p = api.projection();
56 return p ? p.invert(coord) : [0, 0];
57 }
58
59 function positionNode(node, forUpdate) {
60 var meta = node.metaUi,
61 x = meta && meta.x,
62 y = meta && meta.y,
63 xy;
64
Simon Huntfd7106c2016-02-09 15:05:26 -080065 // if the device contains explicit LONG/LAT data, use that to position
66 if (setLongLat(node)) {
67 // indicate we want to update cached meta data...
68 return true;
69 }
70
71 // else if we have [x,y] cached in meta data, use that...
Simon Hunta5487ad2016-06-16 13:10:41 -070072 if (x !== undefined && y !== undefined) {
Simon Hunt3a6eec02015-02-09 21:16:43 -080073 node.fixed = true;
74 node.px = node.x = x;
75 node.py = node.y = y;
76 return;
77 }
78
Simon Hunt3a6eec02015-02-09 21:16:43 -080079 // if this is a node update (not a node add).. skip randomizer
80 if (forUpdate) {
81 return;
82 }
83
84 // Note: Placing incoming unpinned nodes at exactly the same point
85 // (center of the view) causes them to explode outwards when
86 // the force layout kicks in. So, we spread them out a bit
87 // initially, to provide a more serene layout convergence.
88 // Additionally, if the node is a host, we place it near
89 // the device it is connected to.
90
91 function rand() {
92 return {
93 x: rnd.randDim(dim[0]),
94 y: rnd.randDim(dim[1])
95 };
96 }
97
98 function near(node) {
99 return {
100 x: node.x + nearDist + rnd.spread(nearDist),
101 y: node.y + nearDist + rnd.spread(nearDist)
102 };
103 }
104
105 function getDevice(cp) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800106 var d = lu[cp.device];
Simon Hunt3a6eec02015-02-09 21:16:43 -0800107 return d || rand();
108 }
109
110 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
111 angular.extend(node, xy);
112 }
113
Simon Huntfd7106c2016-02-09 15:05:26 -0800114 function setLongLat(node) {
115 var loc = node.location,
116 coord;
117
118 if (loc && loc.type === 'lnglat') {
119 coord = coordFromLngLat(loc);
120 node.fixed = true;
121 node.px = node.x = coord[0];
122 node.py = node.y = coord[1];
123 return true;
124 }
125 }
126
127 function resetAllLocations() {
128 nodes.forEach(function (d) {
129 setLongLat(d);
130 });
131 }
132
Simon Hunt3a6eec02015-02-09 21:16:43 -0800133 function mkSvgCls(dh, t, on) {
134 var ndh = 'node ' + dh,
135 ndht = t ? ndh + ' ' + t : ndh;
136 return on ? ndht + ' online' : ndht;
137 }
138
139 function createDeviceNode(device) {
140 var node = device;
141
142 // Augment as needed...
143 node.class = 'device';
144 node.svgClass = mkSvgCls('device', device.type, device.online);
145 positionNode(node);
146 return node;
147 }
148
149 function createHostNode(host) {
150 var node = host;
151
152 // Augment as needed...
153 node.class = 'host';
154 if (!node.type) {
155 node.type = 'endstation';
156 }
157 node.svgClass = mkSvgCls('host', node.type);
158 positionNode(node);
159 return node;
160 }
161
162 function createHostLink(host) {
163 var src = host.id,
164 dst = host.cp.device,
165 id = host.ingress,
166 lnk = linkEndPoints(src, dst);
167
168 if (!lnk) {
169 return null;
170 }
171
172 // Synthesize link ...
173 angular.extend(lnk, {
174 key: id,
175 class: 'link',
Simon Hunt5f361082015-02-25 11:36:38 -0800176 // NOTE: srcPort left undefined (host end of the link)
177 tgtPort: host.cp.port,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800178
179 type: function () { return 'hostLink'; },
Simon Hunt219f0772016-02-09 10:58:49 -0800180 expected: function () { return true; },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800181 online: function () {
182 // hostlink target is edge switch
183 return lnk.target.online;
184 },
185 linkWidth: function () { return 1; }
186 });
187 return lnk;
188 }
189
190 function createLink(link) {
191 var lnk = linkEndPoints(link.src, link.dst);
192
193 if (!lnk) {
194 return null;
195 }
196
197 angular.extend(lnk, {
198 key: link.id,
199 class: 'link',
200 fromSource: link,
Simon Hunt5f361082015-02-25 11:36:38 -0800201 srcPort: link.srcPort,
202 tgtPort: link.dstPort,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700203 position: {
204 x1: 0,
205 y1: 0,
206 x2: 0,
207 y2: 0
208 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800209
210 // functions to aggregate dual link state
211 type: function () {
212 var s = lnk.fromSource,
213 t = lnk.fromTarget;
214 return (s && s.type) || (t && t.type) || defaultLinkType;
215 },
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800216 expected: function () {
217 var s = lnk.fromSource,
218 t = lnk.fromTarget;
219 return (s && s.expected) && (t && t.expected);
220 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800221 online: function () {
222 var s = lnk.fromSource,
223 t = lnk.fromTarget,
224 both = lnk.source.online && lnk.target.online;
Simon Hunt15813b22016-01-29 08:29:08 -0800225 return both && (s && s.online) && (t && t.online);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800226 },
227 linkWidth: function () {
228 var s = lnk.fromSource,
229 t = lnk.fromTarget,
230 ws = (s && s.linkWidth) || 0,
231 wt = (t && t.linkWidth) || 0;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700232 return lnk.position.multiLink ? 5 : Math.max(ws, wt);
Simon Hunt5c1a9382016-06-01 19:35:35 -0700233 },
234 extra: link.extra
Simon Hunt3a6eec02015-02-09 21:16:43 -0800235 });
236 return lnk;
237 }
238
239
240 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800241 var srcNode = lu[srcId],
242 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800243 sMiss = !srcNode ? missMsg('src', srcId) : '',
244 dMiss = !dstNode ? missMsg('dst', dstId) : '';
245
246 if (sMiss || dMiss) {
247 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
248 //logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
249 return null;
250 }
251 return {
252 source: srcNode,
Simon Hunt5f361082015-02-25 11:36:38 -0800253 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);
309 }
310 }
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 }
352 }
353 }
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
369 function findAttachedHosts(devId) {
370 var hosts = [];
371 nodes.forEach(function (d) {
372 if (d.class === 'host' && d.cp.device === devId) {
373 hosts.push(d);
374 }
375 });
376 return hosts;
377 }
378
379 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700380 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800381 links.forEach(function (d) {
382 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700383 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800384 }
385 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700386 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800387 }
388
Simon Hunt86b7c882015-04-02 23:06:08 -0700389 // returns one-way links or where the internal link types differ
390 function findBadLinks() {
391 var lnks = [],
392 src, tgt;
393 links.forEach(function (d) {
394 // NOTE: skip edge links, which are synthesized
395 if (d.type() !== 'hostLink') {
396 delete d.bad;
397 src = d.fromSource;
398 tgt = d.fromTarget;
399 if (src && !tgt) {
400 d.bad = 'missing link';
401 } else if (src.type !== tgt.type) {
402 d.bad = 'type mismatch';
403 }
404 if (d.bad) {
405 lnks.push(d);
406 }
407 }
408 });
409 return lnks;
410 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800411
Simon Hunt3a6eec02015-02-09 21:16:43 -0800412 // ==========================
413 // Module definition
414
415 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800416 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800417 ['$log', 'FnService', 'RandomService',
418
419 function (_$log_, _fs_, _rnd_) {
420 $log = _$log_;
421 fs = _fs_;
422 rnd = _rnd_;
423
424 function initModel(_api_, _dim_) {
425 api = _api_;
426 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800427 lu = api.network.lookup;
428 rlk = api.network.revLinkToKey;
429 nodes = api.network.nodes;
430 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700431 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800432 }
433
434 function newDim(_dim_) {
435 dim = _dim_;
436 }
437
Simon Huntf542d842015-02-11 16:20:33 -0800438 function destroyModel() { }
439
Simon Hunt3a6eec02015-02-09 21:16:43 -0800440 return {
441 initModel: initModel,
442 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800443 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800444
445 positionNode: positionNode,
Simon Huntfd7106c2016-02-09 15:05:26 -0800446 resetAllLocations: resetAllLocations,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800447 createDeviceNode: createDeviceNode,
448 createHostNode: createHostNode,
449 createHostLink: createHostLink,
450 createLink: createLink,
451 coordFromLngLat: coordFromLngLat,
452 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800453 findLink: findLink,
454 findLinkById: findLinkById,
455 findDevices: findDevices,
456 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700457 findAttachedLinks: findAttachedLinks,
458 findBadLinks: findBadLinks
Simon Hunt3a6eec02015-02-09 21:16:43 -0800459 }
460 }]);
461}());