blob: 0b8910788e4891ae9aa2cb3bfff58d34a6353111 [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'Connorc67f9fa2014-08-07 18:17:46 -070010import net.floodlightcontroller.core.IOFSwitch.PortChangeType;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080011import net.floodlightcontroller.core.IOFSwitchListener;
Brian O'Connor8c166a72013-11-14 18:41:48 -080012import net.floodlightcontroller.core.module.FloodlightModuleContext;
13import net.floodlightcontroller.core.module.FloodlightModuleException;
14import net.floodlightcontroller.core.module.IFloodlightModule;
15import net.floodlightcontroller.core.module.IFloodlightService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080016import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hart23701d12014-04-03 10:45:48 -070017import net.onrc.onos.core.flowprogrammer.web.FlowProgrammerWebRoutable;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart5302b6c2014-08-13 15:57:59 -070019import net.onrc.onos.core.util.Dpid;
Brian O'Connor8c166a72013-11-14 18:41:48 -080020
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070021import org.projectfloodlight.openflow.protocol.OFPortDesc;
Jonathan Harta99ec672014-04-03 11:30:34 -070022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Naoki Shiotae3199732013-11-25 16:14:43 -080025/**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070026 * FlowProgrammer is a module responsible to maintain flows installed to
27 * switches. FlowProgrammer consists of FlowPusher and FlowSynchronizer.
28 * FlowPusher manages the rate of installation, and FlowSynchronizer
29 * synchronizes flows between GraphDB and switches. FlowProgrammer also watch
30 * the event of addition/deletion of switches to start/stop synchronization.
31 * When a switch is added to network, FlowProgrammer immediately kicks
32 * synchronization to keep switch's flow table latest state. Adversely, when a
33 * switch is removed from network, FlowProgrammer immediately stops
34 * synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080035 */
Ray Milkey8e5170e2014-04-02 12:09:55 -070036public class FlowProgrammer implements IFloodlightModule,
Ray Milkey8e5170e2014-04-02 12:09:55 -070037 IOFSwitchListener {
Naoki Shiotae3199732013-11-25 16:14:43 -080038 // flag to enable FlowSynchronizer
Ray Milkey5c9f2db2014-04-09 10:31:21 -070039 private static final boolean ENABLE_FLOW_SYNC = false;
Naoki Shiotadf051d42014-01-20 16:12:41 -080040 protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080041 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080042 protected volatile IControllerRegistryService registryService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080043 protected volatile IRestApiService restApi;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080044
Brian O'Connor8c166a72013-11-14 18:41:48 -080045 protected FlowPusher pusher;
46 private static final int NUM_PUSHER_THREAD = 1;
47
48 protected FlowSynchronizer synchronizer;
Ray Milkey8e5170e2014-04-02 12:09:55 -070049
Brian O'Connor8c166a72013-11-14 18:41:48 -080050 public FlowProgrammer() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070051 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070052 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070053 synchronizer = new FlowSynchronizer();
54 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080055 }
Ray Milkey8e5170e2014-04-02 12:09:55 -070056
Brian O'Connor8c166a72013-11-14 18:41:48 -080057 @Override
58 public void init(FloodlightModuleContext context)
Ray Milkey8e5170e2014-04-02 12:09:55 -070059 throws FloodlightModuleException {
60 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
61 registryService = context.getServiceImpl(IControllerRegistryService.class);
62 restApi = context.getServiceImpl(IRestApiService.class);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070063 pusher.init(context);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070064 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070065 synchronizer.init(pusher);
66 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080067 }
68
69 @Override
70 public void startUp(FloodlightModuleContext context) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070071 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
72 pusher.start();
Ray Milkey8e5170e2014-04-02 12:09:55 -070073 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080074 }
75
76 @Override
77 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070078 Collection<Class<? extends IFloodlightService>> l =
79 new ArrayList<Class<? extends IFloodlightService>>();
80 l.add(IFlowPusherService.class);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070081 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070082 l.add(IFlowSyncService.class);
83 }
84 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -080085 }
86
87 @Override
88 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070089 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
Ray Milkey8e5170e2014-04-02 12:09:55 -070090 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
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700108 // ***********************
109 // IOFSwitchListener
110 // ***********************
111
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800112 @Override
113 public String getName() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700114 return "FlowProgrammer";
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800115 }
116
117 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700118 public void switchActivatedMaster(long swId) {
119 IOFSwitch sw = floodlightProvider.getSwitch(swId);
120 if (sw == null) {
121 log.warn("Added switch not available {} ", swId);
122 return;
123 }
124 log.debug("Switch added: {}", swId);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800125
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700126 if (ENABLE_FLOW_SYNC) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700127 if (registryService.hasControl(swId)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700128 synchronizer.synchronize(sw);
129 }
130 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800131 }
132
133 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700134 public void switchActivatedEqual(long swId) {
135 // TODO Auto-generated method stub
136
137 }
138
139 @Override
140 public void switchMasterToEqual(long swId) {
141 // TODO Auto-generated method stub
142
143 }
144
145 @Override
146 public void switchEqualToMaster(long swId) {
147 // for now treat as switchActivatedMaster
148 switchActivatedMaster(swId);
149 }
150
151 @Override
152 public void switchDisconnected(long swId) {
Jonathan Hart5302b6c2014-08-13 15:57:59 -0700153 Dpid dpid = new Dpid(swId);
154 log.debug("Switch removed: {}", dpid);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700155
Jonathan Hart5302b6c2014-08-13 15:57:59 -0700156 //if (ENABLE_FLOW_SYNC) {
157 //synchronizer.interrupt(sw);
158 //}
159 pusher.deleteQueue(dpid, true);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800160 }
161
162 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700163 public void switchPortChanged(long swId, OFPortDesc port, PortChangeType pct) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700164 // TODO Auto-generated method stub
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800165 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700166
Brian O'Connor8c166a72013-11-14 18:41:48 -0800167}