blob: 6176c3a26ddd5c3e037d24c7222ec7b4302543dd [file] [log] [blame]
Paul Greyson127d7fb2013-03-25 23:39:20 -07001/*global d3, document∆*/
Paul Greyson740bdaf2013-03-18 16:10:48 -07002
Paul Greyson6d9ed862013-03-23 17:37:15 -07003d3.selection.prototype.moveToFront = function() {
4 return this.each(function(){
5 this.parentNode.appendChild(this);
6 });
7};
Paul Greysond1a22d92013-03-19 12:15:19 -07008
Paul Greyson127d7fb2013-03-25 23:39:20 -07009var line = d3.svg.line()
10 .x(function(d) {
11 return d.x;
12 })
13 .y(function(d) {
14 return d.y;
15 });
16
Paul Greyson56378ed2013-03-26 23:17:36 -070017var model;
Paul Greyson29aa98d2013-03-28 00:09:31 -070018var svg;
Paul Greyson56378ed2013-03-26 23:17:36 -070019var updateTopology;
Paul Greyson40c8a592013-03-27 14:10:33 -070020var pendingLinks = {};
Paul Greyson6f918402013-03-28 12:18:30 -070021var selectedFlows = [];
22
Paul Greyson97cc9f52013-04-04 01:45:40 -070023var pendingTimeout = 30000;
Paul Greyson127d7fb2013-03-25 23:39:20 -070024
Paul Greysond1a22d92013-03-19 12:15:19 -070025var colors = [
Paul Greyson3e142162013-03-19 13:56:17 -070026 'color1',
27 'color2',
28 'color3',
29 'color4',
Paul Greyson3e142162013-03-19 13:56:17 -070030 'color7',
31 'color8',
32 'color9',
Paul Greyson5cc35f02013-03-28 10:07:36 -070033// 'color11',
Paul Greyson127d7fb2013-03-25 23:39:20 -070034 'color12'
35];
Paul Greyson01a5dff2013-03-19 15:50:14 -070036colors.reverse();
Paul Greysond1a22d92013-03-19 12:15:19 -070037
38var controllerColorMap = {};
39
Paul Greyson084779b2013-03-27 13:55:49 -070040function setPending(selection) {
41 selection.classed('pending', false);
42 setTimeout(function () {
43 selection.classed('pending', true);
44 })
45}
Paul Greysond1a22d92013-03-19 12:15:19 -070046
Paul Greyson740bdaf2013-03-18 16:10:48 -070047function createTopologyView() {
Paul Greyson56378ed2013-03-26 23:17:36 -070048
49 window.addEventListener('resize', function () {
50 // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied
51 // either that or is it possible to position the paths so they get the automatic transform as well?
Paul Greyson5cc35f02013-03-28 10:07:36 -070052// updateTopology();
Paul Greyson56378ed2013-03-26 23:17:36 -070053 });
54
Paul Greysonb367de22013-03-23 11:09:11 -070055 var svg = d3.select('#svg-container').append('svg:svg');
56
57 svg.append("svg:defs").append("svg:marker")
58 .attr("id", "arrow")
59 .attr("viewBox", "0 -5 10 10")
60 .attr("refX", -1)
61 .attr("markerWidth", 5)
62 .attr("markerHeight", 5)
63 .attr("orient", "auto")
64 .append("svg:path")
Paul Greyson45303ac2013-03-23 16:44:01 -070065 .attr("d", "M0,-3L10,0L0,3");
Paul Greysonb367de22013-03-23 11:09:11 -070066
67 return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none').
Paul Greyson952ccb62013-03-18 22:22:08 -070068 attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)');
Paul Greyson740bdaf2013-03-18 16:10:48 -070069}
70
Paul Greyson13f02b92013-03-28 11:29:35 -070071function updateSelectedFlowsTopology() {
Paul Greyson127d7fb2013-03-25 23:39:20 -070072 // DRAW THE FLOWS
Paul Greyson97cc9f52013-04-04 01:45:40 -070073 var topologyFlows = [];
74 selectedFlows.forEach(function (flow) {
75 if (flow) {
76 topologyFlows.push(flow);
77 }
78 });
79
80 var flows = d3.select('svg').selectAll('.flow').data(topologyFlows);
Paul Greyson127d7fb2013-03-25 23:39:20 -070081
Paul Greyson29aa98d2013-03-28 00:09:31 -070082 flows.enter().append("svg:path").attr('class', 'flow')
83 .attr('stroke-dasharray', '4, 10')
84 .append('svg:animate')
85 .attr('attributeName', 'stroke-dashoffset')
86 .attr('attributeType', 'xml')
87 .attr('from', '500')
88 .attr('to', '-500')
89 .attr('dur', '20s')
90 .attr('repeatCount', 'indefinite');
91
Paul Greyson97cc9f52013-04-04 01:45:40 -070092 flows.exit().remove();
Paul Greyson29aa98d2013-03-28 00:09:31 -070093
94 flows.attr('d', function (d) {
Paul Greyson13f02b92013-03-28 11:29:35 -070095 if (!d) {
96 return;
97 }
98 var pts = [];
Paul Greysonbb403b02013-04-04 17:11:24 -070099 if (!d.dataPath.flowEntries) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700100 // create a temporary vector to indicate the pending flow
101 var s1 = d3.select(document.getElementById(d.dataPath.srcPort.dpid.value));
102 var s2 = d3.select(document.getElementById(d.dataPath.dstPort.dpid.value));
103
104 var pt1 = document.querySelector('svg').createSVGPoint();
105 pt1.x = s1.attr('x');
106 pt1.y = s1.attr('y');
107 pt1 = pt1.matrixTransform(s1[0][0].getCTM());
108 pts.push(pt1);
109
110 var pt2 = document.querySelector('svg').createSVGPoint();
111 pt2.x = s2.attr('x');
112 pt2.y = s2.attr('y');
113 pt2 = pt2.matrixTransform(s2[0][0].getCTM());
114 pts.push(pt2);
115
116 } else {
117 d.dataPath.flowEntries.forEach(function (flowEntry) {
118 var s = d3.select(document.getElementById(flowEntry.dpid.value));
119 // s[0] is null if the flow entry refers to a non-existent switch
120 if (s[0][0]) {
121 var pt = document.querySelector('svg').createSVGPoint();
122 pt.x = s.attr('x');
123 pt.y = s.attr('y');
124 pt = pt.matrixTransform(s[0][0].getCTM());
125 pts.push(pt);
126 } else {
127 console.log('flow refers to non-existent switch: ' + flowEntry.dpid.value);
128 }
129 });
130 }
Paul Greysonbb403b02013-04-04 17:11:24 -0700131 if (pts.length) {
132 return line(pts);
133 } else {
134 return "M0,0";
135 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700136 })
Paul Greyson71b550d2013-03-28 11:56:19 -0700137 .attr('id', function (d) {
138 if (d) {
139 return makeFlowKey(d);
140 }
141 })
Paul Greyson13f02b92013-03-28 11:29:35 -0700142 .classed('pending', function (d) {
Paul Greysonaa812562013-03-28 12:43:12 -0700143 return d && (d.createPending || d.deletePending);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700144 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700145
Paul Greyson56378ed2013-03-26 23:17:36 -0700146 // "marching ants"
Paul Greyson29aa98d2013-03-28 00:09:31 -0700147 flows.select('animate').attr('from', 500);
148
Paul Greyson13f02b92013-03-28 11:29:35 -0700149}
150
151function updateSelectedFlowsTable() {
152 function rowEnter(d) {
153 var row = d3.select(this);
Paul Greyson6f918402013-03-28 12:18:30 -0700154 row.append('div').classed('deleteFlow', true);
Paul Greyson13f02b92013-03-28 11:29:35 -0700155 row.append('div').classed('flowId', true);
156 row.append('div').classed('srcDPID', true);
157 row.append('div').classed('dstDPID', true);
158 row.append('div').classed('iperf', true);
Paul Greyson71b550d2013-03-28 11:56:19 -0700159
Paul Greyson95db7a12013-04-04 14:57:58 -0700160 row.select('.iperf')
161 .append('div')
162 .attr('class', 'iperf-container')
163 .append('svg:svg')
164 .attr('viewBox', '0 0 1000 32')
165 .attr('preserveAspectRatio', 'none')
166 .append('svg:g')
167 .append('svg:path')
168 .attr('class', 'iperfdata');
169
Paul Greyson71b550d2013-03-28 11:56:19 -0700170 row.on('mouseover', function (d) {
171 if (d) {
172 var path = document.getElementById(makeFlowKey(d));
173 d3.select(path).classed('highlight', true);
174 }
175 });
176 row.on('mouseout', function (d) {
177 if (d) {
178 var path = document.getElementById(makeFlowKey(d));
179 d3.select(path).classed('highlight', false);
180 }
Paul Greyson95db7a12013-04-04 14:57:58 -0700181 });
Paul Greyson13f02b92013-03-28 11:29:35 -0700182 }
183
184 function rowUpdate(d) {
185 var row = d3.select(this);
Paul Greyson95db7a12013-04-04 14:57:58 -0700186 row.attr('id', function (d) {
187 if (d) {
188 return makeSelectedFlowKey(d);
189 }
190 });
Paul Greyson2c35f572013-04-04 16:23:48 -0700191
Paul Greysonbb403b02013-04-04 17:11:24 -0700192 if (!d || !hasIPerf(d)) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700193 row.select('.iperfdata')
194 .attr('d', 'M0,0');
195 }
196
Paul Greyson6f918402013-03-28 12:18:30 -0700197 row.select('.deleteFlow').on('click', function () {
Paul Greyson2c35f572013-04-04 16:23:48 -0700198 deselectFlow(d);
Paul Greysonbe8bf872013-04-04 01:53:45 -0700199 });
200 row.on('dblclick', function () {
Paul Greyson6f918402013-03-28 12:18:30 -0700201 if (d) {
202 var prompt = 'Delete flow ' + d.flowId.value + '?';
203 if (confirm(prompt)) {
204 deleteFlow(d);
Paul Greysonaa812562013-03-28 12:43:12 -0700205 d.deletePending = true;
Paul Greyson6f918402013-03-28 12:18:30 -0700206 updateSelectedFlows();
207
208 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700209 d.deletePending = false;
Paul Greyson6f918402013-03-28 12:18:30 -0700210 updateSelectedFlows();
211 }, pendingTimeout)
212 };
213 }
214 });
215
Paul Greyson13f02b92013-03-28 11:29:35 -0700216 row.select('.flowId')
217 .text(function (d) {
218 if (d) {
219 if (d.flowId) {
220 return d.flowId.value;
221 } else {
222 return '0x--';
223 }
224 }
225 })
Paul Greysoncb5d30d2013-04-04 02:00:56 -0700226 .classed('pending', function (d) {
227 return d && (d.createPending || d.deletePending);
228 });
Paul Greyson13f02b92013-03-28 11:29:35 -0700229
230 row.select('.srcDPID')
231 .text(function (d) {
232 if (d) {
233 return d.dataPath.srcPort.dpid.value;
234 }
235 });
236
237 row.select('.dstDPID')
238 .text(function (d) {
239 if (d) {
240 return d.dataPath.dstPort.dpid.value;
241 }
242 });
243 }
244
245 var flows = d3.select('#selectedFlows')
246 .selectAll('.selectedFlow')
247 .data(selectedFlows);
248
249 flows.enter()
250 .append('div')
251 .classed('selectedFlow', true)
252 .each(rowEnter);
253
254 flows.each(rowUpdate);
255
Paul Greyson29aa98d2013-03-28 00:09:31 -0700256 flows.exit().remove();
Paul Greyson127d7fb2013-03-25 23:39:20 -0700257}
258
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700259// TODO: cancel the interval when the flow is desel
260function startIPerfForFlow(flow) {
Paul Greyson95db7a12013-04-04 14:57:58 -0700261 var duration = 10000; // seconds
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700262 var interval = 100; // ms. this is defined by the server
Paul Greysonbb403b02013-04-04 17:11:24 -0700263 var updateRate = 2000; // ms
Paul Greysonf7a77db2013-04-04 18:48:20 -0700264 var pointsToDisplay = 1000;
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700265
Paul Greyson2c35f572013-04-04 16:23:48 -0700266 function makePoints() {
267 var pts = [];
268 var i;
Paul Greysonf7a77db2013-04-04 18:48:20 -0700269 for (i=0; i < pointsToDisplay; ++i) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700270 var sample = flow.iperfData.samples[i];
271 var height = 32 * sample/50000000;
272 if (height > 32)
273 height = 32;
274 pts.push({
Paul Greysonf7a77db2013-04-04 18:48:20 -0700275 x: i * 1000/(pointsToDisplay-1),
Paul Greyson2c35f572013-04-04 16:23:48 -0700276 y: 32 - height
277 })
278 }
279 return pts;
280 }
281
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700282 if (flow.flowId) {
283 console.log('starting iperf for: ' + flow.flowId.value);
284 startIPerf(flow, duration, updateRate/interval);
Paul Greyson2c35f572013-04-04 16:23:48 -0700285 flow.iperfDisplayInterval = setInterval(function () {
286 if (flow.iperfData) {
Paul Greysonf7a77db2013-04-04 18:48:20 -0700287 while (flow.iperfData.samples.length < pointsToDisplay) {
Paul Greysonbb403b02013-04-04 17:11:24 -0700288 flow.iperfData.samples.push(0);
Paul Greyson2c35f572013-04-04 16:23:48 -0700289 }
290 var iperfPath = d3.select(document.getElementById(makeSelectedFlowKey(flow))).select('path');
291 iperfPath.attr('d', line(makePoints()));
292 flow.iperfData.samples.shift();
293 }
294
295
296 }, interval);
297 flow.iperfFetchInterval = setInterval(function () {
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700298 getIPerfData(flow, function (data) {
Paul Greyson95db7a12013-04-04 14:57:58 -0700299 try {
Paul Greyson2c35f572013-04-04 16:23:48 -0700300 if (!flow.iperfData) {
301 flow.iperfData = {
302 samples: []
303 };
304 var i;
Paul Greysonf7a77db2013-04-04 18:48:20 -0700305 for (i = 0; i < pointsToDisplay; ++i) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700306 flow.iperfData.samples.push(0);
307 }
308 }
309
Paul Greyson95db7a12013-04-04 14:57:58 -0700310 var iperfData = JSON.parse(data);
311 // if the data is fresh
Paul Greysonbb403b02013-04-04 17:11:24 -0700312 if (flow.iperfData.timestamp && iperfData.timestamp != flow.iperfData.timestamp) {
Paul Greyson2c35f572013-04-04 16:23:48 -0700313 iperfData.samples.forEach(function (s) {
314 flow.iperfData.samples.push(s);
315 });
Paul Greyson95db7a12013-04-04 14:57:58 -0700316 }
Paul Greyson2c35f572013-04-04 16:23:48 -0700317 flow.iperfData.timestamp = iperfData.timestamp;
Paul Greyson95db7a12013-04-04 14:57:58 -0700318 } catch (e) {
319 console.log('bad iperf data: ' + data);
320 }
321// console.log(data);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700322 });
Paul Greysonbb403b02013-04-04 17:11:24 -0700323 }, updateRate/2); // over sample to avoid gaps
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700324 }
325}
326
Paul Greyson13f02b92013-03-28 11:29:35 -0700327function updateSelectedFlows() {
328 // make sure that all of the selected flows are either
329 // 1) valid (meaning they are in the latest list of flows)
330 // 2) pending
331 if (model) {
332 var flowMap = {};
333 model.flows.forEach(function (flow) {
334 flowMap[makeFlowKey(flow)] = flow;
335 });
336
337 var newSelectedFlows = [];
338 selectedFlows.forEach(function (flow) {
339 if (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700340 var liveFlow = flowMap[makeFlowKey(flow)];
341 if (liveFlow) {
342 newSelectedFlows.push(liveFlow);
Paul Greysonaa812562013-03-28 12:43:12 -0700343 liveFlow.deletePending = flow.deletePending;
Paul Greyson2c35f572013-04-04 16:23:48 -0700344 liveFlow.iperfFetchInterval = flow.iperfFetchInterval;
345 liveFlow.iperfDisplayInterval = flow.iperfDisplayInterval;
Paul Greysonaa812562013-03-28 12:43:12 -0700346 } else if (flow.createPending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700347 newSelectedFlows.push(flow);
Paul Greysonbb403b02013-04-04 17:11:24 -0700348 } else if (hasIPerf(flow)) {
349 clearIPerf(flow);
Paul Greyson13f02b92013-03-28 11:29:35 -0700350 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700351 }
352 });
353 selectedFlows = newSelectedFlows;
354 }
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700355 selectedFlows.forEach(function (flow) {
Paul Greysonbb403b02013-04-04 17:11:24 -0700356 if (!hasIPerf(flow)) {
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700357 startIPerfForFlow(flow);
358 }
359 });
Paul Greyson6f918402013-03-28 12:18:30 -0700360 while (selectedFlows.length < 3) {
361 selectedFlows.push(null);
362 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700363
364 updateSelectedFlowsTable();
365 updateSelectedFlowsTopology();
366}
367
368function selectFlow(flow) {
Paul Greysonc30f75e2013-03-28 11:45:15 -0700369 var flowKey = makeFlowKey(flow);
370 var alreadySelected = false;
371 selectedFlows.forEach(function (f) {
372 if (f && makeFlowKey(f) === flowKey) {
373 alreadySelected = true;
374 }
375 });
376
377 if (!alreadySelected) {
378 selectedFlows.unshift(flow);
379 selectedFlows = selectedFlows.slice(0, 3);
380 updateSelectedFlows();
381 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700382}
383
Paul Greysonbb403b02013-04-04 17:11:24 -0700384function hasIPerf(flow) {
385 return flow && flow.iperfFetchInterval;
386}
387
388function clearIPerf(flow) {
389 console.log('clearing iperf interval for: ' + flow.flowId.value);
390 clearInterval(flow.iperfFetchInterval);
391 delete flow.iperfFetchInterval;
392 clearInterval(flow.iperfDisplayInterval);
393 delete flow.iperfDisplayInterval;
394 delete flow.iperfData;
395}
396
Paul Greysonaa812562013-03-28 12:43:12 -0700397function deselectFlow(flow, ifCreatePending) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700398 var flowKey = makeFlowKey(flow);
399 var newSelectedFlows = [];
400 selectedFlows.forEach(function (flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700401 if (!flow ||
402 flowKey !== makeFlowKey(flow) ||
Paul Greysonaa812562013-03-28 12:43:12 -0700403 flowKey === makeFlowKey(flow) && ifCreatePending && !flow.createPending ) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700404 newSelectedFlows.push(flow);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700405 } else {
Paul Greysonbb403b02013-04-04 17:11:24 -0700406 if (hasIPerf(flow)) {
407 clearIPerf(flow);
Paul Greyson4b4c8af2013-04-04 09:02:09 -0700408 }
Paul Greyson13f02b92013-03-28 11:29:35 -0700409 }
410 });
411 selectedFlows = newSelectedFlows;
412 while (selectedFlows.length < 3) {
413 selectedFlows.push(null);
414 }
415
416 updateSelectedFlows();
417}
418
Paul Greysonaa812562013-03-28 12:43:12 -0700419function deselectFlowIfCreatePending(flow) {
Paul Greysonf430fd02013-03-28 12:32:24 -0700420 deselectFlow(flow, true);
421}
422
Paul Greyson29aa98d2013-03-28 00:09:31 -0700423function showFlowChooser() {
424 function rowEnter(d) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700425 var row = d3.select(this);
426
Paul Greyson127d7fb2013-03-25 23:39:20 -0700427 row.append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -0700428 .classed('black-eye', true).
Paul Greyson29aa98d2013-03-28 00:09:31 -0700429 on('click', function () {
Paul Greyson13f02b92013-03-28 11:29:35 -0700430 selectFlow(d);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700431 });
432
433 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700434 .classed('flowId', true)
435 .text(function (d) {
436 return d.flowId.value;
437 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700438
439 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700440 .classed('srcDPID', true)
441 .text(function (d) {
442 return d.dataPath.srcPort.dpid.value;
443 });
444
Paul Greyson127d7fb2013-03-25 23:39:20 -0700445
446 row.append('div')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700447 .classed('dstDPID', true)
448 .text(function (d) {
449 return d.dataPath.dstPort.dpid.value;
450 });
Paul Greyson127d7fb2013-03-25 23:39:20 -0700451
Paul Greyson127d7fb2013-03-25 23:39:20 -0700452 }
453
Paul Greyson29aa98d2013-03-28 00:09:31 -0700454 var flows = d3.select('#flowChooser')
455 .append('div')
456 .style('pointer-events', 'auto')
Paul Greyson127d7fb2013-03-25 23:39:20 -0700457 .selectAll('.selectedFlow')
Paul Greyson29aa98d2013-03-28 00:09:31 -0700458 .data(model.flows)
Paul Greyson127d7fb2013-03-25 23:39:20 -0700459 .enter()
460 .append('div')
461 .classed('selectedFlow', true)
462 .each(rowEnter);
463
Paul Greyson29aa98d2013-03-28 00:09:31 -0700464 setTimeout(function () {
465 d3.select(document.body).on('click', function () {
466 d3.select('#flowChooser').html('');
467 d3.select(document.body).on('click', null);
468 });
469 }, 0);
470}
471
Paul Greyson29aa98d2013-03-28 00:09:31 -0700472
Paul Greyson127d7fb2013-03-25 23:39:20 -0700473
Paul Greysond1a22d92013-03-19 12:15:19 -0700474function updateHeader(model) {
Paul Greysonb48943b2013-03-19 13:27:57 -0700475 d3.select('#lastUpdate').text(new Date());
Paul Greyson952ccb62013-03-18 22:22:08 -0700476 d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length);
477 d3.select('#activeFlows').text(model.flows.length);
478}
479
480function toRadians (angle) {
481 return angle * (Math.PI / 180);
Paul Greyson740bdaf2013-03-18 16:10:48 -0700482}
483
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700484var widths = {
485 edge: 6,
486 aggregation: 12,
487 core: 18
488}
489
Paul Greysonc17278a2013-03-23 10:17:12 -0700490function createRingsFromModel(model) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700491 var rings = [{
492 radius: 3,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700493 width: widths.edge,
Paul Greyson952ccb62013-03-18 22:22:08 -0700494 switches: model.edgeSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700495 className: 'edge',
496 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700497 }, {
Paul Greysond1a22d92013-03-19 12:15:19 -0700498 radius: 2.25,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700499 width: widths.aggregation,
Paul Greyson952ccb62013-03-18 22:22:08 -0700500 switches: model.aggregationSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700501 className: 'aggregation',
502 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700503 }, {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700504 radius: 0.75,
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700505 width: widths.core,
Paul Greyson952ccb62013-03-18 22:22:08 -0700506 switches: model.coreSwitches,
Paul Greysonde7fad52013-03-19 12:47:32 -0700507 className: 'core',
508 angles: []
Paul Greyson740bdaf2013-03-18 16:10:48 -0700509 }];
510
Paul Greysonde7fad52013-03-19 12:47:32 -0700511
512 var aggRanges = {};
513
514 // arrange edge switches at equal increments
515 var k = 360 / rings[0].switches.length;
516 rings[0].switches.forEach(function (s, i) {
517 var angle = k * i;
518
519 rings[0].angles[i] = angle;
520
521 // record the angle for the agg switch layout
522 var dpid = s.dpid.split(':');
Paul Greyson832d2202013-03-21 13:27:56 -0700523 dpid[7] = '01'; // the last component of the agg switch is always '01'
Paul Greysonde7fad52013-03-19 12:47:32 -0700524 var aggdpid = dpid.join(':');
525 var aggRange = aggRanges[aggdpid];
526 if (!aggRange) {
527 aggRange = aggRanges[aggdpid] = {};
528 aggRange.min = aggRange.max = angle;
529 } else {
530 aggRange.max = angle;
531 }
Paul Greysonde7fad52013-03-19 12:47:32 -0700532 });
533
534 // arrange aggregation switches to "fan out" to edge switches
535 k = 360 / rings[1].switches.length;
536 rings[1].switches.forEach(function (s, i) {
537// rings[1].angles[i] = k * i;
538 var range = aggRanges[s.dpid];
539
Paul Greyson832d2202013-03-21 13:27:56 -0700540 rings[1].angles[i] = (range.min + range.max)/2;
Paul Greysonde7fad52013-03-19 12:47:32 -0700541 });
542
Paul Greyson3f890b62013-03-22 17:39:36 -0700543 // find the association between core switches and aggregation switches
544 var aggregationSwitchMap = {};
545 model.aggregationSwitches.forEach(function (s, i) {
Paul Greysonc17278a2013-03-23 10:17:12 -0700546 aggregationSwitchMap[s.dpid] = i;
Paul Greyson3f890b62013-03-22 17:39:36 -0700547 });
548
Paul Greyson3f890b62013-03-22 17:39:36 -0700549 // put core switches next to linked aggregation switches
Paul Greysonde7fad52013-03-19 12:47:32 -0700550 k = 360 / rings[2].switches.length;
551 rings[2].switches.forEach(function (s, i) {
Paul Greyson3f890b62013-03-22 17:39:36 -0700552// rings[2].angles[i] = k * i;
Paul Greysonc17278a2013-03-23 10:17:12 -0700553 var associatedAggregationSwitches = model.configuration.association[s.dpid];
554 // TODO: go between if there are multiple
555 var index = aggregationSwitchMap[associatedAggregationSwitches[0]];
556
557 rings[2].angles[i] = rings[1].angles[index];
Paul Greysonde7fad52013-03-19 12:47:32 -0700558 });
559
Paul Greyson644d92a2013-03-23 18:00:40 -0700560 // TODO: construct this form initially rather than converting. it works better because
561 // it allows binding by dpid
562 var testRings = [];
563 rings.forEach(function (ring) {
564 var testRing = [];
565 ring.switches.forEach(function (s, i) {
566 var testSwitch = {
567 dpid: s.dpid,
568 state: s.state,
569 radius: ring.radius,
570 width: ring.width,
571 className: ring.className,
572 angle: ring.angles[i],
573 controller: s.controller
Paul Greyson127d7fb2013-03-25 23:39:20 -0700574 };
Paul Greyson644d92a2013-03-23 18:00:40 -0700575 testRing.push(testSwitch);
576 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700577
578
Paul Greyson644d92a2013-03-23 18:00:40 -0700579 testRings.push(testRing);
580 });
Paul Greyson6d9ed862013-03-23 17:37:15 -0700581
582
Paul Greyson644d92a2013-03-23 18:00:40 -0700583// return rings;
584 return testRings;
Paul Greysonc17278a2013-03-23 10:17:12 -0700585}
586
Paul Greyson40c8a592013-03-27 14:10:33 -0700587function makeLinkKey(link) {
588 return link['src-switch'] + '=>' + link['dst-switch'];
589}
590
Paul Greyson13f02b92013-03-28 11:29:35 -0700591function makeFlowKey(flow) {
592 return flow.dataPath.srcPort.dpid.value + '=>' + flow.dataPath.dstPort.dpid.value;
593}
594
Paul Greyson95db7a12013-04-04 14:57:58 -0700595function makeSelectedFlowKey(flow) {
596 return 'S' + makeFlowKey(flow);
597}
598
Paul Greyson40c8a592013-03-27 14:10:33 -0700599function createLinkMap(links) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700600 var linkMap = {};
Paul Greyson40c8a592013-03-27 14:10:33 -0700601 links.forEach(function (link) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700602 var srcDPID = link['src-switch'];
603 var dstDPID = link['dst-switch'];
604
605 var srcMap = linkMap[srcDPID] || {};
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700606
Paul Greyson8d1c6362013-03-27 13:05:24 -0700607 srcMap[dstDPID] = link;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700608
609 linkMap[srcDPID] = srcMap;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700610 });
611 return linkMap;
612}
613
Paul Greyson5cc35f02013-03-28 10:07:36 -0700614// removes links from the pending list that are now in the model
615function reconcilePendingLinks(model) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700616 var links = [];
617 model.links.forEach(function (link) {
618 links.push(link);
619 delete pendingLinks[makeLinkKey(link)]
620 })
621 var linkId;
622 for (linkId in pendingLinks) {
623 links.push(pendingLinks[linkId]);
624 }
Paul Greyson5cc35f02013-03-28 10:07:36 -0700625 return links
626}
Paul Greyson40c8a592013-03-27 14:10:33 -0700627
Paul Greyson5cc35f02013-03-28 10:07:36 -0700628updateTopology = function() {
629
630 // DRAW THE SWITCHES
631 var rings = svg.selectAll('.ring').data(createRingsFromModel(model));
632
633 var links = reconcilePendingLinks(model);
Paul Greyson40c8a592013-03-27 14:10:33 -0700634 var linkMap = createLinkMap(links);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700635
Paul Greyson8d1c6362013-03-27 13:05:24 -0700636 function mouseOverSwitch(data) {
Paul Greyson72f18852013-03-27 15:56:11 -0700637
638 d3.event.preventDefault();
639
Paul Greyson5cc35f02013-03-28 10:07:36 -0700640 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false);
641
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700642 if (data.highlighted) {
643 return;
644 }
645
646 // only highlight valid link or flow destination by checking for class of existing highlighted circle
Paul Greyson421bfcd2013-03-27 22:22:09 -0700647 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700648 if (highlighted.length == 1) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700649 var s = d3.select(highlighted[0]).select('circle');
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700650 // only allow links
651 // edge->edge (flow)
652 // aggregation->core
653 // core->core
654 if (data.className == 'edge' && !s.classed('edge') ||
655 data.className == 'core' && !s.classed('core') && !s.classed('aggregation') ||
656 data.className == 'aggregation' && !s.classed('core')) {
657 return;
658 }
659
660 // don't highlight if there's already a link or flow
661 // var map = linkMap[data.dpid];
662 // console.log(map);
663 // console.log(s.data()[0].dpid);
664 // console.log(map[s.data()[0].dpid]);
665 // if (map && map[s.data()[0].dpid]) {
666 // return;
667 // }
668
669 // the second highlighted switch is the target for a link or flow
670 data.target = true;
671 }
672
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700673 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700674 node.classed('highlight', true).select('circle').transition().duration(100).attr("r", widths.core);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700675 data.highlighted = true;
676 node.moveToFront();
677 }
678
Paul Greyson8d1c6362013-03-27 13:05:24 -0700679 function mouseOutSwitch(data) {
Paul Greyson5cc35f02013-03-28 10:07:36 -0700680 d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true);
681
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700682 if (data.mouseDown)
683 return;
684
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700685 var node = d3.select(document.getElementById(data.dpid));
Paul Greyson421bfcd2013-03-27 22:22:09 -0700686 node.classed('highlight', false).select('circle').transition().duration(100).attr("r", widths[data.className]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700687 data.highlighted = false;
688 data.target = false;
689 }
690
Paul Greyson8d1c6362013-03-27 13:05:24 -0700691 function mouseDownSwitch(data) {
692 mouseOverSwitch(data);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700693 data.mouseDown = true;
Paul Greyson72f18852013-03-27 15:56:11 -0700694 d3.select('#topology').classed('linking', true);
695
Paul Greyson421bfcd2013-03-27 22:22:09 -0700696 d3.select('svg')
697 .append('svg:path')
698 .attr('id', 'linkVector')
699 .attr('d', function () {
700 var s = d3.select(document.getElementById(data.dpid));
701
702 var pt = document.querySelector('svg').createSVGPoint();
703 pt.x = s.attr('x');
704 pt.y = s.attr('y');
705 pt = pt.matrixTransform(s[0][0].getCTM());
706
707 return line([pt, pt]);
708 });
709
710
Paul Greyson72f18852013-03-27 15:56:11 -0700711 if (data.className === 'core') {
712 d3.selectAll('.edge').classed('nodrop', true);
713 }
714 if (data.className === 'edge') {
715 d3.selectAll('.core').classed('nodrop', true);
716 d3.selectAll('.aggregation').classed('nodrop', true);
717 }
718 if (data.className === 'aggregation') {
719 d3.selectAll('.edge').classed('nodrop', true);
720 d3.selectAll('.aggregation').classed('nodrop', true);
721 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700722 }
723
Paul Greyson8d1c6362013-03-27 13:05:24 -0700724 function mouseUpSwitch(data) {
725 if (data.mouseDown) {
726 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700727 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700728 d3.event.stopPropagation();
Paul Greyson72f18852013-03-27 15:56:11 -0700729 d3.selectAll('.nodrop').classed('nodrop', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700730 }
731 }
732
733 function doubleClickSwitch(data) {
Paul Greyson084779b2013-03-27 13:55:49 -0700734 var circle = d3.select(document.getElementById(data.dpid)).select('circle');
Paul Greyson8d1c6362013-03-27 13:05:24 -0700735 if (data.state == 'ACTIVE') {
736 var prompt = 'Deactivate ' + data.dpid + '?';
737 if (confirm(prompt)) {
738 switchDown(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700739 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700740 }
741 } else {
742 var prompt = 'Activate ' + data.dpid + '?';
743 if (confirm(prompt)) {
744 switchUp(data);
Paul Greyson084779b2013-03-27 13:55:49 -0700745 setPending(circle);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700746 }
747 }
748 }
749
Paul Greyson740bdaf2013-03-18 16:10:48 -0700750 function ringEnter(data, i) {
Paul Greyson644d92a2013-03-23 18:00:40 -0700751 if (!data.length) {
Paul Greyson740bdaf2013-03-18 16:10:48 -0700752 return;
753 }
754
Paul Greysonc17278a2013-03-23 10:17:12 -0700755 // create the nodes
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700756 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700757 .data(data, function (data) {
758 return data.dpid;
759 })
Paul Greyson740bdaf2013-03-18 16:10:48 -0700760 .enter().append("svg:g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700761 .attr("id", function (data, i) {
762 return data.dpid;
Paul Greyson23b0cd32013-03-18 23:45:48 -0700763 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700764 .attr("transform", function(data, i) {
765 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700766 });
767
Paul Greysonc17278a2013-03-23 10:17:12 -0700768 // add the cirles representing the switches
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700769 nodes.append("svg:circle")
Paul Greyson644d92a2013-03-23 18:00:40 -0700770 .attr("transform", function(data, i) {
Paul Greysond1a22d92013-03-19 12:15:19 -0700771 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
Paul Greysonf8f43172013-03-18 23:00:30 -0700772 if (data.scale) {
773 m = m.scale(data.scale);
774 }
775 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
Paul Greyson952ccb62013-03-18 22:22:08 -0700776 })
Paul Greyson644d92a2013-03-23 18:00:40 -0700777 .attr("x", function (data) {
778 return -data.width / 2;
779 })
780 .attr("y", function (data) {
781 return -data.width / 2;
782 })
783 .attr("r", function (data) {
784 return data.width;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700785 });
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700786
Paul Greysonc17278a2013-03-23 10:17:12 -0700787 // setup the mouseover behaviors
Paul Greyson8d1c6362013-03-27 13:05:24 -0700788 nodes.on('mouseover', mouseOverSwitch);
789 nodes.on('mouseout', mouseOutSwitch);
790 nodes.on('mouseup', mouseUpSwitch);
791 nodes.on('mousedown', mouseDownSwitch);
792
793 // only do switch up/down for core switches
794 if (i == 2) {
795 nodes.on('dblclick', doubleClickSwitch);
796 }
Paul Greyson740bdaf2013-03-18 16:10:48 -0700797 }
798
Paul Greysonc17278a2013-03-23 10:17:12 -0700799 // append switches
800 rings.enter().append("svg:g")
Paul Greyson740bdaf2013-03-18 16:10:48 -0700801 .attr("class", "ring")
802 .each(ringEnter);
Paul Greysonf8f43172013-03-18 23:00:30 -0700803
Paul Greysonf9edc1a2013-03-19 13:22:06 -0700804
Paul Greysonc17278a2013-03-23 10:17:12 -0700805 function ringUpdate(data, i) {
Paul Greyson127d7fb2013-03-25 23:39:20 -0700806 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -0700807 .data(data, function (data) {
808 return data.dpid;
Paul Greyson127d7fb2013-03-25 23:39:20 -0700809 });
Paul Greyson347fb742013-03-27 13:40:29 -0700810 nodes.select('circle')
811 .each(function (data) {
812 // if there's a pending state changed and then the state changes, clear the pending class
813 var circle = d3.select(this);
814 if (data.state === 'ACTIVE' && circle.classed('inactive') ||
815 data.state === 'INACTIVE' && circle.classed('active')) {
816 circle.classed('pending', false);
817 }
818 })
819 .attr('class', function (data) {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700820 if (data.state === 'ACTIVE' && data.controller) {
Paul Greyson347fb742013-03-27 13:40:29 -0700821 return data.className + ' active ' + controllerColorMap[data.controller];
Paul Greysonc17278a2013-03-23 10:17:12 -0700822 } else {
Paul Greyson347fb742013-03-27 13:40:29 -0700823 return data.className + ' inactive ' + 'colorInactive';
Paul Greysonc17278a2013-03-23 10:17:12 -0700824 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700825 });
Paul Greysonc17278a2013-03-23 10:17:12 -0700826 }
827
828 // update switches
829 rings.each(ringUpdate);
830
Paul Greyson968d1b42013-03-23 16:58:41 -0700831
832 // Now setup the labels
833 // This is done separately because SVG draws in node order and we want the labels
834 // always on top
835 var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model));
836
Paul Greyson421bfcd2013-03-27 22:22:09 -0700837 d3.select(document.body).on('mousemove', function () {
838 if (!d3.select('#topology').classed('linking')) {
839 return;
840 }
841 var linkVector = document.getElementById('linkVector');
842 if (!linkVector) {
843 return;
844 }
845 linkVector = d3.select(linkVector);
846
847 var highlighted = svg.selectAll('.highlight')[0];
848 var s1 = null, s2 = null;
849 if (highlighted.length > 1) {
850 var s1 = d3.select(highlighted[0]);
851 var s2 = d3.select(highlighted[1]);
852
853 } else if (highlighted.length > 0) {
854 var s1 = d3.select(highlighted[0]);
855 }
856 var src = s1;
857 if (s2 && !s2.data()[0].target) {
858 src = s2;
859 }
860 if (src) {
861 linkVector.attr('d', function () {
862 var srcPt = document.querySelector('svg').createSVGPoint();
863 srcPt.x = src.attr('x');
864 srcPt.y = src.attr('y');
865 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
866
867 var svg = document.getElementById('topology');
868 var mouse = d3.mouse(viewbox);
869 var dstPt = document.querySelector('svg').createSVGPoint();
870 dstPt.x = mouse[0];
871 dstPt.y = mouse[1];
872 dstPt = dstPt.matrixTransform(viewbox.getCTM());
873
874 return line([srcPt, dstPt]);
875 });
876 }
877 });
878
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700879 d3.select(document.body).on('mouseup', function () {
880 function clearHighlight() {
881 svg.selectAll('circle').each(function (data) {
882 data.mouseDown = false;
Paul Greyson72f18852013-03-27 15:56:11 -0700883 d3.select('#topology').classed('linking', false);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700884 mouseOutSwitch(data);
Paul Greyson421bfcd2013-03-27 22:22:09 -0700885 });
886 d3.select('#linkVector').remove();
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700887 };
888
Paul Greyson72f18852013-03-27 15:56:11 -0700889 d3.selectAll('.nodrop').classed('nodrop', false);
890
Paul Greyson084779b2013-03-27 13:55:49 -0700891 function removeLink(link) {
892 var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']);
893 var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']);
894
895 if (path1) {
896 setPending(d3.select(path1));
897 }
898 if (path2) {
899 setPending(d3.select(path2));
900 }
901
902 linkDown(link);
903 }
904
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700905
Paul Greyson421bfcd2013-03-27 22:22:09 -0700906 var highlighted = svg.selectAll('.highlight')[0];
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700907 if (highlighted.length == 2) {
Paul Greyson421bfcd2013-03-27 22:22:09 -0700908 var s1Data = highlighted[0].__data__;
909 var s2Data = highlighted[1].__data__;
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700910
911 var srcData, dstData;
912 if (s1Data.target) {
913 dstData = s1Data;
914 srcData = s2Data;
915 } else {
916 dstData = s2Data;
917 srcData = s1Data;
918 }
919
920 if (s1Data.className == 'edge' && s2Data.className == 'edge') {
921 var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?';
922 if (confirm(prompt)) {
Paul Greyson13f02b92013-03-28 11:29:35 -0700923 addFlow(srcData, dstData);
924
925 var flow = {
926 dataPath: {
927 srcPort: {
928 dpid: {
929 value: srcData.dpid
930 }
931 },
932 dstPort: {
933 dpid: {
934 value: dstData.dpid
935 }
936 }
937 },
Paul Greysonaa812562013-03-28 12:43:12 -0700938 createPending: true
Paul Greyson13f02b92013-03-28 11:29:35 -0700939 };
940
941 selectFlow(flow);
942
943 setTimeout(function () {
Paul Greysonaa812562013-03-28 12:43:12 -0700944 deselectFlowIfCreatePending(flow);
Paul Greyson6f918402013-03-28 12:18:30 -0700945 }, pendingTimeout);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700946 }
947 } else {
948 var map = linkMap[srcData.dpid];
949 if (map && map[dstData.dpid]) {
950 var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
951 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700952 removeLink(map[dstData.dpid]);
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700953 }
954 } else {
Paul Greyson8d1c6362013-03-27 13:05:24 -0700955 map = linkMap[dstData.dpid];
956 if (map && map[srcData.dpid]) {
957 var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?';
958 if (confirm(prompt)) {
Paul Greyson084779b2013-03-27 13:55:49 -0700959 removeLink(map[srcData.dpid]);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700960 }
961 } else {
962 var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?';
963 if (confirm(prompt)) {
Paul Greyson40c8a592013-03-27 14:10:33 -0700964 var link1 = {
965 'src-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700966 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700967 'dst-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700968 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700969 pending: true
970 };
971 pendingLinks[makeLinkKey(link1)] = link1;
972 var link2 = {
973 'src-switch': dstData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700974 'src-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700975 'dst-switch': srcData.dpid,
Paul Greyson2913af82013-03-27 14:53:17 -0700976 'dst-port': 1,
Paul Greyson40c8a592013-03-27 14:10:33 -0700977 pending: true
978 };
979 pendingLinks[makeLinkKey(link2)] = link2;
Paul Greyson5cc35f02013-03-28 10:07:36 -0700980 updateTopology();
Paul Greyson40c8a592013-03-27 14:10:33 -0700981
Paul Greyson2913af82013-03-27 14:53:17 -0700982 linkUp(link1);
Paul Greyson40c8a592013-03-27 14:10:33 -0700983
Paul Greyson5cc35f02013-03-28 10:07:36 -0700984 // remove the pending links after 10s
Paul Greyson40c8a592013-03-27 14:10:33 -0700985 setTimeout(function () {
986 delete pendingLinks[makeLinkKey(link1)];
987 delete pendingLinks[makeLinkKey(link2)];
988
Paul Greyson5cc35f02013-03-28 10:07:36 -0700989 updateTopology();
Paul Greyson6f918402013-03-28 12:18:30 -0700990 }, pendingTimeout);
Paul Greyson8d1c6362013-03-27 13:05:24 -0700991 }
Paul Greyson4e6dc3a2013-03-27 11:37:14 -0700992 }
993 }
994 }
995
996 clearHighlight();
997 } else {
998 clearHighlight();
999 }
1000
1001 });
1002
Paul Greyson9066ab02013-03-23 18:15:41 -07001003 function labelRingEnter(data) {
Paul Greyson644d92a2013-03-23 18:00:40 -07001004 if (!data.length) {
Paul Greyson968d1b42013-03-23 16:58:41 -07001005 return;
1006 }
1007
1008 // create the nodes
1009 var nodes = d3.select(this).selectAll("g")
Paul Greyson644d92a2013-03-23 18:00:40 -07001010 .data(data, function (data) {
1011 return data.dpid;
1012 })
Paul Greyson968d1b42013-03-23 16:58:41 -07001013 .enter().append("svg:g")
1014 .classed('nolabel', true)
Paul Greyson9066ab02013-03-23 18:15:41 -07001015 .attr("id", function (data) {
Paul Greyson644d92a2013-03-23 18:00:40 -07001016 return data.dpid + '-label';
Paul Greyson968d1b42013-03-23 16:58:41 -07001017 })
Paul Greyson644d92a2013-03-23 18:00:40 -07001018 .attr("transform", function(data, i) {
1019 return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")";
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001020 })
Paul Greyson968d1b42013-03-23 16:58:41 -07001021
1022 // add the text nodes which show on mouse over
1023 nodes.append("svg:text")
Paul Greyson127d7fb2013-03-25 23:39:20 -07001024 .text(function (data) {return data.dpid;})
Paul Greyson9066ab02013-03-23 18:15:41 -07001025 .attr("x", function (data) {
1026 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1027 if (data.className == 'edge') {
Paul Greyson1eb2dd12013-03-23 18:22:00 -07001028 return - data.width*3 - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -07001029 } else {
Paul Greyson1eb2dd12013-03-23 18:22:00 -07001030 return - data.width - 4;
Paul Greyson9066ab02013-03-23 18:15:41 -07001031 }
1032 } else {
1033 if (data.className == 'edge') {
1034 return data.width*3 + 4;
1035 } else {
1036 return data.width + 4;
1037 }
1038 }
1039 })
1040 .attr("y", function (data) {
1041 var y;
1042 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1043 if (data.className == 'edge') {
1044 y = data.width*3/2 + 4;
1045 } else {
1046 y = data.width/2 + 4;
1047 }
1048 } else {
1049 if (data.className == 'edge') {
1050 y = data.width*3/2 + 4;
1051 } else {
1052 y = data.width/2 + 4;
1053 }
1054 }
1055 return y - 6;
1056 })
1057 .attr("text-anchor", function (data) {
1058 if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) {
1059 return "end";
1060 } else {
1061 return "start";
1062 }
1063 })
1064 .attr("transform", function(data) {
Paul Greyson968d1b42013-03-23 16:58:41 -07001065 var m = document.querySelector('#viewbox').getTransformToElement().inverse();
1066 if (data.scale) {
1067 m = m.scale(data.scale);
1068 }
1069 return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )";
1070 })
1071 }
1072
1073 labelRings.enter().append("svg:g")
1074 .attr("class", "textRing")
1075 .each(labelRingEnter);
1076
Paul Greysonc17278a2013-03-23 10:17:12 -07001077 // switches should not change during operation of the ui so no
1078 // rings.exit()
1079
1080
Paul Greysond1a22d92013-03-19 12:15:19 -07001081 // DRAW THE LINKS
Paul Greysond1a22d92013-03-19 12:15:19 -07001082
Paul Greysonc17278a2013-03-23 10:17:12 -07001083 // key on link dpids since these will come/go during demo
Paul Greyson40c8a592013-03-27 14:10:33 -07001084 var links = d3.select('svg').selectAll('.link').data(links, function (d) {
Paul Greysonc17278a2013-03-23 10:17:12 -07001085 return d['src-switch']+'->'+d['dst-switch'];
1086 });
1087
1088 // add new links
Paul Greysonb367de22013-03-23 11:09:11 -07001089 links.enter().append("svg:path")
Paul Greyson56378ed2013-03-26 23:17:36 -07001090 .attr("class", "link");
1091
Paul Greyson084779b2013-03-27 13:55:49 -07001092 links.attr('id', function (d) {
Paul Greyson40c8a592013-03-27 14:10:33 -07001093 return makeLinkKey(d);
Paul Greyson084779b2013-03-27 13:55:49 -07001094 })
Paul Greyson56378ed2013-03-26 23:17:36 -07001095 .attr("d", function (d) {
Paul Greyson084779b2013-03-27 13:55:49 -07001096 var src = d3.select(document.getElementById(d['src-switch']));
1097 var dst = d3.select(document.getElementById(d['dst-switch']));
Paul Greysonc17278a2013-03-23 10:17:12 -07001098
Paul Greyson084779b2013-03-27 13:55:49 -07001099 var srcPt = document.querySelector('svg').createSVGPoint();
1100 srcPt.x = src.attr('x');
1101 srcPt.y = src.attr('y');
1102 srcPt = srcPt.matrixTransform(src[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001103
Paul Greyson084779b2013-03-27 13:55:49 -07001104 var dstPt = document.querySelector('svg').createSVGPoint();
1105 dstPt.x = dst.attr('x');
Paul Greyson421bfcd2013-03-27 22:22:09 -07001106 dstPt.y = dst.attr('y');
Paul Greyson084779b2013-03-27 13:55:49 -07001107 dstPt = dstPt.matrixTransform(dst[0][0].getCTM());
Paul Greysond1a22d92013-03-19 12:15:19 -07001108
Paul Greyson084779b2013-03-27 13:55:49 -07001109 var midPt = document.querySelector('svg').createSVGPoint();
1110 midPt.x = (srcPt.x + dstPt.x)/2;
1111 midPt.y = (srcPt.y + dstPt.y)/2;
Paul Greysond1a22d92013-03-19 12:15:19 -07001112
Paul Greyson084779b2013-03-27 13:55:49 -07001113 return line([srcPt, midPt, dstPt]);
1114 })
Paul Greyson40c8a592013-03-27 14:10:33 -07001115 .attr("marker-mid", function(d) { return "url(#arrow)"; })
1116 .classed('pending', function (d) {
1117 return d.pending;
1118 });
Paul Greysonc17278a2013-03-23 10:17:12 -07001119
Paul Greyson56378ed2013-03-26 23:17:36 -07001120
Paul Greysonc17278a2013-03-23 10:17:12 -07001121 // remove old links
1122 links.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001123}
1124
Paul Greyson5cc35f02013-03-28 10:07:36 -07001125function updateControllers() {
Paul Greysond1a22d92013-03-19 12:15:19 -07001126 var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers);
Paul Greyson3e142162013-03-19 13:56:17 -07001127 controllers.enter().append('div')
Paul Greysone262a292013-03-23 10:35:23 -07001128 .each(function (c) {
1129 controllerColorMap[c] = colors.pop();
1130 d3.select(document.body).classed(controllerColorMap[c] + '-selected', true);
1131 })
1132 .text(function (d) {
1133 return d;
Paul Greyson2913af82013-03-27 14:53:17 -07001134 })
1135 .append('div')
Paul Greyson8247c3f2013-03-28 00:24:02 -07001136 .attr('class', 'black-eye');
Paul Greysonbcd3c772013-03-21 13:16:44 -07001137
Paul Greysone262a292013-03-23 10:35:23 -07001138 controllers.attr('class', function (d) {
Paul Greysoneed36352013-03-23 11:19:11 -07001139 var color = 'colorInactive';
Paul Greysonbcd3c772013-03-21 13:16:44 -07001140 if (model.activeControllers.indexOf(d) != -1) {
1141 color = controllerColorMap[d];
Paul Greysond1a22d92013-03-19 12:15:19 -07001142 }
Paul Greysonbcd3c772013-03-21 13:16:44 -07001143 var className = 'controller ' + color;
1144 return className;
Paul Greysond1a22d92013-03-19 12:15:19 -07001145 });
Paul Greysond1a22d92013-03-19 12:15:19 -07001146
Paul Greysone262a292013-03-23 10:35:23 -07001147 // this should never be needed
1148 // controllers.exit().remove();
Paul Greysond1a22d92013-03-19 12:15:19 -07001149
Paul Greyson2913af82013-03-27 14:53:17 -07001150 controllers.on('dblclick', function (c) {
1151 if (model.activeControllers.indexOf(c) != -1) {
1152 var prompt = 'Dectivate ' + c + '?';
1153 if (confirm(prompt)) {
1154 controllerDown(c);
1155 setPending(d3.select(this));
1156 };
1157 } else {
1158 var prompt = 'Activate ' + c + '?';
1159 if (confirm(prompt)) {
1160 controllerUp(c);
1161 setPending(d3.select(this));
1162 };
1163 }
1164 });
1165
Paul Greyson8247c3f2013-03-28 00:24:02 -07001166 controllers.select('.black-eye').on('click', function (c) {
Paul Greysonc3e21a02013-03-21 13:56:05 -07001167 var allSelected = true;
1168 for (var key in controllerColorMap) {
1169 if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) {
1170 allSelected = false;
1171 break;
1172 }
1173 }
1174 if (allSelected) {
1175 for (var key in controllerColorMap) {
1176 d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c)
1177 }
1178 } else {
1179 for (var key in controllerColorMap) {
1180 d3.select(document.body).classed(controllerColorMap[key] + '-selected', true)
1181 }
1182 }
1183
1184 // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected');
1185 // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected);
Paul Greysond1a22d92013-03-19 12:15:19 -07001186 });
Paul Greyson8d1c6362013-03-27 13:05:24 -07001187
1188
Paul Greyson740bdaf2013-03-18 16:10:48 -07001189}
1190
Paul Greyson2c35f572013-04-04 16:23:48 -07001191var modelString;
Paul Greyson29aa98d2013-03-28 00:09:31 -07001192function sync(svg) {
Paul Greysonbcd3c772013-03-21 13:16:44 -07001193 var d = Date.now();
Paul Greysonb48943b2013-03-19 13:27:57 -07001194 updateModel(function (newModel) {
Paul Greyson4e6dc3a2013-03-27 11:37:14 -07001195// console.log('Update time: ' + (Date.now() - d)/1000 + 's');
Paul Greyson740bdaf2013-03-18 16:10:48 -07001196
Paul Greysone5991b52013-04-04 01:34:04 -07001197 if (newModel) {
1198 var modelChanged = false;
Paul Greyson2c35f572013-04-04 16:23:48 -07001199 var newModelString = JSON.stringify(newModel);
1200 if (!modelString || newModelString != modelString) {
Paul Greysone5991b52013-04-04 01:34:04 -07001201 modelChanged = true;
1202 model = newModel;
Paul Greyson2c35f572013-04-04 16:23:48 -07001203 modelString = newModelString;
Paul Greysone5991b52013-04-04 01:34:04 -07001204 } else {
1205 // console.log('no change');
1206 }
Paul Greysonb48943b2013-03-19 13:27:57 -07001207
Paul Greysone5991b52013-04-04 01:34:04 -07001208 if (modelChanged) {
1209 updateControllers();
1210 updateSelectedFlows();
1211 updateTopology();
1212 }
Paul Greyson5cc35f02013-03-28 10:07:36 -07001213
Paul Greysone5991b52013-04-04 01:34:04 -07001214 updateHeader(newModel);
1215 }
Paul Greyson740bdaf2013-03-18 16:10:48 -07001216
1217 // do it again in 1s
1218 setTimeout(function () {
Paul Greysona36a9232013-03-22 22:41:27 -07001219 sync(svg)
Paul Greysond1a22d92013-03-19 12:15:19 -07001220 }, 1000);
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001221 });
1222}
Paul Greyson740bdaf2013-03-18 16:10:48 -07001223
Paul Greyson38d8bde2013-03-22 22:07:35 -07001224svg = createTopologyView();
Paul Greyson29aa98d2013-03-28 00:09:31 -07001225updateSelectedFlows();
1226
1227d3.select('#showFlowChooser').on('click', function () {
1228 showFlowChooser();
1229});
1230
Paul Greyson72f18852013-03-27 15:56:11 -07001231
Paul Greyson38d8bde2013-03-22 22:07:35 -07001232// workaround for Chrome v25 bug
1233// if executed immediately, the view box transform logic doesn't work properly
1234// fixed in Chrome v27
1235setTimeout(function () {
1236 // workaround for another Chrome v25 bug
1237 // viewbox transform stuff doesn't work in combination with browser zoom
Paul Greysonc17278a2013-03-23 10:17:12 -07001238 // also works in Chrome v27
Paul Greyson38d8bde2013-03-22 22:07:35 -07001239 d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width);
Paul Greyson29aa98d2013-03-28 00:09:31 -07001240 sync(svg);
Paul Greyson38d8bde2013-03-22 22:07:35 -07001241}, 100);