blob: 1025fee21acd53b34a6405f0011856348c0c7a38 [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 Greysonb48943b2013-03-19 13:27:57 -070031 d3.select('#lastUpdate').text(new Date());
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
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700109 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700110 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700111 return data;
112 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700113 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700114 .classed('nolabel', true)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700115 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700116 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700117 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700118 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700119 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700120 });
121
122 nodes.append("svg:circle")
Paul Greyson952ccb62013-03-18 22:22:08 -0700123 .attr('class', data.className)
124 .attr("transform", function(_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700125 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700126 if (data.scale) {
127 m = m.scale(data.scale);
128 }
129 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700130 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700131 .attr("x", -data.width / 2)
132 .attr("y", -data.width / 2)
Paul Greysond1a22d92013-03-19 12:15:19 -0700133 .attr("r", data.width)
134 .attr("fill", function (_, i) {
135 return controllerColorMap[data.switches[i].controller]
136 })
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700137
138 nodes.append("svg:text")
139 .text(function (d, i) {return d.switches[i].dpid})
140 .attr("x", 0)
141 .attr("y", 0)
142 .attr("transform", function(_, i) {
143 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
144 if (data.scale) {
145 m = m.scale(data.scale);
146 }
147 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
148 })
149
150 function showLabel(data, index) {
151 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', false);
152 }
153
154 function hideLabel(data, index) {
155 d3.select(document.getElementById(data.switches[index].dpid)).classed('nolabel', true);
156 }
157
158 nodes.on('mouseover', showLabel);
159 nodes.on('mouseout', hideLabel);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700160 }
161
162 var ring = svg.selectAll("g")
163 .data(rings)
164 .enter().append("svg:g")
165 .attr("class", "ring")
166 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700167
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700168
169 // do mouseover zoom on edge nodes
Paul Greysond1a22d92013-03-19 12:15:19 -0700170 function zoom(data, index) {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700171 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
172 g.transition().duration(100).attr("r", rings[0].width*3);
Paul Greysonf8f43172013-03-18 23:00:30 -0700173 }
174
175 svg.selectAll('.edge').on('mouseover', zoom);
176 svg.selectAll('.edge').on('mousedown', zoom);
Paul Greysond1a22d92013-03-19 12:15:19 -0700177
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700178 function unzoom(data, index) {
179 var g = d3.select(document.getElementById(data.switches[index].dpid)).select('circle');
180 g.transition().duration(100).attr("r", rings[0].width);
181 }
182 svg.selectAll('.edge').on('mouseout', unzoom);
183
184
Paul Greysond1a22d92013-03-19 12:15:19 -0700185 // DRAW THE LINKS
186 var line = d3.svg.line()
187 .x(function(d) {
188 return d.x;
189 })
190 .y(function(d) {
191 return d.y;
192 })
193 .interpolate("basis");
194
195 d3.select('svg').selectAll('path').data(model.links).enter().append("svg:path").attr("d", function (d) {
196 var src = d3.select(document.getElementById(d['src-switch']));
197 var dst = d3.select(document.getElementById(d['dst-switch']));
198
199 var srcPt = document.querySelector('svg').createSVGPoint();
200 srcPt.x = src.attr('x');
201 srcPt.y = src.attr('y');
202
203 var dstPt = document.querySelector('svg').createSVGPoint();
204 dstPt.x = dst.attr('x');
205 dstPt.y = dst.attr('y');
206
207 return line([srcPt.matrixTransform(src[0][0].getCTM()), dstPt.matrixTransform(dst[0][0].getCTM())]);
208 });
209}
210
211function updateControllers(model) {
212 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
213 controllers.enter().append('div').attr('class', 'controller')
214 .attr('style', function (d) {
215 var color = controllerColorMap[d];
216 if (!color) {
217 color = controllerColorMap[d] = colors.pop();
218 }
219 return 'background-color:' + color;
220 });
221 controllers.text(function (d) {
222 return d;
223 });
224 controllers.exit().remove();
225
226 controllers.on('click', function (data, index) {
227
228 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700229}
230
Paul Greysonb48943b2013-03-19 13:27:57 -0700231var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700232function sync(svg) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700233 updateModel(function (newModel) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700234
Paul Greysonb48943b2013-03-19 13:27:57 -0700235 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
236 updateControllers(newModel);
237 updateTopology(svg, newModel);
238 } else {
239 console.log('no change');
240 }
241 updateHeader(newModel);
242
243 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700244
245 // do it again in 1s
246 setTimeout(function () {
247 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700248 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700249 });
250}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700251
252sync(createTopologyView());