blob: 4a6294707c26b633d0cdb78dc699a633abafdbd7 [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;
Brian O'Connor8c166a72013-11-14 18:41:48 -080019
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070020import org.projectfloodlight.openflow.protocol.OFPortDesc;
Jonathan Harta99ec672014-04-03 11:30:34 -070021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
Naoki Shiotae3199732013-11-25 16:14:43 -080024/**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070025 * FlowProgrammer is a module responsible to maintain flows installed to
26 * switches. FlowProgrammer consists of FlowPusher and FlowSynchronizer.
27 * FlowPusher manages the rate of installation, and FlowSynchronizer
28 * synchronizes flows between GraphDB and switches. FlowProgrammer also watch
29 * the event of addition/deletion of switches to start/stop synchronization.
30 * When a switch is added to network, FlowProgrammer immediately kicks
31 * synchronization to keep switch's flow table latest state. Adversely, when a
32 * switch is removed from network, FlowProgrammer immediately stops
33 * synchronization.
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);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070062 pusher.init(context);
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() {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070088 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
Ray Milkey8e5170e2014-04-02 12:09:55 -070089 new HashMap<Class<? extends IFloodlightService>,
90 IFloodlightService>();
91 m.put(IFlowPusherService.class, pusher);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070092 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070093 m.put(IFlowSyncService.class, synchronizer);
94 }
95 return m;
Brian O'Connor8c166a72013-11-14 18:41:48 -080096 }
97
98 @Override
99 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700100 Collection<Class<? extends IFloodlightService>> l =
101 new ArrayList<Class<? extends IFloodlightService>>();
102 l.add(IFloodlightProviderService.class);
103 l.add(IRestApiService.class);
104 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -0800105 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800106
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700107 // ***********************
108 // IOFSwitchListener
109 // ***********************
110
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800111 @Override
112 public String getName() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700113 return "FlowProgrammer";
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800114 }
115
116 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700117 public void switchActivatedMaster(long swId) {
118 IOFSwitch sw = floodlightProvider.getSwitch(swId);
119 if (sw == null) {
120 log.warn("Added switch not available {} ", swId);
121 return;
122 }
123 log.debug("Switch added: {}", swId);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800124
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700125 if (ENABLE_FLOW_SYNC) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700126 if (registryService.hasControl(swId)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700127 synchronizer.synchronize(sw);
128 }
129 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800130 }
131
132 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700133 public void switchActivatedEqual(long swId) {
134 // TODO Auto-generated method stub
135
136 }
137
138 @Override
139 public void switchMasterToEqual(long swId) {
140 // TODO Auto-generated method stub
141
142 }
143
144 @Override
145 public void switchEqualToMaster(long swId) {
146 // for now treat as switchActivatedMaster
147 switchActivatedMaster(swId);
148 }
149
150 @Override
151 public void switchDisconnected(long swId) {
152 IOFSwitch sw = floodlightProvider.getSwitch(swId);
153 if (sw == null) {
154 log.warn("Removed switch not available {} ", swId);
155 return;
156 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700157 log.debug("Switch removed: {}", sw.getId());
158
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700159 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700160 synchronizer.interrupt(sw);
161 }
Naoki Shiota2c245b82014-07-08 17:08:22 -0700162 pusher.deleteQueue(sw, true);
Yuta HIGUCHIafadeda2014-07-24 17:11:07 -0700163 pusher.invalidate(sw);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800164 }
165
166 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700167 public void switchPortChanged(long swId, OFPortDesc port, PortChangeType pct) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700168 // TODO Auto-generated method stub
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800169 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700170
Brian O'Connor8c166a72013-11-14 18:41:48 -0800171}