HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 6 | import java.util.LinkedList; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 7 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 9 | import java.util.Random; |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 10 | import java.util.SortedMap; |
Pavlin Radoslavov | 85f39d3 | 2014-01-10 19:14:35 -0800 | [diff] [blame] | 11 | import java.util.TreeMap; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 13 | import java.util.concurrent.LinkedBlockingQueue; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 14 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 15 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 18 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 19 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 20 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.util.OFMessageDamper; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 23 | import net.onrc.onos.graph.DBOperation; |
| 24 | import net.onrc.onos.graph.GraphDBManager; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 25 | import net.onrc.onos.datagrid.IDatagridService; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 27 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService; |
Pavlin Radoslavov | c086266 | 2013-12-10 15:31:49 -0800 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.forwarding.IForwardingService; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.topology.Topology; |
Pavlin Radoslavov | 63e4260 | 2013-12-12 12:54:05 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 33 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 35 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
| 36 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 37 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 38 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 39 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
| 40 | import net.onrc.onos.ofcontroller.util.Pair; |
Pavlin Radoslavov | da8d723 | 2013-12-12 12:48:14 -0800 | [diff] [blame] | 41 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 42 | |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 43 | import com.thinkaurelius.titan.core.TitanException; |
Yuta HIGUCHI | 2d5ac52 | 2014-01-22 10:21:41 -0800 | [diff] [blame] | 44 | import com.esotericsoftware.kryo.Kryo; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 45 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 49 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 50 | * Flow Manager class for handling the network flows. |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 51 | */ |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 52 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 53 | |
Pavlin Radoslavov | db1f91c | 2014-01-08 16:04:17 -0800 | [diff] [blame] | 54 | private boolean enableOnrc2014MeasurementsFlows = true; |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 55 | |
yoshitomo | b292c62 | 2013-11-23 14:35:58 -0800 | [diff] [blame] | 56 | protected DBOperation dbHandlerApi; |
| 57 | protected DBOperation dbHandlerInner; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 58 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 59 | protected volatile IFloodlightProviderService floodlightProvider; |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 60 | protected volatile IDatagridService datagridService; |
| 61 | protected IRestApiService restApi; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 62 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 63 | protected FlowEventHandler flowEventHandler; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 64 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 65 | protected IFlowPusherService pusher; |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 66 | protected IForwardingService forwardingService; |
Pavlin Radoslavov | da8d723 | 2013-12-12 12:48:14 -0800 | [diff] [blame] | 67 | |
| 68 | private KryoFactory kryoFactory = new KryoFactory(); |
| 69 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 70 | // Flow Entry ID generation state |
| 71 | private static Random randomGenerator = new Random(); |
| 72 | private static int nextFlowEntryIdPrefix = 0; |
| 73 | private static int nextFlowEntryIdSuffix = 0; |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 74 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 75 | /** The logger. */ |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 76 | private final static Logger log = LoggerFactory.getLogger(FlowManager.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 77 | |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 78 | // The queue to write Flow Entries to the database |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 79 | private BlockingQueue<FlowPath> flowPathsToDatabaseQueue = |
| 80 | new LinkedBlockingQueue<FlowPath>(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 81 | FlowDatabaseWriter flowDatabaseWriter; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 82 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Initialize the Flow Manager. |
| 85 | * |
| 86 | * @param conf the Graph Database configuration string. |
| 87 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 88 | @Override |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 89 | public void init(final String dbStore, final String conf) { |
Yoshi Muroi | 5804ce9 | 2014-02-08 03:58:04 -0800 | [diff] [blame^] | 90 | dbHandlerApi = GraphDBManager.getDBOperation(); |
| 91 | dbHandlerInner = GraphDBManager.getDBOperation(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 92 | } |
| 93 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Shutdown the Flow Manager operation. |
| 96 | */ |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 97 | @Override |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 98 | protected void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 99 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 100 | } |
| 101 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Shutdown the Flow Manager operation. |
| 104 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public void close() { |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 107 | floodlightProvider.removeOFSwitchListener(flowEventHandler); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 108 | datagridService.deregisterFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 109 | dbHandlerApi.close(); |
| 110 | dbHandlerInner.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 111 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 112 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Get the collection of offered module services. |
| 115 | * |
| 116 | * @return the collection of offered module services. |
| 117 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 120 | Collection<Class<? extends IFloodlightService>> l = |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 121 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 122 | l.add(IFlowService.class); |
| 123 | return l; |
| 124 | } |
| 125 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Get the collection of implemented services. |
| 128 | * |
| 129 | * @return the collection of implemented services. |
| 130 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 131 | @Override |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 132 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 133 | getServiceImpls() { |
| 134 | Map<Class<? extends IFloodlightService>, |
Pavlin Radoslavov | e9a3ef9 | 2013-10-18 18:46:45 -0700 | [diff] [blame] | 135 | IFloodlightService> m = |
| 136 | new HashMap<Class<? extends IFloodlightService>, |
| 137 | IFloodlightService>(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 138 | m.put(IFlowService.class, this); |
| 139 | return m; |
| 140 | } |
| 141 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Get the collection of modules this module depends on. |
| 144 | * |
| 145 | * @return the collection of modules this module depends on. |
| 146 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 147 | @Override |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 148 | public Collection<Class<? extends IFloodlightService>> |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 149 | getModuleDependencies() { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 150 | Collection<Class<? extends IFloodlightService>> l = |
| 151 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 152 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 153 | l.add(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 154 | l.add(IRestApiService.class); |
Pavlin Radoslavov | c086266 | 2013-12-10 15:31:49 -0800 | [diff] [blame] | 155 | l.add(IFlowPusherService.class); |
Pavlin Radoslavov | 63c2d05 | 2013-12-18 18:17:55 -0800 | [diff] [blame] | 156 | // |
| 157 | // TODO: Comment-out the dependency on the IForwardingService, |
| 158 | // because it is an optional module. Apparently, adding the dependency |
| 159 | // here automatically enables the module. |
| 160 | // |
| 161 | // l.add(IForwardingService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 162 | return l; |
| 163 | } |
| 164 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Initialize the module. |
| 167 | * |
| 168 | * @param context the module context to use for the initialization. |
| 169 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 170 | @Override |
| 171 | public void init(FloodlightModuleContext context) |
| 172 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 173 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 174 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 175 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 176 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | 939ca6b | 2013-12-03 12:35:37 -0800 | [diff] [blame] | 177 | pusher = context.getServiceImpl(IFlowPusherService.class); |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 178 | forwardingService = context.getServiceImpl(IForwardingService.class); |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 179 | |
yoshi | 0fee3de | 2013-11-23 09:13:37 -0800 | [diff] [blame] | 180 | this.init("",""); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 181 | } |
| 182 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Get the next Flow Entry ID to use. |
| 185 | * |
| 186 | * @return the next Flow Entry ID to use. |
| 187 | */ |
Naoki Shiota | 4e77de9 | 2013-11-18 17:29:54 -0800 | [diff] [blame] | 188 | @Override |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 189 | public synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 190 | // |
| 191 | // Generate the next Flow Entry ID. |
| 192 | // NOTE: For now, the higher 32 bits are random, and |
| 193 | // the lower 32 bits are sequential. |
| 194 | // In the future, we need a better allocation mechanism. |
| 195 | // |
| 196 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 197 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 198 | nextFlowEntryIdSuffix = 0; |
| 199 | } else { |
| 200 | nextFlowEntryIdSuffix++; |
| 201 | } |
| 202 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 203 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 204 | return result; |
| 205 | } |
| 206 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Startup module operation. |
| 209 | * |
| 210 | * @param context the module context to use for the startup. |
| 211 | */ |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 212 | @Override |
| 213 | public void startUp(FloodlightModuleContext context) { |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 214 | restApi.addRestletRoutable(new FlowWebRoutable()); |
Pavlin Radoslavov | 50e532e | 2013-10-20 02:07:51 -0700 | [diff] [blame] | 215 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 216 | // Initialize the Flow Entry ID generator |
| 217 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 218 | |
| 219 | // |
| 220 | // The thread to write to the database |
| 221 | // |
| 222 | flowDatabaseWriter = new FlowDatabaseWriter(this, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 223 | flowPathsToDatabaseQueue); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 224 | flowDatabaseWriter.start(); |
| 225 | |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 226 | // |
Pavlin Radoslavov | c9da532 | 2013-11-22 11:59:46 -0800 | [diff] [blame] | 227 | // The Flow Event Handler thread: |
| 228 | // - create |
| 229 | // - register with the Datagrid Service |
| 230 | // - startup |
Pavlin Radoslavov | 6b79f2b | 2013-10-26 21:31:10 -0700 | [diff] [blame] | 231 | // |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 232 | flowEventHandler = new FlowEventHandler(this, datagridService); |
Pavlin Radoslavov | cc75716 | 2014-01-10 16:26:38 -0800 | [diff] [blame] | 233 | floodlightProvider.addOFSwitchListener(flowEventHandler); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 234 | datagridService.registerFlowEventHandlerService(flowEventHandler); |
Pavlin Radoslavov | 9a85902 | 2013-10-30 10:08:24 -0700 | [diff] [blame] | 235 | flowEventHandler.start(); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Add a flow. |
| 240 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 241 | * @param flowPath the Flow Path to install. |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 242 | * @return the Flow ID on success, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 243 | */ |
| 244 | @Override |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 245 | public FlowId addFlow(FlowPath flowPath) { |
| 246 | |
| 247 | // Allocate the Flow ID if necessary |
Pavlin Radoslavov | 892dd18 | 2013-12-05 23:33:15 -0800 | [diff] [blame] | 248 | if (! flowPath.isValidFlowId()) { |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 249 | long id = getNextFlowEntryId(); |
| 250 | flowPath.setFlowId(new FlowId(id)); |
| 251 | } |
| 252 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 253 | // |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 254 | // NOTE: We need to explicitly initialize some of the state, |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 255 | // in case the application didn't do it. |
| 256 | // |
| 257 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
Pavlin Radoslavov | 07d22b4 | 2013-12-15 16:33:33 -0800 | [diff] [blame] | 258 | // The Flow Entry switch state |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 259 | if (flowEntry.flowEntrySwitchState() == |
| 260 | FlowEntrySwitchState.FE_SWITCH_UNKNOWN) { |
| 261 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 262 | } |
Pavlin Radoslavov | 07d22b4 | 2013-12-15 16:33:33 -0800 | [diff] [blame] | 263 | // The Flow Entry ID |
| 264 | if (! flowEntry.isValidFlowEntryId()) { |
| 265 | long id = getNextFlowEntryId(); |
| 266 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 267 | } |
| 268 | // The Flow ID |
Pavlin Radoslavov | 1c24f22 | 2013-10-30 13:56:46 -0700 | [diff] [blame] | 269 | if (! flowEntry.isValidFlowId()) |
| 270 | flowEntry.setFlowId(new FlowId(flowPath.flowId().value())); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 271 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 272 | |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 273 | if (FlowDatabaseOperation.addFlow(dbHandlerApi, flowPath)) { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 274 | if (enableOnrc2014MeasurementsFlows) { |
Pavlin Radoslavov | 2194d11 | 2014-01-10 13:36:00 -0800 | [diff] [blame] | 275 | datagridService.notificationSendFlowIdAdded(flowPath.flowId(), |
| 276 | flowPath.dataPath().srcPort().dpid()); |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 277 | } else { |
| 278 | datagridService.notificationSendFlowAdded(flowPath); |
| 279 | } |
| 280 | |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 281 | return flowPath.flowId(); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 282 | } |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 283 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 287 | * Delete all previously added flows. |
| 288 | * |
| 289 | * @return true on success, otherwise false. |
| 290 | */ |
| 291 | @Override |
| 292 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 293 | if (FlowDatabaseOperation.deleteAllFlows(dbHandlerApi)) { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 294 | if (enableOnrc2014MeasurementsFlows) { |
| 295 | datagridService.notificationSendAllFlowIdsRemoved(); |
| 296 | } else { |
| 297 | datagridService.notificationSendAllFlowsRemoved(); |
| 298 | } |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 299 | return true; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 300 | } |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 301 | return false; |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 305 | * Delete a previously added flow. |
| 306 | * |
| 307 | * @param flowId the Flow ID of the flow to delete. |
| 308 | * @return true on success, otherwise false. |
| 309 | */ |
| 310 | @Override |
| 311 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 312 | if (FlowDatabaseOperation.deleteFlow(dbHandlerApi, flowId)) { |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 313 | if (enableOnrc2014MeasurementsFlows) { |
| 314 | datagridService.notificationSendFlowIdRemoved(flowId); |
| 315 | } else { |
| 316 | datagridService.notificationSendFlowRemoved(flowId); |
| 317 | } |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 318 | return true; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 319 | } |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 320 | return false; |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 324 | * Get a previously added flow. |
| 325 | * |
| 326 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 327 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 328 | */ |
| 329 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 330 | public FlowPath getFlow(FlowId flowId) { |
Brian O'Connor | 0d9963f | 2014-01-14 14:44:21 -0800 | [diff] [blame] | 331 | log.debug("FlowID: {}", flowId); |
| 332 | if(flowId.value() == -100) { |
| 333 | log.debug("Printing results..."); |
| 334 | PerformanceMonitor.report(); |
| 335 | PerformanceMonitor.clear(); |
| 336 | } |
| 337 | else if(flowId.value() == -200) { |
| 338 | log.debug("Clearing results..."); |
| 339 | PerformanceMonitor.clear(); |
| 340 | } |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 341 | return FlowDatabaseOperation.getFlow(dbHandlerApi, flowId); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 342 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 343 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 344 | /** |
Pavlin Radoslavov | 52119fa | 2014-01-09 13:37:52 -0800 | [diff] [blame] | 345 | * Get a previously added flow entry. |
| 346 | * |
| 347 | * @param flowEntryId the Flow Entry ID of the flow entry to get. |
| 348 | * @return the Flow Entry if found, otherwise null. |
| 349 | */ |
| 350 | public FlowEntry getFlowEntry(FlowEntryId flowEntryId) { |
| 351 | return FlowDatabaseOperation.getFlowEntry(dbHandlerApi, flowEntryId); |
| 352 | } |
| 353 | |
| 354 | /** |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 355 | * Get the source switch DPID of a previously added flow. |
| 356 | * |
| 357 | * @param flowId the Flow ID of the flow to get. |
| 358 | * @return the source switch DPID if found, otherwise null. |
| 359 | */ |
| 360 | public Dpid getFlowSourceDpid(FlowId flowId) { |
| 361 | return FlowDatabaseOperation.getFlowSourceDpid(dbHandlerApi, flowId); |
| 362 | } |
| 363 | |
| 364 | /** |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 365 | * Get all installed flows by all installers. |
| 366 | * |
| 367 | * @return the Flow Paths if found, otherwise null. |
| 368 | */ |
| 369 | @Override |
| 370 | public ArrayList<FlowPath> getAllFlows() { |
Pavlin Radoslavov | bc96ae1 | 2013-11-05 08:44:02 -0800 | [diff] [blame] | 371 | return FlowDatabaseOperation.getAllFlows(dbHandlerApi); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Pavlin Radoslavov | 16b761d | 2014-01-08 09:47:14 -0800 | [diff] [blame] | 375 | * Get all installed flows whose Source Switch is controlled by this |
| 376 | * instance. |
| 377 | * |
| 378 | * @param mySwitches the collection of the switches controlled by this |
| 379 | * instance. |
| 380 | * @return the Flow Paths if found, otherwise null. |
| 381 | */ |
| 382 | public ArrayList<FlowPath> getAllMyFlows(Map<Long, IOFSwitch> mySwitches) { |
| 383 | return FlowDatabaseOperation.getAllMyFlows(dbHandlerApi, mySwitches); |
| 384 | } |
| 385 | |
| 386 | /** |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 387 | * 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] | 388 | * |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 389 | * @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] | 390 | * @param maxFlows the maximum number of flows to be returned. |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 391 | * @return the Flow Paths if found, otherwise null. |
| 392 | */ |
| 393 | @Override |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 394 | public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, |
| 395 | int maxFlows) { |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 396 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 397 | SortedMap<Long, FlowPath> sortedFlowPaths = |
Pavlin Radoslavov | 85f39d3 | 2014-01-10 19:14:35 -0800 | [diff] [blame] | 398 | new TreeMap<Long, FlowPath>(); |
| 399 | |
| 400 | if (enableOnrc2014MeasurementsFlows) { |
| 401 | Collection<FlowPath> databaseFlowPaths = |
| 402 | ParallelFlowDatabaseOperation.getAllFlows(dbHandlerApi); |
| 403 | for (FlowPath flowPath : databaseFlowPaths) { |
| 404 | sortedFlowPaths.put(flowPath.flowId().value(), flowPath); |
| 405 | } |
| 406 | } else { |
| 407 | sortedFlowPaths = flowEventHandler.getAllFlowPathsCopy(); |
| 408 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 409 | |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 410 | // |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 411 | // Truncate each Flow Path and Flow Entry |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 412 | // |
| 413 | for (FlowPath flowPath : sortedFlowPaths.values()) { |
| 414 | // |
| 415 | // TODO: Add only the Flow Paths that have been successfully |
| 416 | // installed. |
| 417 | // |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 418 | flowPath.setFlowEntryMatch(null); |
| 419 | flowPath.setFlowEntryActions(null); |
| 420 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 421 | flowEntry.setFlowEntryMatch(null); |
| 422 | flowEntry.setFlowEntryActions(null); |
| 423 | } |
Pavlin Radoslavov | 5321980 | 2013-12-06 11:02:04 -0800 | [diff] [blame] | 424 | flowPaths.add(flowPath); |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 425 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 426 | |
Pavlin Radoslavov | e79c19a | 2013-12-06 09:28:15 -0800 | [diff] [blame] | 427 | return flowPaths; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 431 | * Get the collection of my switches. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 432 | * |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 433 | * @return the collection of my switches. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 434 | */ |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 435 | public Map<Long, IOFSwitch> getMySwitches() { |
| 436 | return floodlightProvider.getSwitches(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 440 | * Get the network topology. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 441 | * |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 442 | * @return the network topology. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 443 | */ |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 444 | @Override |
Pavlin Radoslavov | ed0f4a8 | 2013-11-04 16:38:36 -0800 | [diff] [blame] | 445 | public Topology getTopology() { |
| 446 | return flowEventHandler.getTopology(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 450 | * Inform the Flow Manager that a Flow Entry on switch expired. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 451 | * |
Pavlin Radoslavov | 3bd5ccf | 2013-11-26 15:10:21 -0800 | [diff] [blame] | 452 | * @param sw the switch the Flow Entry expired on. |
Pavlin Radoslavov | 4df85ae | 2013-11-26 14:48:32 -0800 | [diff] [blame] | 453 | * @param flowEntryId the Flow Entry ID of the expired Flow Entry. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 454 | */ |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 455 | @Override |
Pavlin Radoslavov | c5718e7 | 2013-12-10 15:47:10 -0800 | [diff] [blame] | 456 | public void flowEntryOnSwitchExpired(IOFSwitch sw, |
| 457 | FlowEntryId flowEntryId) { |
| 458 | // Find the Flow Entry |
| 459 | FlowEntry flowEntry = datagridService.getFlowEntry(flowEntryId); |
yoshi | a97632b | 2013-12-17 15:46:08 -0800 | [diff] [blame] | 460 | if (flowEntry == null) |
Pavlin Radoslavov | c5718e7 | 2013-12-10 15:47:10 -0800 | [diff] [blame] | 461 | return; // Flow Entry not found |
| 462 | |
| 463 | // Find the Flow Path |
| 464 | FlowPath flowPath = datagridService.getFlow(flowEntry.flowId()); |
| 465 | if (flowPath == null) |
| 466 | return; // Flow Path not found |
| 467 | |
| 468 | // |
| 469 | // Remove the Flow if the Flow Entry expired on the first switch |
| 470 | // |
| 471 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 472 | if (srcDpid.value() != sw.getId()) |
| 473 | return; |
| 474 | deleteFlow(flowPath.flowId()); |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 475 | |
| 476 | // Send flow deleted notification to the Forwarding module |
| 477 | // TODO This is a quick fix for flow-removed notifications. We |
| 478 | // should think more about the design of these notifications. |
| 479 | notificationFlowPathRemoved(flowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 483 | * Inform the Flow Manager that a collection of Flow Entries have been |
| 484 | * pushed to a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 485 | * |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 486 | * @param entries the collection of <IOFSwitch, FlowEntry> pairs |
| 487 | * that have been pushed. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 488 | */ |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 489 | @Override |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 490 | public void flowEntriesPushedToSwitch( |
| 491 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 492 | |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 493 | if (enableOnrc2014MeasurementsFlows) |
| 494 | return; |
| 495 | |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 496 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 497 | // Process all entries |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 498 | // |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 499 | // TODO: For now we have to create an explicit FlowEntry copy so |
| 500 | // we don't modify the original FlowEntry. |
| 501 | // This should go away after we start using the OpenFlow Barrier |
| 502 | // mechnanism in the FlowPusher. |
| 503 | // |
| 504 | Kryo kryo = kryoFactory.newKryo(); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 505 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
Pavlin Radoslavov | 4535bc1 | 2013-12-05 10:43:49 -0800 | [diff] [blame] | 506 | FlowEntry flowEntry = entry.second; |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 507 | |
| 508 | // |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 509 | // Mark the Flow Entry that it has been pushed to the switch |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 510 | // |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 511 | FlowEntry copyFlowEntry = kryo.copy(flowEntry); |
| 512 | copyFlowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.FE_SWITCH_UPDATED); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 513 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 514 | // |
| 515 | // Write the Flow Entry to the Datagrid |
| 516 | // |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 517 | switch (copyFlowEntry.flowEntryUserState()) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 518 | case FE_USER_ADD: |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 519 | datagridService.notificationSendFlowEntryAdded(copyFlowEntry); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 520 | break; |
| 521 | case FE_USER_MODIFY: |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 522 | datagridService.notificationSendFlowEntryUpdated(copyFlowEntry); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 523 | break; |
| 524 | case FE_USER_DELETE: |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 525 | datagridService.notificationSendFlowEntryRemoved(copyFlowEntry.flowEntryId()); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 526 | break; |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 527 | case FE_USER_UNKNOWN: |
| 528 | assert(false); |
| 529 | break; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 532 | kryoFactory.deleteKryo(kryo); |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 533 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 534 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 535 | /** |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 536 | * Generate a notification that a collection of Flow Paths has been |
| 537 | * installed in the network. |
| 538 | * |
| 539 | * @param flowPaths the collection of installed Flow Paths. |
| 540 | */ |
| 541 | void notificationFlowPathsInstalled(Collection<FlowPath> flowPaths) { |
Pavlin Radoslavov | 63c2d05 | 2013-12-18 18:17:55 -0800 | [diff] [blame] | 542 | // |
| 543 | // TODO: Add an explicit check for null pointer, because |
| 544 | // the IForwardingService is optional. Remove the "if" statement |
| 545 | // after hte Forwarding Module becomes mandatory. |
| 546 | // |
| 547 | if (forwardingService != null) |
| 548 | forwardingService.flowsInstalled(flowPaths); |
Pavlin Radoslavov | 7208e9a | 2013-12-11 14:31:07 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /** |
Jonathan Hart | 0444d93 | 2014-01-22 15:06:17 -0800 | [diff] [blame] | 552 | * Generate a notification that a FlowPath has been removed from the |
| 553 | * network. This means we've received an expiry message for the flow |
| 554 | * from the switch, and send flowmods to remove any remaining parts of |
| 555 | * the path. |
| 556 | * |
| 557 | * @param flowPath FlowPath object that was removed from the network. |
| 558 | */ |
| 559 | void notificationFlowPathRemoved(FlowPath flowPath) { |
| 560 | if (forwardingService != null) { |
| 561 | forwardingService.flowRemoved(flowPath); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 566 | * Push modified Flow-related state as appropriate. |
| 567 | * |
| 568 | * @param modifiedFlowPaths the collection of modified Flow Paths. |
| 569 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 570 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 571 | void pushModifiedFlowState(Collection<FlowPath> modifiedFlowPaths, |
| 572 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 573 | // |
| 574 | // Push the modified Flow state: |
| 575 | // - Flow Entries to switches and the datagrid |
| 576 | // - Flow Paths to the database |
| 577 | // |
| 578 | pushModifiedFlowEntriesToSwitches(modifiedFlowEntries); |
Pavlin Radoslavov | 3a7cc90 | 2014-01-09 02:32:08 -0800 | [diff] [blame] | 579 | if (enableOnrc2014MeasurementsFlows) { |
| 580 | writeModifiedFlowPathsToDatabase(modifiedFlowPaths); |
| 581 | } else { |
| 582 | pushModifiedFlowPathsToDatabase(modifiedFlowPaths); |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 583 | cleanupDeletedFlowEntriesFromDatagrid(modifiedFlowEntries); |
| 584 | } |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 588 | * Push modified Flow Entries to switches. |
| 589 | * |
| 590 | * NOTE: Only the Flow Entries to switches controlled by this instance |
| 591 | * are pushed. |
| 592 | * |
| 593 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 594 | */ |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 595 | void pushModifiedFlowEntriesToSwitches( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 596 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 597 | if (modifiedFlowEntries.isEmpty()) |
| 598 | return; |
| 599 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 600 | List<Pair<IOFSwitch, FlowEntry>> entries = |
| 601 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 602 | |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 603 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 604 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 605 | // |
| 606 | // Create a collection of my Flow Entries to push |
| 607 | // |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 608 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 609 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 610 | if (mySwitch == null) |
| 611 | continue; |
| 612 | |
Pavlin Radoslavov | d1b728c | 2013-12-18 21:39:58 -0800 | [diff] [blame] | 613 | if (flowEntry.flowEntrySwitchState() == |
| 614 | FlowEntrySwitchState.FE_SWITCH_UPDATED) { |
| 615 | // |
| 616 | // Don't push again Flow Entries that were already already |
| 617 | // installed into the switches. |
| 618 | // |
| 619 | continue; |
| 620 | } |
| 621 | |
Pavlin Radoslavov | aca49d1 | 2013-12-04 19:49:17 -0800 | [diff] [blame] | 622 | // |
| 623 | // Assign Flow Entry IDs if missing. |
| 624 | // |
| 625 | // NOTE: This is an additional safeguard, in case the |
| 626 | // mySwitches set has changed (after the Flow Entry IDs |
| 627 | // assignments by the caller). |
| 628 | // |
| 629 | if (! flowEntry.isValidFlowEntryId()) { |
| 630 | long id = getNextFlowEntryId(); |
| 631 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 632 | } |
| 633 | |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 634 | log.debug("Pushing Flow Entry To Switch: {}", flowEntry); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 635 | entries.add(new Pair<IOFSwitch, FlowEntry>(mySwitch, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 636 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 637 | |
| 638 | pusher.pushFlowEntries(entries); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 639 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 640 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 641 | /** |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 642 | * Cleanup deleted Flow Entries from the datagrid. |
| 643 | * |
| 644 | * NOTE: We cleanup only the Flow Entries that are not for our switches. |
| 645 | * This is needed to handle the case a switch going down: |
| 646 | * It has no Master controller instance, hence no controller instance |
| 647 | * will cleanup its flow entries. |
| 648 | * This is sub-optimal: we need to elect a controller instance to handle |
| 649 | * the cleanup of such orphaned flow entries. |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 650 | * |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 651 | * @param modifiedFlowEntries the collection of modified Flow Entries. |
| 652 | */ |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 653 | private void cleanupDeletedFlowEntriesFromDatagrid( |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 654 | Collection<FlowEntry> modifiedFlowEntries) { |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 655 | if (modifiedFlowEntries.isEmpty()) |
| 656 | return; |
| 657 | |
| 658 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
| 659 | |
Pavlin Radoslavov | afc4aa9 | 2013-12-04 12:44:23 -0800 | [diff] [blame] | 660 | for (FlowEntry flowEntry : modifiedFlowEntries) { |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 661 | // |
| 662 | // Process only Flow Entries that should be deleted and have |
| 663 | // a valid Flow Entry ID. |
| 664 | // |
Pavlin Radoslavov | 426a853 | 2013-12-02 17:32:21 -0800 | [diff] [blame] | 665 | if (! flowEntry.isValidFlowEntryId()) |
| 666 | continue; |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 667 | if (flowEntry.flowEntryUserState() != |
| 668 | FlowEntryUserState.FE_USER_DELETE) { |
| 669 | continue; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 672 | // |
| 673 | // NOTE: The deletion of Flow Entries for my switches is handled |
| 674 | // elsewhere. |
| 675 | // |
| 676 | IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value()); |
| 677 | if (mySwitch != null) |
| 678 | continue; |
| 679 | |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 680 | log.debug("Pushing cleanup of Flow Entry To Datagrid: {}", flowEntry); |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 681 | |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 682 | // |
| 683 | // Write the Flow Entry to the Datagrid |
| 684 | // |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 685 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 689 | /** |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 690 | * Class to implement writing to the database in a separate thread. |
| 691 | */ |
| 692 | class FlowDatabaseWriter extends Thread { |
| 693 | private FlowManager flowManager; |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 694 | private BlockingQueue<FlowPath> blockingQueue; |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 695 | |
| 696 | /** |
| 697 | * Constructor. |
| 698 | * |
| 699 | * @param flowManager the Flow Manager to use. |
| 700 | * @param blockingQueue the blocking queue to use. |
| 701 | */ |
| 702 | FlowDatabaseWriter(FlowManager flowManager, |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 703 | BlockingQueue<FlowPath> blockingQueue) { |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 704 | this.flowManager = flowManager; |
| 705 | this.blockingQueue = blockingQueue; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Run the thread. |
| 710 | */ |
| 711 | @Override |
| 712 | public void run() { |
| 713 | // |
| 714 | // The main loop |
| 715 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 716 | Collection<FlowPath> collection = new LinkedList<FlowPath>(); |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 717 | this.setName("FlowDatabaseWriter " + this.getId() ); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 718 | try { |
| 719 | while (true) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 720 | FlowPath flowPath = blockingQueue.take(); |
| 721 | collection.add(flowPath); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 722 | blockingQueue.drainTo(collection); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 723 | flowManager.writeModifiedFlowPathsToDatabase(collection); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 724 | collection.clear(); |
| 725 | } |
| 726 | } catch (Exception exception) { |
| 727 | log.debug("Exception writing to the Database: ", exception); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 733 | * Push Flow Paths to the Network MAP. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 734 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 735 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 736 | * responsible for the first switch. This is to avoid database errors |
| 737 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 738 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 739 | * @param modifiedFlowPaths the collection of Flow Paths to push. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 740 | */ |
Pavlin Radoslavov | a0c1636 | 2013-12-04 13:18:08 -0800 | [diff] [blame] | 741 | private void pushModifiedFlowPathsToDatabase( |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 742 | Collection<FlowPath> modifiedFlowPaths) { |
Pavlin Radoslavov | da8d723 | 2013-12-12 12:48:14 -0800 | [diff] [blame] | 743 | List<FlowPath> copiedFlowPaths = new LinkedList<FlowPath>(); |
| 744 | |
| 745 | // |
| 746 | // Create a copy of the Flow Paths to push, because the pushing |
| 747 | // itself will happen on a separate thread. |
| 748 | // |
| 749 | Kryo kryo = kryoFactory.newKryo(); |
| 750 | for (FlowPath flowPath : modifiedFlowPaths) { |
| 751 | FlowPath copyFlowPath = kryo.copy(flowPath); |
| 752 | copiedFlowPaths.add(copyFlowPath); |
| 753 | } |
| 754 | kryoFactory.deleteKryo(kryo); |
| 755 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 756 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 757 | // We only add the Flow Paths to the Database Queue. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 758 | // The FlowDatabaseWriter thread is responsible for the actual writing. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 759 | // |
Pavlin Radoslavov | da8d723 | 2013-12-12 12:48:14 -0800 | [diff] [blame] | 760 | flowPathsToDatabaseQueue.addAll(copiedFlowPaths); |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /** |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 764 | * Write Flow Paths to the Network MAP. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 765 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 766 | * NOTE: The complete Flow Paths are pushed only on the instance |
| 767 | * responsible for the first switch. This is to avoid database errors |
| 768 | * when multiple instances are writing Flow Entries for the same Flow Path. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 769 | * |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 770 | * @param modifiedFlowPaths the collection of Flow Paths to write. |
Pavlin Radoslavov | 584bd11 | 2013-11-21 20:59:33 -0800 | [diff] [blame] | 771 | */ |
Pavlin Radoslavov | 417398f | 2014-01-10 13:04:33 -0800 | [diff] [blame] | 772 | void writeModifiedFlowPathsToDatabase( |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 773 | Collection<FlowPath> modifiedFlowPaths) { |
| 774 | if (modifiedFlowPaths.isEmpty()) |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 775 | return; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 776 | |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 777 | Map<Long, IOFSwitch> mySwitches = getMySwitches(); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 778 | |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 779 | for (FlowPath flowPath : modifiedFlowPaths) { |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 780 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 781 | // Push the changes only on the instance responsible for the |
| 782 | // first switch. |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 783 | // |
Pavlin Radoslavov | 6311717 | 2013-11-07 02:18:37 -0800 | [diff] [blame] | 784 | Dpid srcDpid = flowPath.dataPath().srcPort().dpid(); |
| 785 | IOFSwitch mySrcSwitch = mySwitches.get(srcDpid.value()); |
| 786 | if (mySrcSwitch == null) |
| 787 | continue; |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 788 | |
| 789 | // |
Pavlin Radoslavov | ef54505 | 2013-12-05 15:17:25 -0800 | [diff] [blame] | 790 | // Delete the Flow Path from the Network Map |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 791 | // |
Pavlin Radoslavov | ef54505 | 2013-12-05 15:17:25 -0800 | [diff] [blame] | 792 | if (flowPath.flowPathUserState() == |
| 793 | FlowPathUserState.FP_USER_DELETE) { |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 794 | log.debug("Deleting Flow Path From Database: {}", flowPath); |
Pavlin Radoslavov | ef54505 | 2013-12-05 15:17:25 -0800 | [diff] [blame] | 795 | |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 796 | boolean retry = false; |
| 797 | do { |
| 798 | retry = false; |
| 799 | try { |
| 800 | if (! FlowDatabaseOperation.deleteFlow( |
| 801 | dbHandlerInner, |
| 802 | flowPath.flowId())) { |
| 803 | log.error("Cannot delete Flow Path {} from Network Map", |
| 804 | flowPath.flowId()); |
| 805 | retry = true; |
| 806 | } |
| 807 | } catch (TitanException te) { |
| 808 | log.error("Titan Exception deleting Flow Path from Network MAP: {}", te); |
| 809 | retry = true; |
| 810 | } catch (Exception e) { |
| 811 | log.error("Exception deleting Flow Path from Network MAP: {}", e); |
Yuta HIGUCHI | 67a7a3e | 2014-01-03 14:51:34 -0800 | [diff] [blame] | 812 | } |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 813 | } while (retry); |
| 814 | |
Pavlin Radoslavov | ef54505 | 2013-12-05 15:17:25 -0800 | [diff] [blame] | 815 | continue; |
| 816 | } |
| 817 | |
| 818 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 819 | // Test whether all Flow Entries are valid |
Pavlin Radoslavov | 9f33edb | 2013-11-06 18:24:37 -0800 | [diff] [blame] | 820 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 821 | boolean allValid = true; |
| 822 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
| 823 | if (flowEntry.flowEntryUserState() == |
| 824 | FlowEntryUserState.FE_USER_DELETE) { |
| 825 | continue; |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 826 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 827 | if (! flowEntry.isValidFlowEntryId()) { |
| 828 | allValid = false; |
| 829 | break; |
| 830 | } |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 831 | if (! enableOnrc2014MeasurementsFlows) { |
| 832 | if (flowEntry.flowEntrySwitchState() != |
| 833 | FlowEntrySwitchState.FE_SWITCH_UPDATED) { |
| 834 | allValid = false; |
| 835 | break; |
| 836 | } |
Pavlin Radoslavov | 237fde7 | 2013-12-17 22:21:06 -0800 | [diff] [blame] | 837 | } |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 838 | } |
| 839 | if (! allValid) |
| 840 | continue; |
| 841 | |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 842 | log.debug("Pushing Flow Path To Database: {}", flowPath); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 843 | |
| 844 | // |
| 845 | // Write the Flow Path to the Network Map |
| 846 | // |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 847 | boolean retry = false; |
| 848 | do { |
| 849 | retry = false; |
| 850 | try { |
Masayoshi Kobayashi | 178ead1 | 2013-12-19 20:50:19 -0800 | [diff] [blame] | 851 | long startTime = System.nanoTime(); |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 852 | if (! FlowDatabaseOperation.addFlow(dbHandlerInner, flowPath)) { |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 853 | log.error("Cannot write to Network Map Flow Path {}", flowPath.flowId()); |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 854 | retry = true; |
| 855 | } |
Masayoshi Kobayashi | 178ead1 | 2013-12-19 20:50:19 -0800 | [diff] [blame] | 856 | // FIXME Flag to turn ON logging |
| 857 | //long endTime = System.nanoTime(); |
| 858 | //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString()); |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 859 | } catch (TitanException te) { |
| 860 | log.error("Titan Exception writing Flow Path to Network MAP: ", te); |
| 861 | retry = true; |
Masayoshi Kobayashi | 178ead1 | 2013-12-19 20:50:19 -0800 | [diff] [blame] | 862 | // FIXME Flag to turn ON logging |
| 863 | //long endTime = System.nanoTime(); |
| 864 | //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString()); |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 865 | } catch (Exception e) { |
| 866 | log.error("Exception writing Flow Path to Network MAP: ", e); |
Masayoshi Kobayashi | 178ead1 | 2013-12-19 20:50:19 -0800 | [diff] [blame] | 867 | // FIXME Flag to turn ON logging |
| 868 | //long endTime = System.nanoTime(); |
| 869 | //log.error("Performance %% Flow path total time {} : {}", endTime - startTime, flowPath.toString()); |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 870 | } |
Pavlin Radoslavov | 262e683 | 2013-12-18 14:37:35 -0800 | [diff] [blame] | 871 | } while (retry); |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 872 | |
| 873 | if (enableOnrc2014MeasurementsFlows) { |
Pavlin Radoslavov | f4bdf26 | 2014-01-09 13:51:40 -0800 | [diff] [blame] | 874 | // Send the notifications |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 875 | |
Pavlin Radoslavov | f4bdf26 | 2014-01-09 13:51:40 -0800 | [diff] [blame] | 876 | for (FlowEntry flowEntry : flowPath.flowEntries()) { |
Pavlin Radoslavov | 8edd441 | 2014-01-10 11:37:32 -0800 | [diff] [blame] | 877 | if (flowEntry.flowEntrySwitchState() != |
Pavlin Radoslavov | f4bdf26 | 2014-01-09 13:51:40 -0800 | [diff] [blame] | 878 | FlowEntrySwitchState.FE_SWITCH_NOT_UPDATED) { |
Pavlin Radoslavov | 8edd441 | 2014-01-10 11:37:32 -0800 | [diff] [blame] | 879 | continue; |
Pavlin Radoslavov | f4bdf26 | 2014-01-09 13:51:40 -0800 | [diff] [blame] | 880 | } |
Pavlin Radoslavov | 8edd441 | 2014-01-10 11:37:32 -0800 | [diff] [blame] | 881 | // datagridService.notificationSendFlowEntryIdAdded(flowEntry.flowEntryId(), flowEntry.dpid()); |
Pavlin Radoslavov | 7847db7 | 2014-01-10 11:35:21 -0800 | [diff] [blame] | 882 | |
| 883 | // |
| 884 | // Write the Flow Entry to the Datagrid |
| 885 | // |
| 886 | switch (flowEntry.flowEntryUserState()) { |
| 887 | case FE_USER_ADD: |
| 888 | datagridService.notificationSendFlowEntryAdded(flowEntry); |
| 889 | break; |
| 890 | case FE_USER_MODIFY: |
| 891 | datagridService.notificationSendFlowEntryUpdated(flowEntry); |
| 892 | break; |
| 893 | case FE_USER_DELETE: |
| 894 | datagridService.notificationSendFlowEntryRemoved(flowEntry.flowEntryId()); |
| 895 | break; |
| 896 | case FE_USER_UNKNOWN: |
| 897 | assert(false); |
| 898 | break; |
| 899 | } |
Pavlin Radoslavov | f4bdf26 | 2014-01-09 13:51:40 -0800 | [diff] [blame] | 900 | } |
Pavlin Radoslavov | 8252fee | 2014-01-07 17:24:29 -0800 | [diff] [blame] | 901 | } |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 902 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 903 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 904 | } |