blob: c5db2bad99b68917bfc52353bfcc8bcd055fc1e1 [file] [log] [blame]
Brian O'Connor8c166a72013-11-14 18:41:48 -08001package net.onrc.onos.ofcontroller.flowprogrammer;
2
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
8import org.openflow.protocol.OFFlowRemoved;
9import org.openflow.protocol.OFMessage;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080010import org.openflow.protocol.OFType;
11import org.slf4j.Logger;
12import org.slf4j.LoggerFactory;
13
Brian O'Connorea1efbe2013-11-25 22:57:43 -080014import net.floodlightcontroller.core.FloodlightContext;
Brian O'Connor8c166a72013-11-14 18:41:48 -080015import net.floodlightcontroller.core.IFloodlightProviderService;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080016import net.floodlightcontroller.core.IOFMessageListener;
17import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080018import net.floodlightcontroller.core.IOFSwitchListener;
Brian O'Connor8c166a72013-11-14 18:41:48 -080019import net.floodlightcontroller.core.module.FloodlightModuleContext;
20import net.floodlightcontroller.core.module.FloodlightModuleException;
21import net.floodlightcontroller.core.module.IFloodlightModule;
22import net.floodlightcontroller.core.module.IFloodlightService;
Brian O'Connorbae524b2013-11-26 15:00:26 -080023import net.onrc.onos.flow.IFlowManager;
24import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
25import net.onrc.onos.ofcontroller.util.FlowEntryId;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080026import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080027
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 * @author Brian
39 *
40 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080041public class FlowProgrammer implements IFloodlightModule,
42 IOFMessageListener,
43 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080044 @SuppressWarnings("unused")
Naoki Shiotae3199732013-11-25 16:14:43 -080045 // flag to enable FlowSynchronizer
Brian O'Connore600f1d2013-11-25 23:09:18 -080046 private static final boolean enableFlowSync = false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080047 protected static Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080048 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080049 protected volatile IControllerRegistryService registryService;
Brian O'Connorbae524b2013-11-26 15:00:26 -080050 protected volatile IFlowService flowManager;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080051
Brian O'Connor8c166a72013-11-14 18:41:48 -080052 protected FlowPusher pusher;
53 private static final int NUM_PUSHER_THREAD = 1;
54
55 protected FlowSynchronizer synchronizer;
Naoki Shiotae3199732013-11-25 16:14:43 -080056
Brian O'Connor8c166a72013-11-14 18:41:48 -080057 public FlowProgrammer() {
58 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080059 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080060 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080061 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080062 }
63
64 @Override
65 public void init(FloodlightModuleContext context)
66 throws FloodlightModuleException {
67 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080068 registryService = context.getServiceImpl(IControllerRegistryService.class);
Brian O'Connorbae524b2013-11-26 15:00:26 -080069 flowManager = context.getServiceImpl(IFlowService.class);
Naoki Shiotac1601d32013-11-20 10:47:34 -080070 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Naoki Shiota8739faa2013-11-18 17:00:25 -080071 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080072 synchronizer.init(pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080073 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080074 }
75
76 @Override
77 public void startUp(FloodlightModuleContext context) {
78 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080079 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() {
85 Collection<Class<? extends IFloodlightService>> l =
86 new ArrayList<Class<? extends IFloodlightService>>();
87 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080088 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080089 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080090 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080091 return l;
92 }
93
94 @Override
95 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
96 Map<Class<? extends IFloodlightService>,
97 IFloodlightService> m =
98 new HashMap<Class<? extends IFloodlightService>,
99 IFloodlightService>();
100 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800101 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -0800102 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800103 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800104 return m;
105 }
106
107 @Override
108 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
109 Collection<Class<? extends IFloodlightService>> l =
110 new ArrayList<Class<? extends IFloodlightService>>();
111 l.add(IFloodlightProviderService.class);
112 return l;
113 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800114
115 @Override
116 public String getName() {
117 // TODO Auto-generated method stub
118 return "FlowProgrammer";
119 }
120
121 @Override
122 public boolean isCallbackOrderingPrereq(OFType type, String name) {
123 // TODO Auto-generated method stub
124 return false;
125 }
126
127 @Override
128 public boolean isCallbackOrderingPostreq(OFType type, String name) {
129 // TODO Auto-generated method stub
130 return false;
131 }
132
133 @Override
134 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
135 switch (msg.getType()) {
136 case FLOW_REMOVED:
137 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
138 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
Brian O'Connorbae524b2013-11-26 15:00:26 -0800139 FlowEntryId id = new FlowEntryId(flowMsg.getCookie());
140 flowManager.flowEntryOnSwitchExpired(sw, id);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800141 break;
142 default:
143 break;
144 }
145
146 return Command.CONTINUE;
147 }
148
149 @Override
150 public void addedSwitch(IOFSwitch sw) {
151 log.debug("Switch added: {}", sw.getId());
152
Brian O'Connore600f1d2013-11-25 23:09:18 -0800153 if (enableFlowSync && registryService.hasControl(sw.getId())) {
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800154 synchronizer.synchronize(sw);
155 }
156 }
157
158 @Override
159 public void removedSwitch(IOFSwitch sw) {
160 log.debug("Switch removed: {}", sw.getId());
161
Brian O'Connore600f1d2013-11-25 23:09:18 -0800162 if (enableFlowSync) {
163 synchronizer.interrupt(sw);
164 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800165 }
166
167 @Override
168 public void switchPortChanged(Long switchId) {
169 // TODO Auto-generated method stub
170 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800171
Brian O'Connor8c166a72013-11-14 18:41:48 -0800172}