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