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