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