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 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 4 | import java.io.PrintWriter; |
| 5 | import java.io.StringWriter; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
| 9 | import java.util.Comparator; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 10 | import java.util.EnumSet; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 11 | import java.util.HashMap; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 12 | import java.util.LinkedList; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 13 | import java.util.List; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 14 | import java.util.Map; |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 15 | import java.util.Random; |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 17 | import java.util.concurrent.Executors; |
| 18 | import java.util.concurrent.ScheduledExecutorService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 19 | import java.util.concurrent.TimeUnit; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 20 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOFSwitch; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 23 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 24 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 27 | import net.floodlightcontroller.restserver.IRestApiService; |
Pavlin Radoslavov | e2f0de8 | 2013-03-12 01:39:30 -0700 | [diff] [blame] | 28 | import net.floodlightcontroller.util.MACAddress; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 29 | import net.floodlightcontroller.util.OFMessageDamper; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 30 | import net.onrc.onos.graph.GraphDBOperation; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.core.INetMapStorage; |
| 32 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 33 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 34 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 35 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 36 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService; |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 39 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 40 | import net.onrc.onos.ofcontroller.util.DataPathEndpoints; |
| 41 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 42 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
| 43 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 44 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
| 45 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 46 | import net.onrc.onos.ofcontroller.util.FlowEntrySwitchState; |
| 47 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
| 48 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 49 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 50 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
| 51 | import net.onrc.onos.ofcontroller.util.Port; |
| 52 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 53 | |
| 54 | import org.openflow.protocol.OFFlowMod; |
| 55 | import org.openflow.protocol.OFMatch; |
| 56 | import org.openflow.protocol.OFPacketOut; |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 57 | import org.openflow.protocol.OFPort; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 58 | import org.openflow.protocol.OFType; |
| 59 | import org.openflow.protocol.action.OFAction; |
| 60 | import org.openflow.protocol.action.OFActionOutput; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 64 | |
Pavlin Radoslavov | 5adf152 | 2013-04-04 17:43:41 -0700 | [diff] [blame] | 65 | public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 66 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 67 | protected GraphDBOperation op; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 68 | |
| 69 | protected IRestApiService restApi; |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 70 | protected volatile IFloodlightProviderService floodlightProvider; |
| 71 | protected volatile ITopoRouteService topoRouteService; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 72 | protected FloodlightModuleContext context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 73 | |
| 74 | protected OFMessageDamper messageDamper; |
| 75 | |
Pavlin Radoslavov | 78c4e49 | 2013-03-12 17:17:48 -0700 | [diff] [blame] | 76 | // |
| 77 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 78 | // The local copy should go away! |
| 79 | // |
| 80 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 81 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
| 82 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 83 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 84 | public static final short PRIORITY_DEFAULT = 100; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 85 | |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 86 | // Flow Entry ID generation state |
| 87 | private static Random randomGenerator = new Random(); |
| 88 | private static int nextFlowEntryIdPrefix = 0; |
| 89 | private static int nextFlowEntryIdSuffix = 0; |
| 90 | private static long nextFlowEntryId = 0; |
| 91 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 92 | // State for measurement purpose |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 93 | private static long measurementFlowId = 100000; |
| 94 | private static String measurementFlowIdStr = "0x186a0"; // 100000 |
| 95 | private long modifiedMeasurementFlowTime = 0; |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 96 | // |
| 97 | private LinkedList<FlowPath> measurementStoredPaths = new LinkedList<FlowPath>(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 98 | private long measurementStartTimeProcessingPaths = 0; |
| 99 | private long measurementEndTimeProcessingPaths = 0; |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 100 | Map<Long, ?> measurementShortestPathTopo = null; |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 101 | private String measurementPerFlowStr = new String(); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 102 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 103 | /** The logger. */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 104 | private static Logger log = LoggerFactory.getLogger(FlowManager.class); |
| 105 | |
| 106 | // The periodic task(s) |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 107 | private ScheduledExecutorService mapReaderScheduler; |
| 108 | private ScheduledExecutorService shortestPathReconcileScheduler; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 109 | |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 110 | final Runnable mapReader = new Runnable() { |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 111 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 112 | try { |
| 113 | runImpl(); |
| 114 | } catch (Exception e) { |
| 115 | log.debug("Exception processing All Flow Entries from the Network MAP: ", e); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 116 | op.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private void runImpl() { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 122 | long startTime = System.nanoTime(); |
| 123 | int counterAllFlowEntries = 0; |
| 124 | int counterMyNotUpdatedFlowEntries = 0; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 125 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 126 | if (floodlightProvider == null) { |
| 127 | log.debug("FloodlightProvider service not found!"); |
| 128 | return; |
| 129 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 130 | Map<Long, IOFSwitch> mySwitches = |
| 131 | floodlightProvider.getSwitches(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 132 | LinkedList<IFlowEntry> addFlowEntries = |
| 133 | new LinkedList<IFlowEntry>(); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 134 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 135 | new LinkedList<IFlowEntry>(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 136 | |
| 137 | // |
Pankaj Berde | a2e14a9 | 2013-04-15 11:59:15 -0700 | [diff] [blame] | 138 | // Fetch all Flow Entries which need to be updated and select only my Flow Entries |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 139 | // that need to be updated into the switches. |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 140 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 141 | boolean processed_measurement_flow = false; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 142 | Iterable<IFlowEntry> allFlowEntries = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 143 | op.getAllSwitchNotUpdatedFlowEntries(); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 144 | for (IFlowEntry flowEntryObj : allFlowEntries) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 145 | counterAllFlowEntries++; |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 146 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 147 | String dpidStr = flowEntryObj.getSwitchDpid(); |
| 148 | if (dpidStr == null) |
| 149 | continue; |
| 150 | Dpid dpid = new Dpid(dpidStr); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 151 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 152 | if (mySwitch == null) |
| 153 | continue; // Ignore the entry: not my switch |
| 154 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 155 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 156 | op.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 157 | if (flowObj == null) |
| 158 | continue; // Should NOT happen |
| 159 | if (flowObj.getFlowId() == null) |
| 160 | continue; // Invalid entry |
| 161 | |
| 162 | // |
| 163 | // NOTE: For now we process the DELETE before the ADD |
| 164 | // to cover the more common scenario. |
| 165 | // TODO: This is error prone and needs to be fixed! |
| 166 | // |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 167 | String userState = flowEntryObj.getUserState(); |
| 168 | if (userState == null) |
| 169 | continue; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 170 | if (userState.equals("FE_USER_DELETE")) { |
| 171 | // An entry that needs to be deleted. |
| 172 | deleteFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 173 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
| 174 | } else { |
| 175 | addFlowEntries.add(flowEntryObj); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 176 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 177 | counterMyNotUpdatedFlowEntries++; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 178 | // Code for measurement purpose |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 179 | // TODO: Commented-out for now |
| 180 | /* |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 181 | { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 182 | if (flowObj.getFlowId().equals(measurementFlowIdStr)) { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 183 | processed_measurement_flow = true; |
| 184 | } |
| 185 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 186 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 189 | // |
| 190 | // Process the Flow Entries that need to be added |
| 191 | // |
| 192 | for (IFlowEntry flowEntryObj : addFlowEntries) { |
| 193 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 194 | op.getFlowPathByFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 195 | if (flowObj == null) |
| 196 | continue; // Should NOT happen |
| 197 | if (flowObj.getFlowId() == null) |
| 198 | continue; // Invalid entry |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 199 | |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 200 | Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid()); |
Pavlin Radoslavov | 01391c9 | 2013-03-14 17:13:21 -0700 | [diff] [blame] | 201 | IOFSwitch mySwitch = mySwitches.get(dpid.value()); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 202 | if (mySwitch == null) |
| 203 | continue; // Shouldn't happen |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 204 | installFlowEntry(mySwitch, flowObj, flowEntryObj); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 205 | } |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 206 | |
| 207 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 208 | // Delete all Flow Entries marked for deletion from the |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 209 | // Network MAP. |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 210 | // |
| 211 | // TODO: We should use the OpenFlow Barrier mechanism |
| 212 | // to check for errors, and delete the Flow Entries after the |
| 213 | // Barrier message is received. |
| 214 | // |
| 215 | while (! deleteFlowEntries.isEmpty()) { |
| 216 | IFlowEntry flowEntryObj = deleteFlowEntries.poll(); |
| 217 | IFlowPath flowObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 218 | op.getFlowPathByFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 219 | if (flowObj == null) { |
| 220 | log.debug("Did not find FlowPath to be deleted"); |
| 221 | continue; |
| 222 | } |
| 223 | flowObj.removeFlowEntry(flowEntryObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 224 | op.removeFlowEntry(flowEntryObj); |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 225 | } |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 226 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 227 | op.commit(); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 228 | |
| 229 | if (processed_measurement_flow) { |
| 230 | long estimatedTime = |
| 231 | System.nanoTime() - modifiedMeasurementFlowTime; |
| 232 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 233 | (double)estimatedTime / 1000000000 + " sec"; |
| 234 | log.debug(logMsg); |
| 235 | } |
| 236 | |
| 237 | long estimatedTime = System.nanoTime() - startTime; |
| 238 | double rate = 0.0; |
| 239 | if (estimatedTime > 0) |
| 240 | rate = ((double)counterAllFlowEntries * 1000000000) / estimatedTime; |
| 241 | String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + |
| 242 | counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + |
| 243 | counterMyNotUpdatedFlowEntries + " in " + |
| 244 | (double)estimatedTime / 1000000000 + " sec: " + |
| 245 | rate + " paths/s"; |
| 246 | log.debug(logMsg); |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | final Runnable shortestPathReconcile = new Runnable() { |
| 251 | public void run() { |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 252 | try { |
| 253 | runImpl(); |
| 254 | } catch (Exception e) { |
| 255 | log.debug("Exception processing All Flows from the Network MAP: ", e); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 256 | op.rollback(); |
Pavlin Radoslavov | a75caea | 2013-04-10 19:11:26 -0700 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private void runImpl() { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 262 | long startTime = System.nanoTime(); |
| 263 | int counterAllFlowPaths = 0; |
| 264 | int counterMyFlowPaths = 0; |
| 265 | |
| 266 | if (floodlightProvider == null) { |
| 267 | log.debug("FloodlightProvider service not found!"); |
| 268 | return; |
| 269 | } |
| 270 | Map<Long, IOFSwitch> mySwitches = |
| 271 | floodlightProvider.getSwitches(); |
| 272 | LinkedList<IFlowPath> deleteFlows = new LinkedList<IFlowPath>(); |
| 273 | |
| 274 | boolean processed_measurement_flow = false; |
| 275 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 276 | // |
| 277 | // Fetch and recompute the Shortest Path for those |
| 278 | // Flow Paths this controller is responsible for. |
| 279 | // |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 280 | Map<Long, ?> shortestPathTopo = |
| 281 | topoRouteService.prepareShortestPathTopo(); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 282 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 283 | for (IFlowPath flowPathObj : allFlowPaths) { |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 284 | counterAllFlowPaths++; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 285 | if (flowPathObj == null) |
| 286 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 287 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 288 | String srcDpidStr = flowPathObj.getSrcSwitch(); |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 289 | if (srcDpidStr == null) |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 290 | continue; |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 291 | Dpid srcDpid = new Dpid(srcDpidStr); |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 292 | // |
| 293 | // Use the source DPID as a heuristic to decide |
| 294 | // which controller is responsible for maintaining the |
| 295 | // shortest path. |
| 296 | // NOTE: This heuristic is error-prone: if the switch |
| 297 | // goes away and no controller is responsible for that |
| 298 | // switch, then the original Flow Path is not cleaned-up |
| 299 | // |
| 300 | IOFSwitch mySwitch = mySwitches.get(srcDpid.value()); |
| 301 | if (mySwitch == null) |
| 302 | continue; // Ignore: not my responsibility |
| 303 | |
Pavlin Radoslavov | 99d1b15 | 2013-04-09 22:57:33 -0700 | [diff] [blame] | 304 | // Test the Data Path Summary string |
| 305 | String dataPathSummaryStr = flowPathObj.getDataPathSummary(); |
| 306 | if (dataPathSummaryStr == null) |
| 307 | continue; // Could be invalid entry? |
| 308 | if (dataPathSummaryStr.isEmpty()) |
| 309 | continue; // No need to maintain this flow |
| 310 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 311 | // |
| 312 | // Test whether we need to complete the Flow cleanup, |
| 313 | // if the Flow has been deleted by the user. |
| 314 | // |
| 315 | String flowUserState = flowPathObj.getUserState(); |
| 316 | if ((flowUserState != null) |
| 317 | && flowUserState.equals("FE_USER_DELETE")) { |
| 318 | Iterable<IFlowEntry> flowEntries = flowPathObj.getFlowEntries(); |
| 319 | boolean empty = true; // TODO: an ugly hack |
| 320 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 321 | empty = false; |
| 322 | break; |
| 323 | } |
| 324 | if (empty) |
| 325 | deleteFlows.add(flowPathObj); |
| 326 | } |
| 327 | |
Pavlin Radoslavov | 6db8c6e | 2013-04-08 00:14:07 +0000 | [diff] [blame] | 328 | // Fetch the fields needed to recompute the shortest path |
| 329 | Short srcPortShort = flowPathObj.getSrcPort(); |
| 330 | String dstDpidStr = flowPathObj.getDstSwitch(); |
| 331 | Short dstPortShort = flowPathObj.getDstPort(); |
| 332 | if ((srcPortShort == null) || |
| 333 | (dstDpidStr == null) || |
| 334 | (dstPortShort == null)) { |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | Port srcPort = new Port(srcPortShort); |
| 339 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 340 | Port dstPort = new Port(dstPortShort); |
| 341 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 342 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 343 | |
Pavlin Radoslavov | 2659a0b | 2013-04-03 20:30:40 -0700 | [diff] [blame] | 344 | counterMyFlowPaths++; |
| 345 | |
Pavlin Radoslavov | 832aa65 | 2013-03-29 16:21:59 -0700 | [diff] [blame] | 346 | // |
| 347 | // NOTE: Using here the regular getShortestPath() method |
| 348 | // won't work here, because that method calls internally |
| 349 | // "conn.endTx(Transaction.COMMIT)", and that will |
| 350 | // invalidate all handlers to the Titan database. |
| 351 | // If we want to experiment with calling here |
| 352 | // getShortestPath(), we need to refactor that code |
| 353 | // to avoid closing the transaction. |
| 354 | // |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 355 | DataPath dataPath = |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 356 | topoRouteService.getTopoShortestPath(shortestPathTopo, |
| 357 | srcSwitchPort, |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 358 | dstSwitchPort); |
Pavlin Radoslavov | 4a32582 | 2013-04-02 22:16:59 +0000 | [diff] [blame] | 359 | if (dataPath == null) { |
| 360 | // We need the DataPath to compare the paths |
| 361 | dataPath = new DataPath(); |
| 362 | dataPath.setSrcPort(srcSwitchPort); |
| 363 | dataPath.setDstPort(dstSwitchPort); |
| 364 | } |
| 365 | |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 366 | String newDataPathSummaryStr = dataPath.dataPathSummary(); |
| 367 | if (dataPathSummaryStr.equals(newDataPathSummaryStr)) |
| 368 | continue; // Nothing changed |
| 369 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 370 | reconcileFlow(flowPathObj, dataPath); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 371 | } |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 372 | |
| 373 | // |
| 374 | // Delete all leftover Flows marked for deletion from the |
| 375 | // Network MAP. |
| 376 | // |
| 377 | while (! deleteFlows.isEmpty()) { |
| 378 | IFlowPath flowPathObj = deleteFlows.poll(); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 379 | op.removeFlowPath(flowPathObj); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 382 | topoRouteService.dropShortestPathTopo(shortestPathTopo); |
Pavlin Radoslavov | 0391b9d | 2013-03-29 11:54:25 -0700 | [diff] [blame] | 383 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 384 | op.commit(); |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 385 | |
| 386 | if (processed_measurement_flow) { |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 387 | long estimatedTime = |
| 388 | System.nanoTime() - modifiedMeasurementFlowTime; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 389 | String logMsg = "MEASUREMENT: Pushed Flow delay: " + |
| 390 | (double)estimatedTime / 1000000000 + " sec"; |
| 391 | log.debug(logMsg); |
| 392 | } |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 393 | |
| 394 | long estimatedTime = System.nanoTime() - startTime; |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 395 | double rate = 0.0; |
| 396 | if (estimatedTime > 0) |
| 397 | rate = ((double)counterAllFlowPaths * 1000000000) / estimatedTime; |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 398 | String logMsg = "MEASUREMENT: Processed AllFlowPaths: " + |
Pavlin Radoslavov | 1552f95 | 2013-04-04 17:51:22 -0700 | [diff] [blame] | 399 | counterAllFlowPaths + " MyFlowPaths: " + |
| 400 | counterMyFlowPaths + " in " + |
| 401 | (double)estimatedTime / 1000000000 + " sec: " + |
| 402 | rate + " paths/s"; |
Pavlin Radoslavov | 42f02ba | 2013-04-03 20:07:30 -0700 | [diff] [blame] | 403 | log.debug(logMsg); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 404 | } |
| 405 | }; |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 406 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 407 | //final ScheduledFuture<?> mapReaderHandle = |
| 408 | //mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 409 | |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 410 | //final ScheduledFuture<?> shortestPathReconcileHandle = |
| 411 | //shortestPathReconcileScheduler.scheduleAtFixedRate(shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 412 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 413 | @Override |
| 414 | public void init(String conf) { |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 415 | op = new GraphDBOperation(conf); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | public void finalize() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 419 | close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void close() { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 424 | op.close(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 425 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 426 | |
| 427 | @Override |
| 428 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 429 | Collection<Class<? extends IFloodlightService>> l = |
| 430 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 431 | l.add(IFlowService.class); |
| 432 | return l; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 437 | getServiceImpls() { |
| 438 | Map<Class<? extends IFloodlightService>, |
| 439 | IFloodlightService> m = |
| 440 | new HashMap<Class<? extends IFloodlightService>, |
| 441 | IFloodlightService>(); |
| 442 | m.put(IFlowService.class, this); |
| 443 | return m; |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public Collection<Class<? extends IFloodlightService>> |
| 448 | getModuleDependencies() { |
| 449 | Collection<Class<? extends IFloodlightService>> l = |
| 450 | new ArrayList<Class<? extends IFloodlightService>>(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 451 | l.add(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 452 | l.add(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 453 | l.add(IRestApiService.class); |
| 454 | return l; |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void init(FloodlightModuleContext context) |
| 459 | throws FloodlightModuleException { |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 460 | this.context = context; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 461 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 462 | topoRouteService = context.getServiceImpl(ITopoRouteService.class); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 463 | restApi = context.getServiceImpl(IRestApiService.class); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 464 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 465 | EnumSet.of(OFType.FLOW_MOD), |
| 466 | OFMESSAGE_DAMPER_TIMEOUT); |
| 467 | // TODO: An ugly hack! |
| 468 | String conf = "/tmp/cassandra.titan"; |
| 469 | this.init(conf); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 470 | |
| 471 | mapReaderScheduler = Executors.newScheduledThreadPool(1); |
| 472 | shortestPathReconcileScheduler = Executors.newScheduledThreadPool(1); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 475 | private synchronized long getNextFlowEntryId() { |
Pavlin Radoslavov | 0b22d0e | 2013-04-02 01:12:46 +0000 | [diff] [blame] | 476 | // |
| 477 | // Generate the next Flow Entry ID. |
| 478 | // NOTE: For now, the higher 32 bits are random, and |
| 479 | // the lower 32 bits are sequential. |
| 480 | // In the future, we need a better allocation mechanism. |
| 481 | // |
| 482 | if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) { |
| 483 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 484 | nextFlowEntryIdSuffix = 0; |
| 485 | } else { |
| 486 | nextFlowEntryIdSuffix++; |
| 487 | } |
| 488 | long result = (long)nextFlowEntryIdPrefix << 32; |
| 489 | result = result | (0xffffffffL & nextFlowEntryIdSuffix); |
| 490 | return result; |
| 491 | } |
| 492 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 493 | @Override |
| 494 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 495 | restApi.addRestletRoutable(new FlowWebRoutable()); |
| 496 | |
| 497 | // Initialize the Flow Entry ID generator |
| 498 | nextFlowEntryIdPrefix = randomGenerator.nextInt(); |
| 499 | |
| 500 | mapReaderScheduler.scheduleAtFixedRate( |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 501 | mapReader, 3, 3, TimeUnit.SECONDS); |
Jonathan Hart | 50a9498 | 2013-04-10 14:49:51 -0700 | [diff] [blame] | 502 | shortestPathReconcileScheduler.scheduleAtFixedRate( |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 503 | shortestPathReconcile, 3, 3, TimeUnit.SECONDS); |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Add a flow. |
| 508 | * |
| 509 | * Internally, ONOS will automatically register the installer for |
| 510 | * receiving Flow Path Notifications for that path. |
| 511 | * |
| 512 | * @param flowPath the Flow Path to install. |
| 513 | * @param flowId the return-by-reference Flow ID as assigned internally. |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 514 | * @param dataPathSummaryStr the data path summary string if the added |
| 515 | * flow will be maintained internally, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 516 | * @return true on success, otherwise false. |
| 517 | */ |
| 518 | @Override |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 519 | public boolean addFlow(FlowPath flowPath, FlowId flowId, |
| 520 | String dataPathSummaryStr) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 521 | /* |
| 522 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 523 | if (flowPath.flowId().value() == measurementFlowId) { |
| 524 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 525 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 526 | */ |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 527 | |
| 528 | IFlowPath flowObj = null; |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 529 | boolean found = false; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 530 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 531 | if ((flowObj = op.searchFlowPath(flowPath.flowId())) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 532 | != null) { |
| 533 | log.debug("Adding FlowPath with FlowId {}: found existing FlowPath", |
| 534 | flowPath.flowId().toString()); |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 535 | found = true; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 536 | } else { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 537 | flowObj = op.newFlowPath(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 538 | log.debug("Adding FlowPath with FlowId {}: creating new FlowPath", |
| 539 | flowPath.flowId().toString()); |
| 540 | } |
| 541 | } catch (Exception e) { |
| 542 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 543 | op.rollback(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 544 | |
| 545 | StringWriter sw = new StringWriter(); |
| 546 | e.printStackTrace(new PrintWriter(sw)); |
| 547 | String stacktrace = sw.toString(); |
| 548 | |
| 549 | log.error(":addFlow FlowId:{} failed: {}", |
| 550 | flowPath.flowId().toString(), |
| 551 | stacktrace); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 552 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 553 | if (flowObj == null) { |
Masayoshi Kobayashi | c9da09e | 2013-03-26 20:52:02 +0000 | [diff] [blame] | 554 | log.error(":addFlow FlowId:{} failed: Flow object not created", |
| 555 | flowPath.flowId().toString()); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 556 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 557 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 558 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 559 | |
| 560 | // |
| 561 | // Set the Flow key: |
| 562 | // - flowId |
| 563 | // |
| 564 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 565 | flowObj.setType("flow"); |
| 566 | |
| 567 | // |
| 568 | // Set the Flow attributes: |
| 569 | // - flowPath.installerId() |
| 570 | // - flowPath.dataPath().srcPort() |
| 571 | // - flowPath.dataPath().dstPort() |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 572 | // - flowPath.matchEthernetFrameType() |
| 573 | // - flowPath.matchSrcIPv4Net() |
| 574 | // - flowPath.matchDstIPv4Net() |
| 575 | // - flowPath.matchSrcMac() |
| 576 | // - flowPath.matchDstMac() |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 577 | // |
| 578 | flowObj.setInstallerId(flowPath.installerId().toString()); |
| 579 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 580 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 581 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 582 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 583 | if (flowPath.flowEntryMatch().matchEthernetFrameType()) { |
| 584 | flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType()); |
| 585 | } |
| 586 | if (flowPath.flowEntryMatch().matchSrcIPv4Net()) { |
| 587 | flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString()); |
| 588 | } |
| 589 | if (flowPath.flowEntryMatch().matchDstIPv4Net()) { |
| 590 | flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString()); |
| 591 | } |
| 592 | if (flowPath.flowEntryMatch().matchSrcMac()) { |
| 593 | flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString()); |
| 594 | } |
| 595 | if (flowPath.flowEntryMatch().matchDstMac()) { |
| 596 | flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString()); |
| 597 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 598 | |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 599 | if (dataPathSummaryStr != null) { |
| 600 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 601 | } else { |
| 602 | flowObj.setDataPathSummary(""); |
| 603 | } |
| 604 | |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 605 | if (found) |
| 606 | flowObj.setUserState("FE_USER_MODIFY"); |
| 607 | else |
| 608 | flowObj.setUserState("FE_USER_ADD"); |
| 609 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 610 | // Flow edges: |
| 611 | // HeadFE |
| 612 | |
| 613 | |
| 614 | // |
| 615 | // Flow Entries: |
| 616 | // flowPath.dataPath().flowEntries() |
| 617 | // |
| 618 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 619 | if (addFlowEntry(flowObj, flowEntry) == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 620 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 621 | return false; |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 622 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 623 | } |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 624 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 625 | |
| 626 | // |
| 627 | // TODO: We need a proper Flow ID allocation mechanism. |
| 628 | // |
| 629 | flowId.setValue(flowPath.flowId().value()); |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 630 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 631 | return true; |
| 632 | } |
| 633 | |
| 634 | /** |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 635 | * Add a flow entry to the Network MAP. |
| 636 | * |
| 637 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 638 | * @param flowEntry the Flow Entry to install. |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 639 | * @return the added Flow Entry object on success, otherwise null. |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 640 | */ |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 641 | private IFlowEntry addFlowEntry(IFlowPath flowObj, FlowEntry flowEntry) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 642 | // Flow edges |
| 643 | // HeadFE (TODO) |
| 644 | |
| 645 | // |
| 646 | // Assign the FlowEntry ID. |
| 647 | // |
| 648 | if ((flowEntry.flowEntryId() == null) || |
| 649 | (flowEntry.flowEntryId().value() == 0)) { |
| 650 | long id = getNextFlowEntryId(); |
| 651 | flowEntry.setFlowEntryId(new FlowEntryId(id)); |
| 652 | } |
| 653 | |
| 654 | IFlowEntry flowEntryObj = null; |
| 655 | boolean found = false; |
| 656 | try { |
| 657 | if ((flowEntryObj = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 658 | op.searchFlowEntry(flowEntry.flowEntryId())) != null) { |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 659 | log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry", |
| 660 | flowEntry.flowEntryId().toString()); |
| 661 | found = true; |
| 662 | } else { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 663 | flowEntryObj = op.newFlowEntry(); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 664 | log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry", |
| 665 | flowEntry.flowEntryId().toString()); |
| 666 | } |
| 667 | } catch (Exception e) { |
| 668 | log.error(":addFlow FlowEntryId:{} failed", |
| 669 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 670 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 671 | } |
| 672 | if (flowEntryObj == null) { |
| 673 | log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created", |
| 674 | flowEntry.flowEntryId().toString()); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 675 | return null; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | // |
| 679 | // Set the Flow Entry key: |
| 680 | // - flowEntry.flowEntryId() |
| 681 | // |
| 682 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 683 | flowEntryObj.setType("flow_entry"); |
| 684 | |
| 685 | // |
| 686 | // Set the Flow Entry Edges and attributes: |
| 687 | // - Switch edge |
| 688 | // - InPort edge |
| 689 | // - OutPort edge |
| 690 | // |
| 691 | // - flowEntry.flowEntryMatch() |
| 692 | // - flowEntry.flowEntryActions() |
| 693 | // - flowEntry.dpid() |
| 694 | // - flowEntry.flowEntryUserState() |
| 695 | // - flowEntry.flowEntrySwitchState() |
| 696 | // - flowEntry.flowEntryErrorState() |
| 697 | // - flowEntry.matchInPort() |
| 698 | // - flowEntry.matchEthernetFrameType() |
| 699 | // - flowEntry.matchSrcIPv4Net() |
| 700 | // - flowEntry.matchDstIPv4Net() |
| 701 | // - flowEntry.matchSrcMac() |
| 702 | // - flowEntry.matchDstMac() |
| 703 | // - flowEntry.actionOutput() |
| 704 | // |
| 705 | ISwitchObject sw = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 706 | op.searchSwitch(flowEntry.dpid().toString()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 707 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
| 708 | flowEntryObj.setSwitch(sw); |
| 709 | if (flowEntry.flowEntryMatch().matchInPort()) { |
| 710 | IPortObject inport = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 711 | op.searchPort(flowEntry.dpid().toString(), |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 712 | flowEntry.flowEntryMatch().inPort().value()); |
| 713 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
| 714 | flowEntryObj.setInPort(inport); |
| 715 | } |
| 716 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) { |
| 717 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 718 | } |
| 719 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) { |
| 720 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 721 | } |
| 722 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) { |
| 723 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 724 | } |
| 725 | if (flowEntry.flowEntryMatch().matchSrcMac()) { |
| 726 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 727 | } |
| 728 | if (flowEntry.flowEntryMatch().matchDstMac()) { |
| 729 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 730 | } |
| 731 | |
| 732 | for (FlowEntryAction fa : flowEntry.flowEntryActions()) { |
| 733 | if (fa.actionOutput() != null) { |
| 734 | IPortObject outport = |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 735 | op.searchPort(flowEntry.dpid().toString(), |
| 736 | fa.actionOutput().port().value()); |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 737 | flowEntryObj.setActionOutput(fa.actionOutput().port().value()); |
| 738 | flowEntryObj.setOutPort(outport); |
| 739 | } |
| 740 | } |
| 741 | // TODO: Hacks with hard-coded state names! |
| 742 | if (found) |
| 743 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 744 | else |
| 745 | flowEntryObj.setUserState("FE_USER_ADD"); |
| 746 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 747 | // |
| 748 | // TODO: Take care of the FlowEntryErrorState. |
| 749 | // |
| 750 | |
| 751 | // Flow Entries edges: |
| 752 | // Flow |
| 753 | // NextFE (TODO) |
| 754 | if (! found) { |
| 755 | flowObj.addFlowEntry(flowEntryObj); |
| 756 | flowEntryObj.setFlow(flowObj); |
| 757 | } |
| 758 | |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 759 | return flowEntryObj; |
Pavlin Radoslavov | 9425f70 | 2013-04-04 19:55:07 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 763 | * Delete all previously added flows. |
| 764 | * |
| 765 | * @return true on success, otherwise false. |
| 766 | */ |
| 767 | @Override |
| 768 | public boolean deleteAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 769 | List<Thread> threads = new LinkedList<Thread>(); |
| 770 | final ConcurrentLinkedQueue<FlowId> concurrentAllFlowIds = |
| 771 | new ConcurrentLinkedQueue<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 772 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 773 | // Get all Flow IDs |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 774 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 775 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 776 | if (flowPathObj == null) |
| 777 | continue; |
| 778 | String flowIdStr = flowPathObj.getFlowId(); |
| 779 | if (flowIdStr == null) |
| 780 | continue; |
| 781 | FlowId flowId = new FlowId(flowIdStr); |
| 782 | concurrentAllFlowIds.add(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 785 | // Delete all flows one-by-one |
| 786 | for (FlowId flowId : concurrentAllFlowIds) |
| 787 | deleteFlow(flowId); |
| 788 | |
| 789 | /* |
| 790 | * TODO: A faster mechanism to delete the Flow Paths by using |
| 791 | * a number of threads. Commented-out for now. |
| 792 | */ |
| 793 | /* |
| 794 | // |
| 795 | // Create the threads to delete the Flow Paths |
| 796 | // |
| 797 | for (int i = 0; i < 10; i++) { |
| 798 | Thread thread = new Thread(new Runnable() { |
| 799 | @Override |
| 800 | public void run() { |
| 801 | while (true) { |
| 802 | FlowId flowId = concurrentAllFlowIds.poll(); |
| 803 | if (flowId == null) |
| 804 | return; |
| 805 | deleteFlow(flowId); |
| 806 | } |
| 807 | }}, "Delete All Flow Paths"); |
| 808 | threads.add(thread); |
| 809 | } |
| 810 | |
| 811 | // Start processing |
| 812 | for (Thread thread : threads) { |
| 813 | thread.start(); |
| 814 | } |
| 815 | |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 816 | // Wait for all threads to complete |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 817 | for (Thread thread : threads) { |
| 818 | try { |
| 819 | thread.join(); |
| 820 | } catch (InterruptedException e) { |
| 821 | log.debug("Exception waiting for a thread to delete a Flow Path: ", e); |
| 822 | } |
| 823 | } |
| 824 | */ |
| 825 | |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 826 | return true; |
| 827 | } |
| 828 | |
| 829 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 830 | * Delete a previously added flow. |
| 831 | * |
| 832 | * @param flowId the Flow ID of the flow to delete. |
| 833 | * @return true on success, otherwise false. |
| 834 | */ |
| 835 | @Override |
| 836 | public boolean deleteFlow(FlowId flowId) { |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 837 | /* |
| 838 | * TODO: Commented-out for now |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 839 | if (flowId.value() == measurementFlowId) { |
| 840 | modifiedMeasurementFlowTime = System.nanoTime(); |
| 841 | } |
Pavlin Radoslavov | 7e154fd | 2013-04-04 11:15:37 -0700 | [diff] [blame] | 842 | */ |
Pavlin Radoslavov | 571cff9 | 2013-03-20 02:01:32 -0700 | [diff] [blame] | 843 | |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 844 | IFlowPath flowObj = null; |
| 845 | // |
| 846 | // We just mark the entries for deletion, |
| 847 | // and let the switches remove each individual entry after |
| 848 | // it has been removed from the switches. |
| 849 | // |
| 850 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 851 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 852 | != null) { |
| 853 | log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath", |
| 854 | flowId.toString()); |
| 855 | } else { |
| 856 | log.debug("Deleting FlowPath with FlowId {}: FlowPath not found", |
| 857 | flowId.toString()); |
| 858 | } |
| 859 | } catch (Exception e) { |
| 860 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 861 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 862 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
| 863 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 864 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 865 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 866 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 867 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 868 | |
| 869 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 870 | // Find and mark for deletion all Flow Entries, |
| 871 | // and the Flow itself. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 872 | // |
Pavlin Radoslavov | 710e2a7 | 2013-04-08 02:31:05 +0000 | [diff] [blame] | 873 | flowObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 874 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 875 | boolean empty = true; // TODO: an ugly hack |
| 876 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 877 | empty = false; |
| 878 | // flowObj.removeFlowEntry(flowEntryObj); |
| 879 | // conn.utils().removeFlowEntry(conn, flowEntryObj); |
| 880 | flowEntryObj.setUserState("FE_USER_DELETE"); |
| 881 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 882 | } |
| 883 | // Remove from the database empty flows |
| 884 | if (empty) |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 885 | op.removeFlowPath(flowObj); |
| 886 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 887 | |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 888 | return true; |
| 889 | } |
| 890 | |
| 891 | /** |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 892 | * Clear the state for all previously added flows. |
| 893 | * |
| 894 | * @return true on success, otherwise false. |
| 895 | */ |
| 896 | @Override |
| 897 | public boolean clearAllFlows() { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 898 | List<FlowId> allFlowIds = new LinkedList<FlowId>(); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 899 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 900 | // Get all Flow IDs |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 901 | Iterable<IFlowPath> allFlowPaths = op.getAllFlowPaths(); |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 902 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 903 | if (flowPathObj == null) |
| 904 | continue; |
| 905 | String flowIdStr = flowPathObj.getFlowId(); |
| 906 | if (flowIdStr == null) |
| 907 | continue; |
| 908 | FlowId flowId = new FlowId(flowIdStr); |
| 909 | allFlowIds.add(flowId); |
| 910 | } |
| 911 | |
| 912 | // Clear all flows one-by-one |
| 913 | for (FlowId flowId : allFlowIds) { |
| 914 | clearFlow(flowId); |
Pavlin Radoslavov | baea924 | 2013-05-08 00:20:09 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | /** |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 921 | * Clear the state for a previously added flow. |
| 922 | * |
| 923 | * @param flowId the Flow ID of the flow to clear. |
| 924 | * @return true on success, otherwise false. |
| 925 | */ |
| 926 | @Override |
| 927 | public boolean clearFlow(FlowId flowId) { |
| 928 | IFlowPath flowObj = null; |
| 929 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 930 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 931 | != null) { |
| 932 | log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath", |
| 933 | flowId.toString()); |
| 934 | } else { |
| 935 | log.debug("Clearing FlowPath with FlowId {}: FlowPath not found", |
| 936 | flowId.toString()); |
| 937 | } |
| 938 | } catch (Exception e) { |
| 939 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 940 | op.rollback(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 941 | log.error(":clearFlow FlowId:{} failed", flowId.toString()); |
| 942 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 943 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 944 | op.commit(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 945 | return true; // OK: No such flow |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 946 | } |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 947 | |
| 948 | // |
| 949 | // Remove all Flow Entries |
| 950 | // |
| 951 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 952 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 953 | flowObj.removeFlowEntry(flowEntryObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 954 | op.removeFlowEntry(flowEntryObj); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 955 | } |
| 956 | // Remove the Flow itself |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 957 | op.removeFlowPath(flowObj); |
| 958 | op.commit(); |
Pavlin Radoslavov | 916832f | 2013-03-14 17:48:41 -0700 | [diff] [blame] | 959 | |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | /** |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 964 | * Get a previously added flow. |
| 965 | * |
| 966 | * @param flowId the Flow ID of the flow to get. |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 967 | * @return the Flow Path if found, otherwise null. |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 968 | */ |
| 969 | @Override |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 970 | public FlowPath getFlow(FlowId flowId) { |
| 971 | IFlowPath flowObj = null; |
| 972 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 973 | if ((flowObj = op.searchFlowPath(flowId)) |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 974 | != null) { |
| 975 | log.debug("Get FlowPath with FlowId {}: found existing FlowPath", |
| 976 | flowId.toString()); |
| 977 | } else { |
| 978 | log.debug("Get FlowPath with FlowId {}: FlowPath not found", |
| 979 | flowId.toString()); |
| 980 | } |
| 981 | } catch (Exception e) { |
| 982 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 983 | op.rollback(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 984 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 985 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 986 | if (flowObj == null) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 987 | op.commit(); |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 988 | return null; // Flow not found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 989 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 990 | |
| 991 | // |
| 992 | // Extract the Flow state |
| 993 | // |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 994 | FlowPath flowPath = extractFlowPath(flowObj); |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 995 | op.commit(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 996 | |
| 997 | return flowPath; |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Get all previously added flows by a specific installer for a given |
| 1002 | * data path endpoints. |
| 1003 | * |
| 1004 | * @param installerId the Caller ID of the installer of the flow to get. |
| 1005 | * @param dataPathEndpoints the data path endpoints of the flow to get. |
| 1006 | * @return the Flow Paths if found, otherwise null. |
| 1007 | */ |
| 1008 | @Override |
| 1009 | public ArrayList<FlowPath> getAllFlows(CallerId installerId, |
| 1010 | DataPathEndpoints dataPathEndpoints) { |
| 1011 | // |
| 1012 | // TODO: The implementation below is not optimal: |
| 1013 | // We fetch all flows, and then return only the subset that match |
| 1014 | // the query conditions. |
| 1015 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1016 | // the flows as appropriate. |
| 1017 | // |
| 1018 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1019 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1020 | |
| 1021 | if (allFlows == null) { |
| 1022 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1023 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1026 | for (FlowPath flow : allFlows) { |
| 1027 | // |
| 1028 | // TODO: String-based comparison is sub-optimal. |
| 1029 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 1030 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1031 | // |
| 1032 | if (! flow.installerId().toString().equals(installerId.toString())) |
| 1033 | continue; |
| 1034 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1035 | continue; |
| 1036 | } |
| 1037 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1038 | continue; |
| 1039 | } |
| 1040 | flowPaths.add(flow); |
| 1041 | } |
| 1042 | |
| 1043 | if (flowPaths.isEmpty()) { |
| 1044 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1045 | } else { |
| 1046 | log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints); |
| 1047 | } |
| 1048 | |
| 1049 | return flowPaths; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * Get all installed flows by all installers for given data path endpoints. |
| 1054 | * |
| 1055 | * @param dataPathEndpoints the data path endpoints of the flows to get. |
| 1056 | * @return the Flow Paths if found, otherwise null. |
| 1057 | */ |
| 1058 | @Override |
| 1059 | public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) { |
| 1060 | // |
| 1061 | // TODO: The implementation below is not optimal: |
| 1062 | // We fetch all flows, and then return only the subset that match |
| 1063 | // the query conditions. |
| 1064 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1065 | // the flows as appropriate. |
| 1066 | // |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1067 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1068 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1069 | |
| 1070 | if (allFlows == null) { |
| 1071 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1072 | return flowPaths; |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1075 | for (FlowPath flow : allFlows) { |
| 1076 | // |
| 1077 | // TODO: String-based comparison is sub-optimal. |
| 1078 | // We are using it for now to save us the extra work of |
Pavlin Radoslavov | c4e76a6 | 2013-03-06 10:52:41 -0800 | [diff] [blame] | 1079 | // implementing the "equals()" and "hashCode()" methods. |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1080 | // |
| 1081 | if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) { |
| 1082 | continue; |
| 1083 | } |
| 1084 | if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | flowPaths.add(flow); |
| 1088 | } |
| 1089 | |
| 1090 | if (flowPaths.isEmpty()) { |
| 1091 | log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1092 | } else { |
| 1093 | log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints); |
| 1094 | } |
| 1095 | |
| 1096 | return flowPaths; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1100 | * Get summary of all installed flows by all installers in a given range |
| 1101 | * |
| 1102 | * @param flowId the data path endpoints of the flows to get. |
| 1103 | * @param maxFlows: the maximum number of flows to be returned |
| 1104 | * @return the Flow Paths if found, otherwise null. |
| 1105 | */ |
| 1106 | @Override |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1107 | public ArrayList<IFlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) { |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1108 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1109 | // TODO: The implementation below is not optimal: |
| 1110 | // We fetch all flows, and then return only the subset that match |
| 1111 | // the query conditions. |
| 1112 | // We should use the appropriate Titan/Gremlin query to filter-out |
| 1113 | // the flows as appropriate. |
| 1114 | // |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1115 | //ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1116 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1117 | ArrayList<IFlowPath> flowPathsWithoutFlowEntries = getAllFlowsWithoutFlowEntries(); |
| 1118 | |
Jonathan Hart | f5315fb | 2013-04-05 11:41:56 -0700 | [diff] [blame] | 1119 | Collections.sort(flowPathsWithoutFlowEntries, |
| 1120 | new Comparator<IFlowPath>(){ |
| 1121 | @Override |
| 1122 | public int compare(IFlowPath first, IFlowPath second) { |
| 1123 | // TODO Auto-generated method stub |
| 1124 | long result = new FlowId(first.getFlowId()).value() |
| 1125 | - new FlowId(second.getFlowId()).value(); |
| 1126 | if (result > 0) return 1; |
| 1127 | else if (result < 0) return -1; |
| 1128 | else return 0; |
| 1129 | } |
| 1130 | } |
| 1131 | ); |
| 1132 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1133 | return flowPathsWithoutFlowEntries; |
| 1134 | |
| 1135 | /* |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1136 | ArrayList<FlowPath> allFlows = getAllFlows(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1137 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1138 | if (allFlows == null) { |
| 1139 | log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1140 | return flowPaths; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Umesh Krishnaswamy | 244b4ae | 2013-03-29 12:05:15 -0700 | [diff] [blame] | 1143 | Collections.sort(allFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1144 | |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1145 | for (FlowPath flow : allFlows) { |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1146 | flow.setFlowEntryMatch(null); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1147 | |
Pavlin Radoslavov | 96b4342 | 2013-04-04 19:14:56 -0700 | [diff] [blame] | 1148 | // start from desired flowId |
| 1149 | if (flow.flowId().value() < flowId.value()) { |
| 1150 | continue; |
| 1151 | } |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1152 | |
| 1153 | // Summarize by making null flow entry fields that are not relevant to report |
| 1154 | for (FlowEntry flowEntry : flow.dataPath().flowEntries()) { |
| 1155 | flowEntry.setFlowEntryActions(null); |
| 1156 | flowEntry.setFlowEntryMatch(null); |
| 1157 | } |
| 1158 | |
| 1159 | flowPaths.add(flow); |
| 1160 | if (maxFlows != 0 && flowPaths.size() >= maxFlows) { |
| 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | if (flowPaths.isEmpty()) { |
| 1166 | log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows); |
| 1169 | } |
| 1170 | |
| 1171 | return flowPaths; |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1172 | */ |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /** |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1176 | * Get all installed flows by all installers. |
| 1177 | * |
| 1178 | * @return the Flow Paths if found, otherwise null. |
| 1179 | */ |
| 1180 | @Override |
| 1181 | public ArrayList<FlowPath> getAllFlows() { |
| 1182 | Iterable<IFlowPath> flowPathsObj = null; |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1183 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1184 | |
| 1185 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1186 | if ((flowPathsObj = op.getAllFlowPaths()) != null) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1187 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1188 | } else { |
| 1189 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1190 | } |
| 1191 | } catch (Exception e) { |
| 1192 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1193 | op.rollback(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1194 | log.error(":getAllFlowPaths failed"); |
| 1195 | } |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1196 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1197 | op.commit(); |
Umesh Krishnaswamy | ea0f4ab | 2013-03-26 18:49:35 -0700 | [diff] [blame] | 1198 | return flowPaths; // No Flows found |
Pavlin Radoslavov | 89c8f43 | 2013-03-15 18:50:46 -0700 | [diff] [blame] | 1199 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1200 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1201 | for (IFlowPath flowObj : flowPathsObj) { |
| 1202 | // |
| 1203 | // Extract the Flow state |
| 1204 | // |
| 1205 | FlowPath flowPath = extractFlowPath(flowObj); |
Pavlin Radoslavov | 3f2af73 | 2013-03-29 15:29:35 -0700 | [diff] [blame] | 1206 | if (flowPath != null) |
| 1207 | flowPaths.add(flowPath); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1210 | op.commit(); |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1211 | |
| 1212 | return flowPaths; |
| 1213 | } |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1214 | |
| 1215 | public ArrayList<IFlowPath> getAllFlowsWithoutFlowEntries(){ |
| 1216 | Iterable<IFlowPath> flowPathsObj = null; |
| 1217 | ArrayList<IFlowPath> flowPathsObjArray = new ArrayList<IFlowPath>(); |
| 1218 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 1219 | |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1220 | op.commit(); |
Jonathan Hart | e6e9187 | 2013-04-13 11:10:32 -0700 | [diff] [blame] | 1221 | |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1222 | try { |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1223 | if ((flowPathsObj = op.getAllFlowPaths()) != null) { |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1224 | log.debug("Get all FlowPaths: found FlowPaths"); |
| 1225 | } else { |
| 1226 | log.debug("Get all FlowPaths: no FlowPaths found"); |
| 1227 | } |
| 1228 | } catch (Exception e) { |
| 1229 | // TODO: handle exceptions |
Toshio Koide | 9fe1cb2 | 2013-06-13 13:51:11 -0700 | [diff] [blame] | 1230 | op.rollback(); |
Jonathan Hart | 01f2d27 | 2013-04-04 20:03:46 -0700 | [diff] [blame] | 1231 | log.error(":getAllFlowPaths failed"); |
| 1232 | } |
| 1233 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 1234 | return new ArrayList<IFlowPath>(); // No Flows found |
| 1235 | } |
| 1236 | |
| 1237 | for (IFlowPath flowObj : flowPathsObj){ |
| 1238 | flowPathsObjArray.add(flowObj); |
| 1239 | } |
| 1240 | /* |
| 1241 | for (IFlowPath flowObj : flowPathsObj) { |
| 1242 | // |
| 1243 | // Extract the Flow state |
| 1244 | // |
| 1245 | FlowPath flowPath = extractFlowPath(flowObj); |
| 1246 | if (flowPath != null) |
| 1247 | flowPaths.add(flowPath); |
| 1248 | } |
| 1249 | */ |
| 1250 | |
| 1251 | //conn.endTx(Transaction.COMMIT); |
| 1252 | |
| 1253 | return flowPathsObjArray; |
| 1254 | } |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1255 | |
| 1256 | /** |
| 1257 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 1258 | * |
| 1259 | * @param flowObj the object to extract the Flow Path State from. |
| 1260 | * @return the extracted Flow Path State. |
| 1261 | */ |
| 1262 | private FlowPath extractFlowPath(IFlowPath flowObj) { |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 1263 | // |
| 1264 | // Extract the Flow state |
| 1265 | // |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1266 | String flowIdStr = flowObj.getFlowId(); |
| 1267 | String installerIdStr = flowObj.getInstallerId(); |
| 1268 | String srcSwitchStr = flowObj.getSrcSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1269 | Short srcPortShort = flowObj.getSrcPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1270 | String dstSwitchStr = flowObj.getDstSwitch(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1271 | Short dstPortShort = flowObj.getDstPort(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1272 | |
| 1273 | if ((flowIdStr == null) || |
| 1274 | (installerIdStr == null) || |
| 1275 | (srcSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1276 | (srcPortShort == null) || |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1277 | (dstSwitchStr == null) || |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1278 | (dstPortShort == null)) { |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1279 | // TODO: A work-around, becauuse of some bogus database objects |
| 1280 | return null; |
| 1281 | } |
| 1282 | |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1283 | FlowPath flowPath = new FlowPath(); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1284 | flowPath.setFlowId(new FlowId(flowIdStr)); |
| 1285 | flowPath.setInstallerId(new CallerId(installerIdStr)); |
| 1286 | flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1287 | flowPath.dataPath().srcPort().setPort(new Port(srcPortShort)); |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1288 | flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr)); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1289 | flowPath.dataPath().dstPort().setPort(new Port(dstPortShort)); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1290 | // |
| 1291 | // Extract the match conditions common for all Flow Entries |
| 1292 | // |
| 1293 | { |
| 1294 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1295 | Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1296 | if (matchEthernetFrameType != null) |
| 1297 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1298 | String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1299 | if (matchSrcIPv4Net != null) |
| 1300 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1301 | String matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1302 | if (matchDstIPv4Net != null) |
| 1303 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1304 | String matchSrcMac = flowObj.getMatchSrcMac(); |
| 1305 | if (matchSrcMac != null) |
| 1306 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1307 | String matchDstMac = flowObj.getMatchDstMac(); |
| 1308 | if (matchDstMac != null) |
| 1309 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1310 | flowPath.setFlowEntryMatch(match); |
| 1311 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1312 | |
| 1313 | // |
| 1314 | // Extract all Flow Entries |
| 1315 | // |
| 1316 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1317 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1318 | FlowEntry flowEntry = extractFlowEntry(flowEntryObj); |
| 1319 | if (flowEntry == null) |
Pavlin Radoslavov | c287768 | 2013-03-27 16:40:07 -0700 | [diff] [blame] | 1320 | continue; |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1321 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 1322 | } |
Pavlin Radoslavov | b6f5354 | 2013-03-01 16:02:14 -0800 | [diff] [blame] | 1323 | |
| 1324 | return flowPath; |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 1325 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1326 | |
| 1327 | /** |
Pavlin Radoslavov | 99b1275 | 2013-04-04 17:28:06 -0700 | [diff] [blame] | 1328 | * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry. |
| 1329 | * |
| 1330 | * @param flowEntryObj the object to extract the Flow Entry State from. |
| 1331 | * @return the extracted Flow Entry State. |
| 1332 | */ |
| 1333 | private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) { |
| 1334 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1335 | String switchDpidStr = flowEntryObj.getSwitchDpid(); |
| 1336 | String userState = flowEntryObj.getUserState(); |
| 1337 | String switchState = flowEntryObj.getSwitchState(); |
| 1338 | |
| 1339 | if ((flowEntryIdStr == null) || |
| 1340 | (switchDpidStr == null) || |
| 1341 | (userState == null) || |
| 1342 | (switchState == null)) { |
| 1343 | // TODO: A work-around, becauuse of some bogus database objects |
| 1344 | return null; |
| 1345 | } |
| 1346 | |
| 1347 | FlowEntry flowEntry = new FlowEntry(); |
| 1348 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr)); |
| 1349 | flowEntry.setDpid(new Dpid(switchDpidStr)); |
| 1350 | |
| 1351 | // |
| 1352 | // Extract the match conditions |
| 1353 | // |
| 1354 | FlowEntryMatch match = new FlowEntryMatch(); |
| 1355 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1356 | if (matchInPort != null) |
| 1357 | match.enableInPort(new Port(matchInPort)); |
| 1358 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1359 | if (matchEthernetFrameType != null) |
| 1360 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 1361 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1362 | if (matchSrcIPv4Net != null) |
| 1363 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 1364 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1365 | if (matchDstIPv4Net != null) |
| 1366 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 1367 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1368 | if (matchSrcMac != null) |
| 1369 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 1370 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1371 | if (matchDstMac != null) |
| 1372 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 1373 | flowEntry.setFlowEntryMatch(match); |
| 1374 | |
| 1375 | // |
| 1376 | // Extract the actions |
| 1377 | // |
| 1378 | ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>(); |
| 1379 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1380 | if (actionOutputPort != null) { |
| 1381 | FlowEntryAction action = new FlowEntryAction(); |
| 1382 | action.setActionOutput(new Port(actionOutputPort)); |
| 1383 | actions.add(action); |
| 1384 | } |
| 1385 | flowEntry.setFlowEntryActions(actions); |
| 1386 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 1387 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 1388 | // |
| 1389 | // TODO: Take care of the FlowEntryMatch, FlowEntryAction set, |
| 1390 | // and FlowEntryErrorState. |
| 1391 | // |
| 1392 | return flowEntry; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1396 | * Add and maintain a shortest-path flow. |
| 1397 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1398 | * NOTE: The Flow Path argument does NOT contain flow entries. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1399 | * |
| 1400 | * @param flowPath the Flow Path with the endpoints and the match |
| 1401 | * conditions to install. |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1402 | * @return the added shortest-path flow on success, otherwise null. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1403 | */ |
| 1404 | @Override |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1405 | public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1406 | // |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1407 | // Don't do the shortest path computation here. |
| 1408 | // Instead, let the Flow reconciliation thread take care of it. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1409 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1410 | |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1411 | // We need the DataPath to populate the Network MAP |
| 1412 | DataPath dataPath = new DataPath(); |
| 1413 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1414 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1415 | |
| 1416 | // |
| 1417 | // Prepare the computed Flow Path |
| 1418 | // |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1419 | FlowPath computedFlowPath = new FlowPath(); |
| 1420 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1421 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
| 1422 | computedFlowPath.setDataPath(dataPath); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1423 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1424 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1425 | FlowId flowId = new FlowId(); |
Pavlin Radoslavov | 8b4b059 | 2013-04-10 04:33:33 +0000 | [diff] [blame] | 1426 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1427 | if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr)) |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1428 | return null; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1429 | |
| 1430 | // TODO: Mark the flow for maintenance purpose |
| 1431 | |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 1432 | return (computedFlowPath); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /** |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1436 | * Reconcile a flow. |
| 1437 | * |
| 1438 | * @param flowObj the flow that needs to be reconciliated. |
| 1439 | * @param newDataPath the new data path to use. |
| 1440 | * @return true on success, otherwise false. |
| 1441 | */ |
| 1442 | public boolean reconcileFlow(IFlowPath flowObj, DataPath newDataPath) { |
| 1443 | Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches(); |
| 1444 | |
| 1445 | // |
| 1446 | // Set the incoming port matching and the outgoing port output |
| 1447 | // actions for each flow entry. |
| 1448 | // |
| 1449 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
| 1450 | // Set the incoming port matching |
| 1451 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1452 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1453 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1454 | |
| 1455 | // Set the outgoing port output action |
| 1456 | ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions(); |
| 1457 | if (flowEntryActions == null) { |
| 1458 | flowEntryActions = new ArrayList<FlowEntryAction>(); |
| 1459 | flowEntry.setFlowEntryActions(flowEntryActions); |
| 1460 | } |
| 1461 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1462 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
| 1463 | flowEntryActions.add(flowEntryAction); |
| 1464 | } |
| 1465 | |
| 1466 | // |
| 1467 | // Remove the old Flow Entries, and add the new Flow Entries |
| 1468 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1469 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 1470 | LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>(); |
| 1471 | for (IFlowEntry flowEntryObj : flowEntries) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1472 | flowEntryObj.setUserState("FE_USER_DELETE"); |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1473 | flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1474 | } |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1475 | for (FlowEntry flowEntry : newDataPath.flowEntries()) { |
Pavlin Radoslavov | 6fb76d1 | 2013-04-09 22:52:25 -0700 | [diff] [blame] | 1476 | addFlowEntry(flowObj, flowEntry); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | // |
| 1480 | // Set the Data Path Summary |
| 1481 | // |
| 1482 | String dataPathSummaryStr = newDataPath.dataPathSummary(); |
| 1483 | flowObj.setDataPathSummary(dataPathSummaryStr); |
| 1484 | |
| 1485 | return true; |
| 1486 | } |
| 1487 | |
| 1488 | /** |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 1489 | * Reconcile all flows in a set. |
| 1490 | * |
| 1491 | * @param flowObjSet the set of flows that need to be reconciliated. |
| 1492 | */ |
| 1493 | public void reconcileFlows(Iterable<IFlowPath> flowObjSet) { |
| 1494 | if (! flowObjSet.iterator().hasNext()) |
| 1495 | return; |
Pavlin Radoslavov | 0eeb15d | 2013-04-05 10:23:51 -0700 | [diff] [blame] | 1496 | // TODO: Not implemented/used yet. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /** |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1500 | * Install a Flow Entry on a switch. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1501 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1502 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1503 | * @param flowObj the flow path object for the flow entry to install. |
| 1504 | * @param flowEntryObj the flow entry object to install. |
| 1505 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1506 | */ |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1507 | public boolean installFlowEntry(IOFSwitch mySwitch, IFlowPath flowObj, |
| 1508 | IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1509 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 1510 | if (flowEntryIdStr == null) |
| 1511 | return false; |
| 1512 | FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1513 | String userState = flowEntryObj.getUserState(); |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1514 | if (userState == null) |
| 1515 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1516 | |
| 1517 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1518 | // Create the Open Flow Flow Modification Entry to push |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1519 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1520 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1521 | .getMessage(OFType.FLOW_MOD); |
| 1522 | long cookie = flowEntryId.value(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1523 | |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1524 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1525 | if (userState.equals("FE_USER_ADD")) { |
| 1526 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1527 | } else if (userState.equals("FE_USER_MODIFY")) { |
| 1528 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1529 | } else if (userState.equals("FE_USER_DELETE")) { |
| 1530 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1531 | } else { |
| 1532 | // Unknown user state. Ignore the entry |
| 1533 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1534 | flowEntryId.toString(), userState); |
| 1535 | return false; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1539 | // Fetch the match conditions. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1540 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1541 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1542 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1543 | // condition set. |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1544 | // |
| 1545 | OFMatch match = new OFMatch(); |
| 1546 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1547 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1548 | // Match the Incoming Port |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1549 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 1550 | if (matchInPort != null) { |
| 1551 | match.setInputPort(matchInPort); |
| 1552 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1553 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1554 | |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1555 | // Match the Ethernet Frame Type |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1556 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 1557 | if (matchEthernetFrameType == null) |
| 1558 | matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 1559 | if (matchEthernetFrameType != null) { |
| 1560 | match.setDataLayerType(matchEthernetFrameType); |
| 1561 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1562 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1563 | |
| 1564 | // Match the Source IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1565 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 1566 | if (matchSrcIPv4Net == null) |
| 1567 | matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 1568 | if (matchSrcIPv4Net != null) { |
| 1569 | match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC); |
| 1570 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1571 | |
| 1572 | // Natch the Destination IPv4 Network prefix |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1573 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 1574 | if (matchDstIPv4Net == null) |
| 1575 | matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 1576 | if (matchDstIPv4Net != null) { |
| 1577 | match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST); |
| 1578 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1579 | |
| 1580 | // Match the Source MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1581 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 1582 | if (matchSrcMac == null) |
| 1583 | matchSrcMac = flowObj.getMatchSrcMac(); |
| 1584 | if (matchSrcMac != null) { |
| 1585 | match.setDataLayerSource(matchSrcMac); |
| 1586 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1587 | } |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1588 | |
| 1589 | // Match the Destination MAC address |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1590 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 1591 | if (matchDstMac == null) |
| 1592 | matchDstMac = flowObj.getMatchDstMac(); |
| 1593 | if (matchDstMac != null) { |
| 1594 | match.setDataLayerDestination(matchDstMac); |
| 1595 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1596 | } |
| 1597 | |
| 1598 | // |
| 1599 | // Fetch the actions |
| 1600 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1601 | // TODO: For now we support only the "OUTPUT" actions. |
| 1602 | // |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1603 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1604 | Short actionOutputPort = flowEntryObj.getActionOutput(); |
| 1605 | if (actionOutputPort != null) { |
| 1606 | OFActionOutput action = new OFActionOutput(); |
| 1607 | // XXX: The max length is hard-coded for now |
| 1608 | action.setMaxLength((short)0xffff); |
| 1609 | action.setPort(actionOutputPort); |
| 1610 | actions.add(action); |
| 1611 | } |
| 1612 | |
| 1613 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1614 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1615 | .setPriority(PRIORITY_DEFAULT) |
| 1616 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1617 | .setCookie(cookie) |
| 1618 | .setCommand(flowModCommand) |
| 1619 | .setMatch(match) |
| 1620 | .setActions(actions) |
| 1621 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1622 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1623 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1624 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1625 | if (actionOutputPort != null) |
| 1626 | fm.setOutPort(actionOutputPort); |
| 1627 | } |
| 1628 | |
| 1629 | // |
| 1630 | // TODO: Set the following flag |
| 1631 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1632 | // See method ForwardingBase::pushRoute() |
| 1633 | // |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1634 | |
| 1635 | // |
| 1636 | // Write the message to the switch |
| 1637 | // |
Pavlin Radoslavov | 20d35a2 | 2013-04-05 10:16:15 -0700 | [diff] [blame] | 1638 | log.debug("MEASUREMENT: Installing flow entry " + userState + |
| 1639 | " into switch DPID: " + |
| 1640 | mySwitch.getStringId() + |
| 1641 | " flowEntryId: " + flowEntryId.toString() + |
| 1642 | " srcMac: " + matchSrcMac + " dstMac: " + matchDstMac + |
| 1643 | " inPort: " + matchInPort + " outPort: " + actionOutputPort |
| 1644 | ); |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1645 | try { |
| 1646 | messageDamper.write(mySwitch, fm, null); |
| 1647 | mySwitch.flush(); |
| 1648 | // |
| 1649 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1650 | // to check for errors, and update the SwitchState |
| 1651 | // for a flow entry after the Barrier message is |
| 1652 | // is received. |
| 1653 | // |
| 1654 | flowEntryObj.setSwitchState("FE_SWITCH_UPDATED"); |
| 1655 | } catch (IOException e) { |
| 1656 | log.error("Failure writing flow mod from network map", e); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1657 | return false; |
Pavlin Radoslavov | ec8e2e6 | 2013-04-04 18:18:29 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | return true; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /** |
| 1664 | * Install a Flow Entry on a switch. |
| 1665 | * |
| 1666 | * @param mySwitch the switch to install the Flow Entry into. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1667 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1668 | * @param flowEntry the flow entry to install. |
| 1669 | * @return true on success, otherwise false. |
| 1670 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1671 | public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1672 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1673 | // |
| 1674 | // Create the OpenFlow Flow Modification Entry to push |
| 1675 | // |
| 1676 | OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 1677 | .getMessage(OFType.FLOW_MOD); |
| 1678 | long cookie = flowEntry.flowEntryId().value(); |
| 1679 | |
| 1680 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1681 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 1682 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 1683 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 1684 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 1685 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 1686 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 1687 | } else { |
| 1688 | // Unknown user state. Ignore the entry |
| 1689 | log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 1690 | flowEntry.flowEntryId().toString(), |
| 1691 | flowEntry.flowEntryUserState()); |
| 1692 | return false; |
| 1693 | } |
| 1694 | |
| 1695 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1696 | // Fetch the match conditions. |
| 1697 | // |
| 1698 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 1699 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 1700 | // condition set. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1701 | // |
| 1702 | OFMatch match = new OFMatch(); |
| 1703 | match.setWildcards(OFMatch.OFPFW_ALL); |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1704 | FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch(); |
| 1705 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 1706 | |
| 1707 | // Match the Incoming Port |
| 1708 | Port matchInPort = flowEntryMatch.inPort(); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1709 | if (matchInPort != null) { |
| 1710 | match.setInputPort(matchInPort.value()); |
| 1711 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 1712 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1713 | |
| 1714 | // Match the Ethernet Frame Type |
| 1715 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
| 1716 | if ((matchEthernetFrameType == null) && (flowPathMatch != null)) { |
| 1717 | matchEthernetFrameType = flowPathMatch.ethernetFrameType(); |
| 1718 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1719 | if (matchEthernetFrameType != null) { |
| 1720 | match.setDataLayerType(matchEthernetFrameType); |
| 1721 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1722 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1723 | |
| 1724 | // Match the Source IPv4 Network prefix |
| 1725 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
| 1726 | if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) { |
| 1727 | matchSrcIPv4Net = flowPathMatch.srcIPv4Net(); |
| 1728 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1729 | if (matchSrcIPv4Net != null) { |
| 1730 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 1731 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1732 | |
| 1733 | // Natch the Destination IPv4 Network prefix |
| 1734 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
| 1735 | if ((matchDstIPv4Net == null) && (flowPathMatch != null)) { |
| 1736 | matchDstIPv4Net = flowPathMatch.dstIPv4Net(); |
| 1737 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1738 | if (matchDstIPv4Net != null) { |
| 1739 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 1740 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1741 | |
| 1742 | // Match the Source MAC address |
| 1743 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
| 1744 | if ((matchSrcMac == null) && (flowPathMatch != null)) { |
| 1745 | matchSrcMac = flowPathMatch.srcMac(); |
| 1746 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1747 | if (matchSrcMac != null) { |
| 1748 | match.setDataLayerSource(matchSrcMac.toString()); |
| 1749 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 1750 | } |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1751 | |
| 1752 | // Match the Destination MAC address |
| 1753 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
| 1754 | if ((matchDstMac == null) && (flowPathMatch != null)) { |
| 1755 | matchDstMac = flowPathMatch.dstMac(); |
| 1756 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1757 | if (matchDstMac != null) { |
| 1758 | match.setDataLayerDestination(matchDstMac.toString()); |
| 1759 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 1760 | } |
| 1761 | |
| 1762 | // |
| 1763 | // Fetch the actions |
| 1764 | // |
| 1765 | // TODO: For now we support only the "OUTPUT" actions. |
| 1766 | // |
| 1767 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 1768 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1769 | ArrayList<FlowEntryAction> flowEntryActions = |
| 1770 | flowEntry.flowEntryActions(); |
| 1771 | for (FlowEntryAction flowEntryAction : flowEntryActions) { |
| 1772 | FlowEntryAction.ActionOutput actionOutput = |
| 1773 | flowEntryAction.actionOutput(); |
| 1774 | if (actionOutput != null) { |
| 1775 | short actionOutputPort = actionOutput.port().value(); |
| 1776 | OFActionOutput action = new OFActionOutput(); |
| 1777 | // XXX: The max length is hard-coded for now |
| 1778 | action.setMaxLength((short)0xffff); |
| 1779 | action.setPort(actionOutputPort); |
| 1780 | actions.add(action); |
| 1781 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) || |
| 1782 | (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 1783 | fm.setOutPort(actionOutputPort); |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 1789 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 1790 | .setPriority(PRIORITY_DEFAULT) |
| 1791 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1792 | .setCookie(cookie) |
| 1793 | .setCommand(flowModCommand) |
| 1794 | .setMatch(match) |
| 1795 | .setActions(actions) |
| 1796 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 1797 | |
| 1798 | // |
| 1799 | // TODO: Set the following flag |
| 1800 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 1801 | // See method ForwardingBase::pushRoute() |
| 1802 | // |
| 1803 | |
| 1804 | // |
| 1805 | // Write the message to the switch |
| 1806 | // |
| 1807 | try { |
| 1808 | messageDamper.write(mySwitch, fm, null); |
| 1809 | mySwitch.flush(); |
Pavlin Radoslavov | 44a3dcd | 2013-04-04 18:42:56 -0700 | [diff] [blame] | 1810 | // |
| 1811 | // TODO: We should use the OpenFlow Barrier mechanism |
| 1812 | // to check for errors, and update the SwitchState |
| 1813 | // for a flow entry after the Barrier message is |
| 1814 | // is received. |
| 1815 | // |
| 1816 | // TODO: The FlowEntry Object in Titan should be set |
| 1817 | // to FE_SWITCH_UPDATED. |
| 1818 | // |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1819 | } catch (IOException e) { |
| 1820 | log.error("Failure writing flow mod from network map", e); |
| 1821 | return false; |
| 1822 | } |
| 1823 | return true; |
| 1824 | } |
| 1825 | |
| 1826 | /** |
| 1827 | * Remove a Flow Entry from a switch. |
| 1828 | * |
Pavlin Radoslavov | 2b858f8 | 2013-03-28 11:37:37 -0700 | [diff] [blame] | 1829 | * @param mySwitch the switch to remove the Flow Entry from. |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1830 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1831 | * @param flowEntry the flow entry to remove. |
| 1832 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1833 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1834 | public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath, |
| 1835 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1836 | // |
| 1837 | // The installFlowEntry() method implements both installation |
| 1838 | // and removal of flow entries. |
| 1839 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1840 | return (installFlowEntry(mySwitch, flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * Install a Flow Entry on a remote controller. |
| 1845 | * |
| 1846 | * TODO: We need it now: Jono |
| 1847 | * - For now it will make a REST call to the remote controller. |
| 1848 | * - Internally, it needs to know the name of the remote controller. |
| 1849 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1850 | * @param flowPath the flow path for the flow entry to install. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1851 | * @param flowEntry the flow entry to install. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1852 | * @return true on success, otherwise false. |
| 1853 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1854 | public boolean installRemoteFlowEntry(FlowPath flowPath, |
| 1855 | FlowEntry flowEntry) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1856 | // TODO: We need it now: Jono |
| 1857 | // - For now it will make a REST call to the remote controller. |
| 1858 | // - Internally, it needs to know the name of the remote controller. |
| 1859 | return true; |
| 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * Remove a flow entry on a remote controller. |
| 1864 | * |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1865 | * @param flowPath the flow path for the flow entry to remove. |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1866 | * @param flowEntry the flow entry to remove. |
| 1867 | * @return true on success, otherwise false. |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1868 | */ |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1869 | public boolean removeRemoteFlowEntry(FlowPath flowPath, |
| 1870 | FlowEntry flowEntry) { |
Pavlin Radoslavov | 6b6f4a8 | 2013-03-28 03:30:00 -0700 | [diff] [blame] | 1871 | // |
| 1872 | // The installRemoteFlowEntry() method implements both installation |
| 1873 | // and removal of flow entries. |
| 1874 | // |
Pavlin Radoslavov | 67b3ef3 | 2013-04-03 02:44:48 -0700 | [diff] [blame] | 1875 | return (installRemoteFlowEntry(flowPath, flowEntry)); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1876 | } |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1877 | |
| 1878 | /** |
| 1879 | * Store a path flow for measurement purpose. |
| 1880 | * |
| 1881 | * NOTE: The Flow Path argument does NOT contain flow entries. |
| 1882 | * The Shortest Path is computed, and the corresponding Flow Entries |
| 1883 | * are stored in the Flow Path. |
| 1884 | * |
| 1885 | * @param flowPath the Flow Path with the endpoints and the match |
| 1886 | * conditions to store. |
| 1887 | * @return the stored shortest-path flow on success, otherwise null. |
| 1888 | */ |
| 1889 | @Override |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1890 | public synchronized FlowPath measurementStorePathFlow(FlowPath flowPath) { |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1891 | // |
| 1892 | // Prepare the Shortest Path computation if the first Flow Path |
| 1893 | // |
| 1894 | if (measurementStoredPaths.isEmpty()) |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 1895 | measurementShortestPathTopo = topoRouteService.prepareShortestPathTopo(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1896 | |
| 1897 | // |
| 1898 | // Compute the Shortest Path |
| 1899 | // |
| 1900 | DataPath dataPath = |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 1901 | topoRouteService.getTopoShortestPath(measurementShortestPathTopo, |
| 1902 | flowPath.dataPath().srcPort(), |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1903 | flowPath.dataPath().dstPort()); |
| 1904 | if (dataPath == null) { |
| 1905 | // We need the DataPath to populate the Network MAP |
| 1906 | dataPath = new DataPath(); |
| 1907 | dataPath.setSrcPort(flowPath.dataPath().srcPort()); |
| 1908 | dataPath.setDstPort(flowPath.dataPath().dstPort()); |
| 1909 | } |
| 1910 | |
| 1911 | // |
| 1912 | // Set the incoming port matching and the outgoing port output |
| 1913 | // actions for each flow entry. |
| 1914 | // |
| 1915 | for (FlowEntry flowEntry : dataPath.flowEntries()) { |
| 1916 | // Set the incoming port matching |
| 1917 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 1918 | flowEntry.setFlowEntryMatch(flowEntryMatch); |
| 1919 | flowEntryMatch.enableInPort(flowEntry.inPort()); |
| 1920 | |
| 1921 | // Set the outgoing port output action |
| 1922 | ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions(); |
| 1923 | if (flowEntryActions == null) { |
| 1924 | flowEntryActions = new ArrayList<FlowEntryAction>(); |
| 1925 | flowEntry.setFlowEntryActions(flowEntryActions); |
| 1926 | } |
| 1927 | FlowEntryAction flowEntryAction = new FlowEntryAction(); |
| 1928 | flowEntryAction.setActionOutput(flowEntry.outPort()); |
| 1929 | flowEntryActions.add(flowEntryAction); |
| 1930 | } |
| 1931 | |
| 1932 | // |
| 1933 | // Prepare the computed Flow Path |
| 1934 | // |
| 1935 | FlowPath computedFlowPath = new FlowPath(); |
| 1936 | computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value())); |
| 1937 | computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value())); |
| 1938 | computedFlowPath.setDataPath(dataPath); |
| 1939 | computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch())); |
| 1940 | |
| 1941 | // |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1942 | // Add the computed Flow Path to the internal storage |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1943 | // |
| 1944 | measurementStoredPaths.add(computedFlowPath); |
| 1945 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1946 | log.debug("Measurement storing path {}", |
| 1947 | computedFlowPath.flowId().toString()); |
| 1948 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1949 | return (computedFlowPath); |
| 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * Install path flows for measurement purpose. |
| 1954 | * |
| 1955 | * @param numThreads the number of threads to use to install the path |
| 1956 | * flows. |
| 1957 | * @return true on success, otherwise false. |
| 1958 | */ |
| 1959 | @Override |
| 1960 | public boolean measurementInstallPaths(Integer numThreads) { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1961 | // Create a copy of the Flow Paths to install |
| 1962 | final ConcurrentLinkedQueue<FlowPath> measurementProcessingPaths = |
| 1963 | new ConcurrentLinkedQueue<FlowPath>(measurementStoredPaths); |
| 1964 | |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 1965 | /** |
| 1966 | * A Thread-wrapper class for executing the threads and collecting |
| 1967 | * the measurement data. |
| 1968 | */ |
| 1969 | class MyThread extends Thread { |
| 1970 | public long[] execTime = new long[2000]; |
| 1971 | public int samples = 0; |
| 1972 | public int threadId = -1; |
| 1973 | @Override |
| 1974 | public void run() { |
| 1975 | while (true) { |
| 1976 | FlowPath flowPath = measurementProcessingPaths.poll(); |
| 1977 | if (flowPath == null) |
| 1978 | return; |
| 1979 | // Install the Flow Path |
| 1980 | FlowId flowId = new FlowId(); |
| 1981 | String dataPathSummaryStr = |
| 1982 | flowPath.dataPath().dataPathSummary(); |
| 1983 | long startTime = System.nanoTime(); |
| 1984 | addFlow(flowPath, flowId, dataPathSummaryStr); |
| 1985 | long endTime = System.nanoTime(); |
| 1986 | execTime[samples] = endTime - startTime; |
| 1987 | samples++; |
| 1988 | } |
| 1989 | } |
| 1990 | }; |
| 1991 | |
| 1992 | List<MyThread> threads = new LinkedList<MyThread>(); |
| 1993 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 1994 | log.debug("Measurement Installing {} flows", |
| 1995 | measurementProcessingPaths.size()); |
Pavlin Radoslavov | e0938f3 | 2013-05-07 23:17:22 +0000 | [diff] [blame] | 1996 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 1997 | // |
| 1998 | // Create the threads to install the Flow Paths |
| 1999 | // |
| 2000 | for (int i = 0; i < numThreads; i++) { |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 2001 | MyThread thread = new MyThread(); |
| 2002 | thread.threadId = i; |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2003 | threads.add(thread); |
| 2004 | } |
| 2005 | |
| 2006 | // |
| 2007 | // Start processing |
| 2008 | // |
| 2009 | measurementEndTimeProcessingPaths = 0; |
| 2010 | measurementStartTimeProcessingPaths = System.nanoTime(); |
| 2011 | for (Thread thread : threads) { |
| 2012 | thread.start(); |
| 2013 | } |
| 2014 | |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 2015 | // Wait for all threads to complete |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2016 | for (Thread thread : threads) { |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2017 | try { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2018 | thread.join(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2019 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2020 | log.debug("Exception waiting for a thread to install a Flow Path: ", e); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2021 | } |
| 2022 | } |
| 2023 | |
Pavlin Radoslavov | 759772f | 2013-05-20 20:50:00 +0000 | [diff] [blame] | 2024 | // Record the end of processing |
| 2025 | measurementEndTimeProcessingPaths = System.nanoTime(); |
| 2026 | |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 2027 | // |
| 2028 | // Prepare the string with measurement data per each Flow Path |
| 2029 | // installation. |
| 2030 | // The string is multiple lines: one line per Flow Path installation: |
| 2031 | // ThreadAndTimePerFlow <ThreadId> <TotalThreads> <Time(ns)> |
| 2032 | // |
| 2033 | measurementPerFlowStr = new String(); |
| 2034 | String eol = System.getProperty("line.separator"); |
| 2035 | for (MyThread thread : threads) { |
| 2036 | for (int i = 0; i < thread.samples; i++) { |
| 2037 | measurementPerFlowStr += "ThreadAndTimePerFlow " + thread.threadId + " " + numThreads + " " + thread.execTime[i] + eol; |
| 2038 | } |
| 2039 | } |
| 2040 | |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2041 | return true; |
| 2042 | } |
| 2043 | |
| 2044 | /** |
| 2045 | * Get the measurement time that took to install the path flows. |
| 2046 | * |
| 2047 | * @return the measurement time (in nanoseconds) it took to install |
| 2048 | * the path flows. |
| 2049 | */ |
| 2050 | @Override |
| 2051 | public Long measurementGetInstallPathsTimeNsec() { |
| 2052 | return new Long(measurementEndTimeProcessingPaths - |
| 2053 | measurementStartTimeProcessingPaths); |
| 2054 | } |
| 2055 | |
| 2056 | /** |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 2057 | * Get the measurement install time per Flow. |
| 2058 | * |
| 2059 | * @return a multi-line string with the following format per line: |
| 2060 | * ThreadAndTimePerFlow <ThreadId> <TotalThreads> <Time(ns)> |
| 2061 | */ |
| 2062 | @Override |
| 2063 | public String measurementGetPerFlowInstallTime() { |
| 2064 | return new String(measurementPerFlowStr); |
| 2065 | } |
| 2066 | |
| 2067 | /** |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2068 | * Clear the path flows stored for measurement purpose. |
| 2069 | * |
| 2070 | * @return true on success, otherwise false. |
| 2071 | */ |
| 2072 | @Override |
| 2073 | public boolean measurementClearAllPaths() { |
| 2074 | measurementStoredPaths.clear(); |
Pavlin Radoslavov | 9556b14 | 2013-05-20 21:49:04 +0000 | [diff] [blame] | 2075 | topoRouteService.dropShortestPathTopo(measurementShortestPathTopo); |
Pavlin Radoslavov | e0938f3 | 2013-05-07 23:17:22 +0000 | [diff] [blame] | 2076 | measurementStartTimeProcessingPaths = 0; |
| 2077 | measurementEndTimeProcessingPaths = 0; |
Pavlin Radoslavov | f5d8041 | 2013-05-24 05:14:07 +0000 | [diff] [blame] | 2078 | measurementPerFlowStr = new String(); |
Pavlin Radoslavov | 4ef543e | 2013-05-07 13:36:57 -0700 | [diff] [blame] | 2079 | |
| 2080 | return true; |
| 2081 | } |
Pavlin Radoslavov | 9e5344c | 2013-02-18 09:58:30 -0800 | [diff] [blame] | 2082 | } |