blob: 5026bd4d8299a44a82258faa38f1f240b2494a32 [file] [log] [blame]
Paul Greyson6f86d1e2013-03-18 14:40:39 -07001/*global async, d3*/
2
3function toD3(results) {
4 var model = {
Paul Greyson740bdaf2013-03-18 16:10:48 -07005 edgeSwitches: [],
Paul Greyson6f86d1e2013-03-18 14:40:39 -07006 aggregationSwitches: [],
Paul Greyson952ccb62013-03-18 22:22:08 -07007 coreSwitches: [],
Paul Greyson127d7fb2013-03-25 23:39:20 -07008 flows: results.flows,
Paul Greysond1a22d92013-03-19 12:15:19 -07009 controllers: results.controllers,
Paul Greysonbcd3c772013-03-21 13:16:44 -070010 activeControllers: results.activeControllers,
Paul Greysonc17278a2013-03-23 10:17:12 -070011 links: results.links,
12 configuration: results.configuration
Paul Greyson127d7fb2013-03-25 23:39:20 -070013 };
Paul Greyson6f86d1e2013-03-18 14:40:39 -070014
Paul Greysonde7fad52013-03-19 12:47:32 -070015 // 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 Greyson832d2202013-03-21 13:27:56 -070021 return parseInt(aA[i], 16) - parseInt(bB[i], 16);
Paul Greysonde7fad52013-03-19 12:47:32 -070022 }
23 }
24 return 0;
25 });
Paul Greyson6f86d1e2013-03-18 14:40:39 -070026
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 Greysonc17278a2013-03-23 10:17:12 -070039 var mapping = results.mapping[s.dpid]
40 if (mapping) {
41 s.controller = mapping[0].controllerId;
42 }
Paul Greysond1a22d92013-03-19 12:15:19 -070043
Paul Greyson6f86d1e2013-03-18 14:40:39 -070044 if (coreSwitchDPIDs[s.dpid]) {
45 model.coreSwitches.push(s);
46 } else if (aggregationSwitchDPIDs[s.dpid]) {
47 model.aggregationSwitches.push(s);
48 } else {
Paul Greyson740bdaf2013-03-18 16:10:48 -070049 model.edgeSwitches.push(s);
Paul Greyson6f86d1e2013-03-18 14:40:39 -070050 }
51 });
52
53 return model;
54}
55
Paul Greysonbcd3c772013-03-21 13:16:44 -070056var urls = {
57 links: '/wm/core/topology/links/json',
58 switches: '/wm/core/topology/switches/all/json',
Paul Greyson56378ed2013-03-26 23:17:36 -070059 flows: '/wm/flow/getsummary/0/0/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070060 activeControllers: '/wm/registry/controllers/json',
Ubuntue23620c2013-03-23 01:14:43 +000061 controllers: 'data/controllers.json',
Paul Greysonbcd3c772013-03-21 13:16:44 -070062 mapping: '/wm/registry/switches/json',
63 configuration: 'data/configuration.json'
64}
65
66var 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 Greysonc3e21a02013-03-21 13:56:05 -070071 controllers: 'data/controllers.json',
Paul Greysonbcd3c772013-03-21 13:16:44 -070072 mapping: 'data/wm_registry_switches_json.json',
73 configuration: 'data/configuration.json'
74}
75
76var proxyURLs = {
Paul Greyson985bb652013-03-22 21:39:04 -070077 links: '/wm/core/topology/links/json?proxy',
78 switches: '/wm/core/topology/switches/all/json?proxy',
Paul Greyson56378ed2013-03-26 23:17:36 -070079 flows: '/wm/flow/getsummary/0/0/json?proxy',
Paul Greyson985bb652013-03-22 21:39:04 -070080 activeControllers: '/wm/registry/controllers/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070081 controllers: 'data/controllers.json',
Paul Greyson985bb652013-03-22 21:39:04 -070082 mapping: '/wm/registry/switches/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070083 configuration: 'data/configuration.json'
84}
85
86var params = parseURLParameters();
87if (params.mock) {
88 urls = mockURLs;
89}
90if (params.proxy) {
91 urls = proxyURLs;
92}
93
Paul Greyson127d7fb2013-03-25 23:39:20 -070094function 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 Greysonbcd3c772013-03-21 13:16:44 -0700102
Paul Greyson127d7fb2013-03-25 23:39:20 -0700103 cb(error, result);
104 });
105 }
106 } else {
107 return function (cb) {
108 cb(null, []);
109 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700110 }
111}
112
113
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700114function updateModel(cb) {
115 async.parallel({
Paul Greyson127d7fb2013-03-25 23:39:20 -0700116 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 Greyson6f86d1e2013-03-18 14:40:39 -0700123 },
124 function(err, results) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700125 if (!err) {
126 var model = toD3(results);
127 cb(model);
128 } else {
Paul Greysone5991b52013-04-04 01:34:04 -0700129 console.log(JSON.stringify(err));
130 cb(null);
Paul Greysonbcd3c772013-03-21 13:16:44 -0700131 }
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700132 });
Ubuntue23620c2013-03-23 01:14:43 +0000133}