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