Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 2 | |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 4 | import java.util.HashMap; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 5 | import java.util.HashSet; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | import java.util.Set; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 9 | import java.util.concurrent.ExecutionException; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 10 | |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 13 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.intent.FlowEntry; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 16 | //import net.onrc.onos.core.topology.NetworkGraph; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.util.Pair; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 18 | |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 19 | import org.openflow.protocol.OFBarrierReply; |
Brian O'Connor | 9b712f6 | 2014-02-20 14:22:20 -0800 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 23 | /** |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 24 | * @author Brian O'Connor <bocon@onlab.us> |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 25 | */ |
| 26 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 27 | public class PlanInstallRuntime { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | // NetworkGraph graph; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 29 | IFlowPusherService pusher; |
| 30 | IFloodlightProviderService provider; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 31 | private static final Logger log = LoggerFactory.getLogger(PlanInstallRuntime.class); |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 32 | |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 33 | public PlanInstallRuntime(//NetworkGraph graph, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | IFloodlightProviderService provider, |
| 35 | IFlowPusherService pusher) { |
| 36 | // this.graph = graph; |
| 37 | this.provider = provider; |
| 38 | this.pusher = pusher; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 39 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 41 | private static class FlowModCount { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | IOFSwitch sw; |
| 43 | long modFlows = 0; |
| 44 | long delFlows = 0; |
| 45 | long errors = 0; |
| 46 | |
| 47 | FlowModCount(IOFSwitch sw) { |
| 48 | this.sw = sw; |
| 49 | } |
| 50 | |
| 51 | void addFlowEntry(FlowEntry entry) { |
| 52 | switch (entry.getOperator()) { |
| 53 | case ADD: |
| 54 | modFlows++; |
| 55 | break; |
| 56 | case ERROR: |
| 57 | errors++; |
| 58 | break; |
| 59 | case REMOVE: |
| 60 | delFlows++; |
| 61 | break; |
| 62 | default: |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public String toString() { |
| 68 | return "sw:" + sw.getStringId() + ": modify " + modFlows + " delete " + delFlows + " error " + errors; |
| 69 | } |
| 70 | |
| 71 | static Map<IOFSwitch, FlowModCount> map = new HashMap<>(); |
| 72 | |
| 73 | static void countFlowEntry(IOFSwitch sw, FlowEntry entry) { |
| 74 | FlowModCount count = map.get(sw); |
| 75 | if (count == null) { |
| 76 | count = new FlowModCount(sw); |
| 77 | map.put(sw, count); |
| 78 | } |
| 79 | count.addFlowEntry(entry); |
| 80 | } |
| 81 | |
| 82 | static void startCount() { |
| 83 | map.clear(); |
| 84 | } |
| 85 | |
| 86 | static void printCount() { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 87 | StringBuilder result = new StringBuilder(); |
| 88 | |
| 89 | result.append("FLOWMOD COUNT:\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | for (FlowModCount count : map.values()) { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 91 | result.append(count.toString() + '\n'); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | } |
| 93 | if (map.values().isEmpty()) { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 94 | result.append("No flow mods installed\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | } |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 96 | log.error(result.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | } |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 98 | } |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 99 | |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 100 | public boolean installPlan(List<Set<FlowEntry>> plan) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | long start = System.nanoTime(); |
| 102 | Map<Long, IOFSwitch> switches = provider.getSwitches(); |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 103 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | log.debug("IOFSwitches: {}", switches); |
| 105 | |
| 106 | FlowModCount.startCount(); |
| 107 | for (Set<FlowEntry> phase : plan) { |
| 108 | Set<Pair<IOFSwitch, net.onrc.onos.core.util.FlowEntry>> entries = new HashSet<>(); |
| 109 | Set<IOFSwitch> modifiedSwitches = new HashSet<>(); |
| 110 | |
| 111 | long step1 = System.nanoTime(); |
| 112 | // convert flow entries and create pairs |
| 113 | for (FlowEntry entry : phase) { |
| 114 | IOFSwitch sw = switches.get(entry.getSwitch()); |
| 115 | if (sw == null) { |
| 116 | // no active switch, skip this flow entry |
| 117 | log.debug("Skipping flow entry: {}", entry); |
| 118 | continue; |
| 119 | } |
| 120 | entries.add(new Pair<>(sw, entry.getFlowEntry())); |
| 121 | modifiedSwitches.add(sw); |
| 122 | FlowModCount.countFlowEntry(sw, entry); |
| 123 | } |
| 124 | long step2 = System.nanoTime(); |
| 125 | |
| 126 | // push flow entries to switches |
| 127 | log.debug("Pushing flow entries: {}", entries); |
| 128 | pusher.pushFlowEntries(entries); |
| 129 | long step3 = System.nanoTime(); |
| 130 | |
| 131 | // TODO: insert a barrier after each phase on each modifiedSwitch |
| 132 | // TODO: wait for confirmation messages before proceeding |
| 133 | List<Pair<IOFSwitch, OFMessageFuture<OFBarrierReply>>> barriers = new ArrayList<>(); |
| 134 | for (IOFSwitch sw : modifiedSwitches) { |
| 135 | barriers.add(new Pair<>(sw, pusher.barrierAsync(sw))); |
| 136 | } |
| 137 | for (Pair<IOFSwitch, OFMessageFuture<OFBarrierReply>> pair : barriers) { |
| 138 | IOFSwitch sw = pair.first; |
| 139 | OFMessageFuture<OFBarrierReply> future = pair.second; |
| 140 | try { |
| 141 | future.get(); |
| 142 | } catch (InterruptedException | ExecutionException e) { |
| 143 | log.error("Barrier message not received for sw: {}", sw); |
| 144 | } |
| 145 | } |
| 146 | long step4 = System.nanoTime(); |
| 147 | log.error("MEASUREMENT: convert: {} ns, push: {} ns, barrierWait: {} ns", |
| 148 | step2 - step1, step3 - step2, step4 - step3); |
| 149 | |
| 150 | } |
| 151 | long end = System.nanoTime(); |
| 152 | log.error("MEASUREMENT: Install plan: {} ns", (end - start)); |
| 153 | FlowModCount.printCount(); |
| 154 | |
| 155 | // TODO: we assume that the plan installation succeeds for now |
| 156 | return true; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 157 | } |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 158 | } |