blob: f7d7b0e1d14139234abd3af213bc16c1ee9f8a04 [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;
Naoki Shiota2a35b442013-11-26 19:17:38 -080023import net.floodlightcontroller.restserver.IRestApiService;
24import net.onrc.onos.ofcontroller.flowprogrammer.web.FlowProgrammerWebRoutable;
Brian O'Connorbae524b2013-11-26 15:00:26 -080025import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
26import net.onrc.onos.ofcontroller.util.FlowEntryId;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080027import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080028
Naoki Shiotae3199732013-11-25 16:14:43 -080029/**
30 * FlowProgrammer is a module responsible to maintain flows installed to switches.
31 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
Naoki Shiotab485d412013-11-26 12:04:19 -080032 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes
Naoki Shiotae3199732013-11-25 16:14:43 -080033 * flows between GraphDB and switches.
Naoki Shiotab485d412013-11-26 12:04:19 -080034 * FlowProgrammer also watch the event of addition/deletion of switches to
35 * start/stop synchronization. When a switch is added to network, FlowProgrammer
36 * immediately kicks synchronization to keep switch's flow table latest state.
37 * Adversely, when a switch is removed from network, FlowProgrammer immediately
38 * stops synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080039 * @author Brian
40 *
41 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080042public class FlowProgrammer implements IFloodlightModule,
43 IOFMessageListener,
44 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080045 @SuppressWarnings("unused")
Naoki Shiotae3199732013-11-25 16:14:43 -080046 // flag to enable FlowSynchronizer
Brian O'Connor056f8262013-12-09 18:10:49 -080047 private static final boolean enableFlowSync = true;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080048 protected static Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080049 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080050 protected volatile IControllerRegistryService registryService;
Naoki Shiota2a35b442013-11-26 19:17:38 -080051 protected volatile IRestApiService restApi;
Brian O'Connorbae524b2013-11-26 15:00:26 -080052 protected volatile IFlowService flowManager;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080053
Brian O'Connor8c166a72013-11-14 18:41:48 -080054 protected FlowPusher pusher;
55 private static final int NUM_PUSHER_THREAD = 1;
56
57 protected FlowSynchronizer synchronizer;
Naoki Shiotae3199732013-11-25 16:14:43 -080058
Brian O'Connor8c166a72013-11-14 18:41:48 -080059 public FlowProgrammer() {
60 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080061 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080062 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080063 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080064 }
65
66 @Override
67 public void init(FloodlightModuleContext context)
68 throws FloodlightModuleException {
69 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080070 registryService = context.getServiceImpl(IControllerRegistryService.class);
Naoki Shiota2a35b442013-11-26 19:17:38 -080071 restApi = context.getServiceImpl(IRestApiService.class);
Brian O'Connorbae524b2013-11-26 15:00:26 -080072 flowManager = context.getServiceImpl(IFlowService.class);
Naoki Shiotac1601d32013-11-20 10:47:34 -080073 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Naoki Shiota8739faa2013-11-18 17:00:25 -080074 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080075 synchronizer.init(pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080076 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080077 }
78
79 @Override
80 public void startUp(FloodlightModuleContext context) {
Naoki Shiota2a35b442013-11-26 19:17:38 -080081 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
Brian O'Connor8c166a72013-11-14 18:41:48 -080082 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080083 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
84 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080085 }
86
87 @Override
88 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
89 Collection<Class<? extends IFloodlightService>> l =
90 new ArrayList<Class<? extends IFloodlightService>>();
91 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080092 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080093 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080094 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080095 return l;
96 }
97
98 @Override
99 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
100 Map<Class<? extends IFloodlightService>,
101 IFloodlightService> m =
102 new HashMap<Class<? extends IFloodlightService>,
103 IFloodlightService>();
104 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800105 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -0800106 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800107 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800108 return m;
109 }
110
111 @Override
112 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
113 Collection<Class<? extends IFloodlightService>> l =
114 new ArrayList<Class<? extends IFloodlightService>>();
115 l.add(IFloodlightProviderService.class);
Naoki Shiota2a35b442013-11-26 19:17:38 -0800116 l.add(IRestApiService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800117 return l;
118 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800119
120 @Override
121 public String getName() {
122 // TODO Auto-generated method stub
123 return "FlowProgrammer";
124 }
125
126 @Override
127 public boolean isCallbackOrderingPrereq(OFType type, String name) {
128 // TODO Auto-generated method stub
129 return false;
130 }
131
132 @Override
133 public boolean isCallbackOrderingPostreq(OFType type, String name) {
134 // TODO Auto-generated method stub
135 return false;
136 }
137
138 @Override
139 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
140 switch (msg.getType()) {
141 case FLOW_REMOVED:
142 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
143 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
Brian O'Connorbae524b2013-11-26 15:00:26 -0800144 FlowEntryId id = new FlowEntryId(flowMsg.getCookie());
145 flowManager.flowEntryOnSwitchExpired(sw, id);
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800146 break;
147 default:
148 break;
149 }
150
151 return Command.CONTINUE;
152 }
153
154 @Override
155 public void addedSwitch(IOFSwitch sw) {
156 log.debug("Switch added: {}", sw.getId());
157
Brian O'Connore600f1d2013-11-25 23:09:18 -0800158 if (enableFlowSync && registryService.hasControl(sw.getId())) {
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800159 synchronizer.synchronize(sw);
160 }
161 }
162
163 @Override
164 public void removedSwitch(IOFSwitch sw) {
165 log.debug("Switch removed: {}", sw.getId());
166
Brian O'Connore600f1d2013-11-25 23:09:18 -0800167 if (enableFlowSync) {
168 synchronizer.interrupt(sw);
169 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800170 }
171
172 @Override
173 public void switchPortChanged(Long switchId) {
174 // TODO Auto-generated method stub
175 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800176
Brian O'Connor8c166a72013-11-14 18:41:48 -0800177}