blob: 2a4921bdabca8b7dbc7613cdfa2a84c56f3395df [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
39 var lu, rlk, nodes, links;
40
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,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800188
189 // functions to aggregate dual link state
190 type: function () {
191 var s = lnk.fromSource,
192 t = lnk.fromTarget;
193 return (s && s.type) || (t && t.type) || defaultLinkType;
194 },
195 online: function () {
196 var s = lnk.fromSource,
197 t = lnk.fromTarget,
198 both = lnk.source.online && lnk.target.online;
199 return both && ((s && s.online) || (t && t.online));
200 },
201 linkWidth: function () {
202 var s = lnk.fromSource,
203 t = lnk.fromTarget,
204 ws = (s && s.linkWidth) || 0,
205 wt = (t && t.linkWidth) || 0;
206 return Math.max(ws, wt);
207 }
208 });
209 return lnk;
210 }
211
212
213 function linkEndPoints(srcId, dstId) {
Simon Huntdc6adea2015-02-09 22:29:36 -0800214 var srcNode = lu[srcId],
215 dstNode = lu[dstId],
Simon Hunt3a6eec02015-02-09 21:16:43 -0800216 sMiss = !srcNode ? missMsg('src', srcId) : '',
217 dMiss = !dstNode ? missMsg('dst', dstId) : '';
218
219 if (sMiss || dMiss) {
220 $log.error('Node(s) not on map for link:' + sMiss + dMiss);
221 //logicError('Node(s) not on map for link:\n' + sMiss + dMiss);
222 return null;
223 }
224 return {
225 source: srcNode,
Simon Hunt5f361082015-02-25 11:36:38 -0800226 target: dstNode
Simon Hunt3a6eec02015-02-09 21:16:43 -0800227 };
228 }
229
230 function missMsg(what, id) {
231 return '\n[' + what + '] "' + id + '" missing';
232 }
233
Simon Huntdc6adea2015-02-09 22:29:36 -0800234
235 function makeNodeKey(d, what) {
236 var port = what + 'Port';
237 return d[what] + '/' + d[port];
238 }
239
240 function makeLinkKey(d, flipped) {
241 var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'),
242 two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst');
243 return one + '-' + two;
244 }
245
246 function findLinkById(id) {
247 // check to see if this is a reverse lookup, else default to given id
248 var key = rlk[id] || id;
249 return key && lu[key];
250 }
251
252 function findLink(linkData, op) {
253 var key = makeLinkKey(linkData),
254 keyrev = makeLinkKey(linkData, 1),
255 link = lu[key],
256 linkRev = lu[keyrev],
257 result = {},
258 ldata = link || linkRev,
259 rawLink;
260
261 if (op === 'add') {
262 if (link) {
263 // trying to add a link that we already know about
264 result.ldata = link;
265 result.badLogic = 'addLink: link already added';
266
267 } else if (linkRev) {
268 // we found the reverse of the link to be added
269 result.ldata = linkRev;
270 if (linkRev.fromTarget) {
271 result.badLogic = 'addLink: link already added';
272 }
273 }
274 } else if (op === 'update') {
275 if (!ldata) {
276 result.badLogic = 'updateLink: link not found';
277 } else {
278 rawLink = link ? ldata.fromSource : ldata.fromTarget;
279 result.updateWith = function (data) {
280 angular.extend(rawLink, data);
281 api.restyleLinkElement(ldata);
282 }
283 }
284 } else if (op === 'remove') {
285 if (!ldata) {
286 result.badLogic = 'removeLink: link not found';
287 } else {
288 rawLink = link ? ldata.fromSource : ldata.fromTarget;
289
290 if (!rawLink) {
291 result.badLogic = 'removeLink: link not found';
292
293 } else {
294 result.removeRawLink = function () {
295 if (link) {
296 // remove fromSource
297 ldata.fromSource = null;
298 if (ldata.fromTarget) {
299 // promote target into source position
300 ldata.fromSource = ldata.fromTarget;
301 ldata.fromTarget = null;
302 ldata.key = keyrev;
303 delete lu[key];
304 lu[keyrev] = ldata;
305 delete rlk[keyrev];
306 }
307 } else {
308 // remove fromTarget
309 ldata.fromTarget = null;
310 delete rlk[keyrev];
311 }
312 if (ldata.fromSource) {
313 api.restyleLinkElement(ldata);
314 } else {
315 api.removeLinkElement(ldata);
316 }
317 }
318 }
319 }
320 }
321 return result;
322 }
323
324 function findDevices(offlineOnly) {
325 var a = [];
326 nodes.forEach(function (d) {
327 if (d.class === 'device' && !(offlineOnly && d.online)) {
328 a.push(d);
329 }
330 });
331 return a;
332 }
333
334 function findAttachedHosts(devId) {
335 var hosts = [];
336 nodes.forEach(function (d) {
337 if (d.class === 'host' && d.cp.device === devId) {
338 hosts.push(d);
339 }
340 });
341 return hosts;
342 }
343
344 function findAttachedLinks(devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700345 var lnks = [];
Simon Huntdc6adea2015-02-09 22:29:36 -0800346 links.forEach(function (d) {
347 if (d.source.id === devId || d.target.id === devId) {
Simon Hunt86b7c882015-04-02 23:06:08 -0700348 lnks.push(d);
Simon Huntdc6adea2015-02-09 22:29:36 -0800349 }
350 });
Simon Hunt86b7c882015-04-02 23:06:08 -0700351 return lnks;
Simon Huntdc6adea2015-02-09 22:29:36 -0800352 }
353
Simon Hunt86b7c882015-04-02 23:06:08 -0700354 // returns one-way links or where the internal link types differ
355 function findBadLinks() {
356 var lnks = [],
357 src, tgt;
358 links.forEach(function (d) {
359 // NOTE: skip edge links, which are synthesized
360 if (d.type() !== 'hostLink') {
361 delete d.bad;
362 src = d.fromSource;
363 tgt = d.fromTarget;
364 if (src && !tgt) {
365 d.bad = 'missing link';
366 } else if (src.type !== tgt.type) {
367 d.bad = 'type mismatch';
368 }
369 if (d.bad) {
370 lnks.push(d);
371 }
372 }
373 });
374 return lnks;
375 }
Simon Huntdc6adea2015-02-09 22:29:36 -0800376
Simon Hunt3a6eec02015-02-09 21:16:43 -0800377 // ==========================
378 // Module definition
379
380 angular.module('ovTopo')
Simon Hunt75ec9692015-02-11 16:40:36 -0800381 .factory('TopoModelService',
Simon Hunt3a6eec02015-02-09 21:16:43 -0800382 ['$log', 'FnService', 'RandomService',
383
384 function (_$log_, _fs_, _rnd_) {
385 $log = _$log_;
386 fs = _fs_;
387 rnd = _rnd_;
388
389 function initModel(_api_, _dim_) {
390 api = _api_;
391 dim = _dim_;
Simon Huntdc6adea2015-02-09 22:29:36 -0800392 lu = api.network.lookup;
393 rlk = api.network.revLinkToKey;
394 nodes = api.network.nodes;
395 links = api.network.links;
Simon Hunt3a6eec02015-02-09 21:16:43 -0800396 }
397
398 function newDim(_dim_) {
399 dim = _dim_;
400 }
401
Simon Huntf542d842015-02-11 16:20:33 -0800402 function destroyModel() { }
403
Simon Hunt3a6eec02015-02-09 21:16:43 -0800404 return {
405 initModel: initModel,
406 newDim: newDim,
Simon Huntf542d842015-02-11 16:20:33 -0800407 destroyModel: destroyModel,
Simon Hunt3a6eec02015-02-09 21:16:43 -0800408
409 positionNode: positionNode,
410 createDeviceNode: createDeviceNode,
411 createHostNode: createHostNode,
412 createHostLink: createHostLink,
413 createLink: createLink,
414 coordFromLngLat: coordFromLngLat,
415 lngLatFromCoord: lngLatFromCoord,
Simon Huntdc6adea2015-02-09 22:29:36 -0800416 findLink: findLink,
417 findLinkById: findLinkById,
418 findDevices: findDevices,
419 findAttachedHosts: findAttachedHosts,
Simon Hunt86b7c882015-04-02 23:06:08 -0700420 findAttachedLinks: findAttachedLinks,
421 findBadLinks: findBadLinks
Simon Hunt3a6eec02015-02-09 21:16:43 -0800422 }
423 }]);
424}());