blob: 69afc3dbbc557fd95b73ec3ea864158c6d67fdfe [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'Connorea1efbe2013-11-25 22:57:43 -080025import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080026
Naoki Shiotae3199732013-11-25 16:14:43 -080027/**
28 * FlowProgrammer is a module responsible to maintain flows installed to switches.
29 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
Naoki Shiotab485d412013-11-26 12:04:19 -080030 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes
Naoki Shiotae3199732013-11-25 16:14:43 -080031 * flows between GraphDB and switches.
Naoki Shiotab485d412013-11-26 12:04:19 -080032 * FlowProgrammer also watch the event of addition/deletion of switches to
33 * start/stop synchronization. When a switch is added to network, FlowProgrammer
34 * immediately kicks synchronization to keep switch's flow table latest state.
35 * Adversely, when a switch is removed from network, FlowProgrammer immediately
36 * stops synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080037 * @author Brian
38 *
39 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080040public class FlowProgrammer implements IFloodlightModule,
41 IOFMessageListener,
42 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080043 @SuppressWarnings("unused")
Naoki Shiotae3199732013-11-25 16:14:43 -080044 // flag to enable FlowSynchronizer
Brian O'Connore600f1d2013-11-25 23:09:18 -080045 private static final boolean enableFlowSync = false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080046 protected static 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;
Naoki Shiotae3199732013-11-25 16:14:43 -080055
Brian O'Connor8c166a72013-11-14 18:41:48 -080056 public FlowProgrammer() {
57 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080058 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080059 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080060 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080061 }
62
63 @Override
64 public void init(FloodlightModuleContext context)
65 throws FloodlightModuleException {
66 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080067 registryService = context.getServiceImpl(IControllerRegistryService.class);
Naoki Shiota2a35b442013-11-26 19:17:38 -080068 restApi = context.getServiceImpl(IRestApiService.class);
69
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) {
Naoki Shiota2a35b442013-11-26 19:17:38 -080078 restApi.addRestletRoutable(new FlowProgrammerWebRoutable());
Brian O'Connor8c166a72013-11-14 18:41:48 -080079 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080080 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
81 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080082 }
83
84 @Override
85 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
86 Collection<Class<? extends IFloodlightService>> l =
87 new ArrayList<Class<? extends IFloodlightService>>();
88 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080089 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080090 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080091 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080092 return l;
93 }
94
95 @Override
96 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
97 Map<Class<? extends IFloodlightService>,
98 IFloodlightService> m =
99 new HashMap<Class<? extends IFloodlightService>,
100 IFloodlightService>();
101 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800102 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -0800103 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -0800104 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800105 return m;
106 }
107
108 @Override
109 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
110 Collection<Class<? extends IFloodlightService>> l =
111 new ArrayList<Class<? extends IFloodlightService>>();
112 l.add(IFloodlightProviderService.class);
Naoki Shiota2a35b442013-11-26 19:17:38 -0800113 l.add(IRestApiService.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -0800114 return l;
115 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800116
117 @Override
118 public String getName() {
119 // TODO Auto-generated method stub
120 return "FlowProgrammer";
121 }
122
123 @Override
124 public boolean isCallbackOrderingPrereq(OFType type, String name) {
125 // TODO Auto-generated method stub
126 return false;
127 }
128
129 @Override
130 public boolean isCallbackOrderingPostreq(OFType type, String name) {
131 // TODO Auto-generated method stub
132 return false;
133 }
134
135 @Override
136 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
137 switch (msg.getType()) {
138 case FLOW_REMOVED:
139 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
140 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
141 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}