Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 3 | var projection = d3.geo.mercator() |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 4 | .center([82, 46]) |
Paul Greyson | 5f20cae | 2013-04-10 09:54:30 -0700 | [diff] [blame] | 5 | .scale(10000) |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 6 | .rotate([-180,0]); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 7 | |
Paul Greyson | 15e5da2 | 2013-04-10 00:16:27 -0700 | [diff] [blame] | 8 | var switchXML; |
| 9 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 10 | function createMap(svg, cb) { |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 11 | topology = svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000'). |
| 12 | attr('id', 'viewbox'); |
| 13 | |
| 14 | var map = topology.append("g").attr('id', 'map'); |
| 15 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 16 | var path = d3.geo.path().projection(projection); |
| 17 | |
| 18 | d3.json('data/world.json', function(error, topology) { |
| 19 | map.selectAll('path') |
| 20 | .data(topojson.object(topology, topology.objects.world).geometries) |
| 21 | .enter() |
| 22 | .append('path') |
| 23 | .attr('d', path) |
| 24 | |
Paul Greyson | 15e5da2 | 2013-04-10 00:16:27 -0700 | [diff] [blame] | 25 | d3.xml('assets/switch.svg', function (xml) { |
| 26 | switchXML = document.importNode(xml.documentElement, true);; |
| 27 | cb(); |
| 28 | }); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 29 | }); |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 30 | } |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 31 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 32 | /*************************************************************************************************** |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 33 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 34 | ***************************************************************************************************/ |
| 35 | var switchMap; |
| 36 | function makeSwitchMap() { |
| 37 | switchMap = {}; |
| 38 | model.coreSwitches.forEach(function (s) { |
| 39 | switchMap[s.dpid] = s; |
| 40 | }); |
| 41 | model.aggregationSwitches.forEach(function (s) { |
| 42 | switchMap[s.dpid] = s; |
| 43 | }); |
| 44 | model.edgeSwitches.forEach(function (s) { |
| 45 | switchMap[s.dpid] = s; |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | /*************************************************************************************************** |
| 50 | create a map from edge->aggregation and aggreation->core switches |
| 51 | ***************************************************************************************************/ |
| 52 | var switchAssociations; |
| 53 | function makeAssociations() { |
| 54 | switchAssociations = {}; |
| 55 | |
| 56 | var key; |
| 57 | for (key in model.configuration.association) { |
| 58 | var aggregationSwitches = model.configuration.association[key]; |
| 59 | aggregationSwitches.forEach(function (s) { |
| 60 | switchAssociations[s] = key; |
| 61 | }); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /*************************************************************************************************** |
| 66 | get the upstream switch. this only makes sense for aggregation and edge switches |
| 67 | ***************************************************************************************************/ |
| 68 | function getUpstream(dpid, className) { |
| 69 | if (className === 'aggregation') { |
| 70 | return switchAssociations[dpid]; |
| 71 | } else if (className === 'edge') { |
| 72 | var aggregationDpid = dpid.split(':'); |
| 73 | aggregationDpid[7] = '01'; // the last component of the agg switch is always '01' |
| 74 | return aggregationDpid.join(':'); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | /*****************a********************************************************************************** |
| 81 | create a map to hold the fanout information for the switches |
| 82 | ***************************************************************************************************/ |
| 83 | var fanouts; |
| 84 | function makeFanouts() { |
| 85 | fanouts = {}; |
| 86 | model.coreSwitches.forEach(function (s) { |
| 87 | fanouts[s.dpid] = model.configuration.geo[s.dpid]; |
| 88 | fanouts[s.dpid].count = 0; |
| 89 | }); |
| 90 | |
| 91 | model.aggregationSwitches.forEach(function (s) { |
| 92 | fanouts[s.dpid] = {count: 0}; |
| 93 | var upstreamFanout = fanouts[getUpstream(s.dpid, 'aggregation')]; |
| 94 | upstreamFanout.count += 1; |
| 95 | }); |
| 96 | |
| 97 | model.edgeSwitches.forEach(function (s) { |
| 98 | fanouts[s.dpid] = {}; |
| 99 | var upstreamFanout = fanouts[getUpstream(s.dpid, 'edge')]; |
| 100 | upstreamFanout.count += 1; |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | var projection; |
| 106 | var switchLayer; |
| 107 | var labelsLayer; |
| 108 | var linksLayer; |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 109 | createTopologyView = function (cb) { |
| 110 | var svg = createRootSVG(); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 111 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 112 | createMap(svg, function () { |
| 113 | switchLayer = topology.append('g'); |
| 114 | labelsLayer = topology.append('g'); |
| 115 | linksLayer = topology.append('g'); |
Paul Greyson | bbd708b | 2013-04-09 22:37:31 -0700 | [diff] [blame] | 116 | flowLayer = topology.append('g'); |
| 117 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 118 | |
| 119 | cb(); |
| 120 | }); |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 121 | } |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 122 | |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 123 | function drawLinkLines() { |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 124 | |
| 125 | // key on link dpids since these will come/go during demo |
| 126 | var linkLines = linksLayer.selectAll('.link').data(links, function (d) { |
| 127 | return d['src-switch']+'->'+d['dst-switch']; |
| 128 | }); |
| 129 | |
| 130 | // add new links |
| 131 | linkLines.enter().append("svg:path").attr("class", "link"); |
| 132 | |
| 133 | linkLines.attr('id', function (d) { |
| 134 | return makeLinkKey(d); |
| 135 | }).attr("d", function (d) { |
| 136 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 137 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
| 138 | |
| 139 | if (src.empty() || dst.empty()) { |
| 140 | return "M0,0"; |
| 141 | } |
| 142 | |
| 143 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 144 | srcPt.x = src.attr('x'); |
| 145 | srcPt.y = src.attr('y'); |
| 146 | |
| 147 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 148 | dstPt.x = dst.attr('x'); |
| 149 | dstPt.y = dst.attr('y'); |
| 150 | |
| 151 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 152 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 153 | midPt.y = (srcPt.y + dstPt.y)/2; |
| 154 | |
| 155 | return line([srcPt, midPt, dstPt]); |
| 156 | }) |
| 157 | .attr("marker-mid", function(d) { return "url(#arrow)"; }) |
| 158 | .classed('pending', function (d) { |
| 159 | return d.pending; |
| 160 | }); |
| 161 | |
| 162 | // remove old links |
| 163 | linkLines.exit().remove(); |
| 164 | } |
| 165 | |
| 166 | var fanOutAngles = { |
Paul Greyson | 5f20cae | 2013-04-10 09:54:30 -0700 | [diff] [blame] | 167 | aggregation: 100, |
| 168 | edge: 5 |
| 169 | } |
| 170 | |
| 171 | var fanOutDistances = { |
| 172 | aggregation: 60, |
| 173 | edge: 140 |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | function makeSwitchesModel(switches, className) { |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 177 | var switchesModel = []; |
| 178 | switches.forEach(function (s) { |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 179 | var geo = model.configuration.geo[s.dpid]; |
| 180 | |
| 181 | var pos, label; |
| 182 | if (geo) { |
| 183 | pos = projection([geo.lng, geo.lat]); |
| 184 | label = geo.label; |
| 185 | } else { |
| 186 | var upstream = getUpstream(s.dpid, className); |
| 187 | if (upstream) { |
| 188 | var upstreamGeo = fanouts[upstream]; |
| 189 | pos = projection([upstreamGeo.lng, upstreamGeo.lat]); |
| 190 | |
| 191 | var fanOutAngle = upstreamGeo.fanOutAngle; |
| 192 | fanOutAngle -= (upstreamGeo.count - 1) * fanOutAngles[className]/2; |
| 193 | |
| 194 | var angle = toRadians(fanOutAngle); |
Paul Greyson | 5f20cae | 2013-04-10 09:54:30 -0700 | [diff] [blame] | 195 | var xOff = Math.sin(angle) * fanOutDistances[className]; |
| 196 | var yOff = Math.cos(angle) * fanOutDistances[className]; |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 197 | |
| 198 | pos = [pos[0] + xOff, pos[1] + yOff]; |
| 199 | |
| 200 | var fakeGeo = projection.invert(pos); |
| 201 | |
| 202 | var fanout = fanouts[s.dpid]; |
| 203 | fanout.fanOutAngle = fanOutAngle; |
| 204 | fanout.lng = fakeGeo[0]; |
| 205 | fanout.lat = fakeGeo[1]; |
| 206 | |
| 207 | upstreamGeo.fanOutAngle += fanOutAngles[className]; |
| 208 | |
| 209 | } else { |
| 210 | pos = projection([-98, 39]); |
| 211 | } |
| 212 | } |
| 213 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 214 | switchesModel.push({ |
| 215 | dpid: s.dpid, |
| 216 | state: s.state, |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 217 | className: className, |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 218 | controller: s.controller, |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 219 | label: label, |
| 220 | x: pos[0], |
| 221 | y: pos[1] |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 222 | }); |
| 223 | }); |
| 224 | |
| 225 | return switchesModel; |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 228 | function switchEnter(d) { |
| 229 | var g = d3.select(this); |
Paul Greyson | 15e5da2 | 2013-04-10 00:16:27 -0700 | [diff] [blame] | 230 | var width; |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 231 | |
Paul Greyson | 15e5da2 | 2013-04-10 00:16:27 -0700 | [diff] [blame] | 232 | // attempt to draw an svg switch |
| 233 | if (false && d.className == 'core') { |
| 234 | width = 30; |
| 235 | g.select(function () { |
| 236 | return this.appendChild(switchXML.cloneNode(true)); |
| 237 | }) |
| 238 | .classed(d.className, true) |
| 239 | .attr('x', d.x - 30) |
| 240 | .attr('y', d.y - 30); |
| 241 | |
| 242 | } else { |
| 243 | width = widths[d.className]; |
| 244 | g.append('svg:circle').attr('r', width) |
| 245 | .classed(d.className, true) |
| 246 | .attr('cx', d.x) |
| 247 | .attr('cy', d.y); |
| 248 | } |
| 249 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 250 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 251 | if (d.label) { |
| 252 | g.append('svg:text') |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 253 | .classed('label', true) |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 254 | .text(d.label) |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 255 | .attr("text-anchor", "end") |
| 256 | .attr('x', d.x - width) |
| 257 | .attr('y', d.y - width); |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 258 | } |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 259 | } |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 260 | |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 261 | function labelsEnter(switches) { |
| 262 | return labelsLayer.selectAll('g').data(switches, function (d) { |
| 263 | return d.dpid; |
| 264 | }).enter().append('svg:g') |
| 265 | .classed('nolabel', true) |
| 266 | .attr("id", function (data) { |
| 267 | return data.dpid + '-label'; |
| 268 | }) |
| 269 | .append("svg:text") |
| 270 | .text(function (data) {return data.dpid;}) |
| 271 | .attr('x', function (d) { |
| 272 | return d.x; |
| 273 | }) |
| 274 | .attr('y', function (d) { |
| 275 | return d.y; |
| 276 | }) |
| 277 | .attr("text-anchor", function (d) { |
| 278 | return d.x > 500 ? "end" : "start"; |
| 279 | }) |
| 280 | |
| 281 | } |
| 282 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 283 | function switchesEnter(switches) { |
| 284 | return switchLayer.selectAll('g').data(switches, function (d) { |
| 285 | return d.dpid; |
| 286 | }) |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 287 | .enter() |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 288 | .append('svg:g') |
| 289 | .attr("id", function (d) { |
| 290 | return d.dpid; |
| 291 | }) |
| 292 | .attr('x', function (d) { |
| 293 | return d.x; |
| 294 | }) |
| 295 | .attr('y', function (d) { |
| 296 | return d.y; |
| 297 | }) |
| 298 | .each(switchEnter); |
| 299 | } |
| 300 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 301 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 302 | function switchesUpdate(switches) { |
| 303 | switches.each(function (d) { |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 304 | // if there's a pending state changed and then the state changes, clear the pending class |
| 305 | var circle = d3.select(this); |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 306 | if (d.state === 'ACTIVE' && circle.classed('inactive') || |
| 307 | d.state === 'INACTIVE' && circle.classed('active')) { |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 308 | circle.classed('pending', false); |
| 309 | } |
| 310 | }) |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 311 | .attr('class', function (d) { |
| 312 | if (d.state === 'ACTIVE' && d.controller) { |
Paul Greyson | b9e879e | 2013-04-09 21:16:16 -0700 | [diff] [blame] | 313 | return 'active ' + controllerColorMap[d.controller]; |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 314 | } else { |
Paul Greyson | b9e879e | 2013-04-09 21:16:16 -0700 | [diff] [blame] | 315 | return 'inactive ' + 'colorInactive'; |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 316 | } |
| 317 | }); |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 318 | } |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame] | 319 | |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 320 | drawTopology = function () { |
| 321 | |
| 322 | makeSwitchMap(); |
| 323 | makeAssociations(); |
| 324 | makeFanouts(); |
| 325 | |
| 326 | var coreSwitches = makeSwitchesModel(model.coreSwitches, 'core'); |
| 327 | var aggregationSwitches = makeSwitchesModel(model.aggregationSwitches, 'aggregation'); |
| 328 | var edgeSwitches = makeSwitchesModel(model.edgeSwitches, 'edge'); |
| 329 | |
| 330 | var switches = coreSwitches.concat(aggregationSwitches).concat(edgeSwitches); |
| 331 | |
| 332 | switchesUpdate(switchesEnter(switches)); |
| 333 | |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 334 | drawLinkLines(); |
Paul Greyson | 4e348b9 | 2013-04-09 21:02:06 -0700 | [diff] [blame] | 335 | |
Paul Greyson | 45aceb2 | 2013-04-09 22:17:03 -0700 | [diff] [blame] | 336 | labelsEnter(switches); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | })(); |