blob: df4a751def0c3589050bcb82bfaeab61990d8151 [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 Greyson4a73a8b2013-04-10 11:47:25 -07008 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 Greyson4a73a8b2013-04-10 11:47:25 -070015 // 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 Greysonde7fad52013-03-19 12:47:32 -070022 // 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 Greyson832d2202013-03-21 13:27:56 -070028 return parseInt(aA[i], 16) - parseInt(bB[i], 16);
Paul Greysonde7fad52013-03-19 12:47:32 -070029 }
30 }
31 return 0;
32 });
Paul Greyson6f86d1e2013-03-18 14:40:39 -070033
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 Greysonc17278a2013-03-23 10:17:12 -070046 var mapping = results.mapping[s.dpid]
47 if (mapping) {
48 s.controller = mapping[0].controllerId;
49 }
Paul Greysond1a22d92013-03-19 12:15:19 -070050
Paul Greyson6f86d1e2013-03-18 14:40:39 -070051 if (coreSwitchDPIDs[s.dpid]) {
52 model.coreSwitches.push(s);
53 } else if (aggregationSwitchDPIDs[s.dpid]) {
54 model.aggregationSwitches.push(s);
55 } else {
Paul Greyson740bdaf2013-03-18 16:10:48 -070056 model.edgeSwitches.push(s);
Paul Greyson6f86d1e2013-03-18 14:40:39 -070057 }
58 });
59
60 return model;
61}
62
Paul Greysonbcd3c772013-03-21 13:16:44 -070063var urls = {
64 links: '/wm/core/topology/links/json',
65 switches: '/wm/core/topology/switches/all/json',
Paul Greyson56378ed2013-03-26 23:17:36 -070066 flows: '/wm/flow/getsummary/0/0/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070067 activeControllers: '/wm/registry/controllers/json',
Ubuntue23620c2013-03-23 01:14:43 +000068 controllers: 'data/controllers.json',
Paul Greysonbcd3c772013-03-21 13:16:44 -070069 mapping: '/wm/registry/switches/json',
70 configuration: 'data/configuration.json'
71}
72
73var 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 Greysonc3e21a02013-03-21 13:56:05 -070078 controllers: 'data/controllers.json',
Paul Greysonbcd3c772013-03-21 13:16:44 -070079 mapping: 'data/wm_registry_switches_json.json',
80 configuration: 'data/configuration.json'
81}
82
83var proxyURLs = {
Paul Greyson985bb652013-03-22 21:39:04 -070084 links: '/wm/core/topology/links/json?proxy',
85 switches: '/wm/core/topology/switches/all/json?proxy',
Paul Greyson56378ed2013-03-26 23:17:36 -070086 flows: '/wm/flow/getsummary/0/0/json?proxy',
Paul Greyson985bb652013-03-22 21:39:04 -070087 activeControllers: '/wm/registry/controllers/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070088 controllers: 'data/controllers.json',
Paul Greyson985bb652013-03-22 21:39:04 -070089 mapping: '/wm/registry/switches/json?proxy',
Paul Greysonbcd3c772013-03-21 13:16:44 -070090 configuration: 'data/configuration.json'
91}
92
93var params = parseURLParameters();
94if (params.mock) {
95 urls = mockURLs;
96}
97if (params.proxy) {
98 urls = proxyURLs;
99}
100
Paul Greyson5ea5da52013-04-08 21:00:55 -0700101var timeoutMS = 20000;
102
Paul Greyson127d7fb2013-03-25 23:39:20 -0700103function makeRequest(key) {
104 var url = urls[key];
105 if (url) {
106 return function (cb) {
Paul Greyson5ea5da52013-04-08 21:00:55 -0700107 var timeout;
108 var xhr = d3.json(url, function (error, result) {
109 clearTimeout(timeout);
110
Paul Greyson127d7fb2013-03-25 23:39:20 -0700111 if (error) {
112 error = url + ' : ' + error.status;
113 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700114
Paul Greyson5ea5da52013-04-08 21:00:55 -0700115 if (cb) {
116 cb(error, result);
117 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700118 });
Paul Greyson5ea5da52013-04-08 21:00:55 -0700119 timeout = setTimeout(function () {
120 xhr.abort();
121 cb(url + ' timed out after ' + timeoutMS + ' ms');
122 cb = null;
123 }, timeoutMS);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700124 }
125 } else {
126 return function (cb) {
127 cb(null, []);
128 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700129 }
130}
131
132
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700133function updateModel(cb) {
134 async.parallel({
Paul Greyson127d7fb2013-03-25 23:39:20 -0700135 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 Greyson6f86d1e2013-03-18 14:40:39 -0700142 },
143 function(err, results) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700144 if (!err) {
145 var model = toD3(results);
146 cb(model);
147 } else {
Paul Greysone5991b52013-04-04 01:34:04 -0700148 console.log(JSON.stringify(err));
149 cb(null);
Paul Greysonbcd3c772013-03-21 13:16:44 -0700150 }
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700151 });
Ubuntue23620c2013-03-23 01:14:43 +0000152}