blob: 046d1300432f8e22bd7b742c2693a0311ab62143 [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 -070033public class FlowProgrammer implements IFloodlightModule,
Ray Milkey8e5170e2014-04-02 12:09:55 -070034 IOFSwitchListener {
Naoki Shiotae3199732013-11-25 16:14:43 -080035 // flag to enable FlowSynchronizer
Ray Milkey5c9f2db2014-04-09 10:31:21 -070036 private static final boolean ENABLE_FLOW_SYNC = false;
Naoki Shiotadf051d42014-01-20 16:12:41 -080037 protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080038 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080039 protected volatile IControllerRegistryService registryService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080040 protected volatile IRestApiService restApi;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080041
Brian O'Connor8c166a72013-11-14 18:41:48 -080042 protected FlowPusher pusher;
43 private static final int NUM_PUSHER_THREAD = 1;
44
45 protected FlowSynchronizer synchronizer;
Ray Milkey8e5170e2014-04-02 12:09:55 -070046
Brian O'Connor8c166a72013-11-14 18:41:48 -080047 public FlowProgrammer() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070048 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070049 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070050 synchronizer = new FlowSynchronizer();
51 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080052 }
Ray Milkey8e5170e2014-04-02 12:09:55 -070053
Brian O'Connor8c166a72013-11-14 18:41:48 -080054 @Override
55 public void init(FloodlightModuleContext context)
Ray Milkey8e5170e2014-04-02 12:09:55 -070056 throws FloodlightModuleException {
57 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
58 registryService = context.getServiceImpl(IControllerRegistryService.class);
59 restApi = context.getServiceImpl(IRestApiService.class);
60 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070061 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070062 synchronizer.init(pusher);
63 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080064 }
65
66 @Override
67 public void startUp(FloodlightModuleContext context) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070068 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
69 pusher.start();
Ray Milkey8e5170e2014-04-02 12:09:55 -070070 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080071 }
72
73 @Override
74 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070075 Collection<Class<? extends IFloodlightService>> l =
76 new ArrayList<Class<? extends IFloodlightService>>();
77 l.add(IFlowPusherService.class);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070078 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070079 l.add(IFlowSyncService.class);
80 }
81 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -080082 }
83
84 @Override
85 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070086 Map<Class<? extends IFloodlightService>,
87 IFloodlightService> m =
88 new HashMap<Class<? extends IFloodlightService>,
89 IFloodlightService>();
90 m.put(IFlowPusherService.class, pusher);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070091 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070092 m.put(IFlowSyncService.class, synchronizer);
93 }
94 return m;
Brian O'Connor8c166a72013-11-14 18:41:48 -080095 }
96
97 @Override
98 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070099 Collection<Class<? extends IFloodlightService>> l =
100 new ArrayList<Class<? extends IFloodlightService>>();
101 l.add(IFloodlightProviderService.class);
102 l.add(IRestApiService.class);
103 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -0800104 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800105
106 @Override
107 public String getName() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700108 // TODO Auto-generated method stub
109 return "FlowProgrammer";
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800110 }
111
112 @Override
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800113 public void addedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700114 log.debug("Switch added: {}", sw.getId());
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800115
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700116 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700117 if (registryService.hasControl(sw.getId())) {
118 synchronizer.synchronize(sw);
119 }
120 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800121 }
122
123 @Override
124 public void removedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700125 log.debug("Switch removed: {}", sw.getId());
126
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700127 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700128 synchronizer.interrupt(sw);
129 }
Naoki Shiota2c245b82014-07-08 17:08:22 -0700130 pusher.deleteQueue(sw, true);
Yuta HIGUCHIafadeda2014-07-24 17:11:07 -0700131 pusher.invalidate(sw);
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}