blob: ed9eeb13f131138464f082c40b261c8a309f2f6f [file] [log] [blame]
Simon Hunt3a6eec02015-02-09 21:16:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
65 // If we have [x,y] already, use that...
66 if (x && y) {
67 node.fixed = true;
68 node.px = node.x = x;
69 node.py = node.y = y;
70 return;
71 }
72
73 var location = node.location,
74 coord;
75
76 if (location && location.type === 'latlng') {
77 coord = coordFromLngLat(location);
78 node.fixed = true;
79 node.px = node.x = coord[0];
80 node.py = node.y = coord[1];
81 return true;
82 }
83
84 // if this is a node update (not a node add).. skip randomizer
85 if (forUpdate) {
86 return;
87 }
88
89 // Note: Placing incoming unpinned nodes at exactly the same point
90 // (center of the view) causes them to explode outwards when
91 // the force layout kicks in. So, we spread them out a bit
92 // initially, to provide a more serene layout convergence.
93 // Additionally, if the node is a host, we place it near
94 // the device it is connected to.
95
96 function rand() {
97 return {
98 x: rnd.randDim(dim[0]),
99 y: rnd.randDim(dim[1])
100 };
101 }
102
103 function near(node) {
104 return {
105 x: node.x + nearDist + rnd.spread(nearDist),
106 y: node.y + nearDist + rnd.spread(nearDist)
107 };
108 }
109
110 function getDevice(cp) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800111 var d = lu[cp.device];
Simon Hunt3a6eec02015-02-09 21:16:43 -0800112 return d || rand();
113 }
114
115 xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand();
116 angular.extend(node, xy);
117 }
118
119 function mkSvgCls(dh, t, on) {
120 var ndh = 'node ' + dh,
121 ndht = t ? ndh + ' ' + t : ndh;
122 return on ? ndht + ' online' : ndht;
123 }
124
125 function createDeviceNode(device) {
126 var node = device;
127
128 // Augment as needed...
129 node.class = 'device';
130 node.svgClass = mkSvgCls('device', device.type, device.online);
131 positionNode(node);
132 return node;
133 }
134
135 function createHostNode(host) {
136 var node = host;
137
138 // Augment as needed...
139 node.class = 'host';
140 if (!node.type) {
141 node.type = 'endstation';
142 }
143 node.svgClass = mkSvgCls('host', node.type);
144 positionNode(node);
145 return node;
146 }
147
148 function createHostLink(host) {
149 var src = host.id,
150 dst = host.cp.device,
151 id = host.ingress,
152 lnk = linkEndPoints(src, dst);
153
154 if (!lnk) {
155 return null;
156 }
157
158 // Synthesize link ...
159 angular.extend(lnk, {
160 key: id,
161 class: 'link',
Simon Hunt5f361082015-02-25 11:36:38 -0800162 // NOTE: srcPort left undefined (host end of the link)
163 tgtPort: host.cp.port,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800164
165 type: function () { return 'hostLink'; },
166 online: function () {
167 // hostlink target is edge switch
168 return lnk.target.online;
169 },
170 linkWidth: function () { return 1; }
171 });
172 return lnk;
173 }
174
175 function createLink(link) {
176 var lnk = linkEndPoints(link.src, link.dst);
177
178 if (!lnk) {
179 return null;
180 }
181
182 angular.extend(lnk, {
183 key: link.id,
184 class: 'link',
185 fromSource: link,
Simon Hunt5f361082015-02-25 11:36:38 -0800186 srcPort: link.srcPort,
187 tgtPort: link.dstPort,
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700188 position: {
189 x1: 0,
190 y1: 0,
191 x2: 0,
192 y2: 0
193 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800194
195 // functions to aggregate dual link state
196 type: function () {
197 var s = lnk.fromSource,
198 t = lnk.fromTarget;
199 return (s && s.type) || (t && t.type) || defaultLinkType;
200 },
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800201 expected: function () {
202 var s = lnk.fromSource,
203 t = lnk.fromTarget;
204 return (s && s.expected) && (t && t.expected);
205 },
Simon Hunt3a6eec02015-02-09 21:16:43 -0800206 online: function () {
207 var s = lnk.fromSource,
208 t = lnk.fromTarget,
209 both = lnk.source.online && lnk.target.online;
Simon Hunt15813b22016-01-29 08:29:08 -0800210 return both && (s && s.online) && (t && t.online);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800211 },
212 linkWidth: function () {
213 var s = lnk.fromSource,
214 t = lnk.fromTarget,
215 ws = (s && s.linkWidth) || 0,
216 wt = (t && t.linkWidth) || 0;
Bri Prebilic Cole038aedd2015-07-13 15:25:16 -0700217 return lnk.position.multiLink ? 5 : Math.max(ws, wt);
Simon Hunt3a6eec02015-02-09 21:16:43 -0800218 }
219 });
220 return lnk;
221 }
222
223
224 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800225 var srcNode = lu[srcId],
226 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800227 sMiss = !srcNode ? missMsg('src', srcId) : '',
228 dMiss = !dstNode ? missMsg('dst', dstId) : '';
229
230 if (sMiss || dMiss) {
231 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
232 //logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
233 return null;
234 }
235 return {
236 source: srcNode,
Simon Hunt5f361082015-02-25 11:36:38 -0800237 target: dstNode
Simon Hunt3a6eec02015-02-09 21:16:43 -0800238 };
239 }
240
241 function missMsg(what, id) {
242 return '\n[' + what + '] "' + id + '" missing';
243 }
244
Simon Huntdc6adea2015-02-09 22:29:36 -0800245
246 function makeNodeKey(d, what) {
247 var port = what + 'Port';
248 return d[what] + '/' + d[port];
249 }
250
251 function makeLinkKey(d, flipped) {
252 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
253 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
254 return one + '-' + two;
255 }
256
257 function findLinkById(id) {
258 // check to see if this is a reverse lookup, else default to given id
259 var key = rlk[id] || id;
260 return key && lu[key];
261 }
262
263 function findLink(linkData, op) {
264 var key = makeLinkKey(linkData),
265 keyrev = makeLinkKey(linkData, 1),
266 link = lu[key],
267 linkRev = lu[keyrev],
268 result = {},
269 ldata = link || linkRev,
270 rawLink;
271
272 if (op === 'add') {
273 if (link) {
274 // trying to add a link that we already know about
275 result.ldata = link;
276 result.badLogic = 'addLink: link already added';
277
278 } else if (linkRev) {
279 // we found the reverse of the link to be added
280 result.ldata = linkRev;
281 if (linkRev.fromTarget) {
282 result.badLogic = 'addLink: link already added';
283 }
284 }
285 } else if (op === 'update') {
286 if (!ldata) {
287 result.badLogic = 'updateLink: link not found';
288 } else {
289 rawLink = link ? ldata.fromSource : ldata.fromTarget;
290 result.updateWith = function (data) {
291 angular.extend(rawLink, data);
292 api.restyleLinkElement(ldata);
293 }
294 }
295 } else if (op === 'remove') {
296 if (!ldata) {
297 result.badLogic = 'removeLink: link not found';
298 } else {
299 rawLink = link ? ldata.fromSource : ldata.fromTarget;
300
301 if (!rawLink) {
302 result.badLogic = 'removeLink: link not found';
303
304 } else {
305 result.removeRawLink = function () {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700306 // remove link out of aggregate linksByDevice list
307 var linksForDevPair = linksByDevice[ldata.devicePair],
308 rmvIdx = fs.find(ldata.key, linksForDevPair, 'key');
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700309 if (rmvIdx >= 0) {
310 linksForDevPair.splice(rmvIdx, 1);
311 }
312 ldata.position.multilink = linksForDevPair.length >= 5;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700313
Simon Huntdc6adea2015-02-09 22:29:36 -0800314 if (link) {
315 // remove fromSource
316 ldata.fromSource = null;
317 if (ldata.fromTarget) {
318 // promote target into source position
319 ldata.fromSource = ldata.fromTarget;
320 ldata.fromTarget = null;
321 ldata.key = keyrev;
322 delete lu[key];
323 lu[keyrev] = ldata;
324 delete rlk[keyrev];
325 }
326 } else {
327 // remove fromTarget
328 ldata.fromTarget = null;
329 delete rlk[keyrev];
330 }
331 if (ldata.fromSource) {
332 api.restyleLinkElement(ldata);
333 } else {
334 api.removeLinkElement(ldata);
335 }
336 }
337 }
338 }
339 }
340 return result;
341 }
342
343 function findDevices(offlineOnly) {
344 var a = [];
345 nodes.forEach(function (d) {
346 if (d.class === 'device' && !(offlineOnly && d.online)) {
347 a.push(d);
348 }
349 });
350 return a;
351 }
352
353 function findAttachedHosts(devId) {
354 var hosts = [];
355 nodes.forEach(function (d) {
356 if (d.class === 'host' && d.cp.device === devId) {
357 hosts.push(d);
358 }
359 });
360 return hosts;
361 }
362
363 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700364 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800365 links.forEach(function (d) {
366 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700367 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800368 }
369 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700370 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800371 }
372
Simon Hunt86b7c882015-04-02 23:06:08 -0700373 // returns one-way links or where the internal link types differ
374 function findBadLinks() {
375 var lnks = [],
376 src, tgt;
377 links.forEach(function (d) {
378 // NOTE: skip edge links, which are synthesized
379 if (d.type() !== 'hostLink') {
380 delete d.bad;
381 src = d.fromSource;
382 tgt = d.fromTarget;
383 if (src && !tgt) {
384 d.bad = 'missing link';
385 } else if (src.type !== tgt.type) {
386 d.bad = 'type mismatch';
387 }
388 if (d.bad) {
389 lnks.push(d);
390 }
391 }
392 });
393 return lnks;
394 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800395
Simon Hunt3a6eec02015-02-09 21:16:43 -0800396 // ==========================
397 // Module definition
398
399 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800400 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800401 ['$log', 'FnService', 'RandomService',
402
403 function (_$log_, _fs_, _rnd_) {
404 $log = _$log_;
405 fs = _fs_;
406 rnd = _rnd_;
407
408 function initModel(_api_, _dim_) {
409 api = _api_;
410 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800411 lu = api.network.lookup;
412 rlk = api.network.revLinkToKey;
413 nodes = api.network.nodes;
414 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700415 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800416 }
417
418 function newDim(_dim_) {
419 dim = _dim_;
420 }
421
Simon Huntf542d842015-02-11 16:20:33 -0800422 function destroyModel() { }
423
Simon Hunt3a6eec02015-02-09 21:16:43 -0800424 return {
425 initModel: initModel,
426 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800427 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800428
429 positionNode: positionNode,
430 createDeviceNode: createDeviceNode,
431 createHostNode: createHostNode,
432 createHostLink: createHostLink,
433 createLink: createLink,
434 coordFromLngLat: coordFromLngLat,
435 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800436 findLink: findLink,
437 findLinkById: findLinkById,
438 findDevices: findDevices,
439 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700440 findAttachedLinks: findAttachedLinks,
441 findBadLinks: findBadLinks
Simon Hunt3a6eec02015-02-09 21:16:43 -0800442 }
443 }]);
444}());