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