blob: abd82ed68eeb7528c39ac52dda737a092413c874 [file] [log] [blame]
Brian O'Connor67c6e662014-02-17 15:20:44 -08001package net.onrc.onos.intent.runtime;
2
Brian O'Connor6dc44e92014-02-24 21:23:46 -08003import java.util.ArrayList;
Brian O'Connor67c6e662014-02-17 15:20:44 -08004import java.util.HashSet;
5import java.util.List;
6import java.util.Map;
7import java.util.Set;
Brian O'Connor6dc44e92014-02-24 21:23:46 -08008import java.util.concurrent.ExecutionException;
Brian O'Connor67c6e662014-02-17 15:20:44 -08009
10import net.floodlightcontroller.core.IFloodlightProviderService;
11import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connor6dc44e92014-02-24 21:23:46 -080012import net.floodlightcontroller.core.internal.OFMessageFuture;
Brian O'Connor67c6e662014-02-17 15:20:44 -080013import net.onrc.onos.intent.FlowEntry;
14import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Brian O'Connor488e5ed2014-02-20 19:50:01 -080015//import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Brian O'Connor67c6e662014-02-17 15:20:44 -080016import net.onrc.onos.ofcontroller.util.Pair;
17
Brian O'Connor6dc44e92014-02-24 21:23:46 -080018import org.openflow.protocol.OFBarrierReply;
Brian O'Connor9b712f62014-02-20 14:22:20 -080019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
Brian O'Connor67c6e662014-02-17 15:20:44 -080022/**
23 *
24 * @author Brian O'Connor <bocon@onlab.us>
25 *
26 */
27
Brian O'Connor12861f72014-02-19 20:40:32 -080028public class PlanInstallRuntime {
Brian O'Connor488e5ed2014-02-20 19:50:01 -080029// NetworkGraph graph;
Brian O'Connor67c6e662014-02-17 15:20:44 -080030 IFlowPusherService pusher;
31 IFloodlightProviderService provider;
Brian O'Connor9b712f62014-02-20 14:22:20 -080032 private final static Logger log = LoggerFactory.getLogger(PlanInstallRuntime.class);
Brian O'Connor67c6e662014-02-17 15:20:44 -080033
Brian O'Connor488e5ed2014-02-20 19:50:01 -080034 public PlanInstallRuntime(//NetworkGraph graph,
Brian O'Connor12861f72014-02-19 20:40:32 -080035 IFloodlightProviderService provider,
36 IFlowPusherService pusher) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -080037// this.graph = graph;
Brian O'Connor12861f72014-02-19 20:40:32 -080038 this.provider = provider;
39 this.pusher = pusher;
Brian O'Connor67c6e662014-02-17 15:20:44 -080040 }
41
Brian O'Connor488e5ed2014-02-20 19:50:01 -080042 public boolean installPlan(List<Set<FlowEntry>> plan) {
Brian O'Connorc366e872014-02-24 14:08:31 -080043 long start = System.nanoTime();
Brian O'Connor67c6e662014-02-17 15:20:44 -080044 Map<Long,IOFSwitch> switches = provider.getSwitches();
Brian O'Connor9b712f62014-02-20 14:22:20 -080045 log.debug("IOFSwitches: {}", switches);
Brian O'Connor67c6e662014-02-17 15:20:44 -080046 for(Set<FlowEntry> phase : plan) {
Brian O'Connor12861f72014-02-19 20:40:32 -080047 Set<Pair<IOFSwitch, net.onrc.onos.ofcontroller.util.FlowEntry>> entries = new HashSet<>();
Brian O'Connor067b95d2014-02-21 18:43:27 -080048 Set<IOFSwitch> modifiedSwitches = new HashSet<>();
49
Brian O'Connor67c6e662014-02-17 15:20:44 -080050 // convert flow entries and create pairs
51 for(FlowEntry entry : phase) {
Brian O'Connor488e5ed2014-02-20 19:50:01 -080052 IOFSwitch sw = switches.get(entry.getSwitch());
Brian O'Connor067b95d2014-02-21 18:43:27 -080053 if(sw == null) {
54 // no active switch, skip this flow entry
55 log.debug("Skipping flow entry: {}", entry);
56 continue;
57 }
Brian O'Connor488e5ed2014-02-20 19:50:01 -080058 entries.add(new Pair<>(sw, entry.getFlowEntry()));
Brian O'Connor067b95d2014-02-21 18:43:27 -080059 modifiedSwitches.add(sw);
Brian O'Connor67c6e662014-02-17 15:20:44 -080060 }
Brian O'Connor067b95d2014-02-21 18:43:27 -080061
Brian O'Connor67c6e662014-02-17 15:20:44 -080062 // push flow entries to switches
Brian O'Connor067b95d2014-02-21 18:43:27 -080063 log.debug("Pushing flow entries: {}", entries);
Brian O'Connor67c6e662014-02-17 15:20:44 -080064 pusher.pushFlowEntries(entries);
Brian O'Connor067b95d2014-02-21 18:43:27 -080065
66 // TODO: insert a barrier after each phase on each modifiedSwitch
Brian O'Connor67c6e662014-02-17 15:20:44 -080067 // TODO: wait for confirmation messages before proceeding
Brian O'Connor6dc44e92014-02-24 21:23:46 -080068 List<Pair<IOFSwitch,OFMessageFuture<OFBarrierReply>>> barriers = new ArrayList<>();
69 for(IOFSwitch sw : modifiedSwitches) {
70 barriers.add(new Pair<>(sw, pusher.barrierAsync(sw)));
71 }
72 for(Pair<IOFSwitch,OFMessageFuture<OFBarrierReply>> pair : barriers) {
73 IOFSwitch sw = pair.first;
74 OFMessageFuture<OFBarrierReply> future = pair.second;
75 try {
76 future.get();
77 } catch (InterruptedException | ExecutionException e) {
78 log.error("Barrier message not received for sw: {}", sw);
79 }
80 }
Brian O'Connor67c6e662014-02-17 15:20:44 -080081 }
Brian O'Connorc366e872014-02-24 14:08:31 -080082 long end = System.nanoTime();
83 log.error("MEASUREMENT: Install plan: {} ns", (end-start));
Brian O'Connor6dc44e92014-02-24 21:23:46 -080084
Brian O'Connor488e5ed2014-02-20 19:50:01 -080085 // TODO: we assume that the plan installation succeeds for now
86 return true;
Brian O'Connor67c6e662014-02-17 15:20:44 -080087 }
88
89}