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