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