blob: 3eb2e7df5b0f4f98028f92bd6285b8b30f4fd371 [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(':');
74 dpid[7] = '00';
75 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 Greysonbcd3c772013-03-21 13:16:44 -070093 if (range) {
94 rings[1].angles[i] = (range.min + range.max)/2;
95 } else {
96 rings[1].angles[i] = 0;
97 }
Paul Greysonde7fad52013-03-19 12:47:32 -070098 });
99
100 // arrange core switches at equal increments
101 k = 360 / rings[2].switches.length;
102 rings[2].switches.forEach(function (s, i) {
103 rings[2].angles[i] = k * i;
104 });
105
Paul Greyson740bdaf2013-03-18 16:10:48 -0700106 function ringEnter(data, i) {
107 if (!data.switches.length) {
108 return;
109 }
110
Paul Greyson740bdaf2013-03-18 16:10:48 -0700111
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700112 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700113 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700114 return data;
115 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700116 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700117 .classed('nolabel', true)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700118 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700119 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700120 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700121 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700122 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700123 });
124
125 nodes.append("svg:circle")
Paul Greyson3e142162013-03-19 13:56:17 -0700126 .attr('class', function (_, i) {
127 return data.className + ' ' + controllerColorMap[data.switches[i].controller];
128 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700129 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700130 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700131 if (data.scale) {
132 m = m.scale(data.scale);
133 }
134 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700135 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700136 .attr("x", -data.width / 2)
137 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700138 .attr("r", data.width)
Paul Greyson3e142162013-03-19 13:56:17 -0700139 // .attr("fill", function (_, i) {
140 // return controllerColorMap[data.switches[i].controller]
141 // })
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700142
143 nodes.append("svg:text")
144 .text(function (d, i) {return d.switches[i].dpid})
145 .attr("x", 0)
146 .attr("y", 0)
147 .attr("transform", function(_, i) {
148 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
149 if (data.scale) {
150 m = m.scale(data.scale);
151 }
152 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
153 })
154
155 function showLabel(data, index) {
156 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false);
157 }
158
159 function hideLabel(data, index) {
160 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true);
161 }
162
163 nodes.on('mouseover', showLabel);
164 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700165 }
166
167 var ring = svg.selectAll("g")
168 .data(rings)
169 .enter().append("svg:g")
170 .attr("class", "ring")
171 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700172
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700173
174 // do mouseover zoom on edge nodes
Paul Greysond1a22d92013-03-19 12:15:19 -0700175 function zoom(data, index) {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700176 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
177 g.transition().duration(100).attr("r", rings[0].width*3);
Paul Greysonf8f43172013-03-18 23:00:30 -0700178 }
179
180 svg.selectAll('.edge').on('mouseover', zoom);
181 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700182
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700183 function unzoom(data, index) {
184 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
185 g.transition().duration(100).attr("r", rings[0].width);
186 }
187 svg.selectAll('.edge').on('mouseout', unzoom);
188
189
Paul Greysond1a22d92013-03-19 12:15:19 -0700190 // DRAW THE LINKS
191 var line = d3.svg.line()
192 .x(function(d) {
193 return d.x;
194 })
195 .y(function(d) {
196 return d.y;
197 })
198 .interpolate("basis");
199
200 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
201 var src = d3.select(document.getElementById(d['src-switch']));
202 var dst = d3.select(document.getElementById(d['dst-switch']));
203
204 var srcPt = document.querySelector('svg').createSVGPoint();
205 srcPt.x = src.attr('x');
206 srcPt.y = src.attr('y');
207
208 var dstPt = document.querySelector('svg').createSVGPoint();
209 dstPt.x = dst.attr('x');
210 dstPt.y = dst.attr('y');
211
212 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
213 });
214}
215
216function updateControllers(model) {
217 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -0700218 controllers.enter().append('div')
219 .attr('class', function (d) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700220
221 var color = 'color0';
222 if (model.activeControllers.indexOf(d) != -1) {
223 color = controllerColorMap[d];
224 if (!color) {
225 color = controllerColorMap[d] = colors.pop();
226 }
227 } else {
228 controllerColorMap[d] = color;
Paul Greysond1a22d92013-03-19 12:15:19 -0700229 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700230 var className = 'controller ' + color;
231 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -0700232 });
233 controllers.text(function (d) {
234 return d;
235 });
236 controllers.exit().remove();
237
Paul Greyson3e142162013-03-19 13:56:17 -0700238 model.controllers.forEach(function (c) {
239 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
240 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700241
Paul Greyson3e142162013-03-19 13:56:17 -0700242 controllers.on('click', function (c, index) {
243 var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
244 d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700245 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700246}
247
Paul Greysonb48943b2013-03-19 13:27:57 -0700248var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700249function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700250 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -0700251 updateModel(function (newModel) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700252 console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -0700253
Paul Greysonb48943b2013-03-19 13:27:57 -0700254 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
255 updateControllers(newModel);
256 updateTopology(svg, newModel);
257 } else {
258 console.log('no change');
259 }
260 updateHeader(newModel);
261
262 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700263
264 // do it again in 1s
265 setTimeout(function () {
266 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700267 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700268 });
269}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700270
271sync(createTopologyView());