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 | * @author Brian |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 34 | */ |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 35 | public class FlowProgrammer implements IFloodlightModule, |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 36 | IOFSwitchListener { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 37 | // flag to enable FlowSynchronizer |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 38 | private static final boolean ENABLE_FLOW_SYNC = false; |
Naoki Shiota | df051d4 | 2014-01-20 16:12:41 -0800 | [diff] [blame] | 39 | protected static final Logger log = LoggerFactory.getLogger(FlowProgrammer.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 40 | protected volatile IFloodlightProviderService floodlightProvider; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 41 | protected volatile IControllerRegistryService registryService; |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 42 | protected volatile IRestApiService restApi; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 43 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 44 | protected FlowPusher pusher; |
| 45 | private static final int NUM_PUSHER_THREAD = 1; |
| 46 | |
| 47 | protected FlowSynchronizer synchronizer; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 48 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 49 | public FlowProgrammer() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 50 | pusher = new FlowPusher(NUM_PUSHER_THREAD); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 51 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 52 | synchronizer = new FlowSynchronizer(); |
| 53 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 54 | } |
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 | @Override |
| 57 | public void init(FloodlightModuleContext context) |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 58 | throws FloodlightModuleException { |
| 59 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 60 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
| 61 | restApi = context.getServiceImpl(IRestApiService.class); |
| 62 | pusher.init(null, context, floodlightProvider.getOFMessageFactory(), null); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 63 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 64 | synchronizer.init(pusher); |
| 65 | } |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void startUp(FloodlightModuleContext context) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 70 | restApi.addRestletRoutable(new FlowProgrammerWebRoutable()); |
| 71 | pusher.start(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 72 | floodlightProvider.addOFSwitchListener(this); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 77 | Collection<Class<? extends IFloodlightService>> l = |
| 78 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 79 | l.add(IFlowPusherService.class); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 80 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 81 | l.add(IFlowSyncService.class); |
| 82 | } |
| 83 | return l; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 88 | Map<Class<? extends IFloodlightService>, |
| 89 | IFloodlightService> m = |
| 90 | new HashMap<Class<? extends IFloodlightService>, |
| 91 | IFloodlightService>(); |
| 92 | m.put(IFlowPusherService.class, pusher); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 93 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 94 | m.put(IFlowSyncService.class, synchronizer); |
| 95 | } |
| 96 | return m; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 101 | Collection<Class<? extends IFloodlightService>> l = |
| 102 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 103 | l.add(IFloodlightProviderService.class); |
| 104 | l.add(IRestApiService.class); |
| 105 | return l; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 106 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public String getName() { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 110 | // TODO Auto-generated method stub |
| 111 | return "FlowProgrammer"; |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 115 | public void addedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 116 | log.debug("Switch added: {}", sw.getId()); |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 117 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 118 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 119 | if (registryService.hasControl(sw.getId())) { |
| 120 | synchronizer.synchronize(sw); |
| 121 | } |
| 122 | } |
Brian O'Connor | ea1efbe | 2013-11-25 22:57:43 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removedSwitch(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 127 | log.debug("Switch removed: {}", sw.getId()); |
| 128 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 129 | if (ENABLE_FLOW_SYNC) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 130 | synchronizer.interrupt(sw); |
| 131 | } |
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 | } |