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