Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 1 | /*global async, d3*/ |
| 2 | |
| 3 | function toD3(results) { |
| 4 | var model = { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 5 | edgeSwitches: [], |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 6 | aggregationSwitches: [], |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 7 | coreSwitches: [], |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 8 | flows: results.flows, |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 9 | controllers: results.controllers, |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 10 | activeControllers: results.activeControllers, |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 11 | links: results.links, |
| 12 | configuration: results.configuration |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 13 | }; |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 14 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 15 | // sort the switches |
| 16 | results.switches.sort(function (a, b) { |
| 17 | var aA = a.dpid.split(':'); |
| 18 | var bB = b.dpid.split(':'); |
| 19 | for (var i=0; i<aA.length; i+=1) { |
| 20 | if (aA[i] != bB[i]) { |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 21 | return parseInt(aA[i], 16) - parseInt(bB[i], 16); |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | return 0; |
| 25 | }); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 26 | |
| 27 | // identify switch types |
| 28 | var coreSwitchDPIDs = {}; |
| 29 | results.configuration.core.forEach(function (dpid) { |
| 30 | coreSwitchDPIDs[dpid] = true; |
| 31 | }); |
| 32 | |
| 33 | var aggregationSwitchDPIDs = {}; |
| 34 | results.configuration.aggregation.forEach(function (dpid) { |
| 35 | aggregationSwitchDPIDs[dpid] = true; |
| 36 | }); |
| 37 | |
| 38 | results.switches.forEach(function (s) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 39 | var mapping = results.mapping[s.dpid] |
| 40 | if (mapping) { |
| 41 | s.controller = mapping[0].controllerId; |
| 42 | } |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 43 | |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 44 | if (coreSwitchDPIDs[s.dpid]) { |
| 45 | model.coreSwitches.push(s); |
| 46 | } else if (aggregationSwitchDPIDs[s.dpid]) { |
| 47 | model.aggregationSwitches.push(s); |
| 48 | } else { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 49 | model.edgeSwitches.push(s); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 50 | } |
| 51 | }); |
| 52 | |
| 53 | return model; |
| 54 | } |
| 55 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 56 | var urls = { |
| 57 | links: '/wm/core/topology/links/json', |
| 58 | switches: '/wm/core/topology/switches/all/json', |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 59 | flows: '/wm/flow/getsummary/0/0/json?proxy', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 60 | activeControllers: '/wm/registry/controllers/json', |
Ubuntu | e23620c | 2013-03-23 01:14:43 +0000 | [diff] [blame] | 61 | controllers: 'data/controllers.json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 62 | mapping: '/wm/registry/switches/json', |
| 63 | configuration: 'data/configuration.json' |
| 64 | } |
| 65 | |
| 66 | var mockURLs = { |
| 67 | links: 'data/wm_core_topology_links_json.json', |
| 68 | switches: 'data/wm_core_topology_switches_all_json.json', |
| 69 | flows: 'data/wm_flow_getall_json.json', |
| 70 | activeControllers: 'data/wm_registry_controllers_json.json', |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 71 | controllers: 'data/controllers.json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 72 | mapping: 'data/wm_registry_switches_json.json', |
| 73 | configuration: 'data/configuration.json' |
| 74 | } |
| 75 | |
| 76 | var proxyURLs = { |
Paul Greyson | 985bb65 | 2013-03-22 21:39:04 -0700 | [diff] [blame] | 77 | links: '/wm/core/topology/links/json?proxy', |
| 78 | switches: '/wm/core/topology/switches/all/json?proxy', |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 79 | flows: '/wm/flow/getsummary/0/0/json?proxy', |
Paul Greyson | 985bb65 | 2013-03-22 21:39:04 -0700 | [diff] [blame] | 80 | activeControllers: '/wm/registry/controllers/json?proxy', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 81 | controllers: 'data/controllers.json', |
Paul Greyson | 985bb65 | 2013-03-22 21:39:04 -0700 | [diff] [blame] | 82 | mapping: '/wm/registry/switches/json?proxy', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 83 | configuration: 'data/configuration.json' |
| 84 | } |
| 85 | |
| 86 | var params = parseURLParameters(); |
| 87 | if (params.mock) { |
| 88 | urls = mockURLs; |
| 89 | } |
| 90 | if (params.proxy) { |
| 91 | urls = proxyURLs; |
| 92 | } |
| 93 | |
Paul Greyson | 5ea5da5 | 2013-04-08 21:00:55 -0700 | [diff] [blame] | 94 | var timeoutMS = 20000; |
| 95 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 96 | function makeRequest(key) { |
| 97 | var url = urls[key]; |
| 98 | if (url) { |
| 99 | return function (cb) { |
Paul Greyson | 5ea5da5 | 2013-04-08 21:00:55 -0700 | [diff] [blame] | 100 | var timeout; |
| 101 | var xhr = d3.json(url, function (error, result) { |
| 102 | clearTimeout(timeout); |
| 103 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 104 | if (error) { |
| 105 | error = url + ' : ' + error.status; |
| 106 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 107 | |
Paul Greyson | 5ea5da5 | 2013-04-08 21:00:55 -0700 | [diff] [blame] | 108 | if (cb) { |
| 109 | cb(error, result); |
| 110 | } |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 111 | }); |
Paul Greyson | 5ea5da5 | 2013-04-08 21:00:55 -0700 | [diff] [blame] | 112 | timeout = setTimeout(function () { |
| 113 | xhr.abort(); |
| 114 | cb(url + ' timed out after ' + timeoutMS + ' ms'); |
| 115 | cb = null; |
| 116 | }, timeoutMS); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 117 | } |
| 118 | } else { |
| 119 | return function (cb) { |
| 120 | cb(null, []); |
| 121 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 126 | function updateModel(cb) { |
| 127 | async.parallel({ |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 128 | links: makeRequest('links'), |
| 129 | switches: makeRequest('switches'), |
| 130 | controllers: makeRequest('controllers'), |
| 131 | activeControllers: makeRequest('activeControllers'), |
| 132 | mapping: makeRequest('mapping'), |
| 133 | configuration: makeRequest('configuration'), |
| 134 | flows: makeRequest('flows') |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 135 | }, |
| 136 | function(err, results) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 137 | if (!err) { |
| 138 | var model = toD3(results); |
| 139 | cb(model); |
| 140 | } else { |
Paul Greyson | e5991b5 | 2013-04-04 01:34:04 -0700 | [diff] [blame] | 141 | console.log(JSON.stringify(err)); |
| 142 | cb(null); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 143 | } |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 144 | }); |
Ubuntu | e23620c | 2013-03-23 01:14:43 +0000 | [diff] [blame] | 145 | } |