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