blob: 0a16fc2992d67614bcdfbd24fe91291449381e4f [file] [log] [blame]
Paul Greyson740bdaf2013-03-18 16:10:48 -07001/*global d3*/
2
Paul Greysond1a22d92013-03-19 12:15:19 -07003
4var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -07005 'color1',
6 'color2',
7 'color3',
8 'color4',
9 'color5',
10 'color6',
11 'color7',
12 'color8',
13 'color9',
14 'color10',
15 'color11',
16 'color12',
Paul Greysond1a22d92013-03-19 12:15:19 -070017]
Paul Greyson01a5dff2013-03-19 15:50:14 -070018colors.reverse();
Paul Greysond1a22d92013-03-19 12:15:19 -070019
20var controllerColorMap = {};
21
22
23
Paul Greyson740bdaf2013-03-18 16:10:48 -070024function createTopologyView() {
Paul Greysond1a22d92013-03-19 12:15:19 -070025 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 -070026 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070027}
28
Paul Greysond1a22d92013-03-19 12:15:19 -070029function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -070030 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -070031 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
32 d3.select('#activeFlows').text(model.flows.length);
33}
34
35function toRadians (angle) {
36 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -070037}
38
Paul Greysond1a22d92013-03-19 12:15:19 -070039function updateTopology(svg, model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -070040
Paul Greysond1a22d92013-03-19 12:15:19 -070041 // DRAW THE NODES
Paul Greyson740bdaf2013-03-18 16:10:48 -070042 var rings = [{
43 radius: 3,
Paul Greysonde7fad52013-03-19 12:47:32 -070044 width: 6,
Paul Greyson952ccb62013-03-18 22:22:08 -070045 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070046 className: 'edge',
47 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070048 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070049 radius: 2.25,
Paul Greysonde7fad52013-03-19 12:47:32 -070050 width: 12,
Paul Greyson952ccb62013-03-18 22:22:08 -070051 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070052 className: 'aggregation',
53 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070054 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070055 radius: .75,
Paul Greysonde7fad52013-03-19 12:47:32 -070056 width: 18,
Paul Greyson952ccb62013-03-18 22:22:08 -070057 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070058 className: 'core',
59 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070060 }];
61
Paul Greysonde7fad52013-03-19 12:47:32 -070062
63 var aggRanges = {};
64
65 // arrange edge switches at equal increments
66 var k = 360 / rings[0].switches.length;
67 rings[0].switches.forEach(function (s, i) {
68 var angle = k * i;
69
70 rings[0].angles[i] = angle;
71
72 // record the angle for the agg switch layout
73 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -070074 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -070075 var aggdpid = dpid.join(':');
76 var aggRange = aggRanges[aggdpid];
77 if (!aggRange) {
78 aggRange = aggRanges[aggdpid] = {};
79 aggRange.min = aggRange.max = angle;
80 } else {
81 aggRange.max = angle;
82 }
83
84
85 });
86
87 // arrange aggregation switches to "fan out" to edge switches
88 k = 360 / rings[1].switches.length;
89 rings[1].switches.forEach(function (s, i) {
90// rings[1].angles[i] = k * i;
91 var range = aggRanges[s.dpid];
92
Paul Greyson832d2202013-03-21 13:27:56 -070093 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -070094 });
95
Paul Greyson3f890b62013-03-22 17:39:36 -070096 // find the association between core switches and aggregation switches
97 var aggregationSwitchMap = {};
98 model.aggregationSwitches.forEach(function (s, i) {
99 aggregationSwitchMap[s.dpid] = i + 1;
100 });
101
102 var coreSwitchMap = {};
103 model.coreSwitches.forEach(function (s, i) {
104 coreSwitchMap[s.dpid] = i + 1;
105 });
106
107 var coreLinks = {};
108 model.links.forEach(function (l) {
109 if (aggregationSwitchMap[l['src-switch']] && coreSwitchMap[l['dst-switch']]) {
110 coreLinks[l['dst-switch']] = aggregationSwitchMap[l['src-switch']] - 1;
111 }
Paul Greysona36a9232013-03-22 22:41:27 -0700112 if (aggregationSwitchMap[l['dst-switch']] && coreSwitchMap[l['src-switch']]) {
113 coreLinks[l['src-switch']] = aggregationSwitchMap[l['dst-switch']] - 1;
114 }
Paul Greyson3f890b62013-03-22 17:39:36 -0700115 });
116
117
118
119 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700120 k = 360 / rings[2].switches.length;
121 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700122// rings[2].angles[i] = k * i;
123 rings[2].angles[i] = rings[1].angles[coreLinks[s.dpid]];
Paul Greysonde7fad52013-03-19 12:47:32 -0700124 });
125
Paul Greyson740bdaf2013-03-18 16:10:48 -0700126 function ringEnter(data, i) {
127 if (!data.switches.length) {
128 return;
129 }
130
Paul Greyson740bdaf2013-03-18 16:10:48 -0700131
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700132 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700133 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700134 return data;
135 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700136 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700137 .classed('nolabel', true)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700138 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700139 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700140 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700141 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700142 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700143 });
144
145 nodes.append("svg:circle")
Paul Greyson3e142162013-03-19 13:56:17 -0700146 .attr('class', function (_, i) {
147 return data.className + ' ' + controllerColorMap[data.switches[i].controller];
148 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700149 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700150 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700151 if (data.scale) {
152 m = m.scale(data.scale);
153 }
154 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700155 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700156 .attr("x", -data.width / 2)
157 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700158 .attr("r", data.width)
Paul Greyson3e142162013-03-19 13:56:17 -0700159 // .attr("fill", function (_, i) {
160 // return controllerColorMap[data.switches[i].controller]
161 // })
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700162
163 nodes.append("svg:text")
164 .text(function (d, i) {return d.switches[i].dpid})
165 .attr("x", 0)
166 .attr("y", 0)
167 .attr("transform", function(_, i) {
168 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
169 if (data.scale) {
170 m = m.scale(data.scale);
171 }
172 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
173 })
174
175 function showLabel(data, index) {
176 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false);
177 }
178
179 function hideLabel(data, index) {
180 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true);
181 }
182
183 nodes.on('mouseover', showLabel);
184 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700185 }
186
187 var ring = svg.selectAll("g")
188 .data(rings)
189 .enter().append("svg:g")
190 .attr("class", "ring")
191 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700192
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700193
194 // do mouseover zoom on edge nodes
Paul Greysond1a22d92013-03-19 12:15:19 -0700195 function zoom(data, index) {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700196 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
197 g.transition().duration(100).attr("r", rings[0].width*3);
Paul Greysonf8f43172013-03-18 23:00:30 -0700198 }
199
200 svg.selectAll('.edge').on('mouseover', zoom);
201 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700202
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700203 function unzoom(data, index) {
204 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
205 g.transition().duration(100).attr("r", rings[0].width);
206 }
207 svg.selectAll('.edge').on('mouseout', unzoom);
208
209
Paul Greysond1a22d92013-03-19 12:15:19 -0700210 // DRAW THE LINKS
211 var line = d3.svg.line()
212 .x(function(d) {
213 return d.x;
214 })
215 .y(function(d) {
216 return d.y;
217 })
218 .interpolate("basis");
219
220 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
221 var src = d3.select(document.getElementById(d['src-switch']));
222 var dst = d3.select(document.getElementById(d['dst-switch']));
223
224 var srcPt = document.querySelector('svg').createSVGPoint();
225 srcPt.x = src.attr('x');
Paul Greysona36a9232013-03-22 22:41:27 -0700226 srcPt.y = src.attr('y') + 10;
Paul Greysond1a22d92013-03-19 12:15:19 -0700227
228 var dstPt = document.querySelector('svg').createSVGPoint();
229 dstPt.x = dst.attr('x');
Paul Greysona36a9232013-03-22 22:41:27 -0700230 dstPt.y = dst.attr('y') - 10;
Paul Greysond1a22d92013-03-19 12:15:19 -0700231
232 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
233 });
234}
235
236function updateControllers(model) {
237 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700238 controllers.enter().append('div')
239 .attr('class', function (d) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700240
241 var color = 'color0';
242 if (model.activeControllers.indexOf(d) != -1) {
243 color = controllerColorMap[d];
244 if (!color) {
245 color = controllerColorMap[d] = colors.pop();
246 }
247 } else {
248 controllerColorMap[d] = color;
Paul Greysond1a22d92013-03-19 12:15:19 -0700249 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700250 var className = 'controller ' + color;
251 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700252 });
253 controllers.text(function (d) {
254 return d;
255 });
256 controllers.exit().remove();
257
Paul Greyson3e142162013-03-19 13:56:17 -0700258 model.controllers.forEach(function (c) {
259 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
260 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700261
Paul Greyson3e142162013-03-19 13:56:17 -0700262 controllers.on('click', function (c, index) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700263 var allSelected = true;
264 for (var key in controllerColorMap) {
265 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
266 allSelected = false;
267 break;
268 }
269 }
270 if (allSelected) {
271 for (var key in controllerColorMap) {
272 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
273 }
274 } else {
275 for (var key in controllerColorMap) {
276 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
277 }
278 }
279
280 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
281 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700282 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700283}
284
Paul Greysonb48943b2013-03-19 13:27:57 -0700285var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700286function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700287 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700288 updateModel(function (newModel) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700289 console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700290
Paul Greysonb48943b2013-03-19 13:27:57 -0700291 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
292 updateControllers(newModel);
293 updateTopology(svg, newModel);
294 } else {
295 console.log('no change');
296 }
297 updateHeader(newModel);
298
299 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700300
301 // do it again in 1s
302 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -0700303 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700304 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700305 });
306}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700307
Paul Greyson38d8bde2013-03-22 22:07:35 -0700308svg = createTopologyView();
309// workaround for Chrome v25 bug
310// if executed immediately, the view box transform logic doesn't work properly
311// fixed in Chrome v27
312setTimeout(function () {
313 // workaround for another Chrome v25 bug
314 // viewbox transform stuff doesn't work in combination with browser zoom
315 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
316 sync(svg);
317}, 100);