blob: b1bb06793e5a8105eb184455348cf4bba75128b3 [file] [log] [blame]
Toshio Koidea03915e2014-07-01 18:39:52 -07001package net.onrc.onos.core.flowmanager;
2
Toshio Koide03eba332014-08-26 10:46:55 -07003import java.util.ArrayList;
Toshio Koide84648822014-08-21 02:07:56 -07004import java.util.Arrays;
5import java.util.Collection;
Toshio Koide03eba332014-08-26 10:46:55 -07006import java.util.HashMap;
Toshio Koide84648822014-08-21 02:07:56 -07007import java.util.List;
Toshio Koide03eba332014-08-26 10:46:55 -07008import java.util.Map;
Toshio Koide84648822014-08-21 02:07:56 -07009
Toshio Koide03eba332014-08-26 10:46:55 -070010import net.floodlightcontroller.core.module.FloodlightModuleContext;
11import net.floodlightcontroller.core.module.FloodlightModuleException;
12import net.floodlightcontroller.core.module.IFloodlightModule;
13import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart68e8dd62014-08-26 18:06:23 -070014import net.floodlightcontroller.restserver.IRestApiService;
Toshio Koidea03915e2014-07-01 18:39:52 -070015import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy;
Toshio Koideb8cea262014-08-12 18:45:46 -070016import net.onrc.onos.api.flowmanager.Flow;
Toshio Koidefc5acc72014-08-12 18:45:46 -070017import net.onrc.onos.api.flowmanager.FlowBatchHandle;
Toshio Koide7db3ce92014-08-27 16:57:43 -070018import net.onrc.onos.api.flowmanager.FlowBatchId;
Toshio Koide4ea84192014-07-31 12:10:12 -070019import net.onrc.onos.api.flowmanager.FlowBatchOperation;
Toshio Koide025a9152014-07-21 11:00:34 -070020import net.onrc.onos.api.flowmanager.FlowId;
Toshio Koide03eba332014-08-26 10:46:55 -070021import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService;
Toshio Koideb8cea262014-08-12 18:45:46 -070022import net.onrc.onos.api.flowmanager.FlowManagerListener;
Toshio Koide7db3ce92014-08-27 16:57:43 -070023import net.onrc.onos.core.datagrid.ISharedCollectionsService;
Jonathan Hart68e8dd62014-08-26 18:06:23 -070024import net.onrc.onos.core.flowmanager.web.FlowManagerWebRoutable;
Toshio Koide77ec9982014-08-27 16:57:43 -070025import net.onrc.onos.core.matchaction.MatchActionFloodlightService;
Toshio Koide77ec9982014-08-27 16:57:43 -070026import net.onrc.onos.core.matchaction.MatchActionService;
Toshio Koide03eba332014-08-26 10:46:55 -070027import net.onrc.onos.core.registry.IControllerRegistryService;
Toshio Koide84648822014-08-21 02:07:56 -070028import net.onrc.onos.core.util.IdBlockAllocator;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070029import net.onrc.onos.core.util.IdGenerator;
Toshio Koidea03915e2014-07-01 18:39:52 -070030
31/**
Toshio Koide7894ca02014-08-15 14:30:13 -070032 * 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 Koidea03915e2014-07-01 18:39:52 -070034 * <p>
35 * TODO: Make all methods thread-safe
36 */
Toshio Koide03eba332014-08-26 10:46:55 -070037public class FlowManagerModule implements FlowManagerFloodlightService, IFloodlightModule {
Toshio Koided46b66d2014-07-21 10:27:27 -070038 private ConflictDetectionPolicy conflictDetectionPolicy;
Toshio Koide7db3ce92014-08-27 16:57:43 -070039 private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator;
40 private FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator;
Toshio Koide77ec9982014-08-27 16:57:43 -070041 private MatchActionService matchActionService;
Toshio Koide03eba332014-08-26 10:46:55 -070042 private IControllerRegistryService registryService;
Toshio Koide7db3ce92014-08-27 16:57:43 -070043 private ISharedCollectionsService sharedCollectionService;
44 private FlowMap flowMap;
45 private FlowBatchMap flowBatchMap;
Toshio Koided6dbb952014-08-22 14:29:04 -070046 private FlowEventDispatcher flowEventDispatcher;
Toshio Koideb0d86242014-08-28 16:05:30 -070047 private FlowBatchOperationExecutor flowBatchOperationExecutor;
Jonathan Hart68e8dd62014-08-26 18:06:23 -070048 private IRestApiService restApi;
Toshio Koided46b66d2014-07-21 10:27:27 -070049
Toshio Koide03eba332014-08-26 10:46:55 -070050 @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 Koide77ec9982014-08-27 16:57:43 -070069 MatchActionFloodlightService.class,
Toshio Koide7db3ce92014-08-27 16:57:43 -070070 ISharedCollectionsService.class,
Jonathan Hart68e8dd62014-08-26 18:06:23 -070071 IControllerRegistryService.class,
72 IRestApiService.class);
Toshio Koide03eba332014-08-26 10:46:55 -070073 }
74
75 @Override
76 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
Toshio Koide77ec9982014-08-27 16:57:43 -070077 matchActionService = context.getServiceImpl(MatchActionFloodlightService.class);
Toshio Koide03eba332014-08-26 10:46:55 -070078 registryService = context.getServiceImpl(IControllerRegistryService.class);
Toshio Koide7db3ce92014-08-27 16:57:43 -070079 sharedCollectionService = context.getServiceImpl(ISharedCollectionsService.class);
Jonathan Hart68e8dd62014-08-26 18:06:23 -070080 restApi = context.getServiceImpl(IRestApiService.class);
Toshio Koide03eba332014-08-26 10:46:55 -070081 }
82
83 @Override
84 public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
Toshio Koide03eba332014-08-26 10:46:55 -070085 IdBlockAllocator idBlockAllocator = registryService;
Toshio Koide7db3ce92014-08-27 16:57:43 -070086 flowIdGenerator =
Toshio Koide079d57c2014-08-21 18:03:58 -070087 new FlowIdGeneratorWithIdBlockAllocator(idBlockAllocator);
Toshio Koide7db3ce92014-08-27 16:57:43 -070088 flowBatchIdGenerator =
89 new FlowBatchIdGeneratorWithIdBlockAllocator(idBlockAllocator);
Toshio Koide03eba332014-08-26 10:46:55 -070090
Toshio Koide7db3ce92014-08-27 16:57:43 -070091 flowMap = new SharedFlowMap(sharedCollectionService);
92 flowBatchMap = new SharedFlowBatchMap(sharedCollectionService);
Toshio Koided6dbb952014-08-22 14:29:04 -070093 flowEventDispatcher =
94 new FlowEventDispatcher(flowMap, flowBatchMap, matchActionService);
95 flowEventDispatcher.start();
Toshio Koideb0d86242014-08-28 16:05:30 -070096 flowBatchOperationExecutor =
97 new FlowBatchOperationExecutor(matchActionService, flowMap, flowBatchMap);
98 flowBatchOperationExecutor.start();
Jonathan Hart68e8dd62014-08-26 18:06:23 -070099
100 restApi.addRestletRoutable(new FlowManagerWebRoutable());
Toshio Koide03eba332014-08-26 10:46:55 -0700101 }
102
103 /**
104 * Constructs FlowManagerModule.
105 */
106 public FlowManagerModule() {
107 this.conflictDetectionPolicy = ConflictDetectionPolicy.FREE;
Toshio Koided46b66d2014-07-21 10:27:27 -0700108 }
109
Toshio Koidea03915e2014-07-01 18:39:52 -0700110 @Override
Toshio Koidefc5acc72014-08-12 18:45:46 -0700111 public FlowBatchHandle addFlow(Flow flow) {
Toshio Koide4ea84192014-07-31 12:10:12 -0700112 FlowBatchOperation ops = new FlowBatchOperation();
113 ops.addAddFlowOperation(flow);
Toshio Koidea03915e2014-07-01 18:39:52 -0700114 return executeBatch(ops);
115 }
116
117 @Override
Toshio Koidefc5acc72014-08-12 18:45:46 -0700118 public FlowBatchHandle removeFlow(FlowId id) {
Toshio Koide4ea84192014-07-31 12:10:12 -0700119 FlowBatchOperation ops = new FlowBatchOperation();
120 ops.addRemoveFlowOperation(id);
Toshio Koidea03915e2014-07-01 18:39:52 -0700121 return executeBatch(ops);
122 }
123
124 @Override
Toshio Koidefad1cd52014-08-07 17:10:07 -0700125 public Flow getFlow(FlowId id) {
Toshio Koide7db3ce92014-08-27 16:57:43 -0700126 return flowMap.get(id);
Toshio Koidea03915e2014-07-01 18:39:52 -0700127 }
128
129 @Override
Toshio Koidefad1cd52014-08-07 17:10:07 -0700130 public Collection<Flow> getFlows() {
Toshio Koide7db3ce92014-08-27 16:57:43 -0700131 return flowMap.getAll();
Toshio Koidea03915e2014-07-01 18:39:52 -0700132 }
133
Toshio Koide84648822014-08-21 02:07:56 -0700134 /**
135 * Executes batch operation of Flow object asynchronously.
136 * <p>
137 * To track the execution result, use the returned FlowBatchHandle object.
138 * <p>
Toshio Koide7db3ce92014-08-27 16:57:43 -0700139 * This method just put the batch-operation object to the global flow batch
Toshio Koide84648822014-08-21 02:07:56 -0700140 * 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 Koide7db3ce92014-08-27 16:57:43 -0700146 * @return {@link FlowBatchHandle} object if succeeded, null otherwise
Toshio Koide84648822014-08-21 02:07:56 -0700147 */
Toshio Koidea03915e2014-07-01 18:39:52 -0700148 @Override
Toshio Koidefc5acc72014-08-12 18:45:46 -0700149 public FlowBatchHandle executeBatch(FlowBatchOperation ops) {
Toshio Koide7db3ce92014-08-27 16:57:43 -0700150 FlowBatchId id = flowBatchIdGenerator.getNewId();
151 flowBatchMap.put(id, ops);
152 return new FlowBatchHandleImpl(flowBatchMap, id);
Toshio Koidea03915e2014-07-01 18:39:52 -0700153 }
154
155 @Override
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -0700156 public IdGenerator<FlowId> getFlowIdGenerator() {
Toshio Koide079d57c2014-08-21 18:03:58 -0700157 return flowIdGenerator;
158 }
159
160 @Override
Toshio Koidea03915e2014-07-01 18:39:52 -0700161 public void setConflictDetectionPolicy(ConflictDetectionPolicy policy) {
Toshio Koided46b66d2014-07-21 10:27:27 -0700162 if (policy == ConflictDetectionPolicy.FREE) {
163 conflictDetectionPolicy = policy;
164 } else {
165 throw new UnsupportedOperationException(
166 policy.toString() + " is not supported.");
167 }
Toshio Koidea03915e2014-07-01 18:39:52 -0700168 }
169
170 @Override
171 public ConflictDetectionPolicy getConflictDetectionPolicy() {
Toshio Koided46b66d2014-07-21 10:27:27 -0700172 return conflictDetectionPolicy;
Toshio Koidea03915e2014-07-01 18:39:52 -0700173 }
174
175 @Override
Toshio Koideb8cea262014-08-12 18:45:46 -0700176 public void addListener(FlowManagerListener listener) {
Toshio Koided6dbb952014-08-22 14:29:04 -0700177 flowEventDispatcher.addListener(listener);
Toshio Koidea03915e2014-07-01 18:39:52 -0700178 }
179
180 @Override
Toshio Koideb8cea262014-08-12 18:45:46 -0700181 public void removeListener(FlowManagerListener listener) {
Toshio Koided6dbb952014-08-22 14:29:04 -0700182 flowEventDispatcher.removeListener(listener);
Toshio Koidea03915e2014-07-01 18:39:52 -0700183 }
Toshio Koidea03915e2014-07-01 18:39:52 -0700184}