Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 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 Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 27 | 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 Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 37 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 38 | // shorthand |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 39 | var lu, rlk, nodes, links, linksByDevice; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 40 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 41 | var dim; // dimensions of layout [w,h] |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 42 | |
| 43 | // configuration 'constants' |
| 44 | var defaultLinkType = 'direct', |
| 45 | nearDist = 15; |
| 46 | |
| 47 | |
| 48 | function coordFromLngLat(loc) { |
| 49 | var p = api.projection(); |
Simon Hunt | ffbad3b | 2017-05-16 15:37:51 -0700 | [diff] [blame] | 50 | return p ? p([loc.longOrX, loc.latOrY]) : [0, 0]; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | function lngLatFromCoord(coord) { |
| 54 | var p = api.projection(); |
| 55 | return p ? p.invert(coord) : [0, 0]; |
| 56 | } |
| 57 | |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 58 | function coordFromXY(loc) { |
| 59 | var bgWidth = 1000, |
| 60 | bgHeight = 1000; |
| 61 | |
| 62 | var scale = 1000 / bgWidth, |
| 63 | yOffset = (1000 - (bgHeight * scale)) / 2; |
| 64 | |
| 65 | // 1000 is a hardcoded HTML value of the SVG element (topo2.html) |
| 66 | var x = scale * loc.longOrX, |
| 67 | y = (scale * loc.latOrY) + yOffset; |
| 68 | |
| 69 | return [x, y]; |
| 70 | } |
| 71 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 72 | function positionNode(node, forUpdate) { |
| 73 | var meta = node.metaUi, |
| 74 | x = meta && meta.x, |
| 75 | y = meta && meta.y, |
| 76 | xy; |
| 77 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 78 | // if the device contains explicit LONG/LAT data, use that to position |
| 79 | if (setLongLat(node)) { |
| 80 | // indicate we want to update cached meta data... |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 81 | return false; |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // else if we have [x,y] cached in meta data, use that... |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 85 | if (x != undefined && y != undefined) { |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 86 | node.fixed = true; |
| 87 | node.px = node.x = x; |
| 88 | node.py = node.y = y; |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 89 | return true; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 92 | // if this is a node update (not a node add).. skip randomizer |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 93 | if (forUpdate && node.x != undefined && node.y != undefined) { |
| 94 | return false; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 98 | // (center of the view) causes them to explode outwards when |
| 99 | // the force layout kicks in. So, we spread them out a bit |
| 100 | // initially, to provide a more serene layout convergence. |
| 101 | // Additionally, if the node is a host, we place it near |
| 102 | // the device it is connected to. |
| 103 | |
| 104 | function rand() { |
| 105 | return { |
| 106 | x: rnd.randDim(dim[0]), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 107 | y: rnd.randDim(dim[1]), |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 108 | }; |
| 109 | } |
| 110 | |
| 111 | function near(node) { |
| 112 | return { |
| 113 | x: node.x + nearDist + rnd.spread(nearDist), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 114 | y: node.y + nearDist + rnd.spread(nearDist), |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 115 | }; |
| 116 | } |
| 117 | |
| 118 | function getDevice(cp) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 119 | var d = lu[cp.device]; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 120 | return d || rand(); |
| 121 | } |
| 122 | |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 123 | node.fixed = false; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 124 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 125 | angular.extend(node, xy); |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 126 | return false; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 129 | function setLongLat(node) { |
| 130 | var loc = node.location, |
| 131 | coord; |
| 132 | |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 133 | if (!loc || loc.locType === 'none') { |
| 134 | node.fixed = false; |
| 135 | |
| 136 | } else if (loc) { |
Thomas Vachuska | c67a991 | 2018-03-06 14:37:45 -0800 | [diff] [blame] | 137 | coord = loc.locType === 'geo' ? coordFromLngLat(loc) : coordFromXY(loc); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 138 | node.fixed = true; |
| 139 | node.px = node.x = coord[0]; |
| 140 | node.py = node.y = coord[1]; |
| 141 | return true; |
| 142 | } |
Thomas Vachuska | c616e17 | 2018-04-17 16:57:12 -0700 | [diff] [blame] | 143 | return false; |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | function resetAllLocations() { |
| 147 | nodes.forEach(function (d) { |
| 148 | setLongLat(d); |
| 149 | }); |
| 150 | } |
| 151 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 152 | function mkSvgCls(dh, t, on) { |
| 153 | var ndh = 'node ' + dh, |
| 154 | ndht = t ? ndh + ' ' + t : ndh; |
| 155 | return on ? ndht + ' online' : ndht; |
| 156 | } |
| 157 | |
| 158 | function createDeviceNode(device) { |
| 159 | var node = device; |
| 160 | |
| 161 | // Augment as needed... |
| 162 | node.class = 'device'; |
| 163 | node.svgClass = mkSvgCls('device', device.type, device.online); |
| 164 | positionNode(node); |
| 165 | return node; |
| 166 | } |
| 167 | |
| 168 | function createHostNode(host) { |
| 169 | var node = host; |
| 170 | |
| 171 | // Augment as needed... |
| 172 | node.class = 'host'; |
| 173 | if (!node.type) { |
| 174 | node.type = 'endstation'; |
| 175 | } |
| 176 | node.svgClass = mkSvgCls('host', node.type); |
| 177 | positionNode(node); |
| 178 | return node; |
| 179 | } |
| 180 | |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 181 | function createHostLink(hostId, devId, devPort) { |
| 182 | var linkKey = hostId + '/0-' + devId + '/' + devPort, |
| 183 | lnk = linkEndPoints(hostId, devId); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 184 | |
| 185 | if (!lnk) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | // Synthesize link ... |
| 190 | angular.extend(lnk, { |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 191 | key: linkKey, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 192 | class: 'link', |
Simon Hunt | 5f36108 | 2015-02-25 11:36:38 -0800 | [diff] [blame] | 193 | // NOTE: srcPort left undefined (host end of the link) |
Simon Hunt | 12c79ed | 2017-09-12 11:58:44 -0700 | [diff] [blame] | 194 | tgtPort: devPort, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 195 | |
| 196 | type: function () { return 'hostLink'; }, |
Simon Hunt | 219f077 | 2016-02-09 10:58:49 -0800 | [diff] [blame] | 197 | expected: function () { return true; }, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 198 | online: function () { |
| 199 | // hostlink target is edge switch |
| 200 | return lnk.target.online; |
| 201 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 202 | linkWidth: function () { return 1; }, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 203 | }); |
| 204 | return lnk; |
| 205 | } |
| 206 | |
| 207 | function createLink(link) { |
| 208 | var lnk = linkEndPoints(link.src, link.dst); |
| 209 | |
| 210 | if (!lnk) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | angular.extend(lnk, { |
| 215 | key: link.id, |
| 216 | class: 'link', |
| 217 | fromSource: link, |
Simon Hunt | 5f36108 | 2015-02-25 11:36:38 -0800 | [diff] [blame] | 218 | srcPort: link.srcPort, |
| 219 | tgtPort: link.dstPort, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 220 | position: { |
| 221 | x1: 0, |
| 222 | y1: 0, |
| 223 | x2: 0, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 224 | y2: 0, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 225 | }, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 226 | |
| 227 | // functions to aggregate dual link state |
| 228 | type: function () { |
| 229 | var s = lnk.fromSource, |
| 230 | t = lnk.fromTarget; |
| 231 | return (s && s.type) || (t && t.type) || defaultLinkType; |
| 232 | }, |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 233 | expected: function () { |
| 234 | var s = lnk.fromSource, |
| 235 | t = lnk.fromTarget; |
| 236 | return (s && s.expected) && (t && t.expected); |
| 237 | }, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 238 | online: function () { |
| 239 | var s = lnk.fromSource, |
| 240 | t = lnk.fromTarget, |
| 241 | both = lnk.source.online && lnk.target.online; |
Simon Hunt | 15813b2 | 2016-01-29 08:29:08 -0800 | [diff] [blame] | 242 | return both && (s && s.online) && (t && t.online); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 243 | }, |
| 244 | linkWidth: function () { |
| 245 | var s = lnk.fromSource, |
| 246 | t = lnk.fromTarget, |
| 247 | ws = (s && s.linkWidth) || 0, |
| 248 | wt = (t && t.linkWidth) || 0; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 249 | return lnk.position.multiLink ? 5 : Math.max(ws, wt); |
Simon Hunt | 5c1a938 | 2016-06-01 19:35:35 -0700 | [diff] [blame] | 250 | }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 251 | extra: link.extra, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 252 | }); |
| 253 | return lnk; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | function linkEndPoints(srcId, dstId) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 258 | var srcNode = lu[srcId], |
| 259 | dstNode = lu[dstId], |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 260 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 261 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 262 | |
| 263 | if (sMiss || dMiss) { |
| 264 | $log.error('Node(s) not on map for link:' + sMiss + dMiss); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 265 | // logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 266 | return null; |
| 267 | } |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 268 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 269 | return { |
| 270 | source: srcNode, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 271 | target: dstNode, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 272 | }; |
| 273 | } |
| 274 | |
| 275 | function missMsg(what, id) { |
| 276 | return '\n[' + what + '] "' + id + '" missing'; |
| 277 | } |
| 278 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 279 | |
| 280 | function makeNodeKey(d, what) { |
| 281 | var port = what + 'Port'; |
| 282 | return d[what] + '/' + d[port]; |
| 283 | } |
| 284 | |
| 285 | function makeLinkKey(d, flipped) { |
| 286 | var one = flipped ? makeNodeKey(d, 'dst') : makeNodeKey(d, 'src'), |
| 287 | two = flipped ? makeNodeKey(d, 'src') : makeNodeKey(d, 'dst'); |
| 288 | return one + '-' + two; |
| 289 | } |
| 290 | |
| 291 | function findLinkById(id) { |
| 292 | // check to see if this is a reverse lookup, else default to given id |
| 293 | var key = rlk[id] || id; |
| 294 | return key && lu[key]; |
| 295 | } |
| 296 | |
| 297 | function findLink(linkData, op) { |
| 298 | var key = makeLinkKey(linkData), |
| 299 | keyrev = makeLinkKey(linkData, 1), |
| 300 | link = lu[key], |
| 301 | linkRev = lu[keyrev], |
| 302 | result = {}, |
| 303 | ldata = link || linkRev, |
| 304 | rawLink; |
| 305 | |
| 306 | if (op === 'add') { |
| 307 | if (link) { |
| 308 | // trying to add a link that we already know about |
| 309 | result.ldata = link; |
| 310 | result.badLogic = 'addLink: link already added'; |
| 311 | |
| 312 | } else if (linkRev) { |
| 313 | // we found the reverse of the link to be added |
| 314 | result.ldata = linkRev; |
| 315 | if (linkRev.fromTarget) { |
| 316 | result.badLogic = 'addLink: link already added'; |
| 317 | } |
| 318 | } |
| 319 | } else if (op === 'update') { |
| 320 | if (!ldata) { |
| 321 | result.badLogic = 'updateLink: link not found'; |
| 322 | } else { |
| 323 | rawLink = link ? ldata.fromSource : ldata.fromTarget; |
| 324 | result.updateWith = function (data) { |
| 325 | angular.extend(rawLink, data); |
| 326 | api.restyleLinkElement(ldata); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 327 | }; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 328 | } |
| 329 | } else if (op === 'remove') { |
| 330 | if (!ldata) { |
| 331 | result.badLogic = 'removeLink: link not found'; |
| 332 | } else { |
| 333 | rawLink = link ? ldata.fromSource : ldata.fromTarget; |
| 334 | |
| 335 | if (!rawLink) { |
| 336 | result.badLogic = 'removeLink: link not found'; |
| 337 | |
| 338 | } else { |
| 339 | result.removeRawLink = function () { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 340 | // remove link out of aggregate linksByDevice list |
| 341 | var linksForDevPair = linksByDevice[ldata.devicePair], |
| 342 | rmvIdx = fs.find(ldata.key, linksForDevPair, 'key'); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 343 | if (rmvIdx >= 0) { |
| 344 | linksForDevPair.splice(rmvIdx, 1); |
| 345 | } |
| 346 | ldata.position.multilink = linksForDevPair.length >= 5; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 347 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 348 | if (link) { |
| 349 | // remove fromSource |
| 350 | ldata.fromSource = null; |
| 351 | if (ldata.fromTarget) { |
| 352 | // promote target into source position |
| 353 | ldata.fromSource = ldata.fromTarget; |
| 354 | ldata.fromTarget = null; |
| 355 | ldata.key = keyrev; |
| 356 | delete lu[key]; |
| 357 | lu[keyrev] = ldata; |
| 358 | delete rlk[keyrev]; |
| 359 | } |
| 360 | } else { |
| 361 | // remove fromTarget |
| 362 | ldata.fromTarget = null; |
| 363 | delete rlk[keyrev]; |
| 364 | } |
| 365 | if (ldata.fromSource) { |
| 366 | api.restyleLinkElement(ldata); |
| 367 | } else { |
| 368 | api.removeLinkElement(ldata); |
| 369 | } |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 370 | }; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | return result; |
| 375 | } |
| 376 | |
| 377 | function findDevices(offlineOnly) { |
| 378 | var a = []; |
| 379 | nodes.forEach(function (d) { |
| 380 | if (d.class === 'device' && !(offlineOnly && d.online)) { |
| 381 | a.push(d); |
| 382 | } |
| 383 | }); |
| 384 | return a; |
| 385 | } |
| 386 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 387 | function findHosts() { |
| 388 | var hosts = []; |
| 389 | nodes.forEach(function (d) { |
| 390 | if (d.class === 'host') { |
| 391 | hosts.push(d); |
| 392 | } |
| 393 | }); |
| 394 | return hosts; |
| 395 | } |
| 396 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 397 | function findAttachedHosts(devId) { |
| 398 | var hosts = []; |
| 399 | nodes.forEach(function (d) { |
| 400 | if (d.class === 'host' && d.cp.device === devId) { |
| 401 | hosts.push(d); |
| 402 | } |
| 403 | }); |
| 404 | return hosts; |
| 405 | } |
| 406 | |
| 407 | function findAttachedLinks(devId) { |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 408 | var lnks = []; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 409 | links.forEach(function (d) { |
| 410 | if (d.source.id === devId || d.target.id === devId) { |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 411 | lnks.push(d); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 412 | } |
| 413 | }); |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 414 | return lnks; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 417 | // returns one-way links or where the internal link types differ |
| 418 | function findBadLinks() { |
| 419 | var lnks = [], |
| 420 | src, tgt; |
| 421 | links.forEach(function (d) { |
| 422 | // NOTE: skip edge links, which are synthesized |
| 423 | if (d.type() !== 'hostLink') { |
| 424 | delete d.bad; |
| 425 | src = d.fromSource; |
| 426 | tgt = d.fromTarget; |
| 427 | if (src && !tgt) { |
| 428 | d.bad = 'missing link'; |
| 429 | } else if (src.type !== tgt.type) { |
| 430 | d.bad = 'type mismatch'; |
| 431 | } |
| 432 | if (d.bad) { |
| 433 | lnks.push(d); |
| 434 | } |
| 435 | } |
| 436 | }); |
| 437 | return lnks; |
| 438 | } |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 439 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 440 | // ========================== |
| 441 | // Module definition |
| 442 | |
| 443 | angular.module('ovTopo') |
Simon Hunt | 75ec969 | 2015-02-11 16:40:36 -0800 | [diff] [blame] | 444 | .factory('TopoModelService', |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 445 | ['$log', 'FnService', 'RandomService', |
| 446 | |
| 447 | function (_$log_, _fs_, _rnd_) { |
| 448 | $log = _$log_; |
| 449 | fs = _fs_; |
| 450 | rnd = _rnd_; |
| 451 | |
| 452 | function initModel(_api_, _dim_) { |
| 453 | api = _api_; |
| 454 | dim = _dim_; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 455 | lu = api.network.lookup; |
| 456 | rlk = api.network.revLinkToKey; |
| 457 | nodes = api.network.nodes; |
| 458 | links = api.network.links; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 459 | linksByDevice = api.network.linksByDevice; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | function newDim(_dim_) { |
| 463 | dim = _dim_; |
| 464 | } |
| 465 | |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 466 | function destroyModel() { } |
| 467 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 468 | return { |
| 469 | initModel: initModel, |
| 470 | newDim: newDim, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 471 | destroyModel: destroyModel, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 472 | |
| 473 | positionNode: positionNode, |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 474 | resetAllLocations: resetAllLocations, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 475 | createDeviceNode: createDeviceNode, |
| 476 | createHostNode: createHostNode, |
| 477 | createHostLink: createHostLink, |
| 478 | createLink: createLink, |
| 479 | coordFromLngLat: coordFromLngLat, |
| 480 | lngLatFromCoord: lngLatFromCoord, |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 481 | findLink: findLink, |
| 482 | findLinkById: findLinkById, |
| 483 | findDevices: findDevices, |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 484 | findHosts: findHosts, |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 485 | findAttachedHosts: findAttachedHosts, |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 486 | findAttachedLinks: findAttachedLinks, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 487 | findBadLinks: findBadLinks, |
| 488 | }; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 489 | }]); |
| 490 | }()); |