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