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