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 | bcd3c77 | 2013-03-21 13:16:44 -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 | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 11 | links: results.links |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 12 | } |
| 13 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 14 | // sort the switches |
| 15 | results.switches.sort(function (a, b) { |
| 16 | var aA = a.dpid.split(':'); |
| 17 | var bB = b.dpid.split(':'); |
| 18 | for (var i=0; i<aA.length; i+=1) { |
| 19 | if (aA[i] != bB[i]) { |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 20 | return parseInt(aA[i], 16) - parseInt(bB[i], 16); |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | return 0; |
| 24 | }); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 25 | |
| 26 | // identify switch types |
| 27 | var coreSwitchDPIDs = {}; |
| 28 | results.configuration.core.forEach(function (dpid) { |
| 29 | coreSwitchDPIDs[dpid] = true; |
| 30 | }); |
| 31 | |
| 32 | var aggregationSwitchDPIDs = {}; |
| 33 | results.configuration.aggregation.forEach(function (dpid) { |
| 34 | aggregationSwitchDPIDs[dpid] = true; |
| 35 | }); |
| 36 | |
| 37 | results.switches.forEach(function (s) { |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 38 | s.controller = results.mapping[s.dpid][0].controllerId; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 39 | |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 40 | if (coreSwitchDPIDs[s.dpid]) { |
| 41 | model.coreSwitches.push(s); |
| 42 | } else if (aggregationSwitchDPIDs[s.dpid]) { |
| 43 | model.aggregationSwitches.push(s); |
| 44 | } else { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 45 | model.edgeSwitches.push(s); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 46 | } |
| 47 | }); |
| 48 | |
| 49 | return model; |
| 50 | } |
| 51 | |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 52 | var urls = { |
| 53 | links: '/wm/core/topology/links/json', |
| 54 | switches: '/wm/core/topology/switches/all/json', |
| 55 | flows: '/wm/flow/getall/json', |
| 56 | activeControllers: '/wm/registry/controllers/json', |
Ubuntu | e23620c | 2013-03-23 01:14:43 +0000 | [diff] [blame] | 57 | controllers: 'data/controllers.json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 58 | mapping: '/wm/registry/switches/json', |
| 59 | configuration: 'data/configuration.json' |
| 60 | } |
| 61 | |
| 62 | var mockURLs = { |
| 63 | links: 'data/wm_core_topology_links_json.json', |
| 64 | switches: 'data/wm_core_topology_switches_all_json.json', |
| 65 | flows: 'data/wm_flow_getall_json.json', |
| 66 | activeControllers: 'data/wm_registry_controllers_json.json', |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 67 | controllers: 'data/controllers.json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 68 | mapping: 'data/wm_registry_switches_json.json', |
| 69 | configuration: 'data/configuration.json' |
| 70 | } |
| 71 | |
| 72 | var proxyURLs = { |
Jonathan Hart | 0767ab7 | 2013-03-24 16:14:29 -0700 | [diff] [blame^] | 73 | links: '/proxy/wm/core/topology/links/json', |
| 74 | switches: '/proxy/wm/core/topology/switches/all/json', |
| 75 | flows: '/proxy/wm/flow/getall/json', |
| 76 | activeControllers: '/proxy/wm/registry/controllers/json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 77 | controllers: 'data/controllers.json', |
Jonathan Hart | 0767ab7 | 2013-03-24 16:14:29 -0700 | [diff] [blame^] | 78 | mapping: '/proxy/wm/registry/switches/json', |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 79 | configuration: 'data/configuration.json' |
| 80 | } |
| 81 | |
| 82 | var params = parseURLParameters(); |
| 83 | if (params.mock) { |
| 84 | urls = mockURLs; |
| 85 | } |
| 86 | if (params.proxy) { |
| 87 | urls = proxyURLs; |
| 88 | } |
| 89 | |
| 90 | function makeRequest(url) { |
| 91 | return function (cb) { |
| 92 | d3.json(url, function (error, result) { |
| 93 | if (error) { |
| 94 | error = url + ' : ' + error.status; |
| 95 | } |
| 96 | |
| 97 | cb(error, result); |
| 98 | }); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 103 | function updateModel(cb) { |
| 104 | async.parallel({ |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 105 | links: makeRequest(urls.links), |
| 106 | switches: makeRequest(urls.switches), |
| 107 | controllers: makeRequest(urls.controllers), |
| 108 | activeControllers: makeRequest(urls.activeControllers), |
| 109 | mapping: makeRequest(urls.mapping), |
| 110 | configuration: makeRequest(urls.configuration) |
| 111 | // flows: makeRequest(urls.flows), |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 112 | }, |
| 113 | function(err, results) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 114 | if (!err) { |
| 115 | var model = toD3(results); |
| 116 | cb(model); |
| 117 | } else { |
| 118 | alert(JSON.stringify(err)); |
| 119 | } |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 120 | }); |
Ubuntu | e23620c | 2013-03-23 01:14:43 +0000 | [diff] [blame] | 121 | } |