/
Handover.java
641 lines (580 loc) · 23.1 KB
/
Handover.java
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package net.floodlightcontroller.handover;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.openflow.protocol.OFFlowMod;
import org.openflow.protocol.OFMatch;
import org.openflow.protocol.OFPort;
import org.openflow.protocol.action.OFAction;
import org.openflow.protocol.action.OFActionDataLayerDestination;
import org.openflow.protocol.action.OFActionDataLayerSource;
import org.openflow.protocol.action.OFActionOutput;
import org.openflow.protocol.action.OFActionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.packet.Ethernet;
import net.floodlightcontroller.packet.IPv4;
import net.floodlightcontroller.packet.UDP;
import net.floodlightcontroller.staticflowentry.IStaticFlowEntryPusherService;
import de.taimos.gpsd4java.*;
import de.taimos.gpsd4java.api.IObjectListener;
import de.taimos.gpsd4java.api.ObjectListener;
import de.taimos.gpsd4java.backend.AbstractResultParser;
import de.taimos.gpsd4java.backend.GPSdEndpoint;
import de.taimos.gpsd4java.backend.ResultParser;
import de.taimos.gpsd4java.types.ATTObject;
import de.taimos.gpsd4java.types.DeviceObject;
import de.taimos.gpsd4java.types.DevicesObject;
import de.taimos.gpsd4java.types.ParseException;
import de.taimos.gpsd4java.types.SKYObject;
import de.taimos.gpsd4java.types.TPVObject;
import de.taimos.gpsd4java.types.subframes.SUBFRAMEObject;
public class Handover implements IFloodlightModule {
protected static Logger log;
protected IFloodlightProviderService floodlightProvider;
protected IStaticFlowEntryPusherService sfp;
private static ScheduledThreadPoolExecutor grcProbeMgr;
private static Runnable grcProbe;
private static GPSdEndpoint GPSD_CONN = null;
private static ResultParser GPSD_RESULT_PARSER = null;
private static int GPSD_TCP_PORT = 0; // default in GPSD of 2947
private static double GPSD_LATITUDE = 0;
private static double GPSD_LONGITUDE = 0;
private static double GPSD_ALTITUDE = 0;
private static long GRC_PROBE_INTERVAL_SECONDS;
private static String GRC_URL = null;
private static String OVS_TAP_DPID;
private static String OVS_WIFI0_DPID;
private static String OVS_WIFI1_DPID;
private static String OVS_WIMAX0_DPID;
private static String OVS_WIMAX1_DPID;
private static String OVS_ETHERNET_DPID;
private static byte[] TAP_MAC;
private static byte[] WIFI0_MAC;
private static byte[] WIFI1_MAC;
private static byte[] WIMAX0_MAC;
private static byte[] WIMAX1_MAC;
private static byte[] ETHERNET_MAC;
private static String TAP_IFACE_NAME;
private static String WIFI0_IFACE_NAME;
private static String WIFI1_IFACE_NAME;
private static String WIMAX0_IFACE_NAME;
private static String WIMAX1_IFACE_NAME;
private static String ETHERNET_IFACE_NAME;
private static short OVS_TAP_TO_WIFI0_PATCH;
private static short OVS_TAP_TO_WIFI1_PATCH;
private static short OVS_TAP_TO_WIMAX0_PATCH;
private static short OVS_TAP_TO_WIMAX1_PATCH;
private static short OVS_TAP_TO_ETHERNET_PATCH;
private static short OVS_WIFI0_TO_TAP_PATCH;
private static short OVS_WIFI1_TO_TAP_PATCH;
private static short OVS_WIMAX0_TO_TAP_PATCH;
private static short OVS_WIMAX1_TO_TAP_PATCH;
private static short OVS_ETHERNET_TO_TAP_PATCH;
private static short OVS_TAP_LOCAL_PORT;
private static short OVS_WIFI0_IFACE_PORT;
private static short OVS_WIFI1_IFACE_PORT;
private static short OVS_WIMAX0_IFACE_PORT;
private static short OVS_WIMAX1_IFACE_PORT;
private static short OVS_ETHERNET_IFACE_PORT;
private static ArrayList<String> ACTIVE_FLOWS;
private static final String OFFLINE = "offline";
private static String ACTIVE_NETWORK_TYPE = OFFLINE;
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
return null;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
return null;
}
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
return null;
}
@Override
public void init(FloodlightModuleContext context) throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
sfp = context.getServiceImpl(IStaticFlowEntryPusherService.class);
log = LoggerFactory.getLogger(Handover.class);
return;
}
@Override
public void startUp(FloodlightModuleContext context) {
Map<String, String> configOptions = context.getConfigParams(this);
try {
GRC_PROBE_INTERVAL_SECONDS = Long.parseLong(configOptions.get("grc-probe-interval-seconds"));
GPSD_TCP_PORT = Integer.parseInt(configOptions.get("gpsd-tcp-port"));
GRC_URL = configOptions.get("grc-url");
TAP_IFACE_NAME = configOptions.get("tap-iface-name");
WIFI0_IFACE_NAME = configOptions.get("wifi0-iface-name");
WIFI1_IFACE_NAME = configOptions.get("wifi1-iface-name");
WIMAX0_IFACE_NAME = configOptions.get("wimax0-iface-name");
WIMAX1_IFACE_NAME = configOptions.get("wimax1-iface-name");
ETHERNET_IFACE_NAME = configOptions.get("ethernet-iface-name");
TAP_MAC = Ethernet.toMACAddress(configOptions.get("tap-mac"));
WIFI0_MAC = Ethernet.toMACAddress(configOptions.get("wifi0-mac"));
WIFI1_MAC = Ethernet.toMACAddress(configOptions.get("wifi1-mac"));
WIMAX0_MAC = Ethernet.toMACAddress(configOptions.get("wimax0-mac"));
WIMAX1_MAC = Ethernet.toMACAddress(configOptions.get("wimax1-mac"));
ETHERNET_MAC = Ethernet.toMACAddress(configOptions.get("ethernet-mac"));
OVS_TAP_DPID = configOptions.get("ovs-tap-dpid");
OVS_WIFI0_DPID = configOptions.get("ovs-wifi0-dpid");
OVS_WIFI1_DPID = configOptions.get("ovs-wifi1-dpid");
OVS_WIMAX0_DPID = configOptions.get("ovs-wimax0-dpid");
OVS_WIMAX1_DPID = configOptions.get("ovs-wimax1-dpid");
OVS_ETHERNET_DPID = configOptions.get("ovs-ethernet-dpid");
OVS_TAP_TO_WIFI0_PATCH = Short.parseShort(configOptions.get("ovs-tap-to-wifi0-patch"));
OVS_TAP_TO_WIFI1_PATCH = Short.parseShort(configOptions.get("ovs-tap-to-wifi1-patch"));
OVS_TAP_TO_WIMAX0_PATCH = Short.parseShort(configOptions.get("ovs-tap-to-wimax0-patch"));
OVS_TAP_TO_WIMAX1_PATCH = Short.parseShort(configOptions.get("ovs-tap-to-wimax1-patch"));
OVS_TAP_TO_ETHERNET_PATCH = Short.parseShort(configOptions.get("ovs-tap-to-ethernet-patch"));
OVS_WIFI0_TO_TAP_PATCH = Short.parseShort(configOptions.get("ovs-wifi0-to-tap-patch"));
OVS_WIFI1_TO_TAP_PATCH = Short.parseShort(configOptions.get("ovs-wifi1-to-tap-patch"));
OVS_WIMAX0_TO_TAP_PATCH = Short.parseShort(configOptions.get("ovs-wimax0-to-tap-patch"));
OVS_WIMAX1_TO_TAP_PATCH = Short.parseShort(configOptions.get("ovs-wimax1-to-tap-patch"));
OVS_ETHERNET_TO_TAP_PATCH = Short.parseShort(configOptions.get("ovs-ethernet-to-tap-patch"));
OVS_TAP_LOCAL_PORT = Short.parseShort(configOptions.get("ovs-tap-local-port"));
OVS_WIFI0_IFACE_PORT = Short.parseShort(configOptions.get("ovs-wifi0-iface-port"));
OVS_WIFI1_IFACE_PORT = Short.parseShort(configOptions.get("ovs-wifi1-iface-port"));
OVS_WIMAX0_IFACE_PORT = Short.parseShort(configOptions.get("ovs-wimax0-iface-port"));
OVS_WIMAX1_IFACE_PORT = Short.parseShort(configOptions.get("ovs-wimax1-iface-port"));
OVS_ETHERNET_IFACE_PORT = Short.parseShort(configOptions.get("ovs-ethernet-iface-port"));
} catch(Exception e) {
log.error("Incorrect Handover configuration options", e);
//throw e;
}
ACTIVE_FLOWS = new ArrayList<String>();
try {
GPSD_CONN = new GPSdEndpoint("127.0.0.1", GPSD_TCP_PORT, new ResultParser());
GPSD_CONN.addListener(new ObjectListener(){
@Override
public void handleTPV(TPVObject tpv) {
log.debug("GPSD: Updated TPV");
GPSD_LATITUDE = tpv.getLatitude();
GPSD_LONGITUDE = tpv.getLongitude();
GPSD_ALTITUDE = tpv.getAltitude();
}
@Override
public void handleSKY(SKYObject sky) {
log.debug("GPSD: Updated SKY");
}
@Override
public void handleATT(ATTObject att) {
log.debug("GPSD: Updated ATT");
}
@Override
public void handleSUBFRAME(SUBFRAMEObject subframe) {
log.debug("GPSD: Updated SUBFRAME");
}
@Override
public void handleDevices(DevicesObject devices) {
log.debug("GPSD: Updated DEVICES");
for (DeviceObject d : devices.getDevices()){
log.debug("Device: " + d.toString());
}
try {
GPSD_CONN.poll();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void handleDevice(DeviceObject device) {
log.debug("GPSD: Updated DEVICE");
}
});
GPSD_CONN.start();
log.debug("Version: " + GPSD_CONN.version().toString());
GPSD_CONN.watch(true, true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw e;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
throw e;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Periodically ask GRC for a handover decision
grcProbeMgr = new ScheduledThreadPoolExecutor(1);
grcProbe = new GRCProbe();
grcProbeMgr.scheduleAtFixedRate(grcProbe, 10, GRC_PROBE_INTERVAL_SECONDS, TimeUnit.SECONDS);
return;
}
private static String cmdExec(String cmdLine) {
String line;
String output = "";
try {
Process p = Runtime.getRuntime().exec(cmdLine);
BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
output += (line + '\n');
}
input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
return output;
}
private void switchInterface(String networkType) {
if(ACTIVE_NETWORK_TYPE.equals(networkType) || networkType.equals(OFFLINE))
return;
OFFlowMod flow;
OFMatch match;
ArrayList<OFAction> actionList = new ArrayList<OFAction>();
OFActionOutput outputAction;
OFActionDataLayerDestination dlDstAction;
OFActionDataLayerSource dlSrcAction;
String flowName;
int flowLength;
String ifaceOVSdpid;
String tapOVSdpid = OVS_TAP_DPID;
short tapOVSpatchPort;
short tapOVShostPort = OVS_TAP_LOCAL_PORT;
short ifaceOVSifacePort;
short ifaceOVSpatchPort;
byte[] ifaceMACaddr;
byte[] tapMACaddr = TAP_MAC;
// based on the iface chosen, set appropriate parameters
if (networkType.equals("Wifi")) {
ifaceOVSdpid = OVS_WIFI0_DPID;
tapOVSpatchPort = OVS_TAP_TO_WIFI0_PATCH;
ifaceOVSifacePort = OVS_WIFI0_IFACE_PORT;
ifaceOVSpatchPort = OVS_WIFI0_TO_TAP_PATCH;
ifaceMACaddr = WIFI0_MAC;
} else if (networkType.equals("WiMAX")) {
ifaceOVSdpid = OVS_WIMAX0_DPID;
tapOVSpatchPort = OVS_TAP_TO_WIMAX0_PATCH;
ifaceOVSifacePort = OVS_WIMAX0_IFACE_PORT;
ifaceOVSpatchPort = OVS_WIMAX0_TO_TAP_PATCH;
ifaceMACaddr = WIMAX0_MAC;
} else {
//TODO: remove this... not needed
//fail over to ethernet
ifaceOVSdpid = OVS_ETHERNET_DPID;
tapOVSpatchPort = OVS_TAP_TO_ETHERNET_PATCH;
ifaceOVSifacePort = OVS_ETHERNET_IFACE_PORT;
ifaceOVSpatchPort = OVS_ETHERNET_TO_TAP_PATCH;
ifaceMACaddr = ETHERNET_MAC;
}
// Remove old flows and switch only if we are switching to a different interface.
if (!networkType.equals(ACTIVE_NETWORK_TYPE)) {
while (!ACTIVE_FLOWS.isEmpty()) {
String tmp = ACTIVE_FLOWS.remove(0);
log.debug("In prep. for switch, removing flow " + tmp);
sfp.deleteFlow(tmp);
}
// update to the new interface we have been told to use
ACTIVE_NETWORK_TYPE = networkType;
// first, insert flows at the tap OVS. Do the MAC rewrites here (just to keep them all on one OVS).
// this means there should be no ARP flows at the tap OVS (all ARP packets will be sent to the controller auto-magically).
// tap --> patch (IP)
flow = new OFFlowMod();
match = new OFMatch();
outputAction = new OFActionOutput();
dlSrcAction = new OFActionDataLayerSource();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(tapOVShostPort);
match.setDataLayerType((short) 0x0800);
dlSrcAction.setType(OFActionType.SET_DL_SRC);
dlSrcAction.setDataLayerAddress(ifaceMACaddr);
dlSrcAction.setLength((short) OFActionDataLayerSource.MINIMUM_LENGTH);
flowLength = flowLength + dlSrcAction.getLengthU();
actionList.add(dlSrcAction);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(tapOVSpatchPort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(tapOVSpatchPort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "tap-to-patch-ip";
sfp.addFlow(flowName, flow, tapOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + tapOVSdpid + flowName);
actionList.clear();
// tap --> patch (CONTROLLER) (ARP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(tapOVShostPort);
match.setDataLayerType((short) 0x0806);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(OFPort.OFPP_CONTROLLER.getValue());
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(OFPort.OFPP_CONTROLLER.getValue());
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "tap-to-patch(controller)-arp";
sfp.addFlow(flowName, flow, tapOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + tapOVSdpid + flowName);
actionList.clear();
// tap <-- patch (IP)
flow = new OFFlowMod();
match = new OFMatch();
dlDstAction = new OFActionDataLayerDestination();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(tapOVSpatchPort);
match.setDataLayerType((short) 0x0800);
dlDstAction.setType(OFActionType.SET_DL_DST);
dlDstAction.setDataLayerAddress(tapMACaddr);
dlDstAction.setLength((short) OFActionDataLayerDestination.MINIMUM_LENGTH);
flowLength = flowLength + dlDstAction.getLengthU();
actionList.add(dlDstAction);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(tapOVShostPort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(tapOVShostPort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "tap-from-patch-ip";
sfp.addFlow(flowName, flow, tapOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + tapOVSdpid + flowName);
actionList.clear();
// tap (CONTROLLER) <-- patch (ARP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(tapOVSpatchPort);
match.setDataLayerType((short) 0x0806);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(OFPort.OFPP_CONTROLLER.getValue());
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(OFPort.OFPP_CONTROLLER.getValue());
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "tap(controller)-from-patch-arp";
sfp.addFlow(flowName, flow, tapOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + tapOVSdpid + flowName);
actionList.clear();
// now, insert the flows on the OVS corresponding to the iface of choice.
// no rewrites should occur here. Forward all packets of any ethertype.
// iface <-- patch (IP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(ifaceOVSpatchPort);
match.setDataLayerType((short) 0x0800);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(ifaceOVSifacePort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(ifaceOVSifacePort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "iface-from-patch-ip";
sfp.addFlow(flowName, flow, ifaceOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + ifaceOVSdpid + flowName);
actionList.clear();
// iface <-- patch (ARP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(ifaceOVSpatchPort);
match.setDataLayerType((short) 0x0806);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(ifaceOVSifacePort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(ifaceOVSifacePort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "iface-from-patch-arp";
sfp.addFlow(flowName, flow, ifaceOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + ifaceOVSdpid + flowName);
actionList.clear();
// iface --> patch (IP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(ifaceOVSifacePort);
match.setDataLayerType((short) 0x0800);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(ifaceOVSpatchPort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(ifaceOVSpatchPort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "iface-to-patch-ip";
sfp.addFlow(flowName, flow, ifaceOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + ifaceOVSdpid + flowName);
actionList.clear();
// iface --> patch (ARP)
flow = new OFFlowMod();
match = new OFMatch();
flowLength = OFFlowMod.MINIMUM_LENGTH;
match.setInputPort(ifaceOVSifacePort);
match.setDataLayerType((short) 0x0806);
outputAction.setType(OFActionType.OUTPUT);
outputAction.setPort(ifaceOVSpatchPort);
outputAction.setLength((short) OFActionOutput.MINIMUM_LENGTH);
flowLength = flowLength + outputAction.getLengthU();
actionList.add(outputAction);
flow.setCookie(0);
flow.setBufferId(-1);
flow.setOutPort(ifaceOVSpatchPort);
flow.setActions(actionList);
flow.setMatch(match);
flow.setPriority((short) 32768);
flow.setLengthU(flowLength);
flowName = "iface-to-patch-arp";
sfp.addFlow(flowName, flow, ifaceOVSdpid);
ACTIVE_FLOWS.add(flowName);
log.info("added flow on SW " + ifaceOVSdpid + flowName);
actionList.clear();
}
return;
}
// HTTP POST request
private String askGRC() {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(GRC_URL);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
//we need a list of all available network types, this shouldn't be hard coded
ArrayList<String> networkTypes = new ArrayList<String>();
networkTypes.add("WiMAX");
networkTypes.add("Wifi");
//create a JSON object for every interface name, containing collected details
ArrayList<JSONObject> netTypeObjs = new ArrayList<JSONObject>();
for(String netType : networkTypes){
JSONObject obj = new JSONObject();
obj.put("name", netType);
obj.put("operator", netType.equals("Wifi")? "" :"Clemson");
netTypeObjs.add(obj);
}
//create a JSON array containing all of our JSON objects, and include it in our request
log.debug("Latitude: " + Double.toString(GPSD_LATITUDE) + ", Longitude: " + Double.toString(GPSD_LONGITUDE));
JSONArray jNetTypesArray = new JSONArray(netTypeObjs);
nvps.add(new BasicNameValuePair("latitude", Double.toString(GPSD_LATITUDE)));
nvps.add(new BasicNameValuePair("longitude", Double.toString(GPSD_LONGITUDE)));
nvps.add(new BasicNameValuePair("network_types", jNetTypesArray.toString()));
//set POST parameters and execute request
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpPost);
int status = response.getStatusLine().getStatusCode();
//retreive JSON response
String body;
if(status >= 200 && status < 300){
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} else {
body = "ERROR";
}
//parse JSON response
JSONObject responseObj = new JSONObject(body);
String responseNet = responseObj.getString("network");
if(responseNet.isEmpty()){
log.debug("ERROR: Empty response from server, check args.");
return OFFLINE;
}
log.debug("Switching to: " + responseNet);
return responseObj.getString("network");
} catch(Exception e) {
e.printStackTrace();
}
return OFFLINE;
}
class GRCProbe implements Runnable {
@Override
public void run() {
log.info("Asking GRC (Cybertiger) for a handover decision...");
// do it here
switchInterface(askGRC());
return;
}
} // END GRCProbe Class
} // END Handover Module