blob: f251c87f87a4b2ef4ab3dceea425e407b2d78fcb [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 Greyson5ea5da52013-04-08 21:00:55 -070094var timeoutMS = 20000;
95
Paul Greyson127d7fb2013-03-25 23:39:20 -070096function makeRequest(key) {
97 var url = urls[key];
98 if (url) {
99 return function (cb) {
Paul Greyson5ea5da52013-04-08 21:00:55 -0700100 var timeout;
101 var xhr = d3.json(url, function (error, result) {
102 clearTimeout(timeout);
103
Paul Greyson127d7fb2013-03-25 23:39:20 -0700104 if (error) {
105 error = url + ' : ' + error.status;
106 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700107
Paul Greyson5ea5da52013-04-08 21:00:55 -0700108 if (cb) {
109 cb(error, result);
110 }
Paul Greyson127d7fb2013-03-25 23:39:20 -0700111 });
Paul Greyson5ea5da52013-04-08 21:00:55 -0700112 timeout = setTimeout(function () {
113 xhr.abort();
114 cb(url + ' timed out after ' + timeoutMS + ' ms');
115 cb = null;
116 }, timeoutMS);
Paul Greyson127d7fb2013-03-25 23:39:20 -0700117 }
118 } else {
119 return function (cb) {
120 cb(null, []);
121 }
Paul Greysonbcd3c772013-03-21 13:16:44 -0700122 }
123}
124
125
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700126function updateModel(cb) {
127 async.parallel({
Paul Greyson127d7fb2013-03-25 23:39:20 -0700128 links: makeRequest('links'),
129 switches: makeRequest('switches'),
130 controllers: makeRequest('controllers'),
131 activeControllers: makeRequest('activeControllers'),
132 mapping: makeRequest('mapping'),
133 configuration: makeRequest('configuration'),
134 flows: makeRequest('flows')
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700135 },
136 function(err, results) {
Paul Greysonbcd3c772013-03-21 13:16:44 -0700137 if (!err) {
138 var model = toD3(results);
139 cb(model);
140 } else {
Paul Greysone5991b52013-04-04 01:34:04 -0700141 console.log(JSON.stringify(err));
142 cb(null);
Paul Greysonbcd3c772013-03-21 13:16:44 -0700143 }
Paul Greyson6f86d1e2013-03-18 14:40:39 -0700144 });
Ubuntue23620c2013-03-23 01:14:43 +0000145}