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