-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathofcupid.js
More file actions
313 lines (285 loc) · 9.18 KB
/
ofcupid.js
File metadata and controls
313 lines (285 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Copyright (c) 2016 Richard Sanger
*
* Licensed under MIT
*/
var app = angular.module('patch_manager', []);
/** Angular filter to convert kbit/s to a human readable form */
app.filter('kbits', function() {
return function(kbits) {
if (kbits == 0) return "Down";
var units = ['kb/s', 'Mb/s', 'Gb/s', 'Tb/s', 'Pb/s'];
var magnitude = Math.floor(Math.log(kbits) / Math.log(1000));
return (kbits / Math.pow(1000, Math.floor(magnitude))) + ' ' + units[magnitude];
}
});
/** Angular filter to print a port in human readable form */
app.filter('print_port', function() {
return function(port) { return port.port + " - " + port.port_name;
}
});
/** Global storage for logging errors so we can access from
* our error controller and the logging provider */
errors = [];
/** Logging provider, warn and error messages are displayed to the
* user */
app.config(["$provide", function($provide) {
$provide.decorator("$log", function($delegate, logIntercept) {
return logIntercept($delegate);
});
}]);
app.factory("logIntercept", function() {
return function($delegate) {
return {
log:function(){
$delegate.log.apply(null, arguments);
},
info:function(){
$delegate.info.apply(null, arguments);
},
error:function(){
$delegate.error.apply(null, arguments);
errors.push({'summary': arguments[0], 'msg': arguments[1],
'type': 'error'});
},
warn:function(){
$delegate.warn.apply(null, arguments);
errors.push({'summary': arguments[0], 'msg': arguments[1],
'type': 'warn'})
},
debug:function(){
$delegate.debug.apply(null, arguments);
}
};
};
});
/** Angular controller to pick the switch, the selected
* switch is then used by all other controllers */
var pickSwitch = function ($scope, $http, $rootScope, $log) {
$scope.select_switch = function() {
$rootScope.dpid = $scope.userChoice;
$rootScope.$emit('refresh');
};
/** Handler for refresh button on nav menu - updates entire interface */
$rootScope.$on('full_refresh', function(event, args) {
$scope.refresh();
});
$scope.refresh = function() {
$http.get('api/switches', {}).then(
function(response) {
$scope.switches = response.data;
// This is buggy so we just zero it to match the UI
$scope.userChoice = "";
$scope.select_switch();
},
function(response) {
$log.warn("Failed to load switches, are you sure the ryu server is running?", response.data);
});
};
$rootScope.dpid = "";
$scope.userChoice = "";
$scope.refresh();
};
/** Angular controller for the ports tab. Fills the table and handles
* logic of the buttons. */
var portsController = function ($scope, $http, $rootScope, $log) {
$scope.refresh = function() {
$http.post('api/ports', {'dpid': $rootScope.dpid}).then(
function(response) {
$scope.ports = response.data;
$scope.sel = [];
},
function(response) {
$log.warn("Failed to load ports, is the server running?", response.data);
}
);
$http.post("api/configs", {}).then(
function(response) {
$scope.configs = response.data;
$scope.userChoice = "";
},
function(response) {
$log.warn("Failed to load configurations, is the server running?", response.data);
});
};
$scope.saveAs = '';
$scope.merge = 'replace_all';
$scope.refresh();
$scope.install_link = function() {
$http.put("api/link",
{'dpid': $scope.sel[0].dpid,
'portA': $scope.sel[0].port,
'portB': $scope.sel[1].port}).then(
function(response) {
$rootScope.$emit('refresh');
},
function(response) {
$log.warn("Failed to link ports", response.data);
});
};
$scope.isConnected = function (port) {
return $scope.sel.length && $scope.sel[0].links.indexOf(port.port) !== -1;
};
$scope.isDisabled = function (port) {
if ($scope.sel.indexOf(port) === -1) {
if ($scope.sel.length >= 2)
return true;
else
return $scope.sel.length && $scope.sel[0].dpid !== port.dpid;
} else {
return false;
}
};
$scope.setSelected = function(port, $event) {
if ($event.ctrlKey || $event.shiftKey) {
if ($scope.sel.indexOf(port) === -1)
$scope.sel = [port];
else
$scope.sel = [];
return;
}
if ($scope.isDisabled(port) || $scope.isConnected(port))
return;
if ($scope.sel.indexOf(port) === -1) {
if ($scope.sel.length < 2) {
$scope.sel.push(port);
}
} else {
$scope.sel.splice($scope.sel.indexOf(port), 1);
}
};
$scope.unlink_port = function() {
if ($scope.sel[0]) {
$http.put('api/unlink_port', {'dpid': $scope.sel[0].dpid,
'port': $scope.sel[0].port}).then(
function(response) {
$rootScope.$emit('refresh');
},
function(response) {
$log.warn("Failed to unlink port", response.data);
});
}
};
$rootScope.$on('refresh', function(event, args) { $scope.refresh(); });
$scope.select_conf = function() {
if ($scope.userChoice == false) {
for (var i in $scope.ports) {
$scope.ports[i].new_links = [];
}
return;
}
$http.put("api/load_conf",
{'dpid': $rootScope.dpid,
'name': $scope.userChoice,
'simulate': true,
'merge': $scope.merge}).then (
function(response) {
$scope.new_configs = response.data;
for (var i in $scope.ports) {
// Check to so if order is consistent otherwise do a full search
if ($scope.ports[i].port === response.data[i].port &&
$scope.ports[i].dpid === response.data[i].dpid) {
$scope.ports[i].new_links = response.data[i].links;
} else {
for (var k in response.data) {
if ($scope.ports[i].port === response.data[k].port &&
$scope.ports[i].dpid === response.data[k].dpid) {
$scope.ports[i].new_links = response.data[k].links;
}
}
}
}
},
function(response) {
$log.warn("Failed to simulate configuration: " + $scope.userChoice, response.data);
$scope.userChoice = "";
for (var i in $scope.ports) {
$scope.ports[i].new_links = [];
}
});
};
$scope.load_conf = function() {
$http.put("api/load_conf",
{'dpid': $rootScope.dpid,
'name': $scope.userChoice,
'simulate': false,
'merge': $scope.merge}).then(
function(response) {
$scope.new_ports = response.data;
$rootScope.$emit('refresh');
},
function(response) {
$log.warn("Failed to load configuration: " + $scope.userChoice, response.data);
});
};
$scope.save_conf = function() {
alert("Saving as '" + $scope.saveAs + "'");
$http.put("api/save_conf", {'name': $scope.saveAs}).then(
function(response) {
$scope.refresh();
},
function(response) {
$log.warn("Failed to save configuration: " + $scope.saveAs, response.data);
});
};
};
/** Angular controller for tab selection. Hides elements related to a different tab */
var tabsController = function ($scope, $http, $rootScope) {
$scope.tab = 1;
$scope.errors = errors;
// Refresh button in nav
$scope.refresh = function() {
$rootScope.$emit("full_refresh");
};
}
/** Angular controller for links tab, maintains table of links and provides button logic. */
var linksController = function ($scope, $http, $rootScope, $log) {
$scope.refresh = function() {
$http.post("api/current_mappings", {'dpid': $rootScope.dpid}).then(
function(response) {
$scope.links = response.data;
},
function(response) {
$log.warn("Failed to link mappings, is the server running?", response.data);
});
};
$scope.refresh();
$scope.compare_items = function(a,b) {
if ((a.src.port === b.src.port && a.dst.port === b.dst.port) ||
(a.dst.port === b.src.port && a.src.port === b.dst.port)) {
return true;
}
return false;
};
$rootScope.$on('refresh', function(event, args) { $scope.refresh(); });
$scope.click = function (link) {
var sel = !link.selected;
for (i in $scope.links) {
var item = $scope.links[i];
if ($scope.compare_items(item, link)) {
item.selected = sel;
}
}
};
$scope.f_reverse = function(value, index, array) {
return value.src.port < value.dst.port || $scope.show_rev;
};
$scope.unlink = function() {
for (i in $scope.links) {
var item = $scope.links[i];
if (item.selected) {
$http.put('api/unlink',
{"dpid": item.src.dpid,
"portA": item.src.port,
"portB": item.dst.port}).then(
(function(item) { return function(response) {
//$scope.links.splice($scope.links.indexOf(item), 1);
$rootScope.$emit('refresh');
}})(item),
function(response) {
$log.warn("Failed to unlink port", response.data);
});
}
}
}
};