Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowmanager; |
| 2 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 4 | import java.util.Arrays; |
| 5 | import java.util.Collection; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 6 | import java.util.HashMap; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 7 | import java.util.List; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 8 | import java.util.Map; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 9 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 10 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 11 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.restserver.IRestApiService; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 15 | import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy; |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 16 | import net.onrc.onos.api.flowmanager.Flow; |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 17 | import net.onrc.onos.api.flowmanager.FlowBatchHandle; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 18 | import net.onrc.onos.api.flowmanager.FlowBatchId; |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 19 | import net.onrc.onos.api.flowmanager.FlowBatchOperation; |
Toshio Koide | 025a915 | 2014-07-21 11:00:34 -0700 | [diff] [blame] | 20 | import net.onrc.onos.api.flowmanager.FlowId; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 21 | import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService; |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 22 | import net.onrc.onos.api.flowmanager.FlowManagerListener; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.datagrid.ISharedCollectionsService; |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.flowmanager.web.FlowManagerWebRoutable; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.matchaction.MatchActionFloodlightService; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.matchaction.MatchActionService; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.IdBlockAllocator; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.IdGenerator; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Toshio Koide | 7894ca0 | 2014-08-15 14:30:13 -0700 | [diff] [blame] | 32 | * Manages a set of Flow objects, computes and maintains a set of Match-Action |
| 33 | * entries based on the Flow objects, and executes Match-Action plans. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 34 | * <p> |
| 35 | * TODO: Make all methods thread-safe |
| 36 | */ |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 37 | public class FlowManagerModule implements FlowManagerFloodlightService, IFloodlightModule { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 38 | private ConflictDetectionPolicy conflictDetectionPolicy; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 39 | private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator; |
| 40 | private FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator; |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 41 | private MatchActionService matchActionService; |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 42 | private IControllerRegistryService registryService; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 43 | private ISharedCollectionsService sharedCollectionService; |
| 44 | private FlowMap flowMap; |
| 45 | private FlowBatchMap flowBatchMap; |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 46 | private FlowEventDispatcher flowEventDispatcher; |
Toshio Koide | b0d8624 | 2014-08-28 16:05:30 -0700 | [diff] [blame] | 47 | private FlowBatchOperationExecutor flowBatchOperationExecutor; |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 48 | private IRestApiService restApi; |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 49 | |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 50 | @Override |
| 51 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 52 | List<Class<? extends IFloodlightService>> services = |
| 53 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 54 | services.add(FlowManagerFloodlightService.class); |
| 55 | return services; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 60 | Map<Class<? extends IFloodlightService>, IFloodlightService> impls = |
| 61 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 62 | impls.put(FlowManagerFloodlightService.class, this); |
| 63 | return impls; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 68 | return Arrays.asList( |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 69 | MatchActionFloodlightService.class, |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 70 | ISharedCollectionsService.class, |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 71 | IControllerRegistryService.class, |
| 72 | IRestApiService.class); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Toshio Koide | 77ec998 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 77 | matchActionService = context.getServiceImpl(MatchActionFloodlightService.class); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 78 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 79 | sharedCollectionService = context.getServiceImpl(ISharedCollectionsService.class); |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 80 | restApi = context.getServiceImpl(IRestApiService.class); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 85 | IdBlockAllocator idBlockAllocator = registryService; |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 86 | flowIdGenerator = |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 87 | new FlowIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 88 | flowBatchIdGenerator = |
| 89 | new FlowBatchIdGeneratorWithIdBlockAllocator(idBlockAllocator); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 90 | |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 91 | flowMap = new SharedFlowMap(sharedCollectionService); |
| 92 | flowBatchMap = new SharedFlowBatchMap(sharedCollectionService); |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 93 | flowEventDispatcher = |
| 94 | new FlowEventDispatcher(flowMap, flowBatchMap, matchActionService); |
| 95 | flowEventDispatcher.start(); |
Toshio Koide | b0d8624 | 2014-08-28 16:05:30 -0700 | [diff] [blame] | 96 | flowBatchOperationExecutor = |
| 97 | new FlowBatchOperationExecutor(matchActionService, flowMap, flowBatchMap); |
| 98 | flowBatchOperationExecutor.start(); |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 99 | |
| 100 | restApi.addRestletRoutable(new FlowManagerWebRoutable()); |
Toshio Koide | 03eba33 | 2014-08-26 10:46:55 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Constructs FlowManagerModule. |
| 105 | */ |
| 106 | public FlowManagerModule() { |
| 107 | this.conflictDetectionPolicy = ConflictDetectionPolicy.FREE; |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 110 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 111 | public FlowBatchHandle addFlow(Flow flow) { |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 112 | FlowBatchOperation ops = new FlowBatchOperation(); |
| 113 | ops.addAddFlowOperation(flow); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 114 | return executeBatch(ops); |
| 115 | } |
| 116 | |
| 117 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 118 | public FlowBatchHandle removeFlow(FlowId id) { |
Toshio Koide | 4ea8419 | 2014-07-31 12:10:12 -0700 | [diff] [blame] | 119 | FlowBatchOperation ops = new FlowBatchOperation(); |
| 120 | ops.addRemoveFlowOperation(id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 121 | return executeBatch(ops); |
| 122 | } |
| 123 | |
| 124 | @Override |
Toshio Koide | fad1cd5 | 2014-08-07 17:10:07 -0700 | [diff] [blame] | 125 | public Flow getFlow(FlowId id) { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 126 | return flowMap.get(id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Toshio Koide | fad1cd5 | 2014-08-07 17:10:07 -0700 | [diff] [blame] | 130 | public Collection<Flow> getFlows() { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 131 | return flowMap.getAll(); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Executes batch operation of Flow object asynchronously. |
| 136 | * <p> |
| 137 | * To track the execution result, use the returned FlowBatchHandle object. |
| 138 | * <p> |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 139 | * 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] | 140 | * operation map with unique ID. The worker process for execution and |
| 141 | * installation will get the appended operation when it gets events from the |
| 142 | * map. This method returns a handler for obtaining the result of this |
| 143 | * operation, control the executing process, etc. |
| 144 | * |
| 145 | * @param ops flow operations to be executed |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 146 | * @return {@link FlowBatchHandle} object if succeeded, null otherwise |
Toshio Koide | 8464882 | 2014-08-21 02:07:56 -0700 | [diff] [blame] | 147 | */ |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 148 | @Override |
Toshio Koide | fc5acc7 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 149 | public FlowBatchHandle executeBatch(FlowBatchOperation ops) { |
Toshio Koide | 7db3ce9 | 2014-08-27 16:57:43 -0700 | [diff] [blame] | 150 | FlowBatchId id = flowBatchIdGenerator.getNewId(); |
| 151 | flowBatchMap.put(id, ops); |
| 152 | return new FlowBatchHandleImpl(flowBatchMap, id); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 156 | public IdGenerator<FlowId> getFlowIdGenerator() { |
Toshio Koide | 079d57c | 2014-08-21 18:03:58 -0700 | [diff] [blame] | 157 | return flowIdGenerator; |
| 158 | } |
| 159 | |
| 160 | @Override |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 161 | public void setConflictDetectionPolicy(ConflictDetectionPolicy policy) { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 162 | if (policy == ConflictDetectionPolicy.FREE) { |
| 163 | conflictDetectionPolicy = policy; |
| 164 | } else { |
| 165 | throw new UnsupportedOperationException( |
| 166 | policy.toString() + " is not supported."); |
| 167 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public ConflictDetectionPolicy getConflictDetectionPolicy() { |
Toshio Koide | d46b66d | 2014-07-21 10:27:27 -0700 | [diff] [blame] | 172 | return conflictDetectionPolicy; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 176 | public void addListener(FlowManagerListener listener) { |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 177 | flowEventDispatcher.addListener(listener); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Toshio Koide | b8cea26 | 2014-08-12 18:45:46 -0700 | [diff] [blame] | 181 | public void removeListener(FlowManagerListener listener) { |
Toshio Koide | d6dbb95 | 2014-08-22 14:29:04 -0700 | [diff] [blame] | 182 | flowEventDispatcher.removeListener(listener); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 183 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 184 | } |