blob: 33c9a6a2034baac91d83e9c5c2ea7db20f74e70a [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'Connorea1efbe2013-11-25 22:57:43 -080023import net.onrc.onos.registry.controller.IControllerRegistryService;
Brian O'Connor8c166a72013-11-14 18:41:48 -080024
Naoki Shiotae3199732013-11-25 16:14:43 -080025/**
26 * FlowProgrammer is a module responsible to maintain flows installed to switches.
27 * FlowProgrammer consists of FlowPusher and FlowSynchronizer.
Naoki Shiotab485d412013-11-26 12:04:19 -080028 * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes
Naoki Shiotae3199732013-11-25 16:14:43 -080029 * flows between GraphDB and switches.
Naoki Shiotab485d412013-11-26 12:04:19 -080030 * FlowProgrammer also watch the event of addition/deletion of switches to
31 * start/stop synchronization. When a switch is added to network, FlowProgrammer
32 * immediately kicks synchronization to keep switch's flow table latest state.
33 * Adversely, when a switch is removed from network, FlowProgrammer immediately
34 * stops synchronization.
Naoki Shiotae3199732013-11-25 16:14:43 -080035 * @author Brian
36 *
37 */
Brian O'Connorea1efbe2013-11-25 22:57:43 -080038public class FlowProgrammer implements IFloodlightModule,
39 IOFMessageListener,
40 IOFSwitchListener {
Naoki Shiota81dbe302013-11-21 15:35:38 -080041 @SuppressWarnings("unused")
Naoki Shiotae3199732013-11-25 16:14:43 -080042 // flag to enable FlowSynchronizer
Brian O'Connore600f1d2013-11-25 23:09:18 -080043 private static final boolean enableFlowSync = false;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080044 protected static Logger log = LoggerFactory.getLogger(FlowProgrammer.class);
Brian O'Connor8c166a72013-11-14 18:41:48 -080045 protected volatile IFloodlightProviderService floodlightProvider;
Brian O'Connorea1efbe2013-11-25 22:57:43 -080046 protected volatile IControllerRegistryService registryService;
47
Brian O'Connor8c166a72013-11-14 18:41:48 -080048 protected FlowPusher pusher;
49 private static final int NUM_PUSHER_THREAD = 1;
50
51 protected FlowSynchronizer synchronizer;
Naoki Shiotae3199732013-11-25 16:14:43 -080052
Brian O'Connor8c166a72013-11-14 18:41:48 -080053 public FlowProgrammer() {
54 pusher = new FlowPusher(NUM_PUSHER_THREAD);
Naoki Shiota8739faa2013-11-18 17:00:25 -080055 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080056 synchronizer = new FlowSynchronizer();
Naoki Shiota8739faa2013-11-18 17:00:25 -080057 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080058 }
59
60 @Override
61 public void init(FloodlightModuleContext context)
62 throws FloodlightModuleException {
63 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Brian O'Connorea1efbe2013-11-25 22:57:43 -080064 registryService = context.getServiceImpl(IControllerRegistryService.class);
Naoki Shiotac1601d32013-11-20 10:47:34 -080065 pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null);
Naoki Shiota8739faa2013-11-18 17:00:25 -080066 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080067 synchronizer.init(pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080068 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080069 }
70
71 @Override
72 public void startUp(FloodlightModuleContext context) {
73 pusher.start();
Brian O'Connorea1efbe2013-11-25 22:57:43 -080074 floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this);
75 floodlightProvider.addOFSwitchListener(this);
Brian O'Connor8c166a72013-11-14 18:41:48 -080076 }
77
78 @Override
79 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
80 Collection<Class<? extends IFloodlightService>> l =
81 new ArrayList<Class<? extends IFloodlightService>>();
82 l.add(IFlowPusherService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080083 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080084 l.add(IFlowSyncService.class);
Naoki Shiota8739faa2013-11-18 17:00:25 -080085 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080086 return l;
87 }
88
89 @Override
90 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
91 Map<Class<? extends IFloodlightService>,
92 IFloodlightService> m =
93 new HashMap<Class<? extends IFloodlightService>,
94 IFloodlightService>();
95 m.put(IFlowPusherService.class, pusher);
Naoki Shiota8739faa2013-11-18 17:00:25 -080096 if (enableFlowSync) {
Brian O'Connore600f1d2013-11-25 23:09:18 -080097 m.put(IFlowSyncService.class, synchronizer);
Naoki Shiota8739faa2013-11-18 17:00:25 -080098 }
Brian O'Connor8c166a72013-11-14 18:41:48 -080099 return m;
100 }
101
102 @Override
103 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
104 Collection<Class<? extends IFloodlightService>> l =
105 new ArrayList<Class<? extends IFloodlightService>>();
106 l.add(IFloodlightProviderService.class);
107 return l;
108 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800109
110 @Override
111 public String getName() {
112 // TODO Auto-generated method stub
113 return "FlowProgrammer";
114 }
115
116 @Override
117 public boolean isCallbackOrderingPrereq(OFType type, String name) {
118 // TODO Auto-generated method stub
119 return false;
120 }
121
122 @Override
123 public boolean isCallbackOrderingPostreq(OFType type, String name) {
124 // TODO Auto-generated method stub
125 return false;
126 }
127
128 @Override
129 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
130 switch (msg.getType()) {
131 case FLOW_REMOVED:
132 OFFlowRemoved flowMsg = (OFFlowRemoved) msg;
133 log.debug("Got flow removed from "+ sw.getId() +": "+ flowMsg.getCookie());
134 break;
135 default:
136 break;
137 }
138
139 return Command.CONTINUE;
140 }
141
142 @Override
143 public void addedSwitch(IOFSwitch sw) {
144 log.debug("Switch added: {}", sw.getId());
145
Brian O'Connore600f1d2013-11-25 23:09:18 -0800146 if (enableFlowSync && registryService.hasControl(sw.getId())) {
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800147 synchronizer.synchronize(sw);
148 }
149 }
150
151 @Override
152 public void removedSwitch(IOFSwitch sw) {
153 log.debug("Switch removed: {}", sw.getId());
154
Brian O'Connore600f1d2013-11-25 23:09:18 -0800155 if (enableFlowSync) {
156 synchronizer.interrupt(sw);
157 }
Brian O'Connorea1efbe2013-11-25 22:57:43 -0800158 }
159
160 @Override
161 public void switchPortChanged(Long switchId) {
162 // TODO Auto-generated method stub
163 }
Brian O'Connor8c166a72013-11-14 18:41:48 -0800164
Brian O'Connor8c166a72013-11-14 18:41:48 -0800165}