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