blob: 8980d02f71261fdbba0beed05fa531c93a764b3c [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
Jonathan Hart0767ab72013-03-24 16:14:29 -070096 // arrange core switches at equal increments
Paul Greysonde7fad52013-03-19 12:47:32 -070097 k = 360 / rings[2].switches.length;
98 rings[2].switches.forEach(function (s, i) {
Jonathan Hart0767ab72013-03-24 16:14:29 -070099 rings[2].angles[i] = k * i;
Paul Greysonde7fad52013-03-19 12:47:32 -0700100 });
101
Paul Greyson740bdaf2013-03-18 16:10:48 -0700102 function ringEnter(data, i) {
103 if (!data.switches.length) {
104 return;
105 }
106
Paul Greyson740bdaf2013-03-18 16:10:48 -0700107
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700108 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700109 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700110 return data;
111 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700112 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700113 .classed('nolabel', true)
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 Greysonf9edc1a2013-03-19 13:22:06 -0700119 });
120
121 nodes.append("svg:circle")
Paul Greyson3e142162013-03-19 13:56:17 -0700122 .attr('class', function (_, i) {
123 return data.className + ' ' + controllerColorMap[data.switches[i].controller];
124 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700125 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700126 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700127 if (data.scale) {
128 m = m.scale(data.scale);
129 }
130 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700131 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700132 .attr("x", -data.width / 2)
133 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700134 .attr("r", data.width)
Paul Greyson3e142162013-03-19 13:56:17 -0700135 // .attr("fill", function (_, i) {
136 // return controllerColorMap[data.switches[i].controller]
137 // })
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700138
139 nodes.append("svg:text")
140 .text(function (d, i) {return d.switches[i].dpid})
141 .attr("x", 0)
142 .attr("y", 0)
143 .attr("transform", function(_, i) {
144 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
145 if (data.scale) {
146 m = m.scale(data.scale);
147 }
148 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
149 })
150
151 function showLabel(data, index) {
152 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false);
153 }
154
155 function hideLabel(data, index) {
156 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true);
157 }
158
159 nodes.on('mouseover', showLabel);
160 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700161 }
162
163 var ring = svg.selectAll("g")
164 .data(rings)
165 .enter().append("svg:g")
166 .attr("class", "ring")
167 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700168
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700169
170 // do mouseover zoom on edge nodes
Paul Greysond1a22d92013-03-19 12:15:19 -0700171 function zoom(data, index) {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700172 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
173 g.transition().duration(100).attr("r", rings[0].width*3);
Paul Greysonf8f43172013-03-18 23:00:30 -0700174 }
175
176 svg.selectAll('.edge').on('mouseover', zoom);
177 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700178
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700179 function unzoom(data, index) {
180 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
181 g.transition().duration(100).attr("r", rings[0].width);
182 }
183 svg.selectAll('.edge').on('mouseout', unzoom);
184
185
Paul Greysond1a22d92013-03-19 12:15:19 -0700186 // DRAW THE LINKS
187 var line = d3.svg.line()
188 .x(function(d) {
189 return d.x;
190 })
191 .y(function(d) {
192 return d.y;
193 })
194 .interpolate("basis");
195
196 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
197 var src = d3.select(document.getElementById(d['src-switch']));
198 var dst = d3.select(document.getElementById(d['dst-switch']));
199
200 var srcPt = document.querySelector('svg').createSVGPoint();
201 srcPt.x = src.attr('x');
202 srcPt.y = src.attr('y');
203
204 var dstPt = document.querySelector('svg').createSVGPoint();
205 dstPt.x = dst.attr('x');
206 dstPt.y = dst.attr('y');
207
208 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
209 });
210}
211
212function updateControllers(model) {
213 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700214 controllers.enter().append('div')
215 .attr('class', function (d) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700216
217 var color = 'color0';
218 if (model.activeControllers.indexOf(d) != -1) {
219 color = controllerColorMap[d];
220 if (!color) {
221 color = controllerColorMap[d] = colors.pop();
222 }
223 } else {
224 controllerColorMap[d] = color;
Paul Greysond1a22d92013-03-19 12:15:19 -0700225 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700226 var className = 'controller ' + color;
227 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700228 });
229 controllers.text(function (d) {
230 return d;
231 });
232 controllers.exit().remove();
233
Paul Greyson3e142162013-03-19 13:56:17 -0700234 model.controllers.forEach(function (c) {
235 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
236 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700237
Paul Greyson3e142162013-03-19 13:56:17 -0700238 controllers.on('click', function (c, index) {
Paul Greysonc3e21a02013-03-21 13:56:05 -0700239 var allSelected = true;
240 for (var key in controllerColorMap) {
241 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
242 allSelected = false;
243 break;
244 }
245 }
246 if (allSelected) {
247 for (var key in controllerColorMap) {
248 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
249 }
250 } else {
251 for (var key in controllerColorMap) {
252 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
253 }
254 }
255
256 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
257 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700258 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700259}
260
Paul Greysonb48943b2013-03-19 13:27:57 -0700261var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700262function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700263 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700264 updateModel(function (newModel) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700265 console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700266
Paul Greysonb48943b2013-03-19 13:27:57 -0700267 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
268 updateControllers(newModel);
269 updateTopology(svg, newModel);
270 } else {
271 console.log('no change');
272 }
273 updateHeader(newModel);
274
275 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700276
277 // do it again in 1s
278 setTimeout(function () {
Jonathan Hart0767ab72013-03-24 16:14:29 -0700279 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700280 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700281 });
282}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700283
Jonathan Hart0767ab72013-03-24 16:14:29 -0700284sync(createTopologyView());