Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction; |
| 2 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 4 | import java.util.EventListener; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.HashSet; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | import java.util.concurrent.ArrayBlockingQueue; |
| 11 | import java.util.concurrent.BlockingQueue; |
| 12 | import java.util.concurrent.ConcurrentHashMap; |
| 13 | import java.util.concurrent.ConcurrentMap; |
| 14 | import java.util.concurrent.ExecutionException; |
| 15 | |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 17 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 18 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 19 | import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy; |
| 20 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 21 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 22 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
| 23 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.util.Dpid; |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.util.IdBlockAllocator; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.util.IdGenerator; |
| 28 | import net.onrc.onos.core.util.SwitchPort; |
| 29 | |
| 30 | import org.apache.commons.lang3.tuple.Pair; |
| 31 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 34 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Manages Match-Action entries. |
| 37 | * <p> |
| 38 | * TODO: Make all methods thread-safe |
| 39 | */ |
| 40 | public class MatchActionComponent implements MatchActionService, IFloodlightService { |
| 41 | |
| 42 | private static final Logger log = LoggerFactory.getLogger(MatchActionService.class); |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 43 | private final IFlowPusherService pusher; |
| 44 | private final IFloodlightProviderService provider; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 45 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 46 | private final ConcurrentMap<MatchActionId, MatchAction> matchActionMap = new ConcurrentHashMap<>(); |
| 47 | private final ConcurrentMap<MatchActionOperationsId, MatchActionOperations> matchSetMap = |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 48 | new ConcurrentHashMap<>(); |
| 49 | // TODO - want something better here for the resolved Queue |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 50 | private final BlockingQueue<MatchActionOperationsId> resolvedQueue = new ArrayBlockingQueue<>(100); |
| 51 | private final BlockingQueue<MatchActionOperations> installationWorkQueue = new ArrayBlockingQueue<>(100); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 52 | |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 53 | private IEventChannel<Long, MatchActionOperations> installSetChannel; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 54 | private IEventChannel<String, SwitchResultList> installSetReplyChannel; |
| 55 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 56 | private final IDatagridService datagrid; |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 57 | private final IControllerRegistryService registryService; |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 58 | |
| 59 | private MatchActionIdGeneratorWithIdBlockAllocator matchActionIdGenerator; |
| 60 | private MatchActionOperationsIdGeneratorWithIdBlockAllocator matchActionOperationsIdGenerator; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 61 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Constructs a MatchActionComponent given the services it depends on. |
| 64 | * |
| 65 | * @param newDatagrid datagrid dependency |
| 66 | * @param newPusher flow pusher dependency |
| 67 | * @param newProvider provider used for switch queries |
| 68 | * @param newRegistryService registry for ID block allocation |
| 69 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 70 | public MatchActionComponent(final IDatagridService newDatagrid, |
| 71 | final IFlowPusherService newPusher, |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 72 | final IFloodlightProviderService newProvider, |
| 73 | final IControllerRegistryService newRegistryService) { |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 74 | datagrid = newDatagrid; |
| 75 | pusher = newPusher; |
| 76 | provider = newProvider; |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 77 | registryService = newRegistryService; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Starts the component. Created channels used for communication and |
| 82 | * creates producer and consumer threads. |
| 83 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 84 | public void start() { |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 85 | IdBlockAllocator idBlockAllocator = registryService; |
| 86 | matchActionIdGenerator = |
| 87 | new MatchActionIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
| 88 | matchActionOperationsIdGenerator = |
| 89 | new MatchActionOperationsIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
| 90 | |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 91 | final Installer installerListener = new Installer(); |
| 92 | installSetChannel = datagrid.addListener( |
| 93 | "onos.matchaction.installSetChannel", |
| 94 | installerListener, |
| 95 | Long.class, |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 96 | MatchActionOperations.class); |
| 97 | |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 98 | |
| 99 | final Coordinator coordinator = new Coordinator(); |
| 100 | coordinator.start(); |
| 101 | installSetReplyChannel = datagrid.addListener( |
| 102 | "onos.matchaction.installSetReplyChannel", |
| 103 | coordinator, |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 104 | String.class, |
| 105 | SwitchResultList.class); |
| 106 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 107 | // TODO Single instance for now, should be a work queue of some sort eventually |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 108 | final InstallerWorker installer = new InstallerWorker(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 109 | installer.start(); |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 110 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Installs a set of MatchActionOperations. |
| 115 | * |
| 116 | * @param matchSet the set of MatchActions to install |
| 117 | * @return identifier of the installed operations |
| 118 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 119 | public MatchActionOperationsId installMatchActionOperations(MatchActionOperations matchSet) { |
| 120 | if (checkResolved(matchSet)) { |
| 121 | matchSet.setState(MatchActionOperationsState.RESOLVED); |
| 122 | } else { |
| 123 | matchSet.setState(MatchActionOperationsState.INIT); |
| 124 | } |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 125 | log.trace("MatchActionsOperations set added: {} {} {}", |
| 126 | matchSet.getOperationsId(), |
| 127 | matchSet.getState(), |
| 128 | matchSet.toString()); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 129 | matchSetMap.put(matchSet.getOperationsId(), matchSet); |
| 130 | if (matchSet.getState() == MatchActionOperationsState.RESOLVED) { |
| 131 | resolvedQueue.add(matchSet.getOperationsId()); |
| 132 | } |
| 133 | return matchSet.getOperationsId(); |
| 134 | } |
| 135 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Returns the state of a set of operations. |
| 138 | * |
| 139 | * @param matchSetId identifier of the MatchActionOperations being queried. |
| 140 | * @return state of the given operations |
| 141 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 142 | public MatchActionOperationsState getMatchActionOperationsState(MatchActionOperationsId matchSetId) { |
| 143 | MatchActionOperations set = matchSetMap.get(matchSetId); |
| 144 | return (set == null) ? null : set.getState(); |
| 145 | } |
| 146 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Checks if a given set of operations has all of its dependencies resolved. |
| 149 | * |
| 150 | * @param matchSet Operations set to check |
| 151 | * @return true if all dependencies are resolved, false otherwise |
| 152 | */ |
| 153 | private boolean checkResolved(MatchActionOperations matchSet) { |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 154 | boolean resolved = true; |
| 155 | for (MatchActionOperationsId setId : matchSet.getDependencies()) { |
| 156 | MatchActionOperations set = matchSetMap.get(setId); |
| 157 | if (set == null || set.getState() != MatchActionOperationsState.RESOLVED) { |
| 158 | resolved = false; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | return resolved; |
| 163 | } |
| 164 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Producer class for MatchActionOperations. An instance of this runs on |
| 167 | * each ONOS node. Requests come in via the resolved queue, and are |
| 168 | * distributed to workers running on each ONOS instance via a channel. |
| 169 | */ |
| 170 | private final class Coordinator extends Thread |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 171 | implements IEventChannelListener<String, SwitchResultList> { |
| 172 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 173 | private final Map<MatchActionOperationsId, |
| 174 | Map<Dpid, SwitchResult>> |
| 175 | pendingMatchActionOperations = new HashMap<>(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 176 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Default constructor. |
| 179 | */ |
| 180 | Coordinator() { |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 181 | // nothing to initialize |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void run() { |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 186 | //noinspection InfiniteLoopStatement - for IntelliJ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 187 | while (true) { |
| 188 | // 1. Remove MatchActionOperations(s) from the Global Resolved Queue |
| 189 | try { |
| 190 | MatchActionOperationsId setId = resolvedQueue.take(); |
| 191 | processSet(setId); |
| 192 | } catch (InterruptedException e) { |
| 193 | log.warn("Error taking from resolved queue: {}", e.getMessage()); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Processes an inbound MatchActionOperations object. |
| 200 | * |
| 201 | * @param setId Identifier of the MatchActionOperations object |
| 202 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 203 | private void processSet(MatchActionOperationsId setId) { |
| 204 | MatchActionOperations matchSet = matchSetMap.get(setId); |
| 205 | matchSet.setState(MatchActionOperationsState.PENDING); |
| 206 | matchSetMap.put(setId, matchSet); |
| 207 | |
| 208 | // TODO apply updates to in-memory flow table and resolve conflicts |
| 209 | // TODO generate apply and undo sets, using MatchActionOperations for now... |
| 210 | |
| 211 | // build pending switches set for coordinator tracking |
| 212 | Map<Dpid, SwitchResult> switches = new HashMap<>(); |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 213 | for (MatchActionOperationEntry matchActionOp : matchSet.getOperations()) { |
| 214 | MatchAction matchAction = matchActionOp.getTarget(); |
| 215 | SwitchPort sw = matchAction.getSwitchPort(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 216 | switches.put(sw.getDpid(), new SwitchResult(setId, sw.getDpid())); |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 217 | switch(matchActionOp.getOperator()) { |
| 218 | case ADD: |
| 219 | matchActionMap.put(matchAction.getId(), matchAction); |
| 220 | break; |
| 221 | case REMOVE: |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 222 | // TODO we may want to be more careful when removing MatchActions |
| 223 | matchActionMap.remove(matchAction.getId()); |
| 224 | break; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 225 | default: |
| 226 | throw new UnsupportedOperationException( |
| 227 | "Unsupported MatchAction operation" + |
| 228 | matchActionOp.getOperator().toString()); |
| 229 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 230 | } |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 231 | pendingMatchActionOperations.put(setId, switches); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 232 | |
| 233 | // distribute apply/undo sets to cluster |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 234 | log.trace("MatchAction Coordinator distributing set: {}", matchSet); |
| 235 | installSetChannel.addTransientEntry(setId.getId(), matchSet); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void entryAdded(SwitchResultList value) { |
| 240 | updateSwitchResults(value); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void entryRemoved(SwitchResultList value) { |
| 245 | // noop |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void entryUpdated(SwitchResultList value) { |
| 250 | updateSwitchResults(value); |
| 251 | } |
| 252 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Processes the response from a consumer. |
| 255 | * |
| 256 | * @param results List of switches modified by the consumer |
| 257 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 258 | private void updateSwitchResults(SwitchResultList results) { |
| 259 | if (results == null || results.size() == 0) { |
| 260 | return; |
| 261 | } |
| 262 | MatchActionOperationsId matchSetId = results.get(0).getMatchActionOperationsId(); |
| 263 | |
| 264 | // apply updates from results list |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 265 | Map<Dpid, SwitchResult> resultMap = pendingMatchActionOperations.get(matchSetId); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 266 | for (SwitchResult result : results) { |
| 267 | SwitchResult resultToUpdate = resultMap.get(result.getSwitch()); |
| 268 | if (resultToUpdate != null) { |
| 269 | resultToUpdate.setStatus(result.getStatus()); |
| 270 | } |
| 271 | // else { |
| 272 | // TODO error! |
| 273 | // } |
| 274 | } |
| 275 | |
| 276 | // check to see the overall outcome of the install operation |
| 277 | SwitchResult.Status setResult = SwitchResult.Status.SUCCESS; |
| 278 | for (SwitchResult result : resultMap.values()) { |
| 279 | if (result.getStatus().equals(SwitchResult.Status.FAILURE)) { |
| 280 | setResult = SwitchResult.Status.FAILURE; |
| 281 | // if any switch fails, we fail the installation |
| 282 | break; |
| 283 | } else if (!setResult.equals(SwitchResult.Status.FAILURE) |
| 284 | && result.getStatus().equals(SwitchResult.Status.UNKNOWN)) { |
| 285 | setResult = SwitchResult.Status.UNKNOWN; |
| 286 | } |
| 287 | } |
| 288 | switch (setResult) { |
| 289 | case SUCCESS: |
| 290 | // mark MatchActionOperations as INSTALLED |
| 291 | MatchActionOperations matchSet = matchSetMap.get(matchSetId); |
| 292 | matchSet.setState(MatchActionOperationsState.INSTALLED); |
| 293 | matchSetMap.replace(matchSetId, matchSet); |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 294 | pendingMatchActionOperations.remove(matchSetId); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 295 | |
| 296 | // TODO update dependent sets as needed |
| 297 | break; |
| 298 | case FAILURE: |
| 299 | // mark MatchActionOperations as FAILED |
| 300 | matchSet = matchSetMap.get(matchSetId); |
| 301 | matchSet.setState(MatchActionOperationsState.FAILED); |
| 302 | matchSetMap.replace(matchSetId, matchSet); |
| 303 | |
| 304 | // TODO instruct installers to install Undo set |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 305 | // TODO the pendingMatchActionOperations state needs to be cleaned-up |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 306 | break; |
| 307 | case UNKNOWN: |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 308 | // FALLTHROUGH |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 309 | default: |
| 310 | // noop, still waiting for results |
| 311 | // TODO: check to see if installers are dead after timeout |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 312 | break; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Worker thread that pushes MatchActionOperations to the switches via |
| 319 | * the FlowPusher. |
| 320 | */ |
| 321 | private class InstallerWorker extends Thread { |
| 322 | |
| 323 | /** |
| 324 | * Default constructor. |
| 325 | */ |
| 326 | InstallerWorker() { |
| 327 | // nothing to initialize |
| 328 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 329 | |
| 330 | // Note: we should consider using an alternative representation for |
| 331 | // apply sets |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 332 | |
| 333 | /** |
| 334 | * Installs a set of MatchActionOperations using the Flow Pusher. |
| 335 | * |
| 336 | * @param matchSet set of MatchActions to install |
| 337 | */ |
| 338 | private void install(MatchActionOperations matchSet) { |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 339 | Set<Long> masterDpids = provider.getAllMasterSwitchDpids(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 340 | |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 341 | Set<MatchActionOperationEntry> installSet = new HashSet<>(); |
| 342 | Set<Dpid> modifiedSwitches = new HashSet<>(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 343 | |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 344 | for (MatchActionOperationEntry matchActionOp : matchSet.getOperations()) { |
| 345 | MatchAction matchAction = matchActionOp.getTarget(); |
| 346 | Dpid dpid = matchAction.getSwitchPort().getDpid(); |
| 347 | if (masterDpids.contains(dpid.value())) { |
| 348 | // only install if we are the master |
| 349 | // TODO this optimization will introduce some nice race |
| 350 | // conditions on failure requiring mastership change |
| 351 | installSet.add(matchActionOp); |
| 352 | modifiedSwitches.add(dpid); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 353 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // push flow entries to switches |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 357 | pusher.pushMatchActions(installSet); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 358 | |
| 359 | // insert a barrier after each phase on each modifiedSwitch |
| 360 | // wait for confirmation messages before proceeding |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 361 | List<Pair<Dpid, OFMessageFuture<OFBarrierReply>>> barriers = new ArrayList<>(); |
| 362 | for (Dpid dpid : modifiedSwitches) { |
| 363 | barriers.add(Pair.of(dpid, pusher.barrierAsync(dpid))); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 364 | } |
| 365 | List<SwitchResult> switchResults = new ArrayList<>(); |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 366 | for (Pair<Dpid, OFMessageFuture<OFBarrierReply>> pair : barriers) { |
| 367 | Dpid dpid = pair.getLeft(); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 368 | OFMessageFuture<OFBarrierReply> future = pair.getRight(); |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 369 | SwitchResult switchResult = new SwitchResult(matchSet.getOperationsId(), |
| 370 | dpid); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 371 | try { |
| 372 | future.get(); |
| 373 | switchResult.setStatus(SwitchResult.Status.SUCCESS); |
| 374 | } catch (InterruptedException | ExecutionException e) { |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 375 | log.error("Barrier message not received for sw: {}", dpid); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 376 | switchResult.setStatus(SwitchResult.Status.FAILURE); |
| 377 | } |
| 378 | switchResults.add(switchResult); |
| 379 | } |
| 380 | |
| 381 | // send update message to coordinator |
| 382 | // TODO: we might want to use another ID here, i.e. GUID, to avoid |
| 383 | // overlap |
| 384 | final SwitchResultList switchResultList = new SwitchResultList(); |
| 385 | switchResultList.addAll(switchResults); |
| 386 | installSetReplyChannel.addTransientEntry(matchSet.getOperationsId().toString(), |
| 387 | switchResultList); |
| 388 | } |
| 389 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 390 | @Override |
| 391 | public void run() { |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 392 | //noinspection InfiniteLoopStatement - for IntelliJ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 393 | while (true) { |
| 394 | // 1. Remove MatchActionOperations(s) from the Global Resolved Queue |
| 395 | try { |
| 396 | MatchActionOperations operations = installationWorkQueue.take(); |
| 397 | install(operations); |
| 398 | } catch (InterruptedException e) { |
| 399 | log.warn("Error taking from installation queue: {}", e.getMessage()); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 405 | /** |
| 406 | * Consumer class for MatchActionOperations. Listens on the MatchAction |
| 407 | * channel and places inbound requests on a queue to be handled by the |
| 408 | * InstallerWorker threads. |
| 409 | */ |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 410 | class Installer |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 411 | implements IEventChannelListener<Long, MatchActionOperations> { |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 412 | |
| 413 | @Override |
| 414 | public void entryAdded(MatchActionOperations value) { |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 415 | try { |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 416 | log.trace("MatchAction Installer receiving set: {}", value); |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 417 | installationWorkQueue.put(value); |
| 418 | } catch (InterruptedException e) { |
| 419 | log.warn("Error adding to installer work queue: {}", |
| 420 | e.getMessage()); |
| 421 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void entryRemoved(MatchActionOperations value) { |
| 426 | // noop |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void entryUpdated(MatchActionOperations value) { |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 431 | try { |
Brian O'Connor | ab3e9e6 | 2014-09-09 23:45:55 -0700 | [diff] [blame] | 432 | log.trace("MatchAction Installer receiving set: {}", value); |
Brian O'Connor | 7e2d914 | 2014-09-02 17:24:27 -0700 | [diff] [blame] | 433 | installationWorkQueue.put(value); |
| 434 | } catch (InterruptedException e) { |
| 435 | log.warn("Error adding to installer work queue: {}", |
| 436 | e.getMessage()); |
| 437 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 441 | @Override |
| 442 | public boolean addMatchAction(MatchAction matchAction) { |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public Set<MatchAction> getMatchActions() { |
Ray Milkey | 80dc8c9 | 2014-09-03 13:49:46 -0700 | [diff] [blame] | 448 | return new HashSet<>(matchActionMap.values()); |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public boolean executeOperations(final MatchActionOperations operations) { |
| 453 | installMatchActionOperations(operations); |
Ray Milkey | 80dc8c9 | 2014-09-03 13:49:46 -0700 | [diff] [blame] | 454 | // TODO how to generate an actual error response here |
| 455 | return true; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public void setConflictDetectionPolicy(ConflictDetectionPolicy policy) { |
| 460 | // TODO Auto-generated method stub |
| 461 | |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public ConflictDetectionPolicy getConflictDetectionPolicy() { |
| 466 | // TODO Auto-generated method stub |
| 467 | return null; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public void addEventListener(EventListener listener) { |
| 472 | // TODO Auto-generated method stub |
| 473 | |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void removeEventListener(EventListener listener) { |
| 478 | // TODO Auto-generated method stub |
| 479 | |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public IdGenerator<MatchActionId> getMatchActionIdGenerator() { |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 484 | return matchActionIdGenerator; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public IdGenerator<MatchActionOperationsId> getMatchActionOperationsIdGenerator() { |
Ray Milkey | 626a2b1 | 2014-09-02 11:19:06 -0700 | [diff] [blame] | 489 | return matchActionOperationsIdGenerator; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | } |