Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 1 | |
| 2 | |
| 3 | |
| 4 | (function () { |
| 5 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 6 | var projection = d3.geo.mercator() |
| 7 | .center([82, 45]) |
| 8 | .scale(10000) |
| 9 | .rotate([-180,0]); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 10 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 11 | function createMap(svg, cb) { |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 12 | topology = svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000'). |
| 13 | attr('id', 'viewbox'); |
| 14 | |
| 15 | var map = topology.append("g").attr('id', 'map'); |
| 16 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 17 | var path = d3.geo.path().projection(projection); |
| 18 | |
| 19 | d3.json('data/world.json', function(error, topology) { |
| 20 | map.selectAll('path') |
| 21 | .data(topojson.object(topology, topology.objects.world).geometries) |
| 22 | .enter() |
| 23 | .append('path') |
| 24 | .attr('d', path) |
| 25 | |
| 26 | cb(); |
| 27 | }); |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 28 | } |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 29 | |
| 30 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 31 | var projection |
| 32 | createTopologyView = function (cb) { |
| 33 | var svg = createRootSVG(); |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 34 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 35 | createMap(svg, cb); |
| 36 | } |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 37 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 38 | function makeSwitchesModel(switches) { |
| 39 | var switchesModel = []; |
| 40 | switches.forEach(function (s) { |
| 41 | switchesModel.push({ |
| 42 | dpid: s.dpid, |
| 43 | state: s.state, |
| 44 | className: 'core', |
| 45 | controller: s.controller, |
| 46 | geo: model.configuration.geo[s.dpid] |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | return switchesModel; |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | drawTopology = function () { |
| 54 | |
Paul Greyson | 981e8c2 | 2013-04-09 17:43:59 -0700 | [diff] [blame^] | 55 | |
| 56 | // enter |
| 57 | function switchEnter(s) { |
| 58 | var g = d3.select(this); |
| 59 | |
| 60 | g.append('svg:circle').attr('r', widths.core); |
| 61 | g.append('svg:text').text(s.geo.label).attr('transform', 'translate(' + widths.core + ' ' + widths.core + ')'); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | var coreSwitches = topology.selectAll('.core').data(makeSwitchesModel(model.coreSwitches)) |
| 66 | .enter() |
| 67 | .append('svg:g') |
| 68 | .attr("id", function (d) { |
| 69 | return d.dpid; |
| 70 | }) |
| 71 | .classed('core', true) |
| 72 | .attr("transform", function(d) { |
| 73 | if (d.geo) { |
| 74 | return "translate(" + projection([d.geo.lng, d.geo.lat]) + ")"; |
| 75 | } |
| 76 | }) |
| 77 | .each(switchEnter); |
| 78 | |
| 79 | |
| 80 | |
| 81 | // update |
| 82 | coreSwitches |
| 83 | .each(function (data) { |
| 84 | // if there's a pending state changed and then the state changes, clear the pending class |
| 85 | var circle = d3.select(this); |
| 86 | if (data.state === 'ACTIVE' && circle.classed('inactive') || |
| 87 | data.state === 'INACTIVE' && circle.classed('active')) { |
| 88 | circle.classed('pending', false); |
| 89 | } |
| 90 | }) |
| 91 | .attr('class', function (data) { |
| 92 | if (data.state === 'ACTIVE' && data.controller) { |
| 93 | return data.className + ' active ' + controllerColorMap[data.controller]; |
| 94 | } else { |
| 95 | return data.className + ' inactive ' + 'colorInactive'; |
| 96 | } |
| 97 | }); |
| 98 | |
Paul Greyson | c090d14 | 2013-04-09 16:59:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | })(); |