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 | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 519 | function unpin() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 520 | var hov = tss.hovered(); |
| 521 | if (hov) { |
| 522 | sendUpdateMeta(hov, true); |
| 523 | hov.fixed = false; |
| 524 | hov.el.classed('fixed', false); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 525 | fResume(); |
| 526 | } |
| 527 | } |
| 528 | |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 529 | function showMastership(masterId) { |
| 530 | if (!masterId) { |
| 531 | restoreLayerState(); |
| 532 | } else { |
| 533 | showMastershipFor(masterId); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | function restoreLayerState() { |
| 538 | // NOTE: this level of indirection required, for when we have |
| 539 | // the layer filter functionality re-implemented |
| 540 | suppressLayers(false); |
| 541 | } |
| 542 | |
| 543 | function showMastershipFor(id) { |
| 544 | suppressLayers(true); |
| 545 | node.each(function (n) { |
| 546 | if (n.master === id) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 547 | n.el.classed('suppressedmax', false); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 548 | } |
| 549 | }); |
| 550 | } |
| 551 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 552 | function supAmt(less) { |
| 553 | return less ? "suppressed" : "suppressedmax"; |
| 554 | } |
| 555 | |
| 556 | function suppressLayers(b, less) { |
| 557 | var cls = supAmt(less); |
| 558 | node.classed(cls, b); |
| 559 | link.classed(cls, b); |
| 560 | } |
| 561 | |
| 562 | function unsuppressNode(id, less) { |
| 563 | var cls = supAmt(less); |
| 564 | node.each(function (n) { |
| 565 | if (n.id === id) { |
| 566 | n.el.classed(cls, false); |
| 567 | } |
| 568 | }); |
| 569 | } |
| 570 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 571 | function unsuppressLink(key, less) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 572 | var cls = supAmt(less); |
| 573 | link.each(function (n) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 574 | if (n.key === key) { |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 575 | n.el.classed(cls, false); |
| 576 | } |
| 577 | }); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 578 | } |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 579 | |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 580 | function showBadLinks() { |
| 581 | var badLinks = tms.findBadLinks(); |
| 582 | flash.flash('Bad Links: ' + badLinks.length); |
| 583 | $log.debug('Bad Link List (' + badLinks.length + '):'); |
| 584 | badLinks.forEach(function (d) { |
| 585 | $log.debug('bad link: (' + d.bad + ') ' + d.key, d); |
| 586 | if (d.el) { |
| 587 | d.el.attr('stroke-width', linkScale(2.8)) |
| 588 | .attr('stroke', 'red'); |
| 589 | } |
| 590 | }); |
| 591 | // back to normal after 2 seconds... |
| 592 | $timeout(updateLinks, 2000); |
| 593 | } |
| 594 | |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 595 | function deviceScale() { |
| 596 | var scale = uplink.zoomer().scale(), |
| 597 | dim = devIconDim, |
| 598 | multiplier = 1; |
| 599 | |
| 600 | if (dim * scale < devIconDimMin) { |
| 601 | multiplier = devIconDimMin / (dim * scale); |
| 602 | } else if (dim * scale > devIconDimMax) { |
| 603 | multiplier = devIconDimMax / (dim * scale); |
| 604 | } |
| 605 | |
| 606 | return multiplier; |
| 607 | } |
| 608 | |
| 609 | function linkWidthScale(scale) { |
| 610 | var scale = uplink.zoomer().scale(); |
| 611 | return linkScale(widthRatio) / scale; |
| 612 | } |
| 613 | |
| 614 | function portLabelScale(scale) { |
| 615 | var scale = uplink.zoomer().scale(); |
| 616 | return portLabelDim / (portLabelDim * scale); |
| 617 | } |
| 618 | |
| 619 | function setNodeScale(scale) { |
| 620 | // Scale the network nodes |
| 621 | _.each(network.nodes, function (node) { |
| 622 | if (node.class === 'host') { |
| 623 | node.el.selectAll('g').style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 624 | node.el.selectAll('text').style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 625 | return; |
| 626 | } |
| 627 | node.el.selectAll('*') |
| 628 | .style('transform', 'scale(' + deviceScale(scale) + ')'); |
| 629 | }); |
| 630 | |
| 631 | // Scale the network links |
| 632 | _.each(network.links, function (link) { |
| 633 | link.el.style('stroke-width', linkWidthScale(scale) + 'px'); |
| 634 | }); |
| 635 | |
| 636 | d3.select('#topo-portLabels') |
| 637 | .selectAll('.portLabel') |
| 638 | .selectAll('*') |
| 639 | .style('transform', 'scale(' + portLabelScale(scale) + ')'); |
| 640 | } |
| 641 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 642 | function resetAllLocations() { |
| 643 | tms.resetAllLocations(); |
| 644 | updateNodes(); |
| 645 | tick(); // force nodes to be redrawn in their new locations |
| 646 | flash.flash('Reset Node Locations'); |
| 647 | } |
| 648 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 649 | // ========================================== |
| 650 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 651 | function updateNodes() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 652 | if (fNodesTimer) { |
| 653 | $timeout.cancel(fNodesTimer); |
| 654 | } |
| 655 | fNodesTimer = $timeout(_updateNodes, 150); |
| 656 | } |
| 657 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 658 | // IMPLEMENTATION NOTE: _updateNodes() should NOT stop, start, or resume |
| 659 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 660 | function _updateNodes() { |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 661 | |
| 662 | var scale = uplink.zoomer().scale(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 663 | // select all the nodes in the layout: |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 664 | node = nodeG.selectAll('.node') |
| 665 | .data(network.nodes, function (d) { return d.id; }); |
| 666 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 667 | // operate on existing nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 668 | node.filter('.device').each(td3.deviceExisting); |
| 669 | node.filter('.host').each(td3.hostExisting); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 670 | |
| 671 | // operate on entering nodes: |
| 672 | var entering = node.enter() |
| 673 | .append('g') |
| 674 | .attr({ |
| 675 | id: function (d) { return sus.safeId(d.id); }, |
| 676 | class: mkSvgClass, |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 677 | transform: function (d) { |
| 678 | // Need to guard against NaN here ?? |
| 679 | return sus.translate(d.x, d.y); |
| 680 | }, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 681 | opacity: 0 |
| 682 | }) |
| 683 | .call(drag) |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 684 | .on('mouseover', tss.nodeMouseOver) |
| 685 | .on('mouseout', tss.nodeMouseOut) |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 686 | .transition() |
| 687 | .attr('opacity', 1); |
| 688 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 689 | // augment entering nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 690 | entering.filter('.device').each(td3.deviceEnter); |
| 691 | entering.filter('.host').each(td3.hostEnter); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 692 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 693 | // operate on both existing and new nodes: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 694 | td3.updateDeviceColors(); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 695 | |
| 696 | // operate on exiting nodes: |
| 697 | // Note that the node is removed after 2 seconds. |
| 698 | // Sub element animations should be shorter than 2 seconds. |
| 699 | var exiting = node.exit() |
| 700 | .transition() |
| 701 | .duration(2000) |
| 702 | .style('opacity', 0) |
| 703 | .remove(); |
| 704 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 705 | // exiting node specifics: |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 706 | exiting.filter('.host').each(td3.hostExit); |
| 707 | exiting.filter('.device').each(td3.deviceExit); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Simon Hunt | 5105659 | 2015-02-03 21:48:07 -0800 | [diff] [blame] | 710 | // ========================== |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 711 | |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 712 | function getDefaultPos(link) { |
| 713 | return { |
| 714 | x1: link.source.x, |
| 715 | y1: link.source.y, |
| 716 | x2: link.target.x, |
| 717 | y2: link.target.y |
| 718 | }; |
| 719 | } |
| 720 | |
| 721 | // returns amount of adjustment along the normal for given link |
| 722 | function amt(numLinks, linkIdx) { |
| 723 | var gap = 6; |
| 724 | return (linkIdx - ((numLinks - 1) / 2)) * gap; |
| 725 | } |
| 726 | |
| 727 | function calcMovement(d, amt, flipped) { |
| 728 | var pos = getDefaultPos(d), |
| 729 | mult = flipped ? -amt : amt, |
| 730 | dx = pos.x2 - pos.x1, |
| 731 | dy = pos.y2 - pos.y1, |
| 732 | length = Math.sqrt((dx * dx) + (dy * dy)); |
| 733 | |
| 734 | return { |
| 735 | x1: pos.x1 + (mult * dy / length), |
| 736 | y1: pos.y1 + (mult * -dx / length), |
| 737 | x2: pos.x2 + (mult * dy / length), |
| 738 | y2: pos.y2 + (mult * -dx / length) |
| 739 | }; |
| 740 | } |
| 741 | |
| 742 | function calcPosition() { |
| 743 | var lines = this, |
| 744 | linkSrcId; |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 745 | linkNums = []; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 746 | lines.each(function (d) { |
| 747 | if (d.type() === 'hostLink') { |
| 748 | d.position = getDefaultPos(d); |
| 749 | } |
| 750 | }); |
| 751 | |
| 752 | function normalizeLinkSrc(link) { |
| 753 | // ensure source device is consistent across set of links |
| 754 | // temporary measure until link modeling is refactored |
| 755 | if (!linkSrcId) { |
| 756 | linkSrcId = link.source.id; |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | return link.source.id !== linkSrcId; |
| 761 | } |
| 762 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 763 | angular.forEach(network.linksByDevice, function (linkArr, key) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 764 | var numLinks = linkArr.length, |
| 765 | link; |
| 766 | |
| 767 | if (numLinks === 1) { |
| 768 | link = linkArr[0]; |
| 769 | link.position = getDefaultPos(link); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 770 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 771 | } else if (numLinks >= 5) { |
| 772 | // this code is inefficient, in the future the way links |
| 773 | // are modeled will be changed |
| 774 | angular.forEach(linkArr, function (link) { |
| 775 | link.position = getDefaultPos(link); |
| 776 | link.position.multiLink = true; |
| 777 | }); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 778 | linkNums.push({ |
| 779 | id: key, |
| 780 | num: numLinks, |
| 781 | linkCoords: linkArr[0].position |
| 782 | }); |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 783 | } else { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 784 | linkSrcId = null; |
| 785 | angular.forEach(linkArr, function (link, index) { |
| 786 | var offsetAmt = amt(numLinks, index), |
| 787 | needToFlip = normalizeLinkSrc(link); |
| 788 | link.position = calcMovement(link, offsetAmt, needToFlip); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 789 | link.position.multiLink = false; |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 790 | }); |
| 791 | } |
| 792 | }); |
| 793 | } |
| 794 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 795 | function updateLinks() { |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 796 | if (fLinksTimer) { |
| 797 | $timeout.cancel(fLinksTimer); |
| 798 | } |
| 799 | fLinksTimer = $timeout(_updateLinks, 150); |
| 800 | } |
| 801 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 802 | // IMPLEMENTATION NOTE: _updateLinks() should NOT stop, start, or resume |
| 803 | // the force layout; that needs to be determined and implemented elsewhere |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 804 | function _updateLinks() { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 805 | var th = ts.theme(); |
| 806 | |
| 807 | link = linkG.selectAll('.link') |
| 808 | .data(network.links, function (d) { return d.key; }); |
| 809 | |
| 810 | // operate on existing links: |
Simon Hunt | d526412 | 2015-02-25 10:17:43 -0800 | [diff] [blame] | 811 | link.each(function (d) { |
| 812 | // this is supposed to be an existing link, but we have observed |
| 813 | // occasions (where links are deleted and added rapidly?) where |
| 814 | // the DOM element has not been defined. So protect against that... |
| 815 | if (d.el) { |
| 816 | restyleLinkElement(d, true); |
| 817 | } |
| 818 | }); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 819 | |
| 820 | // operate on entering links: |
| 821 | var entering = link.enter() |
| 822 | .append('line') |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 823 | .call(calcPosition) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 824 | .attr({ |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 825 | x1: function (d) { return d.position.x1; }, |
| 826 | y1: function (d) { return d.position.y1; }, |
| 827 | x2: function (d) { return d.position.x2; }, |
| 828 | y2: function (d) { return d.position.y2; }, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 829 | stroke: linkConfig[th].inColor, |
| 830 | 'stroke-width': linkConfig.inWidth |
| 831 | }); |
| 832 | |
| 833 | // augment links |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 834 | entering.each(td3.linkEntering); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 835 | |
| 836 | // operate on both existing and new links: |
| 837 | //link.each(...) |
| 838 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 839 | // add labels for how many links are in a thick line |
| 840 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
| 841 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 842 | // apply or remove labels |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 843 | td3.applyLinkLabels(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 844 | |
| 845 | // operate on exiting links: |
| 846 | link.exit() |
| 847 | .attr('stroke-dasharray', '3 3') |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 848 | .attr('stroke', linkConfig[th].outColor) |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 849 | .style('opacity', 0.5) |
| 850 | .transition() |
| 851 | .duration(1500) |
| 852 | .attr({ |
| 853 | 'stroke-dasharray': '3 12', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 854 | 'stroke-width': linkConfig.outWidth |
| 855 | }) |
| 856 | .style('opacity', 0.0) |
| 857 | .remove(); |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 860 | |
| 861 | // ========================== |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 862 | // force layout tick function |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 863 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 864 | function fResume() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 865 | if (!tos.isOblique()) { |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 866 | force.resume(); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | function fStart() { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 871 | if (!tos.isOblique()) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 872 | if (fTimer) { |
| 873 | $timeout.cancel(fTimer); |
| 874 | } |
| 875 | fTimer = $timeout(function () { |
| 876 | $log.debug("Starting force-layout"); |
| 877 | force.start(); |
| 878 | }, 200); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | var tickStuff = { |
| 883 | nodeAttr: { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 884 | transform: function (d) { |
| 885 | var dx = isNaN(d.x) ? 0 : d.x, |
| 886 | dy = isNaN(d.y) ? 0 : d.y; |
| 887 | return sus.translate(dx, dy); |
| 888 | } |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 889 | }, |
| 890 | linkAttr: { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 891 | x1: function (d) { return d.position.x1; }, |
| 892 | y1: function (d) { return d.position.y1; }, |
| 893 | x2: function (d) { return d.position.x2; }, |
| 894 | y2: function (d) { return d.position.y2; } |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 895 | }, |
| 896 | linkLabelAttr: { |
| 897 | transform: function (d) { |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 898 | var lnk = tms.findLinkById(d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 899 | if (lnk) { |
Carmelo Cascone | d01eda6 | 2016-08-02 10:19:15 -0700 | [diff] [blame] | 900 | return td3.transformLabel(lnk.position, d.key); |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | } |
| 904 | }; |
| 905 | |
| 906 | function tick() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 907 | // guard against null (which can happen when our view pages out)... |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 908 | if (node && node.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 909 | node.attr(tickStuff.nodeAttr); |
| 910 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 911 | if (link && link.size()) { |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 912 | link.call(calcPosition) |
| 913 | .attr(tickStuff.linkAttr); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 914 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 915 | } |
Bri Prebilic Cole | 8d00378 | 2015-07-31 15:33:06 -0700 | [diff] [blame] | 916 | if (linkLabel && linkLabel.size()) { |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 917 | linkLabel.attr(tickStuff.linkLabelAttr); |
| 918 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 922 | // ========================== |
| 923 | // === MOUSE GESTURE HANDLERS |
| 924 | |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 925 | function zoomingOrPanning(ev) { |
| 926 | return ev.metaKey || ev.altKey; |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | function atDragEnd(d) { |
| 930 | // once we've finished moving, pin the node in position |
| 931 | d.fixed = true; |
| 932 | d3.select(this).classed('fixed', true); |
| 933 | sendUpdateMeta(d); |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 934 | tss.clickConsumed(true); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | // predicate that indicates when dragging is active |
| 938 | function dragEnabled() { |
| 939 | var ev = d3.event.sourceEvent; |
| 940 | // nodeLock means we aren't allowing nodes to be dragged... |
Simon Hunt | 205099e | 2015-02-07 13:12:01 -0800 | [diff] [blame] | 941 | return !nodeLock && !zoomingOrPanning(ev); |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | // predicate that indicates when clicking is active |
| 945 | function clickEnabled() { |
| 946 | return true; |
| 947 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 948 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 949 | // ============================================= |
| 950 | // function entry points for overlay module |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 951 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 952 | // TODO: find an automatic way of tracking via the "showHighlights" events |
Simon Hunt | e50829c | 2015-06-09 08:39:28 -0700 | [diff] [blame] | 953 | var allTrafficClasses = 'primary secondary optical animated ' + |
Simon Hunt | 21281fd | 2017-03-30 22:28:28 -0700 | [diff] [blame] | 954 | 'port-traffic-green port-traffic-yellow port-traffic-orange ' + |
| 955 | 'port-traffic-red'; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 956 | |
| 957 | function clearLinkTrafficStyle() { |
| 958 | link.style('stroke-width', null) |
| 959 | .classed(allTrafficClasses, false); |
| 960 | } |
| 961 | |
| 962 | function removeLinkLabels() { |
| 963 | network.links.forEach(function (d) { |
| 964 | d.label = ''; |
| 965 | }); |
| 966 | } |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 967 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 968 | function clearNodeDeco() { |
| 969 | node.selectAll('g.badge').remove(); |
| 970 | } |
| 971 | |
| 972 | function removeNodeBadges() { |
| 973 | network.nodes.forEach(function (d) { |
| 974 | d.badge = null; |
| 975 | }); |
| 976 | } |
| 977 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 978 | function updateLinkLabelModel() { |
| 979 | // create the backing data for showing labels.. |
| 980 | var data = []; |
| 981 | link.each(function (d) { |
| 982 | if (d.label) { |
| 983 | data.push({ |
| 984 | id: 'lab-' + d.key, |
| 985 | key: d.key, |
| 986 | label: d.label, |
| 987 | ldata: d |
| 988 | }); |
| 989 | } |
| 990 | }); |
| 991 | |
| 992 | linkLabel = linkLabelG.selectAll('.linkLabel') |
| 993 | .data(data, function (d) { return d.id; }); |
| 994 | } |
| 995 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 996 | // ========================== |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 997 | // Module definition |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 998 | |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 999 | function mkModelApi(uplink) { |
| 1000 | return { |
| 1001 | projection: uplink.projection, |
| 1002 | network: network, |
| 1003 | restyleLinkElement: restyleLinkElement, |
| 1004 | removeLinkElement: removeLinkElement |
| 1005 | }; |
| 1006 | } |
| 1007 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1008 | function mkD3Api() { |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1009 | return { |
| 1010 | node: function () { return node; }, |
| 1011 | link: function () { return link; }, |
| 1012 | linkLabel: function () { return linkLabel; }, |
| 1013 | instVisible: function () { return tis.isVisible(); }, |
| 1014 | posNode: tms.positionNode, |
| 1015 | showHosts: function () { return showHosts; }, |
| 1016 | restyleLinkElement: restyleLinkElement, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1017 | updateLinkLabelModel: updateLinkLabelModel, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1018 | linkConfig: function () { return linkConfig; }, |
| 1019 | deviceScale: deviceScale, |
| 1020 | linkWidthScale: linkWidthScale, |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1021 | }; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1024 | function mkSelectApi() { |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1025 | return { |
| 1026 | node: function () { return node; }, |
| 1027 | zoomingOrPanning: zoomingOrPanning, |
Simon Hunt | 0c6b2d3 | 2015-03-26 17:46:29 -0700 | [diff] [blame] | 1028 | updateDeviceColors: td3.updateDeviceColors, |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 1029 | deselectAllLinks: tls.deselectAllLinks |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1030 | }; |
| 1031 | } |
| 1032 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1033 | function mkTrafficApi() { |
| 1034 | return { |
| 1035 | hovered: tss.hovered, |
| 1036 | somethingSelected: tss.somethingSelected, |
| 1037 | selectOrder: tss.selectOrder |
| 1038 | }; |
| 1039 | } |
| 1040 | |
| 1041 | function mkOverlayApi() { |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1042 | return { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 1043 | clearNodeDeco: clearNodeDeco, |
| 1044 | removeNodeBadges: removeNodeBadges, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1045 | clearLinkTrafficStyle: clearLinkTrafficStyle, |
| 1046 | removeLinkLabels: removeLinkLabels, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1047 | findLinkById: tms.findLinkById, |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame] | 1048 | findNodeById: nodeById, |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1049 | updateLinks: updateLinks, |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 1050 | updateNodes: updateNodes, |
| 1051 | supLayers: suppressLayers, |
| 1052 | unsupNode: unsuppressNode, |
| 1053 | unsupLink: unsuppressLink |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1054 | }; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1057 | function mkObliqueApi(uplink, fltr) { |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1058 | return { |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1059 | force: function() { return force; }, |
| 1060 | zoomLayer: uplink.zoomLayer, |
| 1061 | nodeGBBox: function() { return nodeG.node().getBBox(); }, |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1062 | node: function () { return node; }, |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1063 | link: function () { return link; }, |
| 1064 | linkLabel: function () { return linkLabel; }, |
| 1065 | nodes: function () { return network.nodes; }, |
| 1066 | tickStuff: tickStuff, |
| 1067 | nodeLock: function (b) { |
| 1068 | var old = nodeLock; |
| 1069 | nodeLock = b; |
| 1070 | return old; |
| 1071 | }, |
| 1072 | opacifyMap: uplink.opacifyMap, |
Bri Prebilic Cole | 038aedd | 2015-07-13 15:25:16 -0700 | [diff] [blame] | 1073 | inLayer: fltr.inLayer, |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1074 | calcLinkPos: calcPosition, |
| 1075 | applyNumLinkLabels: function () { |
| 1076 | td3.applyNumLinkLabels(linkNums, numLinkLblsG); |
| 1077 | } |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1078 | }; |
| 1079 | } |
| 1080 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1081 | function mkFilterApi() { |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1082 | return { |
| 1083 | node: function () { return node; }, |
| 1084 | link: function () { return link; } |
| 1085 | }; |
| 1086 | } |
| 1087 | |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1088 | function mkLinkApi(svg, uplink) { |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1089 | return { |
| 1090 | svg: svg, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1091 | zoomer: uplink.zoomer(), |
| 1092 | network: network, |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1093 | portLabelG: function () { return portLabelG; }, |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1094 | showHosts: function () { return showHosts; } |
| 1095 | }; |
| 1096 | } |
| 1097 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1098 | function updateLinksAndNodes() { |
| 1099 | updateLinks(); |
| 1100 | updateNodes(); |
| 1101 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1102 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1103 | angular.module('ovTopo') |
| 1104 | .factory('TopoForceService', |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1105 | ['$log', '$timeout', 'FnService', 'SvgUtilService', |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1106 | 'ThemeService', 'FlashService', 'WebSocketService', |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1107 | 'TopoOverlayService', 'TopoInstService', 'TopoModelService', |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1108 | 'TopoD3Service', 'TopoSelectService', 'TopoTrafficService', |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1109 | 'TopoObliqueService', 'TopoFilterService', 'TopoLinkService', |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1110 | 'TopoProtectedIntentsService', |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1111 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1112 | function (_$log_, _$timeout_, _fs_, _sus_, _ts_, _flash_, _wss_, _tov_, |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1113 | _tis_, _tms_, _td3_, _tss_, _tts_, _tos_, _fltr_, _tls_, _tpis_) { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1114 | $log = _$log_; |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1115 | $timeout = _$timeout_; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1116 | fs = _fs_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1117 | sus = _sus_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1118 | ts = _ts_; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1119 | flash = _flash_; |
Simon Hunt | 237676b5 | 2015-03-10 19:04:26 -0700 | [diff] [blame] | 1120 | wss = _wss_; |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1121 | tov = _tov_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1122 | tis = _tis_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1123 | tms = _tms_; |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1124 | td3 = _td3_; |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1125 | tss = _tss_; |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1126 | tts = _tts_; |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1127 | tos = _tos_; |
Simon Hunt | eb0fa05 | 2015-02-17 19:20:28 -0800 | [diff] [blame] | 1128 | fltr = _fltr_; |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1129 | tls = _tls_; |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1130 | tpis = _tpis_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1131 | |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1132 | ts.addListener(updateLinksAndNodes); |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1133 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1134 | // forceG is the SVG group to display the force layout in |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1135 | // uplink is the api from the main topo source file |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1136 | // dim is the initial dimensions of the SVG as [w,h] |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1137 | // opts are, well, optional :) |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1138 | function initForce(_svg_, forceG, _uplink_, _dim_, opts) { |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1139 | uplink = _uplink_; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1140 | dim = _dim_; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1141 | svg = _svg_; |
| 1142 | |
| 1143 | lu = network.lookup; |
| 1144 | rlk = network.revLinkToKey; |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1145 | |
| 1146 | $log.debug('initForce().. dim = ' + dim); |
| 1147 | |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1148 | tov.setApi(mkOverlayApi(), tss); |
Simon Hunt | dc6adea | 2015-02-09 22:29:36 -0800 | [diff] [blame] | 1149 | tms.initModel(mkModelApi(uplink), dim); |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1150 | td3.initD3(mkD3Api(), uplink.zoomer()); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1151 | tss.initSelect(mkSelectApi()); |
| 1152 | tts.initTraffic(mkTrafficApi()); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1153 | tpis.initProtectedIntents(mkTrafficApi()); |
Simon Hunt | c3c5b67 | 2015-02-20 11:32:13 -0800 | [diff] [blame] | 1154 | tos.initOblique(mkObliqueApi(uplink, fltr)); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1155 | fltr.initFilter(mkFilterApi()); |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1156 | tls.initLink(mkLinkApi(svg, uplink), td3); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 1157 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1158 | settings = angular.extend({}, defaultSettings, opts); |
| 1159 | |
| 1160 | linkG = forceG.append('g').attr('id', 'topo-links'); |
| 1161 | linkLabelG = forceG.append('g').attr('id', 'topo-linkLabels'); |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1162 | numLinkLblsG = forceG.append('g').attr('id', 'topo-numLinkLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1163 | nodeG = forceG.append('g').attr('id', 'topo-nodes'); |
Simon Hunt | 1a5301e | 2015-02-25 15:31:25 -0800 | [diff] [blame] | 1164 | portLabelG = forceG.append('g').attr('id', 'topo-portLabels'); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1165 | |
| 1166 | link = linkG.selectAll('.link'); |
| 1167 | linkLabel = linkLabelG.selectAll('.linkLabel'); |
| 1168 | node = nodeG.selectAll('.node'); |
| 1169 | |
| 1170 | force = d3.layout.force() |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1171 | .size(dim) |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1172 | .nodes(network.nodes) |
| 1173 | .links(network.links) |
| 1174 | .gravity(settings.gravity) |
| 1175 | .friction(settings.friction) |
| 1176 | .charge(settings.charge._def_) |
| 1177 | .linkDistance(settings.linkDistance._def_) |
| 1178 | .linkStrength(settings.linkStrength._def_) |
| 1179 | .on('tick', tick); |
| 1180 | |
| 1181 | drag = sus.createDragBehavior(force, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 1182 | tss.selectObject, atDragEnd, dragEnabled, clickEnabled); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1185 | function newDim(_dim_) { |
| 1186 | dim = _dim_; |
| 1187 | force.size(dim); |
| 1188 | tms.newDim(dim); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1191 | function destroyForce() { |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1192 | force.stop(); |
| 1193 | |
Simon Hunt | fb8ea1f | 2015-02-24 21:38:09 -0800 | [diff] [blame] | 1194 | tls.destroyLink(); |
Simon Hunt | 96f88c6 | 2015-02-19 17:57:25 -0800 | [diff] [blame] | 1195 | tos.destroyOblique(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1196 | tts.destroyTraffic(); |
Andrea Campanella | 732ea83 | 2017-02-06 09:25:59 -0800 | [diff] [blame] | 1197 | tpis.destroyProtectedIntents(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1198 | tss.destroySelect(); |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1199 | td3.destroyD3(); |
Simon Hunt | f542d84 | 2015-02-11 16:20:33 -0800 | [diff] [blame] | 1200 | tms.destroyModel(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 1201 | // note: no need to destroy overlay service |
Simon Hunt | f51bf46 | 2016-06-29 16:22:57 -0700 | [diff] [blame] | 1202 | ts.removeListener(updateLinksAndNodes); |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1203 | |
| 1204 | // clean up the DOM |
| 1205 | svg.selectAll('g').remove(); |
| 1206 | svg.selectAll('defs').remove(); |
| 1207 | |
| 1208 | // clean up internal state |
| 1209 | network.nodes = []; |
| 1210 | network.links = []; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1211 | network.linksByDevice = {}; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1212 | network.lookup = {}; |
| 1213 | network.revLinkToKey = {}; |
| 1214 | |
Bri Prebilic Cole | 8040176 | 2015-07-16 11:36:18 -0700 | [diff] [blame] | 1215 | linkNums = []; |
| 1216 | |
| 1217 | linkG = linkLabelG = numLinkLblsG = nodeG = portLabelG = null; |
Simon Hunt | 3ab2028 | 2015-02-26 20:32:19 -0800 | [diff] [blame] | 1218 | link = linkLabel = node = null; |
| 1219 | force = drag = null; |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1220 | |
| 1221 | // clean up $timeout promises |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1222 | if (fTimer) { |
| 1223 | $timeout.cancel(fTimer); |
| 1224 | } |
Bri Prebilic Cole | aeeb33e | 2015-07-09 15:15:54 -0700 | [diff] [blame] | 1225 | if (fNodesTimer) { |
| 1226 | $timeout.cancel(fNodesTimer); |
| 1227 | } |
| 1228 | if (fLinksTimer) { |
| 1229 | $timeout.cancel(fLinksTimer); |
| 1230 | } |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1233 | return { |
| 1234 | initForce: initForce, |
Simon Hunt | 3a6eec0 | 2015-02-09 21:16:43 -0800 | [diff] [blame] | 1235 | newDim: newDim, |
| 1236 | destroyForce: destroyForce, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1237 | |
Simon Hunt | a4242de | 2015-02-24 17:11:55 -0800 | [diff] [blame] | 1238 | updateDeviceColors: td3.updateDeviceColors, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1239 | toggleHosts: toggleHosts, |
Simon Hunt | 9e2104c | 2015-02-26 10:48:59 -0800 | [diff] [blame] | 1240 | togglePorts: tls.togglePorts, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 1241 | toggleOffline: toggleOffline, |
| 1242 | cycleDeviceLabels: cycleDeviceLabels, |
Simon Hunt | 445e815 | 2015-02-06 13:00:12 -0800 | [diff] [blame] | 1243 | unpin: unpin, |
Simon Hunt | a142dd2 | 2015-02-12 22:07:51 -0800 | [diff] [blame] | 1244 | showMastership: showMastership, |
Simon Hunt | 86b7c88 | 2015-04-02 23:06:08 -0700 | [diff] [blame] | 1245 | showBadLinks: showBadLinks, |
Steven Burrows | f17f0ab | 2017-04-11 11:03:58 -0700 | [diff] [blame] | 1246 | setNodeScale: setNodeScale, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1247 | |
Simon Hunt | fd7106c | 2016-02-09 15:05:26 -0800 | [diff] [blame] | 1248 | resetAllLocations: resetAllLocations, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 1249 | addDevice: addDevice, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1250 | updateDevice: updateDevice, |
| 1251 | removeDevice: removeDevice, |
| 1252 | addHost: addHost, |
| 1253 | updateHost: updateHost, |
Simon Hunt | 95d56fd | 2015-11-12 11:06:44 -0800 | [diff] [blame] | 1254 | moveHost: moveHost, |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 1255 | removeHost: removeHost, |
| 1256 | addLink: addLink, |
| 1257 | updateLink: updateLink, |
Simon Hunt | 4a6b54b | 2015-10-27 22:08:25 -0700 | [diff] [blame] | 1258 | removeLink: removeLink, |
| 1259 | topoStartDone: topoStartDone |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 1260 | }; |
| 1261 | }]); |
| 1262 | }()); |