blob: 25f0a9e86dba46c02d47134825ea3ec46b0db77f [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 }
112 });
113
114
115
116 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700117 k = 360 / rings[2].switches.length;
118 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700119// rings[2].angles[i] = k * i;
120 rings[2].angles[i] = rings[1].angles[coreLinks[s.dpid]];
Paul Greysonde7fad52013-03-19 12:47:32 -0700121 });
122
Paul Greyson740bdaf2013-03-18 16:10:48 -0700123 function ringEnter(data, i) {
124 if (!data.switches.length) {
125 return;
126 }
127
Paul Greyson740bdaf2013-03-18 16:10:48 -0700128
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700129 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700130 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700131 return data;
132 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700133 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700134 .classed('nolabel', true)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700135 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700136 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700137 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700138 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700139 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700140 });
141
142 nodes.append("svg:circle")
Paul Greyson3e142162013-03-19 13:56:17 -0700143 .attr('class', function (_, i) {
144 return data.className + ' ' + controllerColorMap[data.switches[i].controller];
145 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700146 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700147 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700148 if (data.scale) {
149 m = m.scale(data.scale);
150 }
151 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700152 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700153 .attr("x", -data.width / 2)
154 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700155 .attr("r", data.width)
Paul Greyson3e142162013-03-19 13:56:17 -0700156 // .attr("fill", function (_, i) {
157 // return controllerColorMap[data.switches[i].controller]
158 // })
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700159
160 nodes.append("svg:text")
161 .text(function (d, i) {return d.switches[i].dpid})
162 .attr("x", 0)
163 .attr("y", 0)
164 .attr("transform", function(_, i) {
165 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
166 if (data.scale) {
167 m = m.scale(data.scale);
168 }
169 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
170 })
171
172 function showLabel(data, index) {
173 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false);
174 }
175
176 function hideLabel(data, index) {
177 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true);
178 }
179
180 nodes.on('mouseover', showLabel);
181 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700182 }
183
184 var ring = svg.selectAll("g")
185 .data(rings)
186 .enter().append("svg:g")
187 .attr("class", "ring")
188 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700189
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700190
191 // do mouseover zoom on edge nodes
Paul Greysond1a22d92013-03-19 12:15:19 -0700192 function zoom(data, index) {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700193 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
194 g.transition().duration(100).attr("r", rings[0].width*3);
Paul Greysonf8f43172013-03-18 23:00:30 -0700195 }
196
197 svg.selectAll('.edge').on('mouseover', zoom);
198 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700199
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700200 function unzoom(data, index) {
201 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
202 g.transition().duration(100).attr("r", rings[0].width);
203 }
204 svg.selectAll('.edge').on('mouseout', unzoom);
205
206
Paul Greysond1a22d92013-03-19 12:15:19 -0700207 // DRAW THE LINKS
208 var line = d3.svg.line()
209 .x(function(d) {
210 return d.x;
211 })
212 .y(function(d) {
213 return d.y;
214 })
215 .interpolate("basis");
216
217 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
218 var src = d3.select(document.getElementById(d['src-switch']));
219 var dst = d3.select(document.getElementById(d['dst-switch']));
220
221 var srcPt = document.querySelector('svg').createSVGPoint();
222 srcPt.x = src.attr('x');
223 srcPt.y = src.attr('y');
224
225 var dstPt = document.querySelector('svg').createSVGPoint();
226 dstPt.x = dst.attr('x');
227 dstPt.y = dst.attr('y');
228
229 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
230 });
231}
232
233function updateControllers(model) {
234 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700235 controllers.enter().append('div')
236 .attr('class', function (d) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700237
238 var color = 'color0';
239 if (model.activeControllers.indexOf(d) != -1) {
240 color = controllerColorMap[d];
241 if (!color) {
242 color = controllerColorMap[d] = colors.pop();
243 }
244 } else {
245 controllerColorMap[d] = color;
Paul Greysond1a22d92013-03-19 12:15:19 -0700246 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700247 var className = 'controller ' + color;
248 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700249 });
250 controllers.text(function (d) {
251 return d;
252 });
253 controllers.exit().remove();
254
Paul Greyson3e142162013-03-19 13:56:17 -0700255 model.controllers.forEach(function (c) {
256 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
257 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700258
Paul Greyson3e142162013-03-19 13:56:17 -0700259 controllers.on('click', function (c, index) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700260 var allSelected = true;
261 for (var key in controllerColorMap) {
262 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
263 allSelected = false;
264 break;
265 }
266 }
267 if (allSelected) {
268 for (var key in controllerColorMap) {
269 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
270 }
271 } else {
272 for (var key in controllerColorMap) {
273 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
274 }
275 }
276
277 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
278 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700279 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700280}
281
Paul Greysonb48943b2013-03-19 13:27:57 -0700282var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700283function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700284 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700285 updateModel(function (newModel) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700286 console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700287
Paul Greysonb48943b2013-03-19 13:27:57 -0700288 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
289 updateControllers(newModel);
290 updateTopology(svg, newModel);
291 } else {
292 console.log('no change');
293 }
294 updateHeader(newModel);
295
296 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700297
298 // do it again in 1s
299 setTimeout(function () {
300 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700301 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700302 });
303}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700304
305sync(createTopologyView());