Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowmanager; |
| 2 | |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static com.google.common.base.Preconditions.checkState; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 5 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 6 | import java.util.ArrayList; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 7 | import java.util.Arrays; |
| 8 | import java.util.Collection; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 9 | import java.util.HashMap; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 10 | import java.util.List; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 12 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 13 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 17 | import net.onrc.onos.api.batchoperation.BatchOperationEntry; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 18 | import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy; |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 19 | import net.onrc.onos.api.flowmanager.Flow; |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 20 | import net.onrc.onos.api.flowmanager.FlowBatchHandle; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 21 | import net.onrc.onos.api.flowmanager.FlowBatchId; |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 22 | import net.onrc.onos.api.flowmanager.FlowBatchOperation; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 23 | import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator; |
Toshio Koide | 025a915 | 2014-07-21 11:00:34 -0700 | [diff] [blame] | 24 | import net.onrc.onos.api.flowmanager.FlowId; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 25 | import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService; |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 26 | import net.onrc.onos.api.flowmanager.FlowManagerListener; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.datagrid.ISharedCollectionsService; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.matchaction.MatchActionFloodlightService; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.matchaction.MatchActionId; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
| 31 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.matchaction.MatchActionOperationsId; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.matchaction.MatchActionService; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.util.IdBlockAllocator; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.util.IdGenerator; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Toshio Koide | 7894ca0 | 2014-08-15 14:30:13 -0700 | [diff] [blame] | 39 | * Manages a set of Flow objects, computes and maintains a set of Match-Action |
| 40 | * entries based on the Flow objects, and executes Match-Action plans. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 41 | * <p> |
| 42 | * TODO: Make all methods thread-safe |
| 43 | */ |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 44 | public class FlowManagerModule implements FlowManagerFloodlightService, IFloodlightModule { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 45 | private ConflictDetectionPolicy conflictDetectionPolicy; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 46 | private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator; |
| 47 | private FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 48 | private IdGenerator<MatchActionId> maIdGenerator; |
| 49 | private IdGenerator<MatchActionOperationsId> maoIdGenerator; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 50 | private MatchActionService matchActionService; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 51 | private IControllerRegistryService registryService; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 52 | private ISharedCollectionsService sharedCollectionService; |
| 53 | private FlowMap flowMap; |
| 54 | private FlowBatchMap flowBatchMap; |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 55 | private FlowEventDispatcher flowEventDispatcher; |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 56 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 59 | List<Class<? extends IFloodlightService>> services = |
| 60 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 61 | services.add(FlowManagerFloodlightService.class); |
| 62 | return services; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 67 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 68 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 69 | impls.put(FlowManagerFloodlightService.class, this); |
| 70 | return impls; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 75 | return Arrays.asList( |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 76 | MatchActionFloodlightService.class, |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 77 | ISharedCollectionsService.class, |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 78 | IControllerRegistryService.class); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 83 | matchActionService = context.getServiceImpl(MatchActionFloodlightService.class); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 84 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 85 | sharedCollectionService = context.getServiceImpl(ISharedCollectionsService.class); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 90 | IdBlockAllocator idBlockAllocator = registryService; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 91 | flowIdGenerator = |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 92 | new FlowIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 93 | flowBatchIdGenerator = |
| 94 | new FlowBatchIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 95 | maIdGenerator = matchActionService.getMatchActionIdGenerator(); |
| 96 | maoIdGenerator = matchActionService.getMatchActionOperationsIdGenerator(); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 97 | |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 98 | flowMap = new SharedFlowMap(sharedCollectionService); |
| 99 | flowBatchMap = new SharedFlowBatchMap(sharedCollectionService); |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 100 | flowEventDispatcher = |
| 101 | new FlowEventDispatcher(flowMap, flowBatchMap, matchActionService); |
| 102 | flowEventDispatcher.start(); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Constructs FlowManagerModule. |
| 107 | */ |
| 108 | public FlowManagerModule() { |
| 109 | this.conflictDetectionPolicy = ConflictDetectionPolicy.FREE; |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 112 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 113 | public FlowBatchHandle addFlow(Flow flow) { |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 114 | FlowBatchOperation ops = new FlowBatchOperation(); |
| 115 | ops.addAddFlowOperation(flow); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 116 | return executeBatch(ops); |
| 117 | } |
| 118 | |
| 119 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 120 | public FlowBatchHandle removeFlow(FlowId id) { |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 121 | FlowBatchOperation ops = new FlowBatchOperation(); |
| 122 | ops.addRemoveFlowOperation(id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 123 | return executeBatch(ops); |
| 124 | } |
| 125 | |
| 126 | @Override |
Toshio Koide | fad1cd5 | 2014-08-07 17:10:07 -0700 | [diff] [blame] | 127 | public Flow getFlow(FlowId id) { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 128 | return flowMap.get(id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
Toshio Koide | fad1cd5 | 2014-08-07 17:10:07 -0700 | [diff] [blame] | 132 | public Collection<Flow> getFlows() { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 133 | return flowMap.getAll(); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Executes batch operation of Flow object asynchronously. |
| 138 | * <p> |
| 139 | * To track the execution result, use the returned FlowBatchHandle object. |
| 140 | * <p> |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 141 | * This method just put the batch-operation object to the global flow batch |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 142 | * operation map with unique ID. The worker process for execution and |
| 143 | * installation will get the appended operation when it gets events from the |
| 144 | * map. This method returns a handler for obtaining the result of this |
| 145 | * operation, control the executing process, etc. |
| 146 | * |
| 147 | * @param ops flow operations to be executed |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 148 | * @return {@link FlowBatchHandle} object if succeeded, null otherwise |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 149 | */ |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 150 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 151 | public FlowBatchHandle executeBatch(FlowBatchOperation ops) { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 152 | FlowBatchId id = flowBatchIdGenerator.getNewId(); |
| 153 | flowBatchMap.put(id, ops); |
| 154 | return new FlowBatchHandleImpl(flowBatchMap, id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 158 | public IdGenerator<FlowId> getFlowIdGenerator() { |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 159 | return flowIdGenerator; |
| 160 | } |
| 161 | |
| 162 | @Override |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 163 | public void setConflictDetectionPolicy(ConflictDetectionPolicy policy) { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 164 | if (policy == ConflictDetectionPolicy.FREE) { |
| 165 | conflictDetectionPolicy = policy; |
| 166 | } else { |
| 167 | throw new UnsupportedOperationException( |
| 168 | policy.toString() + " is not supported."); |
| 169 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public ConflictDetectionPolicy getConflictDetectionPolicy() { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 174 | return conflictDetectionPolicy; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 178 | public void addListener(FlowManagerListener listener) { |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 179 | flowEventDispatcher.addListener(listener); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 183 | public void removeListener(FlowManagerListener listener) { |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 184 | flowEventDispatcher.removeListener(listener); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 185 | } |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 186 | |
| 187 | private MatchActionOperations createNewMatchActionOperations() { |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 188 | return new MatchActionOperations(maoIdGenerator.getNewId()); |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Generates the series of MatchActionOperations from the |
| 193 | * {@link FlowBatchOperation}. |
| 194 | * <p> |
| 195 | * Note: Currently supporting ADD operations only. |
| 196 | * <p> |
| 197 | * Note: Currently supporting PacketPathFlow and SingleDstTreeFlow only. |
| 198 | * |
| 199 | * @param op the {@link FlowBatchOperation} object |
| 200 | * @return the list of {@link MatchActionOperations} objects |
| 201 | */ |
| 202 | private List<MatchActionOperations> |
| 203 | generateMatchActionOperationsList(FlowBatchOperation op) { |
| 204 | MatchActionOperations firstOps = createNewMatchActionOperations(); |
| 205 | MatchActionOperations secondOps = createNewMatchActionOperations(); |
| 206 | |
| 207 | for (BatchOperationEntry<Operator, ?> e : op.getOperations()) { |
| 208 | if (e.getOperator() != FlowBatchOperation.Operator.ADD) { |
| 209 | throw new UnsupportedOperationException( |
| 210 | "FlowManager supports ADD operations only."); |
| 211 | } |
| 212 | if (!(e.getTarget() instanceof Flow)) { |
| 213 | throw new IllegalStateException( |
| 214 | "The target is not Flow object: " + e.getTarget()); |
| 215 | } |
| 216 | |
| 217 | Flow flow = (Flow) e.getTarget(); |
| 218 | List<MatchActionOperations> maOps = flow.compile( |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 219 | e.getOperator(), maIdGenerator, maoIdGenerator); |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 220 | checkNotNull(maOps, "Could not compile the flow: " + flow); |
| 221 | checkState(maOps.size() == 2, |
| 222 | "The flow generates unspported match-action operations."); |
| 223 | |
| 224 | for (MatchActionOperationEntry mae : maOps.get(0).getOperations()) { |
| 225 | checkState(mae.getOperator() == MatchActionOperations.Operator.ADD); |
| 226 | firstOps.addOperation(mae); |
| 227 | } |
| 228 | |
| 229 | for (MatchActionOperationEntry mae : maOps.get(1).getOperations()) { |
| 230 | checkState(mae.getOperator() == MatchActionOperations.Operator.ADD); |
| 231 | secondOps.addOperation(mae); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return Arrays.asList(firstOps, secondOps); |
| 236 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 237 | } |