HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 6 | import java.util.LinkedList; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 7 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 9 | import java.util.Random; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 10 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 11 | import java.util.concurrent.LinkedBlockingQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 12 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 13 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 14 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 22 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 23 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 24 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
| 25 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 26 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.floodlightlistener.INetworkGraphService; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.util.*; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 32 | |
Naoki Shiota | 1a37ca1 | 2013-11-18 10:55:23 -0800 | [diff] [blame] | 33 | import org.openflow.protocol.OFType; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 37 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 38 | * Flow Manager class for handling the network flows. |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 39 | */ |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 40 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 41 | protected GraphDBOperation dbHandlerApi; |
| 42 | protected GraphDBOperation dbHandlerInner; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 43 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 44 | protected volatile IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 45 | protected volatile IDatagridService datagridService; |
| 46 | protected IRestApiService restApi; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 47 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 48 | protected FlowEventHandler flowEventHandler; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 49 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 50 | protected IFlowPusherService pusher; |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 51 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 52 | // Flow Entry ID generation state |
| 53 | private static Random randomGenerator = new Random(); |
| 54 | private static int nextFlowEntryIdPrefix = 0; |
| 55 | private static int nextFlowEntryIdSuffix = 0; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 56 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 57 | /** The logger. */ |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 58 | private final static Logger log = LoggerFactory.getLogger(FlowManager.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 59 | |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 60 | // The queue to write Flow Entries to the database |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 61 | private BlockingQueue<FlowPath> flowPathsToDatabaseQueue = |
| 62 | new LinkedBlockingQueue<FlowPath>(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 63 | FlowDatabaseWriter flowDatabaseWriter; |
| 64 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Initialize the Flow Manager. |
| 67 | * |
| 68 | * @param conf the Graph Database configuration string. |
| 69 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 70 | @Override |
| 71 | public void init(String conf) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 72 | dbHandlerApi = new GraphDBOperation(conf); |
| 73 | dbHandlerInner = new GraphDBOperation(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 74 | } |
| 75 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Shutdown the Flow Manager operation. |
| 78 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 79 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 80 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 81 | } |
| 82 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Shutdown the Flow Manager operation. |
| 85 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 86 | @Override |
| 87 | public void close() { |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 88 | datagridService.deregisterFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 89 | dbHandlerApi.close(); |
| 90 | dbHandlerInner.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 91 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 92 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Get the collection of offered module services. |
| 95 | * |
| 96 | * @return the collection of offered module services. |
| 97 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 98 | @Override |
| 99 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 100 | Collection<Class<? extends IFloodlightService>> l = |
| 101 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 102 | l.add(IFlowService.class); |
| 103 | return l; |
| 104 | } |
| 105 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Get the collection of implemented services. |
| 108 | * |
| 109 | * @return the collection of implemented services. |
| 110 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 113 | getServiceImpls() { |
| 114 | Map<Class<? extends IFloodlightService>, |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 115 | IFloodlightService> m = |
| 116 | new HashMap<Class<? extends IFloodlightService>, |
| 117 | IFloodlightService>(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 118 | m.put(IFlowService.class, this); |
| 119 | return m; |
| 120 | } |
| 121 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Get the collection of modules this module depends on. |
| 124 | * |
| 125 | * @return the collection of modules this module depends on. |
| 126 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 127 | @Override |
| 128 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 129 | getModuleDependencies() { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 130 | Collection<Class<? extends IFloodlightService>> l = |
| 131 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 132 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 133 | l.add(INetworkGraphService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 134 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 135 | l.add(IRestApiService.class); |
| 136 | return l; |
| 137 | } |
| 138 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Initialize the module. |
| 141 | * |
| 142 | * @param context the module context to use for the initialization. |
| 143 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 144 | @Override |
| 145 | public void init(FloodlightModuleContext context) |
| 146 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 147 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 148 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 149 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 150 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 939ca6b | 2013-12-03 12:35:37 -0800 | [diff] [blame] | 151 | pusher = context.getServiceImpl(IFlowPusherService.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 152 | |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 153 | this.init(""); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 154 | } |
| 155 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Get the next Flow Entry ID to use. |
| 158 | * |
| 159 | * @return the next Flow Entry ID to use. |
| 160 | */ |
Naoki Shiota | 4e77de9 | 2013-11-18 17:29:54 -0800 | [diff] [blame] | 161 | @Override |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 162 | public synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 163 | // |
| 164 | // Generate the next Flow Entry ID. |
| 165 | // NOTE: For now, the higher 32 bits are random, and |
| 166 | // the lower 32 bits are sequential. |
| 167 | // In the future, we need a better allocation mechanism. |
| 168 | // |
| 169 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 170 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 171 | nextFlowEntryIdSuffix = 0; |
| 172 | } else { |
| 173 | nextFlowEntryIdSuffix++; |
| 174 | } |
| 175 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 176 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 177 | return result; |
| 178 | } |
| 179 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Startup module operation. |
| 182 | * |
| 183 | * @param context the module context to use for the startup. |
| 184 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 185 | @Override |
| 186 | public void startUp(FloodlightModuleContext context) { |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 187 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 188 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 189 | // Initialize the Flow Entry ID generator |
| 190 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 191 | |
| 192 | // |
| 193 | // The thread to write to the database |
| 194 | // |
| 195 | flowDatabaseWriter = new FlowDatabaseWriter(this, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 196 | flowPathsToDatabaseQueue); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 197 | flowDatabaseWriter.start(); |
| 198 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 199 | // |
Pavlin Radoslavov | c9da532 | 2013-11-22 11:59:46 -0800 | [diff] [blame] | 200 | // The Flow Event Handler thread: |
| 201 | // - create |
| 202 | // - register with the Datagrid Service |
| 203 | // - startup |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 204 | // |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 205 | flowEventHandler = new FlowEventHandler(this, datagridService); |
| 206 | datagridService.registerFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 207 | flowEventHandler.start(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Add a flow. |
| 212 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 213 | * @param flowPath the Flow Path to install. |
| 214 | * @param flowId the return-by-reference Flow ID as assigned internally. |
| 215 | * @return true on success, otherwise false. |
| 216 | */ |
| 217 | @Override |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 218 | public boolean addFlow(FlowPath flowPath, FlowId flowId) { |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 219 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 220 | // NOTE: We need to explicitly initialize some of the state, |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 221 | // in case the application didn't do it. |
| 222 | // |
| 223 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 224 | if (flowEntry.flowEntrySwitchState() == |
| 225 | FlowEntrySwitchState.FE_SWITCH_UNKNOWN) { |
| 226 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
| 227 | } |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 228 | if (! flowEntry.isValidFlowId()) |
| 229 | flowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Pavlin Radoslavov | 67bf762 | 2013-12-04 12:28:23 -0800 | [diff] [blame] | 232 | if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 233 | datagridService.notificationSendFlowAdded(flowPath); |
| 234 | return true; |
| 235 | } |
| 236 | return false; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 240 | * Add a flow entry to the Network MAP. |
| 241 | * |
| 242 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 243 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 244 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 245 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 246 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 67bf762 | 2013-12-04 12:28:23 -0800 | [diff] [blame] | 247 | return FlowDatabaseOperation.addFlowEntry(dbHandlerInner, flowObj, |
| 248 | flowEntry); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 252 | * Delete a flow entry from the Network MAP. |
| 253 | * |
| 254 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 255 | * @param flowEntry the Flow Entry to delete. |
| 256 | * @return true on success, otherwise false. |
| 257 | */ |
| 258 | private boolean deleteFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 259 | return FlowDatabaseOperation.deleteFlowEntry(dbHandlerInner, |
| 260 | flowObj, flowEntry); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 264 | * Delete all previously added flows. |
| 265 | * |
| 266 | * @return true on success, otherwise false. |
| 267 | */ |
| 268 | @Override |
| 269 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 270 | if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 271 | datagridService.notificationSendAllFlowsRemoved(); |
| 272 | return true; |
| 273 | } |
| 274 | return false; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 278 | * Delete a previously added flow. |
| 279 | * |
| 280 | * @param flowId the Flow ID of the flow to delete. |
| 281 | * @return true on success, otherwise false. |
| 282 | */ |
| 283 | @Override |
| 284 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 285 | if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) { |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 286 | datagridService.notificationSendFlowRemoved(flowId); |
| 287 | return true; |
| 288 | } |
| 289 | return false; |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 293 | * Get a previously added flow. |
| 294 | * |
| 295 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 296 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 297 | */ |
| 298 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 299 | public FlowPath getFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 300 | return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 301 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 302 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Get all installed flows by all installers. |
| 305 | * |
| 306 | * @return the Flow Paths if found, otherwise null. |
| 307 | */ |
| 308 | @Override |
| 309 | public ArrayList<FlowPath> getAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 310 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Get all previously added flows by a specific installer for a given |
| 315 | * data path endpoints. |
| 316 | * |
| 317 | * @param installerId the Caller ID of the installer of the flow to get. |
| 318 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 319 | * @return the Flow Paths if found, otherwise null. |
| 320 | */ |
| 321 | @Override |
| 322 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 323 | DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 324 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi, installerId, |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 325 | dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Get all installed flows by all installers for given data path endpoints. |
| 330 | * |
| 331 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 332 | * @return the Flow Paths if found, otherwise null. |
| 333 | */ |
| 334 | @Override |
| 335 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 336 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi, |
| 337 | dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 341 | * Get summary of all installed flows by all installers in a given range. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 342 | * |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 343 | * @param flowId the Flow ID of the first flow in the flow range to get. |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 344 | * @param maxFlows the maximum number of flows to be returned. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 345 | * @return the Flow Paths if found, otherwise null. |
| 346 | */ |
| 347 | @Override |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 348 | public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, |
| 349 | int maxFlows) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 350 | return FlowDatabaseOperation.getAllFlowsSummary(dbHandlerApi, flowId, |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 351 | maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 355 | * Add and maintain a shortest-path flow. |
| 356 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 357 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 358 | * |
| 359 | * @param flowPath the Flow Path with the endpoints and the match |
| 360 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 361 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 362 | */ |
| 363 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 364 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 365 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 366 | // Don't do the shortest path computation here. |
| 367 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 368 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 369 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 370 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 371 | if (! addFlow(flowPath, flowId)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 372 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 373 | |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 374 | return (flowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 378 | * Get the collection of my switches. |
| 379 | * |
| 380 | * @return the collection of my switches. |
| 381 | */ |
| 382 | public Map<Long, IOFSwitch> getMySwitches() { |
| 383 | return floodlightProvider.getSwitches(); |
| 384 | } |
| 385 | |
| 386 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 387 | * Get the network topology. |
| 388 | * |
| 389 | * @return the network topology. |
| 390 | */ |
| 391 | public Topology getTopology() { |
| 392 | return flowEventHandler.getTopology(); |
| 393 | } |
| 394 | |
| 395 | /** |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 396 | * Inform the Flow Manager that a Flow Entry on switch expired. |
| 397 | * |
Pavlin Radoslavov | 3bd5ccf | 2013-11-26 15:10:21 -0800 | [diff] [blame] | 398 | * @param sw the switch the Flow Entry expired on. |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 399 | * @param flowEntryId the Flow Entry ID of the expired Flow Entry. |
| 400 | */ |
Brian O'Connor | 4f0b60c | 2013-11-26 15:00:04 -0800 | [diff] [blame] | 401 | public void flowEntryOnSwitchExpired(IOFSwitch sw, FlowEntryId flowEntryId) { |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 402 | // TODO: Not implemented yet |
| 403 | } |
| 404 | |
| 405 | /** |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 406 | * Inform the Flow Manager that a collection of Flow Entries have been |
| 407 | * pushed to a switch. |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 408 | * |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 409 | * @param entries the collection of <IOFSwitch, FlowEntry> pairs |
| 410 | * that have been pushed. |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 411 | */ |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 412 | public void flowEntriesPushedToSwitch( |
| 413 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 414 | |
| 415 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 416 | // Process all entries |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 417 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 418 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
| 419 | IOFSwitch sw = entry.left; |
| 420 | FlowEntry flowEntry = entry.right; |
| 421 | |
| 422 | // |
| 423 | // Mark the Flow Entry that it has been pushed to the switch |
| 424 | // |
| 425 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
| 426 | |
| 427 | // |
| 428 | // Write the Flow Entry to the Datagrid |
| 429 | // |
| 430 | switch (flowEntry.flowEntryUserState()) { |
| 431 | case FE_USER_ADD: |
| 432 | datagridService.notificationSendFlowEntryAdded(flowEntry); |
| 433 | break; |
| 434 | case FE_USER_MODIFY: |
| 435 | datagridService.notificationSendFlowEntryUpdated(flowEntry); |
| 436 | break; |
| 437 | case FE_USER_DELETE: |
| 438 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 439 | break; |
| 440 | } |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 445 | * Push modified Flow-related state as appropriate. |
| 446 | * |
| 447 | * @param modifiedFlowPaths the collection of modified Flow Paths. |
| 448 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 449 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 450 | void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths, |
| 451 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 452 | // |
| 453 | // Push the modified Flow state: |
| 454 | // - Flow Entries to switches and the datagrid |
| 455 | // - Flow Paths to the database |
| 456 | // |
| 457 | pushModifiedFlowEntriesToSwitches(modifiedFlowEntries); |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 458 | pushModifiedFlowPathsToDatabase(modifiedFlowPaths); |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 459 | cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries); |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 463 | * Push modified Flow Entries to switches. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 464 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 465 | * NOTE: Only the Flow Entries to switches controlled by this instance |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 466 | * are pushed. |
| 467 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 468 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 469 | */ |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 470 | private void pushModifiedFlowEntriesToSwitches( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 471 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 472 | if (modifiedFlowEntries.isEmpty()) |
| 473 | return; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 474 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 475 | List<Pair<IOFSwitch, FlowEntry>> entries = |
| 476 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 477 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 478 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 479 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 480 | // |
| 481 | // Create a collection of my Flow Entries to push |
| 482 | // |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 483 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 484 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 485 | if (mySwitch == null) |
| 486 | continue; |
| 487 | |
| 488 | log.debug("Pushing Flow Entry To Switch: {}", flowEntry.toString()); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 489 | entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry)); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 490 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame^] | 491 | |
| 492 | pusher.pushFlowEntries(entries); |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 493 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 494 | |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 495 | /** |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 496 | * Cleanup deleted Flow Entries from the datagrid. |
| 497 | * |
| 498 | * NOTE: We cleanup only the Flow Entries that are not for our switches. |
| 499 | * This is needed to handle the case a switch going down: |
| 500 | * It has no Master controller instance, hence no controller instance |
| 501 | * will cleanup its flow entries. |
| 502 | * This is sub-optimal: we need to elect a controller instance to handle |
| 503 | * the cleanup of such orphaned flow entries. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 504 | * |
| 505 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 506 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 507 | private void cleanupDeletedFlowEntriesFromDatagrid( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 508 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 509 | if (modifiedFlowEntries.isEmpty()) |
| 510 | return; |
| 511 | |
| 512 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 513 | |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 514 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 515 | // |
| 516 | // Process only Flow Entries that should be deleted and have |
| 517 | // a valid Flow Entry ID. |
| 518 | // |
Pavlin Radoslavov | 426a853 | 2013-12-02 17:32:21 -0800 | [diff] [blame] | 519 | if (! flowEntry.isValidFlowEntryId()) |
| 520 | continue; |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 521 | if (flowEntry.flowEntryUserState() != |
| 522 | FlowEntryUserState.FE_USER_DELETE) { |
| 523 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 526 | // |
| 527 | // NOTE: The deletion of Flow Entries for my switches is handled |
| 528 | // elsewhere. |
| 529 | // |
| 530 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 531 | if (mySwitch != null) |
| 532 | continue; |
| 533 | |
| 534 | log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry.toString()); |
| 535 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 536 | // |
| 537 | // Write the Flow Entry to the Datagrid |
| 538 | // |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 539 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 542 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 543 | /** |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 544 | * Class to implement writing to the database in a separate thread. |
| 545 | */ |
| 546 | class FlowDatabaseWriter extends Thread { |
| 547 | private FlowManager flowManager; |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 548 | private BlockingQueue<FlowPath> blockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 549 | |
| 550 | /** |
| 551 | * Constructor. |
| 552 | * |
| 553 | * @param flowManager the Flow Manager to use. |
| 554 | * @param blockingQueue the blocking queue to use. |
| 555 | */ |
| 556 | FlowDatabaseWriter(FlowManager flowManager, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 557 | BlockingQueue<FlowPath> blockingQueue) { |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 558 | this.flowManager = flowManager; |
| 559 | this.blockingQueue = blockingQueue; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Run the thread. |
| 564 | */ |
| 565 | @Override |
| 566 | public void run() { |
| 567 | // |
| 568 | // The main loop |
| 569 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 570 | Collection<FlowPath> collection = new LinkedList<FlowPath>(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 571 | try { |
| 572 | while (true) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 573 | FlowPath flowPath = blockingQueue.take(); |
| 574 | collection.add(flowPath); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 575 | blockingQueue.drainTo(collection); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 576 | flowManager.writeModifiedFlowPathsToDatabase(collection); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 577 | collection.clear(); |
| 578 | } |
| 579 | } catch (Exception exception) { |
| 580 | log.debug("Exception writing to the Database: ", exception); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 586 | * Push Flow Paths to the Network MAP. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 587 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 588 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 589 | * responsible for the first switch. This is to avoid database errors |
| 590 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 591 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 592 | * @param modifiedFlowPaths the collection of Flow Paths to push. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 593 | */ |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 594 | private void pushModifiedFlowPathsToDatabase( |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 595 | Collection<FlowPath> modifiedFlowPaths) { |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 596 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 597 | // We only add the Flow Paths to the Database Queue. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 598 | // The FlowDatabaseWriter thread is responsible for the actual writing. |
| 599 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 600 | flowPathsToDatabaseQueue.addAll(modifiedFlowPaths); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 604 | * Write Flow Paths to the Network MAP. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 605 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 606 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 607 | * responsible for the first switch. This is to avoid database errors |
| 608 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 609 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 610 | * @param modifiedFlowPaths the collection of Flow Paths to write. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 611 | */ |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 612 | private void writeModifiedFlowPathsToDatabase( |
| 613 | Collection<FlowPath> modifiedFlowPaths) { |
| 614 | if (modifiedFlowPaths.isEmpty()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 615 | return; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 616 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 617 | FlowId dummyFlowId = new FlowId(); |
| 618 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 619 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 620 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 621 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 622 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 623 | // Push the changes only on the instance responsible for the |
| 624 | // first switch. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 625 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 626 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 627 | IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value()); |
| 628 | if (mySrcSwitch == null) |
| 629 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 630 | |
| 631 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 632 | // Test whether all Flow Entries are valid |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 633 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 634 | boolean allValid = true; |
| 635 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 636 | if (flowEntry.flowEntryUserState() == |
| 637 | FlowEntryUserState.FE_USER_DELETE) { |
| 638 | continue; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 639 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 640 | if (! flowEntry.isValidFlowEntryId()) { |
| 641 | allValid = false; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | if (! allValid) |
| 646 | continue; |
| 647 | |
| 648 | log.debug("Pushing Flow Path To Database: {}", flowPath.toString()); |
| 649 | |
| 650 | // |
| 651 | // Write the Flow Path to the Network Map |
| 652 | // |
| 653 | try { |
Pavlin Radoslavov | 67bf762 | 2013-12-04 12:28:23 -0800 | [diff] [blame] | 654 | if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath, |
| 655 | dummyFlowId)) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 656 | String logMsg = "Cannot write to Network Map Flow Path " + |
| 657 | flowPath.flowId(); |
| 658 | log.error(logMsg); |
| 659 | } |
| 660 | } catch (Exception e) { |
| 661 | log.error("Exception writing Flow Path to Network MAP: ", e); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 664 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 665 | } |