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 | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 94 | function makeRequest(key) { |
| 95 | var url = urls[key]; |
| 96 | if (url) { |
| 97 | return function (cb) { |
| 98 | d3.json(url, function (error, result) { |
| 99 | if (error) { |
| 100 | error = url + ' : ' + error.status; |
| 101 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 102 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 103 | cb(error, result); |
| 104 | }); |
| 105 | } |
| 106 | } else { |
| 107 | return function (cb) { |
| 108 | cb(null, []); |
| 109 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 114 | function updateModel(cb) { |
| 115 | async.parallel({ |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 116 | links: makeRequest('links'), |
| 117 | switches: makeRequest('switches'), |
| 118 | controllers: makeRequest('controllers'), |
| 119 | activeControllers: makeRequest('activeControllers'), |
| 120 | mapping: makeRequest('mapping'), |
| 121 | configuration: makeRequest('configuration'), |
| 122 | flows: makeRequest('flows') |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 123 | }, |
| 124 | function(err, results) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 125 | if (!err) { |
| 126 | var model = toD3(results); |
| 127 | cb(model); |
| 128 | } else { |
| 129 | alert(JSON.stringify(err)); |
| 130 | } |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 131 | }); |
Ubuntu | e23620c | 2013-03-23 01:14:43 +0000 | [diff] [blame] | 132 | } |