Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Map; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 7 | |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 8 | import net.floodlightcontroller.core.FloodlightContext; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 9 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 10 | import net.floodlightcontroller.core.IOFMessageListener; |
| 11 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.IOFSwitchListener; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 13 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.flowprogrammer.web.FlowProgrammerWebRoutable; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.util.FlowEntryId; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 21 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 22 | import org.openflow.protocol.OFFlowRemoved; |
| 23 | import org.openflow.protocol.OFMessage; |
| 24 | import org.openflow.protocol.OFType; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 28 | /** |
| 29 | * FlowProgrammer is a module responsible to maintain flows installed to switches. |
| 30 | * FlowProgrammer consists of FlowPusher and FlowSynchronizer. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 31 | * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 32 | * flows between GraphDB and switches. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 33 | * 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 Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 38 | * |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 39 | * @author Brian |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 40 | */ |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 41 | public class FlowProgrammer implements IFloodlightModule, |
| 42 | IOFMessageListener, |
| 43 | IOFSwitchListener { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 44 | // flag to enable FlowSynchronizer |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 45 | private static final boolean ENABLE_FLOW_SYNC = false; |
Naoki Shiota | df051d4 | 2014-01-20 16:12:41 -0800 | [diff] [blame] | 46 | protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 47 | protected volatile IFloodlightProviderService floodlightProvider; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 48 | protected volatile IControllerRegistryService registryService; |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 49 | protected volatile IRestApiService restApi; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 50 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 51 | protected FlowPusher pusher; |
| 52 | private static final int NUM_PUSHER_THREAD = 1; |
| 53 | |
| 54 | protected FlowSynchronizer synchronizer; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 55 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 56 | public FlowProgrammer() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 57 | pusher = new FlowPusher(NUM_PUSHER_THREAD); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 58 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 59 | synchronizer = new FlowSynchronizer(); |
| 60 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 61 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 62 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 63 | @Override |
| 64 | public void init(FloodlightModuleContext context) |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 65 | 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 Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 70 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 71 | synchronizer.init(pusher); |
| 72 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 77 | restApi.addRestletRoutable(new FlowProgrammerWebRoutable()); |
| 78 | pusher.start(); |
| 79 | floodlightProvider.addOFMessageListener(OFType.FLOW_REMOVED, this); |
| 80 | floodlightProvider.addOFSwitchListener(this); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 85 | Collection<Class<? extends IFloodlightService>> l = |
| 86 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 87 | l.add(IFlowPusherService.class); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 88 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 89 | l.add(IFlowSyncService.class); |
| 90 | } |
| 91 | return l; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 96 | Map<Class<? extends IFloodlightService>, |
| 97 | IFloodlightService> m = |
| 98 | new HashMap<Class<? extends IFloodlightService>, |
| 99 | IFloodlightService>(); |
| 100 | m.put(IFlowPusherService.class, pusher); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 101 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 102 | m.put(IFlowSyncService.class, synchronizer); |
| 103 | } |
| 104 | return m; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 109 | 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'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 114 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 115 | |
| 116 | @Override |
| 117 | public String getName() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 118 | // TODO Auto-generated method stub |
| 119 | return "FlowProgrammer"; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 124 | // TODO Auto-generated method stub |
| 125 | return false; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 130 | // TODO Auto-generated method stub |
| 131 | return false; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 136 | 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'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 147 | return Command.CONTINUE; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void addedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 152 | log.debug("Switch added: {}", sw.getId()); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 153 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 154 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 155 | if (registryService.hasControl(sw.getId())) { |
| 156 | synchronizer.synchronize(sw); |
| 157 | } |
| 158 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void removedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 163 | log.debug("Switch removed: {}", sw.getId()); |
| 164 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 165 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 166 | synchronizer.interrupt(sw); |
| 167 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void switchPortChanged(Long switchId) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 172 | // TODO Auto-generated method stub |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 173 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 174 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 175 | } |