blob: ff3268e1ee57314c97464fc2fc77d7a2b8b4f491 [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'Connorea1efbe2013-11-25 22:57:43 -08008import net.floodlightcontroller.core.FloodlightContext;
Brian O'Connor8c166a72013-11-14 18:41:48 -08009import net.floodlightcontroller.core.IFloodlightProviderService;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080010import net.floodlightcontroller.core.IOFMessageListener;
11import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080012import net.floodlightcontroller.core.IOFSwitchListener;
Brian O'Connor8c166a72013-11-14 18:41:48 -080013import net.floodlightcontroller.core.module.FloodlightModuleContext;
14import net.floodlightcontroller.core.module.FloodlightModuleException;
15import net.floodlightcontroller.core.module.IFloodlightModule;
16import net.floodlightcontroller.core.module.IFloodlightService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080017import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hart23701d12014-04-03 10:45:48 -070018import net.onrc.onos.core.flowprogrammer.web.FlowProgrammerWebRoutable;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070019import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart23701d12014-04-03 10:45:48 -070020import net.onrc.onos.core.util.FlowEntryId;
Brian O'Connor8c166a72013-11-14 18:41:48 -080021
Jonathan Harta99ec672014-04-03 11:30:34 -070022import org.openflow.protocol.OFFlowRemoved;
23import org.openflow.protocol.OFMessage;
24import org.openflow.protocol.OFType;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Naoki Shiotae3199732013-11-25 16:14:43 -080028/**
29 * FlowProgrammer is a module responsible to maintain flows installed to switches.
30 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
Naoki Shiotab485d412013-11-26 12:04:19 -080031 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes
Naoki Shiotae3199732013-11-25 16:14:43 -080032 * flows between GraphDB and switches.
Naoki Shiotab485d412013-11-26 12:04:19 -080033 * FlowProgrammer also watch the event of addition/deletion of switches to
34 * start/stop synchronization. When a switch is added to network, FlowProgrammer
35 * immediately kicks synchronization to keep switch's flow table latest state.
36 * Adversely, when a switch is removed from network, FlowProgrammer immediately
37 * stops synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080038 *
Ray Milkey8e5170e2014-04-02 12:09:55 -070039 * @author Brian
Naoki Shiotae3199732013-11-25 16:14:43 -080040 */
Ray Milkey8e5170e2014-04-02 12:09:55 -070041public class FlowProgrammer implements IFloodlightModule,
42 IOFMessageListener,
43 IOFSwitchListener {
Naoki Shiotae3199732013-11-25 16:14:43 -080044 // flag to enable FlowSynchronizer
Ray Milkey5c9f2db2014-04-09 10:31:21 -070045 private static final boolean ENABLE_FLOW_SYNC = false;
Naoki Shiotadf051d42014-01-20 16:12:41 -080046 protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080047 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080048 protected volatile IControllerRegistryService registryService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080049 protected volatile IRestApiService restApi;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080050
Brian O'Connor8c166a72013-11-14 18:41:48 -080051 protected FlowPusher pusher;
52 private static final int NUM_PUSHER_THREAD = 1;
53
54 protected FlowSynchronizer synchronizer;
Ray Milkey8e5170e2014-04-02 12:09:55 -070055
Brian O'Connor8c166a72013-11-14 18:41:48 -080056 public FlowProgrammer() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070057 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070058 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070059 synchronizer = new FlowSynchronizer();
60 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080061 }
Ray Milkey8e5170e2014-04-02 12:09:55 -070062
Brian O'Connor8c166a72013-11-14 18:41:48 -080063 @Override
64 public void init(FloodlightModuleContext context)
Ray Milkey8e5170e2014-04-02 12:09:55 -070065 throws FloodlightModuleException {
66 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
67 registryService = context.getServiceImpl(IControllerRegistryService.class);
68 restApi = context.getServiceImpl(IRestApiService.class);
69 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070070 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070071 synchronizer.init(pusher);
72 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080073 }
74
75 @Override
76 public void startUp(FloodlightModuleContext context) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070077 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
78 pusher.start();
79 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
80 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080081 }
82
83 @Override
84 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070085 Collection<Class<? extends IFloodlightService>> l =
86 new ArrayList<Class<? extends IFloodlightService>>();
87 l.add(IFlowPusherService.class);
Ray Milkey5c9f2db2014-04-09 10:31:21 -070088 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -070089 l.add(IFlowSyncService.class);
90 }
91 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -080092 }
93
94 @Override
95 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Ray Milkey8e5170e2014-04-02 12:09:55 -070096 Map<Class<? extends IFloodlightService>,
97 IFloodlightService> m =
98 new HashMap<Class<? extends IFloodlightService>,
99 IFloodlightService>();
100 m.put(IFlowPusherService.class, pusher);
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700101 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700102 m.put(IFlowSyncService.class, synchronizer);
103 }
104 return m;
Brian O'Connor8c166a72013-11-14 18:41:48 -0800105 }
106
107 @Override
108 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700109 Collection<Class<? extends IFloodlightService>> l =
110 new ArrayList<Class<? extends IFloodlightService>>();
111 l.add(IFloodlightProviderService.class);
112 l.add(IRestApiService.class);
113 return l;
Brian O'Connor8c166a72013-11-14 18:41:48 -0800114 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800115
116 @Override
117 public String getName() {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700118 // TODO Auto-generated method stub
119 return "FlowProgrammer";
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800120 }
121
122 @Override
123 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700124 // TODO Auto-generated method stub
125 return false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800126 }
127
128 @Override
129 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700130 // TODO Auto-generated method stub
131 return false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800132 }
133
134 @Override
135 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700136 switch (msg.getType()) {
137 case FLOW_REMOVED:
138 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
139 FlowEntryId id = new FlowEntryId(flowMsg.getCookie());
140 log.debug("Got flow entry removed from {}: {}", sw.getId(), id);
141 // TODO: Inform the Forwarding module that a flow has expired
142 break;
143 default:
144 break;
145 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800146
Ray Milkey8e5170e2014-04-02 12:09:55 -0700147 return Command.CONTINUE;
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800148 }
149
150 @Override
151 public void addedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700152 log.debug("Switch added: {}", sw.getId());
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800153
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700154 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700155 if (registryService.hasControl(sw.getId())) {
156 synchronizer.synchronize(sw);
157 }
158 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800159 }
160
161 @Override
162 public void removedSwitch(IOFSwitch sw) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700163 log.debug("Switch removed: {}", sw.getId());
164
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700165 if (ENABLE_FLOW_SYNC) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700166 synchronizer.interrupt(sw);
167 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800168 }
169
170 @Override
171 public void switchPortChanged(Long switchId) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700172 // TODO Auto-generated method stub
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800173 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700174
Brian O'Connor8c166a72013-11-14 18:41:48 -0800175}