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