Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 1 | /*global d3, document∆*/ |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 2 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 3 | d3.selection.prototype.moveToFront = function() { |
| 4 | return this.each(function(){ |
| 5 | this.parentNode.appendChild(this); |
| 6 | }); |
| 7 | }; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 8 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 9 | var line = d3.svg.line() |
| 10 | .x(function(d) { |
| 11 | return d.x; |
| 12 | }) |
| 13 | .y(function(d) { |
| 14 | return d.y; |
| 15 | }); |
| 16 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 17 | var model; |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 18 | var svg; |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 19 | var updateTopology; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 20 | var pendingLinks = {}; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 21 | var selectedFlows = []; |
| 22 | |
Paul Greyson | 97cc9f5 | 2013-04-04 01:45:40 -0700 | [diff] [blame] | 23 | var pendingTimeout = 30000; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 24 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 25 | var colors = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 26 | 'color1', |
| 27 | 'color2', |
| 28 | 'color3', |
| 29 | 'color4', |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 30 | 'color7', |
| 31 | 'color8', |
| 32 | 'color9', |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 33 | // 'color11', |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 34 | 'color12' |
| 35 | ]; |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 36 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 37 | |
| 38 | var controllerColorMap = {}; |
| 39 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 40 | function setPending(selection) { |
| 41 | selection.classed('pending', false); |
| 42 | setTimeout(function () { |
| 43 | selection.classed('pending', true); |
| 44 | }) |
| 45 | } |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 46 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 47 | function createTopologyView() { |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 48 | |
| 49 | window.addEventListener('resize', function () { |
| 50 | // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied |
| 51 | // either that or is it possible to position the paths so they get the automatic transform as well? |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 52 | // updateTopology(); |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 53 | }); |
| 54 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 55 | var svg = d3.select('#svg-container').append('svg:svg'); |
| 56 | |
| 57 | svg.append("svg:defs").append("svg:marker") |
| 58 | .attr("id", "arrow") |
| 59 | .attr("viewBox", "0 -5 10 10") |
| 60 | .attr("refX", -1) |
| 61 | .attr("markerWidth", 5) |
| 62 | .attr("markerHeight", 5) |
| 63 | .attr("orient", "auto") |
| 64 | .append("svg:path") |
Paul Greyson | 45303ac | 2013-03-23 16:44:01 -0700 | [diff] [blame] | 65 | .attr("d", "M0,-3L10,0L0,3"); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 66 | |
| 67 | return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none'). |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 68 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 71 | function updateSelectedFlowsTopology() { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 72 | // DRAW THE FLOWS |
Paul Greyson | 97cc9f5 | 2013-04-04 01:45:40 -0700 | [diff] [blame] | 73 | var topologyFlows = []; |
| 74 | selectedFlows.forEach(function (flow) { |
| 75 | if (flow) { |
| 76 | topologyFlows.push(flow); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | var flows = d3.select('svg').selectAll('.flow').data(topologyFlows); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 81 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 82 | flows.enter().append("svg:path").attr('class', 'flow') |
| 83 | .attr('stroke-dasharray', '4, 10') |
| 84 | .append('svg:animate') |
| 85 | .attr('attributeName', 'stroke-dashoffset') |
| 86 | .attr('attributeType', 'xml') |
| 87 | .attr('from', '500') |
| 88 | .attr('to', '-500') |
| 89 | .attr('dur', '20s') |
| 90 | .attr('repeatCount', 'indefinite'); |
| 91 | |
Paul Greyson | 97cc9f5 | 2013-04-04 01:45:40 -0700 | [diff] [blame] | 92 | flows.exit().remove(); |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 93 | |
| 94 | flows.attr('d', function (d) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 95 | if (!d) { |
| 96 | return; |
| 97 | } |
| 98 | var pts = []; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 99 | if (!d.dataPath.flowEntries) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 100 | // create a temporary vector to indicate the pending flow |
| 101 | var s1 = d3.select(document.getElementById(d.dataPath.srcPort.dpid.value)); |
| 102 | var s2 = d3.select(document.getElementById(d.dataPath.dstPort.dpid.value)); |
| 103 | |
| 104 | var pt1 = document.querySelector('svg').createSVGPoint(); |
| 105 | pt1.x = s1.attr('x'); |
| 106 | pt1.y = s1.attr('y'); |
| 107 | pt1 = pt1.matrixTransform(s1[0][0].getCTM()); |
| 108 | pts.push(pt1); |
| 109 | |
| 110 | var pt2 = document.querySelector('svg').createSVGPoint(); |
| 111 | pt2.x = s2.attr('x'); |
| 112 | pt2.y = s2.attr('y'); |
| 113 | pt2 = pt2.matrixTransform(s2[0][0].getCTM()); |
| 114 | pts.push(pt2); |
| 115 | |
| 116 | } else { |
| 117 | d.dataPath.flowEntries.forEach(function (flowEntry) { |
| 118 | var s = d3.select(document.getElementById(flowEntry.dpid.value)); |
| 119 | // s[0] is null if the flow entry refers to a non-existent switch |
| 120 | if (s[0][0]) { |
| 121 | var pt = document.querySelector('svg').createSVGPoint(); |
| 122 | pt.x = s.attr('x'); |
| 123 | pt.y = s.attr('y'); |
| 124 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 125 | pts.push(pt); |
| 126 | } else { |
| 127 | console.log('flow refers to non-existent switch: ' + flowEntry.dpid.value); |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | return line(pts); |
| 132 | }) |
Paul Greyson | 71b550d | 2013-03-28 11:56:19 -0700 | [diff] [blame] | 133 | .attr('id', function (d) { |
| 134 | if (d) { |
| 135 | return makeFlowKey(d); |
| 136 | } |
| 137 | }) |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 138 | .classed('pending', function (d) { |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 139 | return d && (d.createPending || d.deletePending); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 140 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 141 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 142 | // "marching ants" |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 143 | flows.select('animate').attr('from', 500); |
| 144 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | function updateSelectedFlowsTable() { |
| 148 | function rowEnter(d) { |
| 149 | var row = d3.select(this); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 150 | row.append('div').classed('deleteFlow', true); |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 151 | row.append('div').classed('flowId', true); |
| 152 | row.append('div').classed('srcDPID', true); |
| 153 | row.append('div').classed('dstDPID', true); |
| 154 | row.append('div').classed('iperf', true); |
Paul Greyson | 71b550d | 2013-03-28 11:56:19 -0700 | [diff] [blame] | 155 | |
| 156 | row.on('mouseover', function (d) { |
| 157 | if (d) { |
| 158 | var path = document.getElementById(makeFlowKey(d)); |
| 159 | d3.select(path).classed('highlight', true); |
| 160 | } |
| 161 | }); |
| 162 | row.on('mouseout', function (d) { |
| 163 | if (d) { |
| 164 | var path = document.getElementById(makeFlowKey(d)); |
| 165 | d3.select(path).classed('highlight', false); |
| 166 | } |
| 167 | }) |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | function rowUpdate(d) { |
| 171 | var row = d3.select(this); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 172 | row.select('.deleteFlow').on('click', function () { |
Paul Greyson | be8bf87 | 2013-04-04 01:53:45 -0700 | [diff] [blame^] | 173 | selectedFlows[selectedFlows.indexOf(d)] = null; |
| 174 | updateSelectedFlows(); |
| 175 | }); |
| 176 | row.on('dblclick', function () { |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 177 | if (d) { |
| 178 | var prompt = 'Delete flow ' + d.flowId.value + '?'; |
| 179 | if (confirm(prompt)) { |
| 180 | deleteFlow(d); |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 181 | d.deletePending = true; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 182 | updateSelectedFlows(); |
| 183 | |
| 184 | setTimeout(function () { |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 185 | d.deletePending = false; |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 186 | updateSelectedFlows(); |
| 187 | }, pendingTimeout) |
| 188 | }; |
| 189 | } |
| 190 | }); |
| 191 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 192 | row.select('.flowId') |
| 193 | .text(function (d) { |
| 194 | if (d) { |
| 195 | if (d.flowId) { |
| 196 | return d.flowId.value; |
| 197 | } else { |
| 198 | return '0x--'; |
| 199 | } |
| 200 | } |
| 201 | }) |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 202 | .classed('pending', d && (d.deletePending || d.createPending)); |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 203 | |
| 204 | row.select('.srcDPID') |
| 205 | .text(function (d) { |
| 206 | if (d) { |
| 207 | return d.dataPath.srcPort.dpid.value; |
| 208 | } |
| 209 | }); |
| 210 | |
| 211 | row.select('.dstDPID') |
| 212 | .text(function (d) { |
| 213 | if (d) { |
| 214 | return d.dataPath.dstPort.dpid.value; |
| 215 | } |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | var flows = d3.select('#selectedFlows') |
| 220 | .selectAll('.selectedFlow') |
| 221 | .data(selectedFlows); |
| 222 | |
| 223 | flows.enter() |
| 224 | .append('div') |
| 225 | .classed('selectedFlow', true) |
| 226 | .each(rowEnter); |
| 227 | |
| 228 | flows.each(rowUpdate); |
| 229 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 230 | flows.exit().remove(); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 233 | function updateSelectedFlows() { |
| 234 | // make sure that all of the selected flows are either |
| 235 | // 1) valid (meaning they are in the latest list of flows) |
| 236 | // 2) pending |
| 237 | if (model) { |
| 238 | var flowMap = {}; |
| 239 | model.flows.forEach(function (flow) { |
| 240 | flowMap[makeFlowKey(flow)] = flow; |
| 241 | }); |
| 242 | |
| 243 | var newSelectedFlows = []; |
| 244 | selectedFlows.forEach(function (flow) { |
| 245 | if (flow) { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame] | 246 | var liveFlow = flowMap[makeFlowKey(flow)]; |
| 247 | if (liveFlow) { |
| 248 | newSelectedFlows.push(liveFlow); |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 249 | liveFlow.deletePending = flow.deletePending; |
| 250 | } else if (flow.createPending) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 251 | newSelectedFlows.push(flow); |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 252 | } |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 253 | } |
| 254 | }); |
| 255 | selectedFlows = newSelectedFlows; |
| 256 | } |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 257 | while (selectedFlows.length < 3) { |
| 258 | selectedFlows.push(null); |
| 259 | } |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 260 | |
| 261 | updateSelectedFlowsTable(); |
| 262 | updateSelectedFlowsTopology(); |
| 263 | } |
| 264 | |
| 265 | function selectFlow(flow) { |
Paul Greyson | c30f75e | 2013-03-28 11:45:15 -0700 | [diff] [blame] | 266 | var flowKey = makeFlowKey(flow); |
| 267 | var alreadySelected = false; |
| 268 | selectedFlows.forEach(function (f) { |
| 269 | if (f && makeFlowKey(f) === flowKey) { |
| 270 | alreadySelected = true; |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | if (!alreadySelected) { |
| 275 | selectedFlows.unshift(flow); |
| 276 | selectedFlows = selectedFlows.slice(0, 3); |
| 277 | updateSelectedFlows(); |
| 278 | } |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 281 | function deselectFlow(flow, ifCreatePending) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 282 | var flowKey = makeFlowKey(flow); |
| 283 | var newSelectedFlows = []; |
| 284 | selectedFlows.forEach(function (flow) { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame] | 285 | if (!flow || |
| 286 | flowKey !== makeFlowKey(flow) || |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 287 | flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 288 | newSelectedFlows.push(flow); |
| 289 | } |
| 290 | }); |
| 291 | selectedFlows = newSelectedFlows; |
| 292 | while (selectedFlows.length < 3) { |
| 293 | selectedFlows.push(null); |
| 294 | } |
| 295 | |
| 296 | updateSelectedFlows(); |
| 297 | } |
| 298 | |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 299 | function deselectFlowIfCreatePending(flow) { |
Paul Greyson | f430fd0 | 2013-03-28 12:32:24 -0700 | [diff] [blame] | 300 | deselectFlow(flow, true); |
| 301 | } |
| 302 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 303 | function showFlowChooser() { |
| 304 | function rowEnter(d) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 305 | var row = d3.select(this); |
| 306 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 307 | row.append('div') |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 308 | .classed('black-eye', true). |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 309 | on('click', function () { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 310 | selectFlow(d); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 311 | }); |
| 312 | |
| 313 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 314 | .classed('flowId', true) |
| 315 | .text(function (d) { |
| 316 | return d.flowId.value; |
| 317 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 318 | |
| 319 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 320 | .classed('srcDPID', true) |
| 321 | .text(function (d) { |
| 322 | return d.dataPath.srcPort.dpid.value; |
| 323 | }); |
| 324 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 325 | |
| 326 | row.append('div') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 327 | .classed('dstDPID', true) |
| 328 | .text(function (d) { |
| 329 | return d.dataPath.dstPort.dpid.value; |
| 330 | }); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 331 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 334 | var flows = d3.select('#flowChooser') |
| 335 | .append('div') |
| 336 | .style('pointer-events', 'auto') |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 337 | .selectAll('.selectedFlow') |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 338 | .data(model.flows) |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 339 | .enter() |
| 340 | .append('div') |
| 341 | .classed('selectedFlow', true) |
| 342 | .each(rowEnter); |
| 343 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 344 | setTimeout(function () { |
| 345 | d3.select(document.body).on('click', function () { |
| 346 | d3.select('#flowChooser').html(''); |
| 347 | d3.select(document.body).on('click', null); |
| 348 | }); |
| 349 | }, 0); |
| 350 | } |
| 351 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 352 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 353 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 354 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 355 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 356 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 357 | d3.select('#activeFlows').text(model.flows.length); |
| 358 | } |
| 359 | |
| 360 | function toRadians (angle) { |
| 361 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 364 | var widths = { |
| 365 | edge: 6, |
| 366 | aggregation: 12, |
| 367 | core: 18 |
| 368 | } |
| 369 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 370 | function createRingsFromModel(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 371 | var rings = [{ |
| 372 | radius: 3, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 373 | width: widths.edge, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 374 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 375 | className: 'edge', |
| 376 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 377 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 378 | radius: 2.25, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 379 | width: widths.aggregation, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 380 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 381 | className: 'aggregation', |
| 382 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 383 | }, { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 384 | radius: 0.75, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 385 | width: widths.core, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 386 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 387 | className: 'core', |
| 388 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 389 | }]; |
| 390 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 391 | |
| 392 | var aggRanges = {}; |
| 393 | |
| 394 | // arrange edge switches at equal increments |
| 395 | var k = 360 / rings[0].switches.length; |
| 396 | rings[0].switches.forEach(function (s, i) { |
| 397 | var angle = k * i; |
| 398 | |
| 399 | rings[0].angles[i] = angle; |
| 400 | |
| 401 | // record the angle for the agg switch layout |
| 402 | var dpid = s.dpid.split(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 403 | dpid[7] = '01'; // the last component of the agg switch is always '01' |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 404 | var aggdpid = dpid.join(':'); |
| 405 | var aggRange = aggRanges[aggdpid]; |
| 406 | if (!aggRange) { |
| 407 | aggRange = aggRanges[aggdpid] = {}; |
| 408 | aggRange.min = aggRange.max = angle; |
| 409 | } else { |
| 410 | aggRange.max = angle; |
| 411 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 412 | }); |
| 413 | |
| 414 | // arrange aggregation switches to "fan out" to edge switches |
| 415 | k = 360 / rings[1].switches.length; |
| 416 | rings[1].switches.forEach(function (s, i) { |
| 417 | // rings[1].angles[i] = k * i; |
| 418 | var range = aggRanges[s.dpid]; |
| 419 | |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 420 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 421 | }); |
| 422 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 423 | // find the association between core switches and aggregation switches |
| 424 | var aggregationSwitchMap = {}; |
| 425 | model.aggregationSwitches.forEach(function (s, i) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 426 | aggregationSwitchMap[s.dpid] = i; |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 427 | }); |
| 428 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 429 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 430 | k = 360 / rings[2].switches.length; |
| 431 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 432 | // rings[2].angles[i] = k * i; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 433 | var associatedAggregationSwitches = model.configuration.association[s.dpid]; |
| 434 | // TODO: go between if there are multiple |
| 435 | var index = aggregationSwitchMap[associatedAggregationSwitches[0]]; |
| 436 | |
| 437 | rings[2].angles[i] = rings[1].angles[index]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 438 | }); |
| 439 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 440 | // TODO: construct this form initially rather than converting. it works better because |
| 441 | // it allows binding by dpid |
| 442 | var testRings = []; |
| 443 | rings.forEach(function (ring) { |
| 444 | var testRing = []; |
| 445 | ring.switches.forEach(function (s, i) { |
| 446 | var testSwitch = { |
| 447 | dpid: s.dpid, |
| 448 | state: s.state, |
| 449 | radius: ring.radius, |
| 450 | width: ring.width, |
| 451 | className: ring.className, |
| 452 | angle: ring.angles[i], |
| 453 | controller: s.controller |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 454 | }; |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 455 | testRing.push(testSwitch); |
| 456 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 457 | |
| 458 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 459 | testRings.push(testRing); |
| 460 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 461 | |
| 462 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 463 | // return rings; |
| 464 | return testRings; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 467 | function makeLinkKey(link) { |
| 468 | return link['src-switch'] + '=>' + link['dst-switch']; |
| 469 | } |
| 470 | |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 471 | function makeFlowKey(flow) { |
| 472 | return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value; |
| 473 | } |
| 474 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 475 | function createLinkMap(links) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 476 | var linkMap = {}; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 477 | links.forEach(function (link) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 478 | var srcDPID = link['src-switch']; |
| 479 | var dstDPID = link['dst-switch']; |
| 480 | |
| 481 | var srcMap = linkMap[srcDPID] || {}; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 482 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 483 | srcMap[dstDPID] = link; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 484 | |
| 485 | linkMap[srcDPID] = srcMap; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 486 | }); |
| 487 | return linkMap; |
| 488 | } |
| 489 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 490 | // removes links from the pending list that are now in the model |
| 491 | function reconcilePendingLinks(model) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 492 | var links = []; |
| 493 | model.links.forEach(function (link) { |
| 494 | links.push(link); |
| 495 | delete pendingLinks[makeLinkKey(link)] |
| 496 | }) |
| 497 | var linkId; |
| 498 | for (linkId in pendingLinks) { |
| 499 | links.push(pendingLinks[linkId]); |
| 500 | } |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 501 | return links |
| 502 | } |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 503 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 504 | updateTopology = function() { |
| 505 | |
| 506 | // DRAW THE SWITCHES |
| 507 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 508 | |
| 509 | var links = reconcilePendingLinks(model); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 510 | var linkMap = createLinkMap(links); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 511 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 512 | function mouseOverSwitch(data) { |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 513 | |
| 514 | d3.event.preventDefault(); |
| 515 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 516 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false); |
| 517 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 518 | if (data.highlighted) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | // only highlight valid link or flow destination by checking for class of existing highlighted circle |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 523 | var highlighted = svg.selectAll('.highlight')[0]; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 524 | if (highlighted.length == 1) { |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 525 | var s = d3.select(highlighted[0]).select('circle'); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 526 | // only allow links |
| 527 | // edge->edge (flow) |
| 528 | // aggregation->core |
| 529 | // core->core |
| 530 | if (data.className == 'edge' && !s.classed('edge') || |
| 531 | data.className == 'core' && !s.classed('core') && !s.classed('aggregation') || |
| 532 | data.className == 'aggregation' && !s.classed('core')) { |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | // don't highlight if there's already a link or flow |
| 537 | // var map = linkMap[data.dpid]; |
| 538 | // console.log(map); |
| 539 | // console.log(s.data()[0].dpid); |
| 540 | // console.log(map[s.data()[0].dpid]); |
| 541 | // if (map && map[s.data()[0].dpid]) { |
| 542 | // return; |
| 543 | // } |
| 544 | |
| 545 | // the second highlighted switch is the target for a link or flow |
| 546 | data.target = true; |
| 547 | } |
| 548 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 549 | var node = d3.select(document.getElementById(data.dpid)); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 550 | node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 551 | data.highlighted = true; |
| 552 | node.moveToFront(); |
| 553 | } |
| 554 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 555 | function mouseOutSwitch(data) { |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 556 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true); |
| 557 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 558 | if (data.mouseDown) |
| 559 | return; |
| 560 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 561 | var node = d3.select(document.getElementById(data.dpid)); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 562 | node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 563 | data.highlighted = false; |
| 564 | data.target = false; |
| 565 | } |
| 566 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 567 | function mouseDownSwitch(data) { |
| 568 | mouseOverSwitch(data); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 569 | data.mouseDown = true; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 570 | d3.select('#topology').classed('linking', true); |
| 571 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 572 | d3.select('svg') |
| 573 | .append('svg:path') |
| 574 | .attr('id', 'linkVector') |
| 575 | .attr('d', function () { |
| 576 | var s = d3.select(document.getElementById(data.dpid)); |
| 577 | |
| 578 | var pt = document.querySelector('svg').createSVGPoint(); |
| 579 | pt.x = s.attr('x'); |
| 580 | pt.y = s.attr('y'); |
| 581 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 582 | |
| 583 | return line([pt, pt]); |
| 584 | }); |
| 585 | |
| 586 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 587 | if (data.className === 'core') { |
| 588 | d3.selectAll('.edge').classed('nodrop', true); |
| 589 | } |
| 590 | if (data.className === 'edge') { |
| 591 | d3.selectAll('.core').classed('nodrop', true); |
| 592 | d3.selectAll('.aggregation').classed('nodrop', true); |
| 593 | } |
| 594 | if (data.className === 'aggregation') { |
| 595 | d3.selectAll('.edge').classed('nodrop', true); |
| 596 | d3.selectAll('.aggregation').classed('nodrop', true); |
| 597 | } |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 600 | function mouseUpSwitch(data) { |
| 601 | if (data.mouseDown) { |
| 602 | data.mouseDown = false; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 603 | d3.select('#topology').classed('linking', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 604 | d3.event.stopPropagation(); |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 605 | d3.selectAll('.nodrop').classed('nodrop', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | function doubleClickSwitch(data) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 610 | var circle = d3.select(document.getElementById(data.dpid)).select('circle'); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 611 | if (data.state == 'ACTIVE') { |
| 612 | var prompt = 'Deactivate ' + data.dpid + '?'; |
| 613 | if (confirm(prompt)) { |
| 614 | switchDown(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 615 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 616 | } |
| 617 | } else { |
| 618 | var prompt = 'Activate ' + data.dpid + '?'; |
| 619 | if (confirm(prompt)) { |
| 620 | switchUp(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 621 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 626 | function ringEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 627 | if (!data.length) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 631 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 632 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 633 | .data(data, function (data) { |
| 634 | return data.dpid; |
| 635 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 636 | .enter().append("svg:g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 637 | .attr("id", function (data, i) { |
| 638 | return data.dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 639 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 640 | .attr("transform", function(data, i) { |
| 641 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 642 | }); |
| 643 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 644 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 645 | nodes.append("svg:circle") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 646 | .attr("transform", function(data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 647 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 648 | if (data.scale) { |
| 649 | m = m.scale(data.scale); |
| 650 | } |
| 651 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 652 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 653 | .attr("x", function (data) { |
| 654 | return -data.width / 2; |
| 655 | }) |
| 656 | .attr("y", function (data) { |
| 657 | return -data.width / 2; |
| 658 | }) |
| 659 | .attr("r", function (data) { |
| 660 | return data.width; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 661 | }); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 662 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 663 | // setup the mouseover behaviors |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 664 | nodes.on('mouseover', mouseOverSwitch); |
| 665 | nodes.on('mouseout', mouseOutSwitch); |
| 666 | nodes.on('mouseup', mouseUpSwitch); |
| 667 | nodes.on('mousedown', mouseDownSwitch); |
| 668 | |
| 669 | // only do switch up/down for core switches |
| 670 | if (i == 2) { |
| 671 | nodes.on('dblclick', doubleClickSwitch); |
| 672 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 675 | // append switches |
| 676 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 677 | .attr("class", "ring") |
| 678 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 679 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 680 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 681 | function ringUpdate(data, i) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 682 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 683 | .data(data, function (data) { |
| 684 | return data.dpid; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 685 | }); |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 686 | nodes.select('circle') |
| 687 | .each(function (data) { |
| 688 | // if there's a pending state changed and then the state changes, clear the pending class |
| 689 | var circle = d3.select(this); |
| 690 | if (data.state === 'ACTIVE' && circle.classed('inactive') || |
| 691 | data.state === 'INACTIVE' && circle.classed('active')) { |
| 692 | circle.classed('pending', false); |
| 693 | } |
| 694 | }) |
| 695 | .attr('class', function (data) { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 696 | if (data.state === 'ACTIVE' && data.controller) { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 697 | return data.className + ' active ' + controllerColorMap[data.controller]; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 698 | } else { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 699 | return data.className + ' inactive ' + 'colorInactive'; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 700 | } |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 701 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | // update switches |
| 705 | rings.each(ringUpdate); |
| 706 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 707 | |
| 708 | // Now setup the labels |
| 709 | // This is done separately because SVG draws in node order and we want the labels |
| 710 | // always on top |
| 711 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 712 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 713 | d3.select(document.body).on('mousemove', function () { |
| 714 | if (!d3.select('#topology').classed('linking')) { |
| 715 | return; |
| 716 | } |
| 717 | var linkVector = document.getElementById('linkVector'); |
| 718 | if (!linkVector) { |
| 719 | return; |
| 720 | } |
| 721 | linkVector = d3.select(linkVector); |
| 722 | |
| 723 | var highlighted = svg.selectAll('.highlight')[0]; |
| 724 | var s1 = null, s2 = null; |
| 725 | if (highlighted.length > 1) { |
| 726 | var s1 = d3.select(highlighted[0]); |
| 727 | var s2 = d3.select(highlighted[1]); |
| 728 | |
| 729 | } else if (highlighted.length > 0) { |
| 730 | var s1 = d3.select(highlighted[0]); |
| 731 | } |
| 732 | var src = s1; |
| 733 | if (s2 && !s2.data()[0].target) { |
| 734 | src = s2; |
| 735 | } |
| 736 | if (src) { |
| 737 | linkVector.attr('d', function () { |
| 738 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 739 | srcPt.x = src.attr('x'); |
| 740 | srcPt.y = src.attr('y'); |
| 741 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
| 742 | |
| 743 | var svg = document.getElementById('topology'); |
| 744 | var mouse = d3.mouse(viewbox); |
| 745 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 746 | dstPt.x = mouse[0]; |
| 747 | dstPt.y = mouse[1]; |
| 748 | dstPt = dstPt.matrixTransform(viewbox.getCTM()); |
| 749 | |
| 750 | return line([srcPt, dstPt]); |
| 751 | }); |
| 752 | } |
| 753 | }); |
| 754 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 755 | d3.select(document.body).on('mouseup', function () { |
| 756 | function clearHighlight() { |
| 757 | svg.selectAll('circle').each(function (data) { |
| 758 | data.mouseDown = false; |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 759 | d3.select('#topology').classed('linking', false); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 760 | mouseOutSwitch(data); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 761 | }); |
| 762 | d3.select('#linkVector').remove(); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 763 | }; |
| 764 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 765 | d3.selectAll('.nodrop').classed('nodrop', false); |
| 766 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 767 | function removeLink(link) { |
| 768 | var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']); |
| 769 | var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']); |
| 770 | |
| 771 | if (path1) { |
| 772 | setPending(d3.select(path1)); |
| 773 | } |
| 774 | if (path2) { |
| 775 | setPending(d3.select(path2)); |
| 776 | } |
| 777 | |
| 778 | linkDown(link); |
| 779 | } |
| 780 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 781 | |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 782 | var highlighted = svg.selectAll('.highlight')[0]; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 783 | if (highlighted.length == 2) { |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 784 | var s1Data = highlighted[0].__data__; |
| 785 | var s2Data = highlighted[1].__data__; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 786 | |
| 787 | var srcData, dstData; |
| 788 | if (s1Data.target) { |
| 789 | dstData = s1Data; |
| 790 | srcData = s2Data; |
| 791 | } else { |
| 792 | dstData = s2Data; |
| 793 | srcData = s1Data; |
| 794 | } |
| 795 | |
| 796 | if (s1Data.className == 'edge' && s2Data.className == 'edge') { |
| 797 | var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?'; |
| 798 | if (confirm(prompt)) { |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 799 | addFlow(srcData, dstData); |
| 800 | |
| 801 | var flow = { |
| 802 | dataPath: { |
| 803 | srcPort: { |
| 804 | dpid: { |
| 805 | value: srcData.dpid |
| 806 | } |
| 807 | }, |
| 808 | dstPort: { |
| 809 | dpid: { |
| 810 | value: dstData.dpid |
| 811 | } |
| 812 | } |
| 813 | }, |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 814 | createPending: true |
Paul Greyson | 13f02b9 | 2013-03-28 11:29:35 -0700 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | selectFlow(flow); |
| 818 | |
| 819 | setTimeout(function () { |
Paul Greyson | aa81256 | 2013-03-28 12:43:12 -0700 | [diff] [blame] | 820 | deselectFlowIfCreatePending(flow); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 821 | }, pendingTimeout); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 822 | } |
| 823 | } else { |
| 824 | var map = linkMap[srcData.dpid]; |
| 825 | if (map && map[dstData.dpid]) { |
| 826 | var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 827 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 828 | removeLink(map[dstData.dpid]); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 829 | } |
| 830 | } else { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 831 | map = linkMap[dstData.dpid]; |
| 832 | if (map && map[srcData.dpid]) { |
| 833 | var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?'; |
| 834 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 835 | removeLink(map[srcData.dpid]); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 836 | } |
| 837 | } else { |
| 838 | var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 839 | if (confirm(prompt)) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 840 | var link1 = { |
| 841 | 'src-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 842 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 843 | 'dst-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 844 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 845 | pending: true |
| 846 | }; |
| 847 | pendingLinks[makeLinkKey(link1)] = link1; |
| 848 | var link2 = { |
| 849 | 'src-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 850 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 851 | 'dst-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 852 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 853 | pending: true |
| 854 | }; |
| 855 | pendingLinks[makeLinkKey(link2)] = link2; |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 856 | updateTopology(); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 857 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 858 | linkUp(link1); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 859 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 860 | // remove the pending links after 10s |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 861 | setTimeout(function () { |
| 862 | delete pendingLinks[makeLinkKey(link1)]; |
| 863 | delete pendingLinks[makeLinkKey(link2)]; |
| 864 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 865 | updateTopology(); |
Paul Greyson | 6f91840 | 2013-03-28 12:18:30 -0700 | [diff] [blame] | 866 | }, pendingTimeout); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 867 | } |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | clearHighlight(); |
| 873 | } else { |
| 874 | clearHighlight(); |
| 875 | } |
| 876 | |
| 877 | }); |
| 878 | |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 879 | function labelRingEnter(data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 880 | if (!data.length) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
| 884 | // create the nodes |
| 885 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 886 | .data(data, function (data) { |
| 887 | return data.dpid; |
| 888 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 889 | .enter().append("svg:g") |
| 890 | .classed('nolabel', true) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 891 | .attr("id", function (data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 892 | return data.dpid + '-label'; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 893 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 894 | .attr("transform", function(data, i) { |
| 895 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 896 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 897 | |
| 898 | // add the text nodes which show on mouse over |
| 899 | nodes.append("svg:text") |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 900 | .text(function (data) {return data.dpid;}) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 901 | .attr("x", function (data) { |
| 902 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 903 | if (data.className == 'edge') { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 904 | return - data.width*3 - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 905 | } else { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 906 | return - data.width - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 907 | } |
| 908 | } else { |
| 909 | if (data.className == 'edge') { |
| 910 | return data.width*3 + 4; |
| 911 | } else { |
| 912 | return data.width + 4; |
| 913 | } |
| 914 | } |
| 915 | }) |
| 916 | .attr("y", function (data) { |
| 917 | var y; |
| 918 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 919 | if (data.className == 'edge') { |
| 920 | y = data.width*3/2 + 4; |
| 921 | } else { |
| 922 | y = data.width/2 + 4; |
| 923 | } |
| 924 | } else { |
| 925 | if (data.className == 'edge') { |
| 926 | y = data.width*3/2 + 4; |
| 927 | } else { |
| 928 | y = data.width/2 + 4; |
| 929 | } |
| 930 | } |
| 931 | return y - 6; |
| 932 | }) |
| 933 | .attr("text-anchor", function (data) { |
| 934 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 935 | return "end"; |
| 936 | } else { |
| 937 | return "start"; |
| 938 | } |
| 939 | }) |
| 940 | .attr("transform", function(data) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 941 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 942 | if (data.scale) { |
| 943 | m = m.scale(data.scale); |
| 944 | } |
| 945 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 946 | }) |
| 947 | } |
| 948 | |
| 949 | labelRings.enter().append("svg:g") |
| 950 | .attr("class", "textRing") |
| 951 | .each(labelRingEnter); |
| 952 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 953 | // switches should not change during operation of the ui so no |
| 954 | // rings.exit() |
| 955 | |
| 956 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 957 | // DRAW THE LINKS |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 958 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 959 | // key on link dpids since these will come/go during demo |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 960 | var links = d3.select('svg').selectAll('.link').data(links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 961 | return d['src-switch']+'->'+d['dst-switch']; |
| 962 | }); |
| 963 | |
| 964 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 965 | links.enter().append("svg:path") |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 966 | .attr("class", "link"); |
| 967 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 968 | links.attr('id', function (d) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 969 | return makeLinkKey(d); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 970 | }) |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 971 | .attr("d", function (d) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 972 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 973 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 974 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 975 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 976 | srcPt.x = src.attr('x'); |
| 977 | srcPt.y = src.attr('y'); |
| 978 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 979 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 980 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 981 | dstPt.x = dst.attr('x'); |
Paul Greyson | 421bfcd | 2013-03-27 22:22:09 -0700 | [diff] [blame] | 982 | dstPt.y = dst.attr('y'); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 983 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 984 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 985 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 986 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 987 | midPt.y = (srcPt.y + dstPt.y)/2; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 988 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 989 | return line([srcPt, midPt, dstPt]); |
| 990 | }) |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 991 | .attr("marker-mid", function(d) { return "url(#arrow)"; }) |
| 992 | .classed('pending', function (d) { |
| 993 | return d.pending; |
| 994 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 995 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 996 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 997 | // remove old links |
| 998 | links.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 1001 | function updateControllers() { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1002 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 1003 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 1004 | .each(function (c) { |
| 1005 | controllerColorMap[c] = colors.pop(); |
| 1006 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 1007 | }) |
| 1008 | .text(function (d) { |
| 1009 | return d; |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 1010 | }) |
| 1011 | .append('div') |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 1012 | .attr('class', 'black-eye'); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1013 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 1014 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 1015 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1016 | if (model.activeControllers.indexOf(d) != -1) { |
| 1017 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1018 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1019 | var className = 'controller ' + color; |
| 1020 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1021 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1022 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 1023 | // this should never be needed |
| 1024 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1025 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 1026 | controllers.on('dblclick', function (c) { |
| 1027 | if (model.activeControllers.indexOf(c) != -1) { |
| 1028 | var prompt = 'Dectivate ' + c + '?'; |
| 1029 | if (confirm(prompt)) { |
| 1030 | controllerDown(c); |
| 1031 | setPending(d3.select(this)); |
| 1032 | }; |
| 1033 | } else { |
| 1034 | var prompt = 'Activate ' + c + '?'; |
| 1035 | if (confirm(prompt)) { |
| 1036 | controllerUp(c); |
| 1037 | setPending(d3.select(this)); |
| 1038 | }; |
| 1039 | } |
| 1040 | }); |
| 1041 | |
Paul Greyson | 8247c3f | 2013-03-28 00:24:02 -0700 | [diff] [blame] | 1042 | controllers.select('.black-eye').on('click', function (c) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 1043 | var allSelected = true; |
| 1044 | for (var key in controllerColorMap) { |
| 1045 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 1046 | allSelected = false; |
| 1047 | break; |
| 1048 | } |
| 1049 | } |
| 1050 | if (allSelected) { |
| 1051 | for (var key in controllerColorMap) { |
| 1052 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 1053 | } |
| 1054 | } else { |
| 1055 | for (var key in controllerColorMap) { |
| 1056 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 1061 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1062 | }); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 1063 | |
| 1064 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1067 | function sync(svg) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 1068 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1069 | updateModel(function (newModel) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 1070 | // console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1071 | |
Paul Greyson | e5991b5 | 2013-04-04 01:34:04 -0700 | [diff] [blame] | 1072 | if (newModel) { |
| 1073 | var modelChanged = false; |
| 1074 | if (!model || JSON.stringify(model) != JSON.stringify(newModel)) { |
| 1075 | modelChanged = true; |
| 1076 | model = newModel; |
| 1077 | } else { |
| 1078 | // console.log('no change'); |
| 1079 | } |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 1080 | |
Paul Greyson | e5991b5 | 2013-04-04 01:34:04 -0700 | [diff] [blame] | 1081 | if (modelChanged) { |
| 1082 | updateControllers(); |
| 1083 | updateSelectedFlows(); |
| 1084 | updateTopology(); |
| 1085 | } |
Paul Greyson | 5cc35f0 | 2013-03-28 10:07:36 -0700 | [diff] [blame] | 1086 | |
Paul Greyson | e5991b5 | 2013-04-04 01:34:04 -0700 | [diff] [blame] | 1087 | updateHeader(newModel); |
| 1088 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1089 | |
| 1090 | // do it again in 1s |
| 1091 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 1092 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 1093 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 1094 | }); |
| 1095 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 1096 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1097 | svg = createTopologyView(); |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1098 | updateSelectedFlows(); |
| 1099 | |
| 1100 | d3.select('#showFlowChooser').on('click', function () { |
| 1101 | showFlowChooser(); |
| 1102 | }); |
| 1103 | |
Paul Greyson | 72f1885 | 2013-03-27 15:56:11 -0700 | [diff] [blame] | 1104 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1105 | // workaround for Chrome v25 bug |
| 1106 | // if executed immediately, the view box transform logic doesn't work properly |
| 1107 | // fixed in Chrome v27 |
| 1108 | setTimeout(function () { |
| 1109 | // workaround for another Chrome v25 bug |
| 1110 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 1111 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1112 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
Paul Greyson | 29aa98d | 2013-03-28 00:09:31 -0700 | [diff] [blame] | 1113 | sync(svg); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 1114 | }, 100); |