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