blob: 8a3739df8c04f493b55b8b7d5ffca15b7d729475 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.flowprogrammer;
Brian O'Connor8c166a72013-11-14 18:41:48 -08002
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.HashMap;
6import java.util.Map;
Brian O'Connorea1efbe2013-11-25 22:57:43 -08007
Brian O'Connor8c166a72013-11-14 18:41:48 -08008import net.floodlightcontroller.core.IFloodlightProviderService;
Brian O'Connorea1efbe2013-11-25 22:57:43 -08009import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080010import net.floodlightcontroller.core.IOFSwitchListener;
Brian O'Connor8c166a72013-11-14 18:41:48 -080011import net.floodlightcontroller.core.module.FloodlightModuleContext;
12import net.floodlightcontroller.core.module.FloodlightModuleException;
13import net.floodlightcontroller.core.module.IFloodlightModule;
14import net.floodlightcontroller.core.module.IFloodlightService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080015import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hart23701d12014-04-03 10:45:48 -070016import net.onrc.onos.core.flowprogrammer.web.FlowProgrammerWebRoutable;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070017import net.onrc.onos.core.registry.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080018
Jonathan Harta99ec672014-04-03 11:30:34 -070019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
Naoki Shiotae3199732013-11-25 16:14:43 -080022/**
23 * FlowProgrammer is a module responsible to maintain flows installed to switches.
24 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
Naoki Shiotab485d412013-11-26 12:04:19 -080025 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes
Naoki Shiotae3199732013-11-25 16:14:43 -080026 * flows between GraphDB and switches.
Naoki Shiotab485d412013-11-26 12:04:19 -080027 * FlowProgrammer also watch the event of addition/deletion of switches to
28 * start/stop synchronization. When a switch is added to network, FlowProgrammer
29 * immediately kicks synchronization to keep switch's flow table latest state.
30 * Adversely, when a switch is removed from network, FlowProgrammer immediately
31 * stops synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080032 *
Ray Milkey8e5170e2014-04-02 12:09:55 -070033 * @author Brian
Naoki Shiotae3199732013-11-25 16:14:43 -080034 */
Ray Milkey8e5170e2014-04-02 12:09:55 -070035public class FlowProgrammer implements IFloodlightModule,
Ray Milkey8e5170e2014-04-02 12:09:55 -070036 IOFSwitchListener {
Naoki Shiotae3199732013-11-25 16:14:43 -080037 // flag to enable FlowSynchronizer
Ray Milkey5c9f2db2014-04-09 10:31:21 -070038 private static final boolean ENABLE_FLOW_SYNC = false;
Naoki Shiotadf051d42014-01-20 16:12:41 -080039 protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080040 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080041 protected volatile IControllerRegistryService registryService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080042 protected volatile IRestApiService restApi;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080043
Brian O'Connor8c166a72013-11-14 18:41:48 -080044 protected FlowPusher pusher;
45 private static final int NUM_PUSHER_THREAD = 1;
46
47 protected FlowSynchronizer synchronizer;
Ray Milkey8e5170e2014-04-02 12:09:55 -070048
Brian O'Connor8c166a72013-11-14 18:41:48 -080049 public FlowProgrammer() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070050 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070051 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070052 synchronizer = new FlowSynchronizer();
53 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080054 }
Ray Milkey8e5170e2014-04-02 12:09:55 -070055
Brian O'Connor8c166a72013-11-14 18:41:48 -080056 @Override
57 public void init(FloodlightModuleContext context)
Ray Milkey8e5170e2014-04-02 12:09:55 -070058 throws FloodlightModuleException {
59 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
60 registryService = context.getServiceImpl(IControllerRegistryService.class);
61 restApi = context.getServiceImpl(IRestApiService.class);
62 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070063 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070064 synchronizer.init(pusher);
65 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080066 }
67
68 @Override
69 public void startUp(FloodlightModuleContext context) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070070 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
71 pusher.start();
Ray Milkey8e5170e2014-04-02 12:09:55 -070072 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080073 }
74
75 @Override
76 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070077 Collection<Class<? extends IFloodlightService>> l =
78 new ArrayList<Class<? extends IFloodlightService>>();
79 l.add(IFlowPusherService.class);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070080 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070081 l.add(IFlowSyncService.class);
82 }
83 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -080084 }
85
86 @Override
87 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070088 Map<Class<? extends IFloodlightService>,
89 IFloodlightService> m =
90 new HashMap<Class<? extends IFloodlightService>,
91 IFloodlightService>();
92 m.put(IFlowPusherService.class, pusher);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070093 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070094 m.put(IFlowSyncService.class, synchronizer);
95 }
96 return m;
Brian O'Connor8c166a72013-11-14 18:41:48 -080097 }
98
99 @Override
100 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700101 Collection<Class<? extends IFloodlightService>> l =
102 new ArrayList<Class<? extends IFloodlightService>>();
103 l.add(IFloodlightProviderService.class);
104 l.add(IRestApiService.class);
105 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -0800106 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800107
108 @Override
109 public String getName() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700110 // TODO Auto-generated method stub
111 return "FlowProgrammer";
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800112 }
113
114 @Override
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800115 public void addedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700116 log.debug("Switch added: {}", sw.getId());
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800117
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700118 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700119 if (registryService.hasControl(sw.getId())) {
120 synchronizer.synchronize(sw);
121 }
122 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800123 }
124
125 @Override
126 public void removedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700127 log.debug("Switch removed: {}", sw.getId());
128
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700129 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700130 synchronizer.interrupt(sw);
131 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800132 }
133
134 @Override
135 public void switchPortChanged(Long switchId) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700136 // TODO Auto-generated method stub
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800137 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700138
Brian O'Connor8c166a72013-11-14 18:41:48 -0800139}