blob: 418285c1a071b7d11ddd7fbc6bd351d5343d9481 [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
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 Hunt3a6eec02015-02-09 21:16:43 -080072 if (x && y) {
73 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 Hunt3a6eec02015-02-09 21:16:43 -0800233 }
234 });
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);
247 //logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
248 return null;
249 }
250 return {
251 source: srcNode,
Simon Hunt5f361082015-02-25 11:36:38 -0800252 target: dstNode
Simon Hunt3a6eec02015-02-09 21:16:43 -0800253 };
254 }
255
256 function missMsg(what, id) {
257 return '\n[' + what + '] "' + id + '" missing';
258 }
259
Simon Huntdc6adea2015-02-09 22:29:36 -0800260
261 function makeNodeKey(d, what) {
262 var port = what + 'Port';
263 return d[what] + '/' + d[port];
264 }
265
266 function makeLinkKey(d, flipped) {
267 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
268 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
269 return one + '-' + two;
270 }
271
272 function findLinkById(id) {
273 // check to see if this is a reverse lookup, else default to given id
274 var key = rlk[id] || id;
275 return key && lu[key];
276 }
277
278 function findLink(linkData, op) {
279 var key = makeLinkKey(linkData),
280 keyrev = makeLinkKey(linkData, 1),
281 link = lu[key],
282 linkRev = lu[keyrev],
283 result = {},
284 ldata = link || linkRev,
285 rawLink;
286
287 if (op === 'add') {
288 if (link) {
289 // trying to add a link that we already know about
290 result.ldata = link;
291 result.badLogic = 'addLink: link already added';
292
293 } else if (linkRev) {
294 // we found the reverse of the link to be added
295 result.ldata = linkRev;
296 if (linkRev.fromTarget) {
297 result.badLogic = 'addLink: link already added';
298 }
299 }
300 } else if (op === 'update') {
301 if (!ldata) {
302 result.badLogic = 'updateLink: link not found';
303 } else {
304 rawLink = link ? ldata.fromSource : ldata.fromTarget;
305 result.updateWith = function (data) {
306 angular.extend(rawLink, data);
307 api.restyleLinkElement(ldata);
308 }
309 }
310 } else if (op === 'remove') {
311 if (!ldata) {
312 result.badLogic = 'removeLink: link not found';
313 } else {
314 rawLink = link ? ldata.fromSource : ldata.fromTarget;
315
316 if (!rawLink) {
317 result.badLogic = 'removeLink: link not found';
318
319 } else {
320 result.removeRawLink = function () {
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700321 // remove link out of aggregate linksByDevice list
322 var linksForDevPair = linksByDevice[ldata.devicePair],
323 rmvIdx = fs.find(ldata.key, linksForDevPair, 'key');
Bri Prebilic Cole80401762015-07-16 11:36:18 -0700324 if (rmvIdx >= 0) {
325 linksForDevPair.splice(rmvIdx, 1);
326 }
327 ldata.position.multilink = linksForDevPair.length >= 5;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700328
Simon Huntdc6adea2015-02-09 22:29:36 -0800329 if (link) {
330 // remove fromSource
331 ldata.fromSource = null;
332 if (ldata.fromTarget) {
333 // promote target into source position
334 ldata.fromSource = ldata.fromTarget;
335 ldata.fromTarget = null;
336 ldata.key = keyrev;
337 delete lu[key];
338 lu[keyrev] = ldata;
339 delete rlk[keyrev];
340 }
341 } else {
342 // remove fromTarget
343 ldata.fromTarget = null;
344 delete rlk[keyrev];
345 }
346 if (ldata.fromSource) {
347 api.restyleLinkElement(ldata);
348 } else {
349 api.removeLinkElement(ldata);
350 }
351 }
352 }
353 }
354 }
355 return result;
356 }
357
358 function findDevices(offlineOnly) {
359 var a = [];
360 nodes.forEach(function (d) {
361 if (d.class === 'device' && !(offlineOnly && d.online)) {
362 a.push(d);
363 }
364 });
365 return a;
366 }
367
368 function findAttachedHosts(devId) {
369 var hosts = [];
370 nodes.forEach(function (d) {
371 if (d.class === 'host' && d.cp.device === devId) {
372 hosts.push(d);
373 }
374 });
375 return hosts;
376 }
377
378 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700379 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800380 links.forEach(function (d) {
381 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700382 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800383 }
384 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700385 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800386 }
387
Simon Hunt86b7c882015-04-02 23:06:08 -0700388 // returns one-way links or where the internal link types differ
389 function findBadLinks() {
390 var lnks = [],
391 src, tgt;
392 links.forEach(function (d) {
393 // NOTE: skip edge links, which are synthesized
394 if (d.type() !== 'hostLink') {
395 delete d.bad;
396 src = d.fromSource;
397 tgt = d.fromTarget;
398 if (src && !tgt) {
399 d.bad = 'missing link';
400 } else if (src.type !== tgt.type) {
401 d.bad = 'type mismatch';
402 }
403 if (d.bad) {
404 lnks.push(d);
405 }
406 }
407 });
408 return lnks;
409 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800410
Simon Hunt3a6eec02015-02-09 21:16:43 -0800411 // ==========================
412 // Module definition
413
414 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800415 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800416 ['$log', 'FnService', 'RandomService',
417
418 function (_$log_, _fs_, _rnd_) {
419 $log = _$log_;
420 fs = _fs_;
421 rnd = _rnd_;
422
423 function initModel(_api_, _dim_) {
424 api = _api_;
425 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800426 lu = api.network.lookup;
427 rlk = api.network.revLinkToKey;
428 nodes = api.network.nodes;
429 links = api.network.links;
Bri Prebilic Coleaeeb33e2015-07-09 15:15:54 -0700430 linksByDevice = api.network.linksByDevice;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800431 }
432
433 function newDim(_dim_) {
434 dim = _dim_;
435 }
436
Simon Huntf542d842015-02-11 16:20:33 -0800437 function destroyModel() { }
438
Simon Hunt3a6eec02015-02-09 21:16:43 -0800439 return {
440 initModel: initModel,
441 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800442 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800443
444 positionNode: positionNode,
Simon Huntfd7106c2016-02-09 15:05:26 -0800445 resetAllLocations: resetAllLocations,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800446 createDeviceNode: createDeviceNode,
447 createHostNode: createHostNode,
448 createHostLink: createHostLink,
449 createLink: createLink,
450 coordFromLngLat: coordFromLngLat,
451 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800452 findLink: findLink,
453 findLinkById: findLinkById,
454 findDevices: findDevices,
455 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700456 findAttachedLinks: findAttachedLinks,
457 findBadLinks: findBadLinks
Simon Hunt3a6eec02015-02-09 21:16:43 -0800458 }
459 }]);
460}());