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 | |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 3 | import java.lang.ref.WeakReference; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
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; |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 9 | import java.util.WeakHashMap; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 10 | import java.util.concurrent.ExecutionException; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 11 | |
| 12 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 13 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | 6dc44e9 | 2014-02-24 21:23:46 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.intent.FlowEntry; |
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 | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 19 | import org.projectfloodlight.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 | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 24 | * This class is responsible for installing plans (lists of sets of FlowEntries) |
| 25 | * into local switches. In this context, a local switch is a switch for which |
| 26 | * this ONOS instance is the master. It also is responsible for sending barrier |
| 27 | * messages between sets. |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 28 | */ |
| 29 | |
Brian O'Connor | 12861f7 | 2014-02-19 20:40:32 -0800 | [diff] [blame] | 30 | public class PlanInstallRuntime { |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 31 | |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 32 | IFlowPusherService pusher; |
| 33 | IFloodlightProviderService provider; |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 34 | private static final Logger log = LoggerFactory.getLogger(PlanInstallRuntime.class); |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 35 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Constructor. |
| 38 | * |
| 39 | * @param provider the FloodlightProviderService for list of local switches |
| 40 | * @param pusher the FlowPusherService to use for FlowEntry installation |
| 41 | */ |
| 42 | public PlanInstallRuntime(IFloodlightProviderService provider, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | IFlowPusherService pusher) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | this.provider = provider; |
| 45 | this.pusher = pusher; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 46 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 48 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 49 | * This class is a temporary class for collecting FlowMod installation |
| 50 | * information. It is largely used for debugging purposes, and it should not |
| 51 | * be depended on for other purposes. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 52 | * <p> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 53 | * TODO: This class should be wrapped into a more generic debugging |
| 54 | * framework when available. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 55 | */ |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 56 | private static class FlowModCount { |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 57 | WeakReference<IOFSwitch> sw; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | long modFlows = 0; |
| 59 | long delFlows = 0; |
| 60 | long errors = 0; |
| 61 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Constructor. |
| 64 | * |
| 65 | * @param sw the switch for FlowMod statistics collection |
| 66 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | FlowModCount(IOFSwitch sw) { |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 68 | this.sw = new WeakReference<>(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Include the FlowEntry in this switch statistics object. |
| 73 | * |
| 74 | * @param entry the FlowEntry to count |
| 75 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | void addFlowEntry(FlowEntry entry) { |
| 77 | switch (entry.getOperator()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 78 | case ADD: |
| 79 | modFlows++; |
| 80 | break; |
| 81 | case ERROR: |
| 82 | errors++; |
| 83 | break; |
| 84 | case REMOVE: |
| 85 | delFlows++; |
| 86 | break; |
| 87 | default: |
| 88 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Returns a string representation of this object. |
| 94 | * |
| 95 | * @return string representation of this object |
| 96 | */ |
TeruU | f911165 | 2014-05-14 23:10:35 -0700 | [diff] [blame] | 97 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | public String toString() { |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 99 | final IOFSwitch swTemp = sw.get(); |
| 100 | return "sw:" + ((swTemp == null) ? "disconnected" : swTemp.getStringId()) |
| 101 | + ": modify " + modFlows + " delete " + delFlows + " error " + errors; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 104 | static Map<IOFSwitch, FlowModCount> map = new WeakHashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 106 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 107 | * This function is used for collecting statistics information. It |
| 108 | * should be called for every FlowEntry that is pushed to the switch for |
| 109 | * accurate statistics. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 110 | * <p> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 111 | * This class maintains a map of Switches and FlowModCount collection |
| 112 | * objects, which are used for collection. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 113 | * <p> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 114 | * TODO: This should be refactored to use a more generic mechanism when |
| 115 | * available. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 116 | * |
| 117 | * @param sw the switch that entry is being pushed to |
| 118 | * @param entry the FlowEntry being pushed |
| 119 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | static void countFlowEntry(IOFSwitch sw, FlowEntry entry) { |
| 121 | FlowModCount count = map.get(sw); |
| 122 | if (count == null) { |
| 123 | count = new FlowModCount(sw); |
| 124 | map.put(sw, count); |
| 125 | } |
| 126 | count.addFlowEntry(entry); |
| 127 | } |
| 128 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 129 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 130 | * Reset the statistics collection. It should be called when required |
| 131 | * for debugging. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 132 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 133 | static void startCount() { |
| 134 | map.clear(); |
| 135 | } |
| 136 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Print out the statistics information when required for debugging. |
| 139 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | static void printCount() { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 141 | StringBuilder result = new StringBuilder(); |
| 142 | |
| 143 | result.append("FLOWMOD COUNT:\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | for (FlowModCount count : map.values()) { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 145 | result.append(count.toString() + '\n'); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | } |
| 147 | if (map.values().isEmpty()) { |
Pavlin Radoslavov | 424150c | 2014-04-09 12:12:36 -0700 | [diff] [blame] | 148 | result.append("No flow mods installed\n"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | } |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 150 | log.debug(result.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 151 | } |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 152 | } |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 153 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 154 | /** |
| 155 | * This function should be called to install the FlowEntries in the plan. |
| 156 | * <p> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 157 | * Each set of FlowEntries can be installed together, but all entries should |
| 158 | * be installed proceeded to the next set. |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 159 | * <p> |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 160 | * TODO: This method lack coordination between the other ONOS instances |
| 161 | * before proceeded with the next set of entries |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 162 | * |
| 163 | * @param plan list of set of FlowEntries for installation on local switches |
| 164 | * @return true (we assume installation is successful) |
| 165 | */ |
Brian O'Connor | 488e5ed | 2014-02-20 19:50:01 -0800 | [diff] [blame] | 166 | public boolean installPlan(List<Set<FlowEntry>> plan) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | long start = System.nanoTime(); |
| 168 | Map<Long, IOFSwitch> switches = provider.getSwitches(); |
Brian O'Connor | 2fab2f6 | 2014-02-26 12:40:01 -0800 | [diff] [blame] | 169 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | log.debug("IOFSwitches: {}", switches); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | FlowModCount.startCount(); |
| 172 | for (Set<FlowEntry> phase : plan) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 173 | Set<Pair<IOFSwitch, FlowEntry>> entries = new HashSet<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | Set<IOFSwitch> modifiedSwitches = new HashSet<>(); |
| 175 | |
| 176 | long step1 = System.nanoTime(); |
| 177 | // convert flow entries and create pairs |
| 178 | for (FlowEntry entry : phase) { |
| 179 | IOFSwitch sw = switches.get(entry.getSwitch()); |
| 180 | if (sw == null) { |
| 181 | // no active switch, skip this flow entry |
| 182 | log.debug("Skipping flow entry: {}", entry); |
| 183 | continue; |
| 184 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 185 | entries.add(new Pair<>(sw, entry)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | modifiedSwitches.add(sw); |
| 187 | FlowModCount.countFlowEntry(sw, entry); |
| 188 | } |
| 189 | long step2 = System.nanoTime(); |
| 190 | |
| 191 | // push flow entries to switches |
| 192 | log.debug("Pushing flow entries: {}", entries); |
| 193 | pusher.pushFlowEntries(entries); |
| 194 | long step3 = System.nanoTime(); |
| 195 | |
Brian O'Connor | a15fb86 | 2014-06-12 23:38:54 -0700 | [diff] [blame] | 196 | // insert a barrier after each phase on each modifiedSwitch |
| 197 | // wait for confirmation messages before proceeding |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 198 | List<Pair<IOFSwitch, OFMessageFuture<OFBarrierReply>>> barriers = new ArrayList<>(); |
| 199 | for (IOFSwitch sw : modifiedSwitches) { |
| 200 | barriers.add(new Pair<>(sw, pusher.barrierAsync(sw))); |
| 201 | } |
| 202 | for (Pair<IOFSwitch, OFMessageFuture<OFBarrierReply>> pair : barriers) { |
Sho SHIMIZU | 26d7789 | 2014-06-10 11:07:06 -0700 | [diff] [blame] | 203 | IOFSwitch sw = pair.getFirst(); |
| 204 | OFMessageFuture<OFBarrierReply> future = pair.getSecond(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | try { |
| 206 | future.get(); |
| 207 | } catch (InterruptedException | ExecutionException e) { |
| 208 | log.error("Barrier message not received for sw: {}", sw); |
| 209 | } |
| 210 | } |
| 211 | long step4 = System.nanoTime(); |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 212 | log.debug("MEASUREMENT: convert: {} ns, push: {} ns, barrierWait: {} ns", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | step2 - step1, step3 - step2, step4 - step3); |
| 214 | |
| 215 | } |
| 216 | long end = System.nanoTime(); |
Pavlin Radoslavov | 964f8ae | 2014-04-18 16:44:14 -0700 | [diff] [blame] | 217 | log.debug("MEASUREMENT: Install plan: {} ns", (end - start)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | FlowModCount.printCount(); |
| 219 | |
| 220 | // TODO: we assume that the plan installation succeeds for now |
| 221 | return true; |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 222 | } |
Brian O'Connor | 67c6e66 | 2014-02-17 15:20:44 -0800 | [diff] [blame] | 223 | } |