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