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