blob: ce5752c01ad493fa2b36594b8e88c26889f0b877 [file] [log] [blame]
Paul Greyson740bdaf2013-03-18 16:10:48 -07001/*global d3*/
2
Paul Greysond1a22d92013-03-19 12:15:19 -07003
4var colors = [
5 '#EC0033',
6 '#FFBA00',
7 '#3714B0',
8 '#B12C49',
9 '#BF9830',
10 '#402C84',
11 '#990021',
12 '#A67900',
13 '#F53D65',
14 '#1F0772',
15 '#F56E8B',
16 '#FFCB40',
17 '#6949D7',
18 '#FFD973'
19]
20
21var controllerColorMap = {};
22
23
24
Paul Greyson740bdaf2013-03-18 16:10:48 -070025function createTopologyView() {
Paul Greysond1a22d92013-03-19 12:15:19 -070026 return d3.select('#svg-container').append('svg:svg').append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070027 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070028}
29
Paul Greysond1a22d92013-03-19 12:15:19 -070030function updateHeader(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -070031 d3.select('#lastUpdate').text(model.timestamp);
Paul Greyson952ccb62013-03-18 22:22:08 -070032 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
33 d3.select('#activeFlows').text(model.flows.length);
34}
35
36function toRadians (angle) {
37 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -070038}
39
Paul Greysond1a22d92013-03-19 12:15:19 -070040function updateTopology(svg, model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -070041
Paul Greysond1a22d92013-03-19 12:15:19 -070042 // DRAW THE NODES
Paul Greyson740bdaf2013-03-18 16:10:48 -070043 var rings = [{
44 radius: 3,
Paul Greyson23b0cd32013-03-18 23:45:48 -070045 width: 4,
Paul Greyson952ccb62013-03-18 22:22:08 -070046 switches: model.edgeSwitches,
47 className: 'edge'
Paul Greyson740bdaf2013-03-18 16:10:48 -070048 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070049 radius: 2.25,
50 width: 8,
Paul Greyson952ccb62013-03-18 22:22:08 -070051 switches: model.aggregationSwitches,
52 className: 'aggregation'
Paul Greyson740bdaf2013-03-18 16:10:48 -070053 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070054 radius: .75,
55 width: 16,
Paul Greyson952ccb62013-03-18 22:22:08 -070056 switches: model.coreSwitches,
57 className: 'core'
Paul Greyson740bdaf2013-03-18 16:10:48 -070058 }];
59
60 function ringEnter(data, i) {
61 if (!data.switches.length) {
62 return;
63 }
64
65 var k = 360 / data.switches.length;
66
67 d3.select(this).selectAll("g")
68 .data(d3.range(data.switches.length).map(function() {
69 return data;
70 }))
71 .enter().append("svg:g")
Paul Greyson23b0cd32013-03-18 23:45:48 -070072 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -070073 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -070074 })
Paul Greyson740bdaf2013-03-18 16:10:48 -070075 .attr("transform", function(_, i) {
Paul Greysonf8f43172013-03-18 23:00:30 -070076 return "rotate(" + i * k + ")translate(" + data.radius * 150 + ")rotate(" + (-i * k) + ")";
77 })
Paul Greyson952ccb62013-03-18 22:22:08 -070078 .append("svg:circle")
79 .attr('class', data.className)
80 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -070081 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -070082 if (data.scale) {
83 m = m.scale(data.scale);
84 }
85 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -070086 })
Paul Greyson740bdaf2013-03-18 16:10:48 -070087 .attr("x", -data.width / 2)
88 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -070089 .attr("r", data.width)
90 .attr("fill", function (_, i) {
91 return controllerColorMap[data.switches[i].controller]
92 })
Paul Greyson740bdaf2013-03-18 16:10:48 -070093 }
94
95 var ring = svg.selectAll("g")
96 .data(rings)
97 .enter().append("svg:g")
98 .attr("class", "ring")
99 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700100
Paul Greysond1a22d92013-03-19 12:15:19 -0700101 function zoom(data, index) {
Paul Greysonf8f43172013-03-18 23:00:30 -0700102 svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700103 .attr("r", function (data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700104 return rings[0].width * (index == i ? 2 : 1);
Paul Greysonf8f43172013-03-18 23:00:30 -0700105 })
106 }
107
108 svg.selectAll('.edge').on('mouseover', zoom);
109 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700110
111 // DRAW THE LINKS
112 var line = d3.svg.line()
113 .x(function(d) {
114 return d.x;
115 })
116 .y(function(d) {
117 return d.y;
118 })
119 .interpolate("basis");
120
121 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
122 var src = d3.select(document.getElementById(d['src-switch']));
123 var dst = d3.select(document.getElementById(d['dst-switch']));
124
125 var srcPt = document.querySelector('svg').createSVGPoint();
126 srcPt.x = src.attr('x');
127 srcPt.y = src.attr('y');
128
129 var dstPt = document.querySelector('svg').createSVGPoint();
130 dstPt.x = dst.attr('x');
131 dstPt.y = dst.attr('y');
132
133 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
134 });
135}
136
137function updateControllers(model) {
138 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
139 controllers.enter().append('div').attr('class', 'controller')
140 .attr('style', function (d) {
141 var color = controllerColorMap[d];
142 if (!color) {
143 color = controllerColorMap[d] = colors.pop();
144 }
145 return 'background-color:' + color;
146 });
147 controllers.text(function (d) {
148 return d;
149 });
150 controllers.exit().remove();
151
152 controllers.on('click', function (data, index) {
153
154 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700155}
156
157function sync(svg) {
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700158 updateModel(function (model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700159
Paul Greysond1a22d92013-03-19 12:15:19 -0700160 updateHeader(model);
161 updateControllers(model);
162 updateTopology(svg, model);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700163
164 // do it again in 1s
165 setTimeout(function () {
166 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700167 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700168 });
169}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700170
171sync(createTopologyView());