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 | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 8 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 9 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 10 | import net.floodlightcontroller.core.IOFSwitchListener; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 12 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 13 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightService; |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.flowprogrammer.web.FlowProgrammerWebRoutable; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 22 | /** |
| 23 | * FlowProgrammer is a module responsible to maintain flows installed to switches. |
| 24 | * FlowProgrammer consists of FlowPusher and FlowSynchronizer. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 25 | * FlowPusher manages the rate of installation, and FlowSynchronizer synchronizes |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 26 | * flows between GraphDB and switches. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 27 | * FlowProgrammer also watch the event of addition/deletion of switches to |
| 28 | * start/stop synchronization. When a switch is added to network, FlowProgrammer |
| 29 | * immediately kicks synchronization to keep switch's flow table latest state. |
| 30 | * Adversely, when a switch is removed from network, FlowProgrammer immediately |
| 31 | * stops synchronization. |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 32 | */ |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 33 | public class FlowProgrammer implements IFloodlightModule, |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 34 | IOFSwitchListener { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 35 | // flag to enable FlowSynchronizer |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 36 | private static final boolean ENABLE_FLOW_SYNC = false; |
Naoki Shiota | df051d4 | 2014-01-20 16:12:41 -0800 | [diff] [blame] | 37 | protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 38 | protected volatile IFloodlightProviderService floodlightProvider; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 39 | protected volatile IControllerRegistryService registryService; |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 40 | protected volatile IRestApiService restApi; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 41 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 42 | protected FlowPusher pusher; |
| 43 | private static final int NUM_PUSHER_THREAD = 1; |
| 44 | |
| 45 | protected FlowSynchronizer synchronizer; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 46 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 47 | public FlowProgrammer() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 48 | pusher = new FlowPusher(NUM_PUSHER_THREAD); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 49 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 50 | synchronizer = new FlowSynchronizer(); |
| 51 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 52 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 53 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 54 | @Override |
| 55 | public void init(FloodlightModuleContext context) |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 56 | throws FloodlightModuleException { |
| 57 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 58 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
| 59 | restApi = context.getServiceImpl(IRestApiService.class); |
| 60 | pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 61 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 62 | synchronizer.init(pusher); |
| 63 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 68 | restApi.addRestletRoutable(new FlowProgrammerWebRoutable()); |
| 69 | pusher.start(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 70 | floodlightProvider.addOFSwitchListener(this); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 75 | Collection<Class<? extends IFloodlightService>> l = |
| 76 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 77 | l.add(IFlowPusherService.class); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 78 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 79 | l.add(IFlowSyncService.class); |
| 80 | } |
| 81 | return l; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 86 | Map<Class<? extends IFloodlightService>, |
| 87 | IFloodlightService> m = |
| 88 | new HashMap<Class<? extends IFloodlightService>, |
| 89 | IFloodlightService>(); |
| 90 | m.put(IFlowPusherService.class, pusher); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 91 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 92 | m.put(IFlowSyncService.class, synchronizer); |
| 93 | } |
| 94 | return m; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 99 | Collection<Class<? extends IFloodlightService>> l = |
| 100 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 101 | l.add(IFloodlightProviderService.class); |
| 102 | l.add(IRestApiService.class); |
| 103 | return l; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 104 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public String getName() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 108 | // TODO Auto-generated method stub |
| 109 | return "FlowProgrammer"; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 113 | public void addedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 114 | log.debug("Switch added: {}", sw.getId()); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 115 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 116 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 117 | if (registryService.hasControl(sw.getId())) { |
| 118 | synchronizer.synchronize(sw); |
| 119 | } |
| 120 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void removedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 125 | log.debug("Switch removed: {}", sw.getId()); |
| 126 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 127 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 128 | synchronizer.interrupt(sw); |
| 129 | } |
Naoki Shiota | 2c245b8 | 2014-07-08 17:08:22 -0700 | [diff] [blame] | 130 | pusher.deleteQueue(sw, true); |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 131 | pusher.invalidate(sw); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void switchPortChanged(Long switchId) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 136 | // TODO Auto-generated method stub |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 137 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 138 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 139 | } |