blob: 7f2b15992269979dbe2ef8fdbbf8b63cb542e3cd [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 Greysonde7fad52013-03-19 12:47:32 -070045 width: 6,
Paul Greyson952ccb62013-03-18 22:22:08 -070046 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070047 className: 'edge',
48 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070049 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070050 radius: 2.25,
Paul Greysonde7fad52013-03-19 12:47:32 -070051 width: 12,
Paul Greyson952ccb62013-03-18 22:22:08 -070052 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070053 className: 'aggregation',
54 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070055 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070056 radius: .75,
Paul Greysonde7fad52013-03-19 12:47:32 -070057 width: 18,
Paul Greyson952ccb62013-03-18 22:22:08 -070058 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070059 className: 'core',
60 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070061 }];
62
Paul Greysonde7fad52013-03-19 12:47:32 -070063
64 var aggRanges = {};
65
66 // arrange edge switches at equal increments
67 var k = 360 / rings[0].switches.length;
68 rings[0].switches.forEach(function (s, i) {
69 var angle = k * i;
70
71 rings[0].angles[i] = angle;
72
73 // record the angle for the agg switch layout
74 var dpid = s.dpid.split(':');
75 dpid[7] = '00';
76 var aggdpid = dpid.join(':');
77 var aggRange = aggRanges[aggdpid];
78 if (!aggRange) {
79 aggRange = aggRanges[aggdpid] = {};
80 aggRange.min = aggRange.max = angle;
81 } else {
82 aggRange.max = angle;
83 }
84
85
86 });
87
88 // arrange aggregation switches to "fan out" to edge switches
89 k = 360 / rings[1].switches.length;
90 rings[1].switches.forEach(function (s, i) {
91// rings[1].angles[i] = k * i;
92 var range = aggRanges[s.dpid];
93
94 rings[1].angles[i] = (range.min + range.max)/2;
95 });
96
97 // arrange core switches at equal increments
98 k = 360 / rings[2].switches.length;
99 rings[2].switches.forEach(function (s, i) {
100 rings[2].angles[i] = k * i;
101 });
102
Paul Greyson740bdaf2013-03-18 16:10:48 -0700103 function ringEnter(data, i) {
104 if (!data.switches.length) {
105 return;
106 }
107
Paul Greyson740bdaf2013-03-18 16:10:48 -0700108
109 d3.select(this).selectAll("g")
110 .data(d3.range(data.switches.length).map(function() {
111 return data;
112 }))
113 .enter().append("svg:g")
Paul Greyson23b0cd32013-03-18 23:45:48 -0700114 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700115 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700116 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700117 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700118 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf8f43172013-03-18 23:00:30 -0700119 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700120 .append("svg:circle")
121 .attr('class', data.className)
122 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700123 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700124 if (data.scale) {
125 m = m.scale(data.scale);
126 }
127 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700128 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700129 .attr("x", -data.width / 2)
130 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700131 .attr("r", data.width)
132 .attr("fill", function (_, i) {
133 return controllerColorMap[data.switches[i].controller]
134 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700135 }
136
137 var ring = svg.selectAll("g")
138 .data(rings)
139 .enter().append("svg:g")
140 .attr("class", "ring")
141 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700142
Paul Greysond1a22d92013-03-19 12:15:19 -0700143 function zoom(data, index) {
Paul Greysonf8f43172013-03-18 23:00:30 -0700144 svg.selectAll('.edge').data(model.edgeSwitches).transition().duration(100)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700145 .attr("r", function (data, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700146 return rings[0].width * (index == i ? 3 : 1);
Paul Greysonf8f43172013-03-18 23:00:30 -0700147 })
148 }
149
150 svg.selectAll('.edge').on('mouseover', zoom);
151 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700152
153 // DRAW THE LINKS
154 var line = d3.svg.line()
155 .x(function(d) {
156 return d.x;
157 })
158 .y(function(d) {
159 return d.y;
160 })
161 .interpolate("basis");
162
163 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
164 var src = d3.select(document.getElementById(d['src-switch']));
165 var dst = d3.select(document.getElementById(d['dst-switch']));
166
167 var srcPt = document.querySelector('svg').createSVGPoint();
168 srcPt.x = src.attr('x');
169 srcPt.y = src.attr('y');
170
171 var dstPt = document.querySelector('svg').createSVGPoint();
172 dstPt.x = dst.attr('x');
173 dstPt.y = dst.attr('y');
174
175 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
176 });
177}
178
179function updateControllers(model) {
180 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
181 controllers.enter().append('div').attr('class', 'controller')
182 .attr('style', function (d) {
183 var color = controllerColorMap[d];
184 if (!color) {
185 color = controllerColorMap[d] = colors.pop();
186 }
187 return 'background-color:' + color;
188 });
189 controllers.text(function (d) {
190 return d;
191 });
192 controllers.exit().remove();
193
194 controllers.on('click', function (data, index) {
195
196 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700197}
198
199function sync(svg) {
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700200 updateModel(function (model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700201
Paul Greysond1a22d92013-03-19 12:15:19 -0700202 updateHeader(model);
203 updateControllers(model);
204 updateTopology(svg, model);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700205
206 // do it again in 1s
207 setTimeout(function () {
208 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700209 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700210 });
211}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700212
213sync(createTopologyView());