Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -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 | /* |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 18 | ONOS GUI -- Topology Force Module. |
| 19 | Visualization of the topology in an SVG layer, using a D3 Force Layout. |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
| 25 | // injected refs |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 26 | var $log, $timeout, fs, sus, ts, flash, wss, tov, |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 27 | tis, tms, td3, tss, tts, tos, fltr, tls, uplink, svg, tpis; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 28 | |
| 29 | // configuration |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 30 | var linkConfig = { |
| 31 | light: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 32 | baseColor: '#939598', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 33 | inColor: '#66f', |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 34 | outColor: '#f00' |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 35 | }, |
| 36 | dark: { |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 37 | // TODO : theme |
| 38 | baseColor: '#939598', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 39 | inColor: '#66f', |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 40 | outColor: '#f00' |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 41 | }, |
| 42 | inWidth: 12, |
| 43 | outWidth: 10 |
| 44 | }; |
| 45 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 46 | // internal state |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 47 | var settings, // merged default settings and options |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 48 | force, // force layout object |
| 49 | drag, // drag behavior handler |
| 50 | network = { |
| 51 | nodes: [], |
| 52 | links: [], |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 53 | linksByDevice: {}, |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 54 | lookup: {}, |
| 55 | revLinkToKey: {} |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 56 | }, |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 57 | lu, // shorthand for lookup |
| 58 | rlk, // shorthand for revLinktoKey |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 59 | showHosts = false, // whether hosts are displayed |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 60 | showOffline = true, // whether offline devices are displayed |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 61 | nodeLock = false, // whether nodes can be dragged or not (locked) |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 62 | fTimer, // timer for delayed force layout |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 63 | fNodesTimer, // timer for delayed nodes update |
| 64 | fLinksTimer, // timer for delayed links update |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 65 | dim, // the dimensions of the force layout [w,h] |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 66 | linkNums = [], // array of link number labels |
| 67 | devIconDim = 36, // node target dimension |
| 68 | devIconDimMin = 20, // node minimum dimension when zoomed out |
| 69 | devIconDimMax = 40, // node maximum dimension when zoomed in |
| 70 | portLabelDim = 30; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 71 | |
| 72 | // SVG elements; |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 73 | var linkG, linkLabelG, numLinkLblsG, portLabelG, nodeG; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 74 | |
| 75 | // D3 selections; |
| 76 | var link, linkLabel, node; |
| 77 | |
| 78 | // default settings for force layout |
| 79 | var defaultSettings = { |
| 80 | gravity: 0.4, |
| 81 | friction: 0.7, |
| 82 | charge: { |
| 83 | // note: key is node.class |
| 84 | device: -8000, |
| 85 | host: -5000, |
| 86 | _def_: -12000 |
| 87 | }, |
| 88 | linkDistance: { |
| 89 | // note: key is link.type |
| 90 | direct: 100, |
| 91 | optical: 120, |
| 92 | hostLink: 3, |
| 93 | _def_: 50 |
| 94 | }, |
| 95 | linkStrength: { |
| 96 | // note: key is link.type |
| 97 | // range: {0.0 ... 1.0} |
| 98 | //direct: 1.0, |
| 99 | //optical: 1.0, |
| 100 | //hostLink: 1.0, |
| 101 | _def_: 1.0 |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 106 | // ========================== |
| 107 | // === EVENT HANDLERS |
| 108 | |
| 109 | function addDevice(data) { |
| 110 | var id = data.id, |
| 111 | d; |
| 112 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 113 | uplink.showNoDevs(false); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 114 | |
| 115 | // although this is an add device event, if we already have the |
| 116 | // device, treat it as an update instead.. |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 117 | if (lu[id]) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 118 | updateDevice(data); |
| 119 | return; |
| 120 | } |
| 121 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 122 | d = tms.createDeviceNode(data); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 123 | network.nodes.push(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 124 | lu[id] = d; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 125 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 126 | fStart(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | function updateDevice(data) { |
| 130 | var id = data.id, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 131 | d = lu[id], |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 132 | wasOnline; |
| 133 | |
| 134 | if (d) { |
| 135 | wasOnline = d.online; |
| 136 | angular.extend(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 137 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 138 | sendUpdateMeta(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 139 | } |
| 140 | updateNodes(); |
| 141 | if (wasOnline !== d.online) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 142 | tms.findAttachedLinks(d.id).forEach(restyleLinkElement); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 143 | updateOfflineVisibility(d); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 144 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 148 | function removeDevice(data) { |
| 149 | var id = data.id, |
| 150 | d = lu[id]; |
| 151 | if (d) { |
| 152 | removeDeviceElement(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | function addHost(data) { |
| 157 | var id = data.id, |
| 158 | d, lnk; |
| 159 | |
| 160 | // although this is an add host event, if we already have the |
| 161 | // host, treat it as an update instead.. |
| 162 | if (lu[id]) { |
| 163 | updateHost(data); |
| 164 | return; |
| 165 | } |
| 166 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 167 | d = tms.createHostNode(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 168 | network.nodes.push(d); |
| 169 | lu[id] = d; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 170 | updateNodes(); |
| 171 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 172 | lnk = tms.createHostLink(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 173 | if (lnk) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 174 | d.linkData = lnk; // cache ref on its host |
| 175 | network.links.push(lnk); |
| 176 | lu[d.ingress] = lnk; |
| 177 | lu[d.egress] = lnk; |
| 178 | updateLinks(); |
| 179 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 180 | fStart(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | function updateHost(data) { |
| 184 | var id = data.id, |
| 185 | d = lu[id]; |
| 186 | if (d) { |
| 187 | angular.extend(d, data); |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 188 | if (tms.positionNode(d, true)) { |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 189 | sendUpdateMeta(d); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 190 | } |
| 191 | updateNodes(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 195 | function moveHost(data) { |
| 196 | var id = data.id, |
| 197 | d = lu[id], |
| 198 | lnk; |
| 199 | if (d) { |
| 200 | // first remove the old host link |
| 201 | removeLinkElement(d.linkData); |
| 202 | |
| 203 | // merge new data |
| 204 | angular.extend(d, data); |
| 205 | if (tms.positionNode(d, true)) { |
| 206 | sendUpdateMeta(d); |
| 207 | } |
| 208 | |
| 209 | // now create a new host link |
| 210 | lnk = tms.createHostLink(data); |
| 211 | if (lnk) { |
| 212 | d.linkData = lnk; |
| 213 | network.links.push(lnk); |
| 214 | lu[d.ingress] = lnk; |
| 215 | lu[d.egress] = lnk; |
| 216 | } |
| 217 | |
| 218 | updateNodes(); |
| 219 | updateLinks(); |
| 220 | fResume(); |
| 221 | } |
| 222 | } |
| 223 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 224 | function removeHost(data) { |
| 225 | var id = data.id, |
| 226 | d = lu[id]; |
| 227 | if (d) { |
| 228 | removeHostElement(d, true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | function addLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 233 | var result = tms.findLink(data, 'add'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 234 | bad = result.badLogic, |
| 235 | d = result.ldata; |
| 236 | |
| 237 | if (bad) { |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 238 | $log.debug(bad + ': ' + link.id); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (d) { |
| 243 | // we already have a backing store link for src/dst nodes |
| 244 | addLinkUpdate(d, data); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // no backing store link yet |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 249 | d = tms.createLink(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 250 | if (d) { |
| 251 | network.links.push(d); |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 252 | aggregateLink(d, data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 253 | lu[d.key] = d; |
| 254 | updateLinks(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 255 | fStart(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | function updateLink(data) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 260 | var result = tms.findLink(data, 'update'), |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 261 | bad = result.badLogic; |
| 262 | if (bad) { |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 263 | $log.debug(bad + ': ' + link.id); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 264 | return; |
| 265 | } |
Simon Hunt | eb18f52 | 2016-01-28 19:22:23 -0800 | [diff] [blame] | 266 | result.updateWith(data); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | function removeLink(data) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 270 | var result = tms.findLink(data, 'remove'); |
| 271 | |
| 272 | if (!result.badLogic) { |
| 273 | result.removeRawLink(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 274 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 277 | function topoStartDone(data) { |
| 278 | // called when the initial barrage of data has been sent from server |
| 279 | uplink.topoStartDone(); |
| 280 | } |
| 281 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 282 | // ======================== |
| 283 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 284 | function nodeById(id) { |
| 285 | return lu[id]; |
| 286 | } |
| 287 | |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 288 | function makeNodeKey(node1, node2) { |
| 289 | return node1 + '-' + node2; |
| 290 | } |
| 291 | |
| 292 | function findNodePair(key, keyRev) { |
| 293 | if (network.linksByDevice[key]) { |
| 294 | return key; |
| 295 | } else if (network.linksByDevice[keyRev]) { |
| 296 | return keyRev; |
| 297 | } else { |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | function aggregateLink(ldata, link) { |
| 303 | var key = makeNodeKey(link.src, link.dst), |
| 304 | keyRev = makeNodeKey(link.dst, link.src), |
| 305 | found = findNodePair(key, keyRev); |
| 306 | |
| 307 | if (found) { |
| 308 | network.linksByDevice[found].push(ldata); |
| 309 | ldata.devicePair = found; |
| 310 | } else { |
| 311 | network.linksByDevice[key] = [ ldata ]; |
| 312 | ldata.devicePair = key; |
| 313 | } |
| 314 | } |
| 315 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 316 | function addLinkUpdate(ldata, link) { |
| 317 | // add link event, but we already have the reverse link installed |
| 318 | ldata.fromTarget = link; |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 319 | rlk[link.id] = ldata.key; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 320 | // possible solution to el being undefined in restyleLinkElement: |
| 321 | //_updateLinks(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 322 | restyleLinkElement(ldata); |
| 323 | } |
| 324 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 325 | |
| 326 | var widthRatio = 1.4, |
| 327 | linkScale = d3.scale.linear() |
| 328 | .domain([1, 12]) |
| 329 | .range([widthRatio, 12 * widthRatio]) |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 330 | .clamp(true), |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 331 | allLinkTypes = 'direct indirect optical tunnel', |
| 332 | allLinkSubTypes = 'inactive not-permitted'; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 333 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 334 | function restyleLinkElement(ldata, immediate) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 335 | // this fn's job is to look at raw links and decide what svg classes |
| 336 | // need to be applied to the line element in the DOM |
| 337 | var th = ts.theme(), |
| 338 | el = ldata.el, |
| 339 | type = ldata.type(), |
| 340 | lw = ldata.linkWidth(), |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 341 | online = ldata.online(), |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 342 | modeCls = ldata.expected() ? 'inactive' : 'not-permitted', |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 343 | delay = immediate ? 0 : 1000; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 344 | |
Simon Hunt | f44d726 | 2016-06-14 14:46:56 -0700 | [diff] [blame] | 345 | // NOTE: understand why el is sometimes undefined on addLink events... |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 346 | // Investigated: |
| 347 | // el is undefined when it's a reverse link that is being added. |
| 348 | // updateLinks (which sets ldata.el) isn't called before this is called. |
| 349 | // Calling _updateLinks in addLinkUpdate fixes it, but there might be |
| 350 | // a more efficient way to fix it. |
| 351 | if (el && !el.empty()) { |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 352 | el.classed('link', true); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 353 | el.classed(allLinkSubTypes, false); |
| 354 | el.classed(modeCls, !online); |
Thomas Vachuska | cb5016f | 2015-05-18 14:11:43 -0700 | [diff] [blame] | 355 | el.classed(allLinkTypes, false); |
| 356 | if (type) { |
| 357 | el.classed(type, true); |
| 358 | } |
| 359 | el.transition() |
| 360 | .duration(delay) |
| 361 | .attr('stroke-width', linkScale(lw)) |
| 362 | .attr('stroke', linkConfig[th].baseColor); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 363 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 366 | function removeLinkElement(d) { |
| 367 | var idx = fs.find(d.key, network.links, 'key'), |
| 368 | removed; |
| 369 | if (idx >=0) { |
| 370 | // remove from links array |
| 371 | removed = network.links.splice(idx, 1); |
| 372 | // remove from lookup cache |
| 373 | delete lu[removed[0].key]; |
| 374 | updateLinks(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 375 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | function removeHostElement(d, upd) { |
| 380 | // first, remove associated hostLink... |
| 381 | removeLinkElement(d.linkData); |
| 382 | |
| 383 | // remove hostLink bindings |
| 384 | delete lu[d.ingress]; |
| 385 | delete lu[d.egress]; |
| 386 | |
| 387 | // remove from lookup cache |
| 388 | delete lu[d.id]; |
| 389 | // remove from nodes array |
| 390 | var idx = fs.find(d.id, network.nodes); |
| 391 | network.nodes.splice(idx, 1); |
| 392 | |
| 393 | // remove from SVG |
| 394 | // NOTE: upd is false if we were called from removeDeviceElement() |
| 395 | if (upd) { |
| 396 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 397 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | function removeDeviceElement(d) { |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 402 | var id = d.id, |
| 403 | idx; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 404 | // first, remove associated hosts and links.. |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 405 | tms.findAttachedHosts(id).forEach(removeHostElement); |
| 406 | tms.findAttachedLinks(id).forEach(removeLinkElement); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 407 | |
| 408 | // remove from lookup cache |
| 409 | delete lu[id]; |
| 410 | // remove from nodes array |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 411 | idx = fs.find(id, network.nodes); |
| 412 | if (idx > -1) { |
| 413 | network.nodes.splice(idx, 1); |
| 414 | } |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 415 | |
| 416 | if (!network.nodes.length) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 417 | uplink.showNoDevs(true); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // remove from SVG |
| 421 | updateNodes(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 422 | fResume(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 425 | function updateHostVisibility() { |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 426 | sus.visible(nodeG.selectAll('.host'), showHosts); |
| 427 | sus.visible(linkG.selectAll('.hostLink'), showHosts); |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 428 | sus.visible(linkLabelG.selectAll('.hostLinkLabel'), showHosts); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | function updateOfflineVisibility(dev) { |
| 432 | function updDev(d, show) { |
Simon Hunt | 8eb4d3a | 2015-02-23 18:23:29 -0800 | [diff] [blame] | 433 | var b; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 434 | sus.visible(d.el, show); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 435 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 436 | tms.findAttachedLinks(d.id).forEach(function (link) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 437 | b = show && ((link.type() !== 'hostLink') || showHosts); |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 438 | sus.visible(link.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 439 | }); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 440 | tms.findAttachedHosts(d.id).forEach(function (host) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 441 | b = show && showHosts; |
Simon Hunt | 18bf982 | 2015-02-12 17:35:45 -0800 | [diff] [blame] | 442 | sus.visible(host.el, b); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 443 | }); |
| 444 | } |
| 445 | |
| 446 | if (dev) { |
| 447 | // updating a specific device that just toggled off/on-line |
| 448 | updDev(dev, dev.online || showOffline); |
| 449 | } else { |
| 450 | // updating all offline devices |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 451 | tms.findDevices(true).forEach(function (d) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 452 | updDev(d, showOffline); |
| 453 | }); |
| 454 | } |
| 455 | } |
| 456 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 457 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 458 | function sendUpdateMeta(d, clearPos) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 459 | var metaUi = {}, |
| 460 | ll; |
| 461 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 462 | // if we are not clearing the position data (unpinning), |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 463 | // attach the x, y, (and equivalent longitude, latitude)... |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 464 | if (!clearPos) { |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 465 | ll = tms.lngLatFromCoord([d.x, d.y]); |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 466 | metaUi = { |
| 467 | x: d.x, |
| 468 | y: d.y, |
| 469 | equivLoc: { |
| 470 | lng: ll[0], |
| 471 | lat: ll[1] |
| 472 | } |
| 473 | }; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 474 | } |
| 475 | d.metaUi = metaUi; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 476 | wss.sendEvent('updateMeta', { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 477 | id: d.id, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 478 | class: d.class, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 479 | memento: metaUi |
| 480 | }); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 483 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 484 | function mkSvgClass(d) { |
| 485 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 486 | } |
| 487 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 488 | function vis(b) { |
| 489 | return b ? 'visible' : 'hidden'; |
| 490 | } |
| 491 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 492 | function toggleHosts(x) { |
| 493 | var kev = (x === 'keyev'), |
| 494 | on = kev ? !showHosts : !!x; |
| 495 | |
| 496 | showHosts = on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 497 | updateHostVisibility(); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 498 | flash.flash('Hosts ' + vis(on)); |
| 499 | return on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 502 | function toggleOffline(x) { |
| 503 | var kev = (x === 'keyev'), |
| 504 | on = kev ? !showOffline : !!x; |
| 505 | |
| 506 | showOffline = on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 507 | updateOfflineVisibility(); |
Simon Hunt | fcbde89 | 2015-04-16 12:05:28 -0700 | [diff] [blame] | 508 | flash.flash('Offline devices ' + vis(on)); |
| 509 | return on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | function cycleDeviceLabels() { |
Bri Prebilic Cole | 9cf1a8d | 2015-04-21 13:15:29 -0700 | [diff] [blame] | 513 | flash.flash(td3.incDevLabIndex()); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 514 | tms.findDevices().forEach(function (d) { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 515 | td3.updateDeviceLabel(d); |
Simon Hunt | 1c36711 | 2015-02-05 18:02:46 -0800 | [diff] [blame] | 516 | }); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame^] | 519 | function cycleHostLabels() { |
| 520 | flash.flash(td3.incHostLabIndex()); |
| 521 | tms.findHosts().forEach(function (d) { |
| 522 | td3.updateHostLabel(d); |
| 523 | }); |
| 524 | } |
| 525 | |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 526 | function unpin() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 527 | var hov = tss.hovered(); |
| 528 | if (hov) { |
| 529 | sendUpdateMeta(hov, true); |
| 530 | hov.fixed = false; |
| 531 | hov.el.classed('fixed', false); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 532 | fResume(); |
| 533 | } |
| 534 | } |
| 535 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 536 | function showMastership(masterId) { |
| 537 | if (!masterId) { |
| 538 | restoreLayerState(); |
| 539 | } else { |
| 540 | showMastershipFor(masterId); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | function restoreLayerState() { |
| 545 | // NOTE: this level of indirection required, for when we have |
| 546 | // the layer filter functionality re-implemented |
| 547 | suppressLayers(false); |
| 548 | } |
| 549 | |
| 550 | function showMastershipFor(id) { |
| 551 | suppressLayers(true); |
| 552 | node.each(function (n) { |
| 553 | if (n.master === id) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 554 | n.el.classed('suppressedmax', false); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 555 | } |
| 556 | }); |
| 557 | } |
| 558 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 559 | function supAmt(less) { |
| 560 | return less ? "suppressed" : "suppressedmax"; |
| 561 | } |
| 562 | |
| 563 | function suppressLayers(b, less) { |
| 564 | var cls = supAmt(less); |
| 565 | node.classed(cls, b); |
| 566 | link.classed(cls, b); |
| 567 | } |
| 568 | |
| 569 | function unsuppressNode(id, less) { |
| 570 | var cls = supAmt(less); |
| 571 | node.each(function (n) { |
| 572 | if (n.id === id) { |
| 573 | n.el.classed(cls, false); |
| 574 | } |
| 575 | }); |
| 576 | } |
| 577 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 578 | function unsuppressLink(key, less) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 579 | var cls = supAmt(less); |
| 580 | link.each(function (n) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 581 | if (n.key === key) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 582 | n.el.classed(cls, false); |
| 583 | } |
| 584 | }); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 585 | } |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 586 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 587 | function showBadLinks() { |
| 588 | var badLinks = tms.findBadLinks(); |
| 589 | flash.flash('Bad Links: ' + badLinks.length); |
| 590 | $log.debug('Bad Link List (' + badLinks.length + '):'); |
| 591 | badLinks.forEach(function (d) { |
| 592 | $log.debug('bad link: (' + d.bad + ') ' + d.key, d); |
| 593 | if (d.el) { |
| 594 | d.el.attr('stroke-width', linkScale(2.8)) |
| 595 | .attr('stroke', 'red'); |
| 596 | } |
| 597 | }); |
| 598 | // back to normal after 2 seconds... |
| 599 | $timeout(updateLinks, 2000); |
| 600 | } |
| 601 | |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 602 | function deviceScale() { |
| 603 | var scale = uplink.zoomer().scale(), |
| 604 | dim = devIconDim, |
| 605 | multiplier = 1; |
| 606 | |
| 607 | if (dim * scale < devIconDimMin) { |
| 608 | multiplier = devIconDimMin / (dim * scale); |
| 609 | } else if (dim * scale > devIconDimMax) { |
| 610 | multiplier = devIconDimMax / (dim * scale); |
| 611 | } |
| 612 | |
| 613 | return multiplier; |
| 614 | } |
| 615 | |
| 616 | function linkWidthScale(scale) { |
| 617 | var scale = uplink.zoomer().scale(); |
| 618 | return linkScale(widthRatio) / scale; |
| 619 | } |
| 620 | |
| 621 | function portLabelScale(scale) { |
| 622 | var scale = uplink.zoomer().scale(); |
| 623 | return portLabelDim / (portLabelDim * scale); |
| 624 | } |
| 625 | |
| 626 | function setNodeScale(scale) { |
| 627 | // Scale the network nodes |
| 628 | _.each(network.nodes, function (node) { |
| 629 | if (node.class === 'host') { |
| 630 | node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 631 | node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 632 | return; |
| 633 | } |
| 634 | node.el.selectAll('*') |
| 635 | .style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 636 | }); |
| 637 | |
| 638 | // Scale the network links |
| 639 | _.each(network.links, function (link) { |
| 640 | link.el.style('stroke-width', linkWidthScale(scale) + 'px'); |
| 641 | }); |
| 642 | |
| 643 | d3.select('#topo-portLabels') |
| 644 | .selectAll('.portLabel') |
| 645 | .selectAll('*') |
| 646 | .style('transform', 'scale(' + portLabelScale(scale) + ')'); |
| 647 | } |
| 648 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 649 | function resetAllLocations() { |
| 650 | tms.resetAllLocations(); |
| 651 | updateNodes(); |
| 652 | tick(); // force nodes to be redrawn in their new locations |
| 653 | flash.flash('Reset Node Locations'); |
| 654 | } |
| 655 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 656 | // ========================================== |
| 657 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 658 | function updateNodes() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 659 | if (fNodesTimer) { |
| 660 | $timeout.cancel(fNodesTimer); |
| 661 | } |
| 662 | fNodesTimer = $timeout(_updateNodes, 150); |
| 663 | } |
| 664 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 665 | // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume |
| 666 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 667 | function _updateNodes() { |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 668 | |
| 669 | var scale = uplink.zoomer().scale(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 670 | // select all the nodes in the layout: |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 671 | node = nodeG.selectAll('.node') |
| 672 | .data(network.nodes, function (d) { return d.id; }); |
| 673 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 674 | // operate on existing nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 675 | node.filter('.device').each(td3.deviceExisting); |
| 676 | node.filter('.host').each(td3.hostExisting); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 677 | |
| 678 | // operate on entering nodes: |
| 679 | var entering = node.enter() |
| 680 | .append('g') |
| 681 | .attr({ |
| 682 | id: function (d) { return sus.safeId(d.id); }, |
| 683 | class: mkSvgClass, |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 684 | transform: function (d) { |
| 685 | // Need to guard against NaN here ?? |
| 686 | return sus.translate(d.x, d.y); |
| 687 | }, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 688 | opacity: 0 |
| 689 | }) |
| 690 | .call(drag) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 691 | .on('mouseover', tss.nodeMouseOver) |
| 692 | .on('mouseout', tss.nodeMouseOut) |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 693 | .transition() |
| 694 | .attr('opacity', 1); |
| 695 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 696 | // augment entering nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 697 | entering.filter('.device').each(td3.deviceEnter); |
| 698 | entering.filter('.host').each(td3.hostEnter); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 699 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 700 | // operate on both existing and new nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 701 | td3.updateDeviceColors(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 702 | |
| 703 | // operate on exiting nodes: |
| 704 | // Note that the node is removed after 2 seconds. |
| 705 | // Sub element animations should be shorter than 2 seconds. |
| 706 | var exiting = node.exit() |
| 707 | .transition() |
| 708 | .duration(2000) |
| 709 | .style('opacity', 0) |
| 710 | .remove(); |
| 711 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 712 | // exiting node specifics: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 713 | exiting.filter('.host').each(td3.hostExit); |
| 714 | exiting.filter('.device').each(td3.deviceExit); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 717 | // ========================== |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 718 | |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 719 | function getDefaultPos(link) { |
| 720 | return { |
| 721 | x1: link.source.x, |
| 722 | y1: link.source.y, |
| 723 | x2: link.target.x, |
| 724 | y2: link.target.y |
| 725 | }; |
| 726 | } |
| 727 | |
| 728 | // returns amount of adjustment along the normal for given link |
| 729 | function amt(numLinks, linkIdx) { |
| 730 | var gap = 6; |
| 731 | return (linkIdx - ((numLinks - 1) / 2)) * gap; |
| 732 | } |
| 733 | |
| 734 | function calcMovement(d, amt, flipped) { |
| 735 | var pos = getDefaultPos(d), |
| 736 | mult = flipped ? -amt : amt, |
| 737 | dx = pos.x2 - pos.x1, |
| 738 | dy = pos.y2 - pos.y1, |
| 739 | length = Math.sqrt((dx * dx) + (dy * dy)); |
| 740 | |
| 741 | return { |
| 742 | x1: pos.x1 + (mult * dy / length), |
| 743 | y1: pos.y1 + (mult * -dx / length), |
| 744 | x2: pos.x2 + (mult * dy / length), |
| 745 | y2: pos.y2 + (mult * -dx / length) |
| 746 | }; |
| 747 | } |
| 748 | |
| 749 | function calcPosition() { |
| 750 | var lines = this, |
| 751 | linkSrcId; |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 752 | linkNums = []; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 753 | lines.each(function (d) { |
| 754 | if (d.type() === 'hostLink') { |
| 755 | d.position = getDefaultPos(d); |
| 756 | } |
| 757 | }); |
| 758 | |
| 759 | function normalizeLinkSrc(link) { |
| 760 | // ensure source device is consistent across set of links |
| 761 | // temporary measure until link modeling is refactored |
| 762 | if (!linkSrcId) { |
| 763 | linkSrcId = link.source.id; |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | return link.source.id !== linkSrcId; |
| 768 | } |
| 769 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 770 | angular.forEach(network.linksByDevice, function (linkArr, key) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 771 | var numLinks = linkArr.length, |
| 772 | link; |
| 773 | |
| 774 | if (numLinks === 1) { |
| 775 | link = linkArr[0]; |
| 776 | link.position = getDefaultPos(link); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 777 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 778 | } else if (numLinks >= 5) { |
| 779 | // this code is inefficient, in the future the way links |
| 780 | // are modeled will be changed |
| 781 | angular.forEach(linkArr, function (link) { |
| 782 | link.position = getDefaultPos(link); |
| 783 | link.position.multiLink = true; |
| 784 | }); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 785 | linkNums.push({ |
| 786 | id: key, |
| 787 | num: numLinks, |
| 788 | linkCoords: linkArr[0].position |
| 789 | }); |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 790 | } else { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 791 | linkSrcId = null; |
| 792 | angular.forEach(linkArr, function (link, index) { |
| 793 | var offsetAmt = amt(numLinks, index), |
| 794 | needToFlip = normalizeLinkSrc(link); |
| 795 | link.position = calcMovement(link, offsetAmt, needToFlip); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 796 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 797 | }); |
| 798 | } |
| 799 | }); |
| 800 | } |
| 801 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 802 | function updateLinks() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 803 | if (fLinksTimer) { |
| 804 | $timeout.cancel(fLinksTimer); |
| 805 | } |
| 806 | fLinksTimer = $timeout(_updateLinks, 150); |
| 807 | } |
| 808 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 809 | // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume |
| 810 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 811 | function _updateLinks() { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 812 | var th = ts.theme(); |
| 813 | |
| 814 | link = linkG.selectAll('.link') |
| 815 | .data(network.links, function (d) { return d.key; }); |
| 816 | |
| 817 | // operate on existing links: |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 818 | link.each(function (d) { |
| 819 | // this is supposed to be an existing link, but we have observed |
| 820 | // occasions (where links are deleted and added rapidly?) where |
| 821 | // the DOM element has not been defined. So protect against that... |
| 822 | if (d.el) { |
| 823 | restyleLinkElement(d, true); |
| 824 | } |
| 825 | }); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 826 | |
| 827 | // operate on entering links: |
| 828 | var entering = link.enter() |
| 829 | .append('line') |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 830 | .call(calcPosition) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 831 | .attr({ |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 832 | x1: function (d) { return d.position.x1; }, |
| 833 | y1: function (d) { return d.position.y1; }, |
| 834 | x2: function (d) { return d.position.x2; }, |
| 835 | y2: function (d) { return d.position.y2; }, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 836 | stroke: linkConfig[th].inColor, |
| 837 | 'stroke-width': linkConfig.inWidth |
| 838 | }); |
| 839 | |
| 840 | // augment links |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 841 | entering.each(td3.linkEntering); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 842 | |
| 843 | // operate on both existing and new links: |
| 844 | //link.each(...) |
| 845 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 846 | // add labels for how many links are in a thick line |
| 847 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
| 848 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 849 | // apply or remove labels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 850 | td3.applyLinkLabels(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 851 | |
| 852 | // operate on exiting links: |
| 853 | link.exit() |
| 854 | .attr('stroke-dasharray', '3 3') |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 855 | .attr('stroke', linkConfig[th].outColor) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 856 | .style('opacity', 0.5) |
| 857 | .transition() |
| 858 | .duration(1500) |
| 859 | .attr({ |
| 860 | 'stroke-dasharray': '3 12', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 861 | 'stroke-width': linkConfig.outWidth |
| 862 | }) |
| 863 | .style('opacity', 0.0) |
| 864 | .remove(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 867 | |
| 868 | // ========================== |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 869 | // force layout tick function |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 870 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 871 | function fResume() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 872 | if (!tos.isOblique()) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 873 | force.resume(); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | function fStart() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 878 | if (!tos.isOblique()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 879 | if (fTimer) { |
| 880 | $timeout.cancel(fTimer); |
| 881 | } |
| 882 | fTimer = $timeout(function () { |
| 883 | $log.debug("Starting force-layout"); |
| 884 | force.start(); |
| 885 | }, 200); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | var tickStuff = { |
| 890 | nodeAttr: { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 891 | transform: function (d) { |
| 892 | var dx = isNaN(d.x) ? 0 : d.x, |
| 893 | dy = isNaN(d.y) ? 0 : d.y; |
| 894 | return sus.translate(dx, dy); |
| 895 | } |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 896 | }, |
| 897 | linkAttr: { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 898 | x1: function (d) { return d.position.x1; }, |
| 899 | y1: function (d) { return d.position.y1; }, |
| 900 | x2: function (d) { return d.position.x2; }, |
| 901 | y2: function (d) { return d.position.y2; } |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 902 | }, |
| 903 | linkLabelAttr: { |
| 904 | transform: function (d) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 905 | var lnk = tms.findLinkById(d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 906 | if (lnk) { |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 907 | return td3.transformLabel(lnk.position, d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | } |
| 911 | }; |
| 912 | |
| 913 | function tick() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 914 | // guard against null (which can happen when our view pages out)... |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 915 | if (node && node.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 916 | node.attr(tickStuff.nodeAttr); |
| 917 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 918 | if (link && link.size()) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 919 | link.call(calcPosition) |
| 920 | .attr(tickStuff.linkAttr); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 921 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 922 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 923 | if (linkLabel && linkLabel.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 924 | linkLabel.attr(tickStuff.linkLabelAttr); |
| 925 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 929 | // ========================== |
| 930 | // === MOUSE GESTURE HANDLERS |
| 931 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 932 | function zoomingOrPanning(ev) { |
| 933 | return ev.metaKey || ev.altKey; |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | function atDragEnd(d) { |
| 937 | // once we've finished moving, pin the node in position |
| 938 | d.fixed = true; |
| 939 | d3.select(this).classed('fixed', true); |
| 940 | sendUpdateMeta(d); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 941 | tss.clickConsumed(true); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | // predicate that indicates when dragging is active |
| 945 | function dragEnabled() { |
| 946 | var ev = d3.event.sourceEvent; |
| 947 | // nodeLock means we aren't allowing nodes to be dragged... |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 948 | return !nodeLock && !zoomingOrPanning(ev); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // predicate that indicates when clicking is active |
| 952 | function clickEnabled() { |
| 953 | return true; |
| 954 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 955 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 956 | // ============================================= |
| 957 | // function entry points for overlay module |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 958 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 959 | // TODO: find an automatic way of tracking via the "showHighlights" events |
Simon Hunt | e50829c | 2015-06-09 08:39:28 -0700 | [diff] [blame] | 960 | var allTrafficClasses = 'primary secondary optical animated ' + |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 961 | 'port-traffic-green port-traffic-yellow port-traffic-orange ' + |
| 962 | 'port-traffic-red'; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 963 | |
| 964 | function clearLinkTrafficStyle() { |
| 965 | link.style('stroke-width', null) |
| 966 | .classed(allTrafficClasses, false); |
| 967 | } |
| 968 | |
| 969 | function removeLinkLabels() { |
| 970 | network.links.forEach(function (d) { |
| 971 | d.label = ''; |
| 972 | }); |
| 973 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 974 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 975 | function clearNodeDeco() { |
| 976 | node.selectAll('g.badge').remove(); |
| 977 | } |
| 978 | |
| 979 | function removeNodeBadges() { |
| 980 | network.nodes.forEach(function (d) { |
| 981 | d.badge = null; |
| 982 | }); |
| 983 | } |
| 984 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 985 | function updateLinkLabelModel() { |
| 986 | // create the backing data for showing labels.. |
| 987 | var data = []; |
| 988 | link.each(function (d) { |
| 989 | if (d.label) { |
| 990 | data.push({ |
| 991 | id: 'lab-' + d.key, |
| 992 | key: d.key, |
| 993 | label: d.label, |
| 994 | ldata: d |
| 995 | }); |
| 996 | } |
| 997 | }); |
| 998 | |
| 999 | linkLabel = linkLabelG.selectAll('.linkLabel') |
| 1000 | .data(data, function (d) { return d.id; }); |
| 1001 | } |
| 1002 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1003 | // ========================== |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1004 | // Module definition |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1005 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1006 | function mkModelApi(uplink) { |
| 1007 | return { |
| 1008 | projection: uplink.projection, |
| 1009 | network: network, |
| 1010 | restyleLinkElement: restyleLinkElement, |
| 1011 | removeLinkElement: removeLinkElement |
| 1012 | }; |
| 1013 | } |
| 1014 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1015 | function mkD3Api() { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1016 | return { |
| 1017 | node: function () { return node; }, |
| 1018 | link: function () { return link; }, |
| 1019 | linkLabel: function () { return linkLabel; }, |
| 1020 | instVisible: function () { return tis.isVisible(); }, |
| 1021 | posNode: tms.positionNode, |
| 1022 | showHosts: function () { return showHosts; }, |
| 1023 | restyleLinkElement: restyleLinkElement, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1024 | updateLinkLabelModel: updateLinkLabelModel, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1025 | linkConfig: function () { return linkConfig; }, |
| 1026 | deviceScale: deviceScale, |
Simon Hunt | 4a850cc | 2017-06-08 14:30:04 -0700 | [diff] [blame] | 1027 | linkWidthScale: linkWidthScale |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1028 | }; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1031 | function mkSelectApi() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1032 | return { |
| 1033 | node: function () { return node; }, |
| 1034 | zoomingOrPanning: zoomingOrPanning, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 1035 | updateDeviceColors: td3.updateDeviceColors, |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 1036 | deselectAllLinks: tls.deselectAllLinks |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1037 | }; |
| 1038 | } |
| 1039 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1040 | function mkTrafficApi() { |
| 1041 | return { |
| 1042 | hovered: tss.hovered, |
| 1043 | somethingSelected: tss.somethingSelected, |
| 1044 | selectOrder: tss.selectOrder |
| 1045 | }; |
| 1046 | } |
| 1047 | |
| 1048 | function mkOverlayApi() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1049 | return { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 1050 | clearNodeDeco: clearNodeDeco, |
| 1051 | removeNodeBadges: removeNodeBadges, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1052 | clearLinkTrafficStyle: clearLinkTrafficStyle, |
| 1053 | removeLinkLabels: removeLinkLabels, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1054 | findLinkById: tms.findLinkById, |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 1055 | findNodeById: nodeById, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1056 | updateLinks: updateLinks, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1057 | updateNodes: updateNodes, |
| 1058 | supLayers: suppressLayers, |
| 1059 | unsupNode: unsuppressNode, |
| 1060 | unsupLink: unsuppressLink |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1061 | }; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1064 | function mkObliqueApi(uplink, fltr) { |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1065 | return { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1066 | force: function() { return force; }, |
| 1067 | zoomLayer: uplink.zoomLayer, |
| 1068 | nodeGBBox: function() { return nodeG.node().getBBox(); }, |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1069 | node: function () { return node; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1070 | link: function () { return link; }, |
| 1071 | linkLabel: function () { return linkLabel; }, |
| 1072 | nodes: function () { return network.nodes; }, |
| 1073 | tickStuff: tickStuff, |
| 1074 | nodeLock: function (b) { |
| 1075 | var old = nodeLock; |
| 1076 | nodeLock = b; |
| 1077 | return old; |
| 1078 | }, |
| 1079 | opacifyMap: uplink.opacifyMap, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 1080 | inLayer: fltr.inLayer, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1081 | calcLinkPos: calcPosition, |
| 1082 | applyNumLinkLabels: function () { |
| 1083 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
| 1084 | } |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1085 | }; |
| 1086 | } |
| 1087 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1088 | function mkFilterApi() { |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1089 | return { |
| 1090 | node: function () { return node; }, |
| 1091 | link: function () { return link; } |
| 1092 | }; |
| 1093 | } |
| 1094 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1095 | function mkLinkApi(svg, uplink) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1096 | return { |
| 1097 | svg: svg, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1098 | zoomer: uplink.zoomer(), |
| 1099 | network: network, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1100 | portLabelG: function () { return portLabelG; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1101 | showHosts: function () { return showHosts; } |
| 1102 | }; |
| 1103 | } |
| 1104 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1105 | function updateLinksAndNodes() { |
| 1106 | updateLinks(); |
| 1107 | updateNodes(); |
| 1108 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1109 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1110 | angular.module('ovTopo') |
| 1111 | .factory('TopoForceService', |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1112 | ['$log', '$timeout', 'FnService', 'SvgUtilService', |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1113 | 'ThemeService', 'FlashService', 'WebSocketService', |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1114 | 'TopoOverlayService', 'TopoInstService', 'TopoModelService', |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1115 | 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1116 | 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService', |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1117 | 'TopoProtectedIntentsService', |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1118 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1119 | function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_, |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1120 | _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1121 | $log = _$log_; |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1122 | $timeout = _$timeout_; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1123 | fs = _fs_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1124 | sus = _sus_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1125 | ts = _ts_; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1126 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 1127 | wss = _wss_; |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1128 | tov = _tov_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1129 | tis = _tis_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1130 | tms = _tms_; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1131 | td3 = _td3_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1132 | tss = _tss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1133 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1134 | tos = _tos_; |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1135 | fltr = _fltr_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1136 | tls = _tls_; |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1137 | tpis = _tpis_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1138 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1139 | ts.addListener(updateLinksAndNodes); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1140 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1141 | // forceG is the SVG group to display the force layout in |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1142 | // uplink is the api from the main topo source file |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1143 | // dim is the initial dimensions of the SVG as [w,h] |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1144 | // opts are, well, optional :) |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1145 | function initForce(_svg_, forceG, _uplink_, _dim_, opts) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1146 | uplink = _uplink_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1147 | dim = _dim_; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1148 | svg = _svg_; |
| 1149 | |
| 1150 | lu = network.lookup; |
| 1151 | rlk = network.revLinkToKey; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1152 | |
| 1153 | $log.debug('initForce().. dim = ' + dim); |
| 1154 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1155 | tov.setApi(mkOverlayApi(), tss); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1156 | tms.initModel(mkModelApi(uplink), dim); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1157 | td3.initD3(mkD3Api(), uplink.zoomer()); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1158 | tss.initSelect(mkSelectApi()); |
| 1159 | tts.initTraffic(mkTrafficApi()); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1160 | tpis.initProtectedIntents(mkTrafficApi()); |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1161 | tos.initOblique(mkObliqueApi(uplink, fltr)); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1162 | fltr.initFilter(mkFilterApi()); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1163 | tls.initLink(mkLinkApi(svg, uplink), td3); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 1164 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1165 | settings = angular.extend({}, defaultSettings, opts); |
| 1166 | |
| 1167 | linkG = forceG.append('g').attr('id', 'topo-links'); |
| 1168 | linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels'); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1169 | numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1170 | nodeG = forceG.append('g').attr('id', 'topo-nodes'); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1171 | portLabelG = forceG.append('g').attr('id', 'topo-portLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1172 | |
| 1173 | link = linkG.selectAll('.link'); |
| 1174 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
| 1175 | node = nodeG.selectAll('.node'); |
| 1176 | |
| 1177 | force = d3.layout.force() |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1178 | .size(dim) |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1179 | .nodes(network.nodes) |
| 1180 | .links(network.links) |
| 1181 | .gravity(settings.gravity) |
| 1182 | .friction(settings.friction) |
| 1183 | .charge(settings.charge._def_) |
| 1184 | .linkDistance(settings.linkDistance._def_) |
| 1185 | .linkStrength(settings.linkStrength._def_) |
| 1186 | .on('tick', tick); |
| 1187 | |
| 1188 | drag = sus.createDragBehavior(force, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1189 | tss.selectObject, atDragEnd, dragEnabled, clickEnabled); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1192 | function newDim(_dim_) { |
| 1193 | dim = _dim_; |
| 1194 | force.size(dim); |
| 1195 | tms.newDim(dim); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1198 | function destroyForce() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1199 | force.stop(); |
| 1200 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1201 | tls.destroyLink(); |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1202 | tos.destroyOblique(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1203 | tts.destroyTraffic(); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1204 | tpis.destroyProtectedIntents(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1205 | tss.destroySelect(); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1206 | td3.destroyD3(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1207 | tms.destroyModel(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1208 | // note: no need to destroy overlay service |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1209 | ts.removeListener(updateLinksAndNodes); |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1210 | |
| 1211 | // clean up the DOM |
| 1212 | svg.selectAll('g').remove(); |
| 1213 | svg.selectAll('defs').remove(); |
| 1214 | |
| 1215 | // clean up internal state |
| 1216 | network.nodes = []; |
| 1217 | network.links = []; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1218 | network.linksByDevice = {}; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1219 | network.lookup = {}; |
| 1220 | network.revLinkToKey = {}; |
| 1221 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1222 | linkNums = []; |
| 1223 | |
| 1224 | linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1225 | link = linkLabel = node = null; |
| 1226 | force = drag = null; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1227 | |
| 1228 | // clean up $timeout promises |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1229 | if (fTimer) { |
| 1230 | $timeout.cancel(fTimer); |
| 1231 | } |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1232 | if (fNodesTimer) { |
| 1233 | $timeout.cancel(fNodesTimer); |
| 1234 | } |
| 1235 | if (fLinksTimer) { |
| 1236 | $timeout.cancel(fLinksTimer); |
| 1237 | } |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1240 | return { |
| 1241 | initForce: initForce, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1242 | newDim: newDim, |
| 1243 | destroyForce: destroyForce, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1244 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1245 | updateDeviceColors: td3.updateDeviceColors, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1246 | toggleHosts: toggleHosts, |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1247 | togglePorts: tls.togglePorts, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1248 | toggleOffline: toggleOffline, |
| 1249 | cycleDeviceLabels: cycleDeviceLabels, |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame^] | 1250 | cycleHostLabels: cycleHostLabels, |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 1251 | unpin: unpin, |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1252 | showMastership: showMastership, |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1253 | showBadLinks: showBadLinks, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1254 | setNodeScale: setNodeScale, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1255 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 1256 | resetAllLocations: resetAllLocations, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1257 | addDevice: addDevice, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1258 | updateDevice: updateDevice, |
| 1259 | removeDevice: removeDevice, |
| 1260 | addHost: addHost, |
| 1261 | updateHost: updateHost, |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 1262 | moveHost: moveHost, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1263 | removeHost: removeHost, |
| 1264 | addLink: addLink, |
| 1265 | updateLink: updateLink, |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 1266 | removeLink: removeLink, |
| 1267 | topoStartDone: topoStartDone |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1268 | }; |
| 1269 | }]); |
| 1270 | }()); |