blob: 6f259d07b54a7e55351cb196ae9dd290c7f6e2e8 [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]
18
19var controllerColorMap = {};
20
21
22
Paul Greyson740bdaf2013-03-18 16:10:48 -070023function createTopologyView() {
Paul Greysond1a22d92013-03-19 12:15:19 -070024 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 -070025 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070026}
27
Paul Greysond1a22d92013-03-19 12:15:19 -070028function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -070029 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -070030 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
31 d3.select('#activeFlows').text(model.flows.length);
32}
33
34function toRadians (angle) {
35 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -070036}
37
Paul Greysond1a22d92013-03-19 12:15:19 -070038function updateTopology(svg, model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -070039
Paul Greysond1a22d92013-03-19 12:15:19 -070040 // DRAW THE NODES
Paul Greyson740bdaf2013-03-18 16:10:48 -070041 var rings = [{
42 radius: 3,
Paul Greysonde7fad52013-03-19 12:47:32 -070043 width: 6,
Paul Greyson952ccb62013-03-18 22:22:08 -070044 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070045 className: 'edge',
46 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070047 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070048 radius: 2.25,
Paul Greysonde7fad52013-03-19 12:47:32 -070049 width: 12,
Paul Greyson952ccb62013-03-18 22:22:08 -070050 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070051 className: 'aggregation',
52 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070053 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -070054 radius: .75,
Paul Greysonde7fad52013-03-19 12:47:32 -070055 width: 18,
Paul Greyson952ccb62013-03-18 22:22:08 -070056 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -070057 className: 'core',
58 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -070059 }];
60
Paul Greysonde7fad52013-03-19 12:47:32 -070061
62 var aggRanges = {};
63
64 // arrange edge switches at equal increments
65 var k = 360 / rings[0].switches.length;
66 rings[0].switches.forEach(function (s, i) {
67 var angle = k * i;
68
69 rings[0].angles[i] = angle;
70
71 // record the angle for the agg switch layout
72 var dpid = s.dpid.split(':');
73 dpid[7] = '00';
74 var aggdpid = dpid.join(':');
75 var aggRange = aggRanges[aggdpid];
76 if (!aggRange) {
77 aggRange = aggRanges[aggdpid] = {};
78 aggRange.min = aggRange.max = angle;
79 } else {
80 aggRange.max = angle;
81 }
82
83
84 });
85
86 // arrange aggregation switches to "fan out" to edge switches
87 k = 360 / rings[1].switches.length;
88 rings[1].switches.forEach(function (s, i) {
89// rings[1].angles[i] = k * i;
90 var range = aggRanges[s.dpid];
91
92 rings[1].angles[i] = (range.min + range.max)/2;
93 });
94
95 // arrange core switches at equal increments
96 k = 360 / rings[2].switches.length;
97 rings[2].switches.forEach(function (s, i) {
98 rings[2].angles[i] = k * i;
99 });
100
Paul Greyson740bdaf2013-03-18 16:10:48 -0700101 function ringEnter(data, i) {
102 if (!data.switches.length) {
103 return;
104 }
105
Paul Greyson740bdaf2013-03-18 16:10:48 -0700106
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700107 var nodes = d3.select(this).selectAll("g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700108 .data(d3.range(data.switches.length).map(function() {
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700109 return data;
110 }))
Paul Greyson740bdaf2013-03-18 16:10:48 -0700111 .enter().append("svg:g")
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700112 .classed('nolabel', true)
Paul Greyson23b0cd32013-03-18 23:45:48 -0700113 .attr("id", function (_, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700114 return data.switches[i].dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700115 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700116 .attr("transform", function(_, i) {
Paul Greysonde7fad52013-03-19 12:47:32 -0700117 return "rotate(" + data.angles[i]+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angles[i]) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700118 });
119
120 nodes.append("svg:circle")
Paul Greyson3e142162013-03-19 13:56:17 -0700121 .attr('class', function (_, i) {
122 return data.className + ' ' + controllerColorMap[data.switches[i].controller];
123 })
Paul Greyson952ccb62013-03-18 22:22:08 -0700124 .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)
Paul Greyson3e142162013-03-19 13:56:17 -0700134 // .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);
Paul Greyson3e142162013-03-19 13:56:17 -0700213 controllers.enter().append('div')
214 .attr('class', function (d) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700215 var color = controllerColorMap[d];
216 if (!color) {
217 color = controllerColorMap[d] = colors.pop();
218 }
Paul Greyson3e142162013-03-19 13:56:17 -0700219 return 'controller ' + color;
Paul Greysond1a22d92013-03-19 12:15:19 -0700220 });
221 controllers.text(function (d) {
222 return d;
223 });
224 controllers.exit().remove();
225
Paul Greyson3e142162013-03-19 13:56:17 -0700226 model.controllers.forEach(function (c) {
227 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
228 });
Paul Greysond1a22d92013-03-19 12:15:19 -0700229
Paul Greyson3e142162013-03-19 13:56:17 -0700230 controllers.on('click', function (c, index) {
231 var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
232 d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -0700233 });
Paul Greyson740bdaf2013-03-18 16:10:48 -0700234}
235
Paul Greysonb48943b2013-03-19 13:27:57 -0700236var oldModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700237function sync(svg) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700238 updateModel(function (newModel) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700239
Paul Greysonb48943b2013-03-19 13:27:57 -0700240 if (!oldModel && JSON.stringify(oldModel) != JSON.stringify(newModel)) {
241 updateControllers(newModel);
242 updateTopology(svg, newModel);
243 } else {
244 console.log('no change');
245 }
246 updateHeader(newModel);
247
248 oldModel = newModel;
Paul Greyson740bdaf2013-03-18 16:10:48 -0700249
250 // do it again in 1s
251 setTimeout(function () {
252 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -0700253 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700254 });
255}
Paul Greyson740bdaf2013-03-18 16:10:48 -0700256
257sync(createTopologyView());