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