blob: d53e7f08cf2a49bb740546438f3d855c919c72bb [file] [log] [blame]
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001package net.floodlightcontroller.flowcache;
2
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08003import java.io.IOException;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08004import java.util.ArrayList;
5import java.util.Collection;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +00006import java.util.Collections;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08007import java.util.EnumSet;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08008import java.util.HashMap;
Pavlin Radoslavove0575292013-03-28 05:35:25 -07009import java.util.HashSet;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +000010import java.util.LinkedList;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080011import java.util.List;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080012import java.util.Map;
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000013import java.util.Random;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070014import java.util.TreeMap;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070015import java.util.concurrent.BlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080016import java.util.concurrent.Executors;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070017import java.util.concurrent.LinkedBlockingQueue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080018import java.util.concurrent.ScheduledExecutorService;
19import java.util.concurrent.ScheduledFuture;
Pavlin Radoslavov4da61282013-03-20 20:31:36 -070020import java.util.concurrent.ThreadPoolExecutor;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080021import java.util.concurrent.TimeUnit;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080022
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080023import net.floodlightcontroller.core.IFloodlightProviderService;
24import net.floodlightcontroller.core.INetMapStorage;
25import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowEntry;
26import net.floodlightcontroller.core.INetMapTopologyObjects.IFlowPath;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070027import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berded0079742013-03-27 17:53:25 -070028import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070029import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080030import net.floodlightcontroller.core.IOFSwitch;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080031import net.floodlightcontroller.core.module.FloodlightModuleContext;
32import net.floodlightcontroller.core.module.FloodlightModuleException;
33import net.floodlightcontroller.core.module.IFloodlightModule;
34import net.floodlightcontroller.core.module.IFloodlightService;
35import net.floodlightcontroller.flowcache.IFlowService;
36import net.floodlightcontroller.flowcache.web.FlowWebRoutable;
37import net.floodlightcontroller.restserver.IRestApiService;
38import net.floodlightcontroller.util.CallerId;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080039import net.floodlightcontroller.util.DataPath;
40import net.floodlightcontroller.util.Dpid;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080041import net.floodlightcontroller.util.DataPathEndpoints;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080042import net.floodlightcontroller.util.FlowEntry;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070043import net.floodlightcontroller.util.FlowEntryAction;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080044import net.floodlightcontroller.util.FlowEntryId;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070045import net.floodlightcontroller.util.FlowEntryMatch;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080046import net.floodlightcontroller.util.FlowEntrySwitchState;
47import net.floodlightcontroller.util.FlowEntryUserState;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080048import net.floodlightcontroller.util.FlowId;
49import net.floodlightcontroller.util.FlowPath;
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -070050import net.floodlightcontroller.util.IPv4Net;
51import net.floodlightcontroller.util.MACAddress;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080052import net.floodlightcontroller.util.OFMessageDamper;
53import net.floodlightcontroller.util.Port;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070054import net.floodlightcontroller.util.SwitchPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080055import net.onrc.onos.util.GraphDBConnection;
56import net.onrc.onos.util.GraphDBConnection.Transaction;
57
58import org.openflow.protocol.OFFlowMod;
59import org.openflow.protocol.OFMatch;
60import org.openflow.protocol.OFPacketOut;
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070061import org.openflow.protocol.OFPort;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080062import org.openflow.protocol.OFType;
63import org.openflow.protocol.action.OFAction;
64import org.openflow.protocol.action.OFActionOutput;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080065
66import org.slf4j.Logger;
67import org.slf4j.LoggerFactory;
68
Pavlin Radoslavov5adf1522013-04-04 17:43:41 -070069public class FlowManager implements IFloodlightModule, IFlowService, INetMapStorage {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080070
71 public GraphDBConnection conn;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080072
73 protected IRestApiService restApi;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080074 protected IFloodlightProviderService floodlightProvider;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070075 protected ITopoRouteService topoRouteService;
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070076 protected FloodlightModuleContext context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080077
78 protected OFMessageDamper messageDamper;
79
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -070080 //
81 // TODO: Values copied from elsewhere (class LearningSwitch).
82 // The local copy should go away!
83 //
84 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
85 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
86 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
87 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
88 public static final short PRIORITY_DEFAULT = 100;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -080089
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +000090 // Flow Entry ID generation state
91 private static Random randomGenerator = new Random();
92 private static int nextFlowEntryIdPrefix = 0;
93 private static int nextFlowEntryIdSuffix = 0;
94 private static long nextFlowEntryId = 0;
95
Pavlin Radoslavov571cff92013-03-20 02:01:32 -070096 private static long measurementFlowId = 100000;
97 private static String measurementFlowIdStr = "0x186a0"; // 100000
98 private long modifiedMeasurementFlowTime = 0;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -070099
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800100 /** The logger. */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800101 private static Logger log = LoggerFactory.getLogger(FlowManager.class);
102
103 // The periodic task(s)
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700104 private final ScheduledExecutorService mapReaderScheduler =
105 Executors.newScheduledThreadPool(1);
106
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700107 final Runnable mapReader = new Runnable() {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800108 public void run() {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700109 long startTime = System.nanoTime();
110 int counterAllFlowEntries = 0;
111 int counterMyNotUpdatedFlowEntries = 0;
112 int counterAllFlowPaths = 0;
113 int counterMyFlowPaths = 0;
114
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800115 if (floodlightProvider == null) {
116 log.debug("FloodlightProvider service not found!");
117 return;
118 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000119 Map<Long, IOFSwitch> mySwitches =
120 floodlightProvider.getSwitches();
121 Map<Long, IFlowEntry> myFlowEntries =
122 new TreeMap<Long, IFlowEntry>();
123 LinkedList<IFlowEntry> deleteFlowEntries =
124 new LinkedList<IFlowEntry>();
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700125
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700126
127 //
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700128 // Fetch all Flow Entries and select only my Flow Entries
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700129 // that need to be updated into the switches.
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700130 //
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000131 Iterable<IFlowEntry> allFlowEntries =
132 conn.utils().getAllFlowEntries(conn);
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700133 for (IFlowEntry flowEntryObj : allFlowEntries) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700134 counterAllFlowEntries++;
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000135 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700136 String userState = flowEntryObj.getUserState();
137 String switchState = flowEntryObj.getSwitchState();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000138 String dpidStr = flowEntryObj.getSwitchDpid();
139 if ((flowEntryIdStr == null) ||
140 (userState == null) ||
141 (switchState == null) ||
142 (dpidStr == null)) {
143 log.debug("IGNORING Flow Entry entry with null fields");
144 continue;
145 }
146 FlowEntryId flowEntryId = new FlowEntryId(flowEntryIdStr);
147 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800148
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000149 if (! switchState.equals("FE_SWITCH_NOT_UPDATED"))
150 continue; // Ignore the entry: nothing to do
151
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800152 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000153 if (mySwitch == null)
154 continue; // Ignore the entry: not my switch
155
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700156 myFlowEntries.put(flowEntryId.value(), flowEntryObj);
157 }
158
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700159 log.debug("MEASUREMENT: Found {} My Flow Entries NOT_UPDATED",
160 myFlowEntries.size());
161
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700162 //
163 // Process my Flow Entries
164 //
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700165 boolean processed_measurement_flow = false;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700166 for (Map.Entry<Long, IFlowEntry> entry : myFlowEntries.entrySet()) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700167 counterMyNotUpdatedFlowEntries++;
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700168 IFlowEntry flowEntryObj = entry.getValue();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700169 IFlowPath flowObj =
170 conn.utils().getFlowPathByFlowEntry(conn,
171 flowEntryObj);
172 if (flowObj == null)
173 continue; // Should NOT happen
174
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700175 // Code for measurement purpose
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700176 // TODO: Commented-out for now
177 /*
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700178 {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700179 if (flowObj.getFlowId().equals(measurementFlowIdStr)) {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700180 processed_measurement_flow = true;
181 }
182 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700183 */
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700184
185 //
186 // TODO: Eliminate the re-fetching of flowEntryId,
187 // userState, switchState, and dpid from the flowEntryObj.
188 //
189 FlowEntryId flowEntryId =
190 new FlowEntryId(flowEntryObj.getFlowEntryId());
191 Dpid dpid = new Dpid(flowEntryObj.getSwitchDpid());
192 String userState = flowEntryObj.getUserState();
193 String switchState = flowEntryObj.getSwitchState();
194 IOFSwitch mySwitch = mySwitches.get(dpid.value());
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000195 if (mySwitch == null)
196 continue; // Shouldn't happen
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800197
198 //
199 // Create the Open Flow Flow Modification Entry to push
200 //
201 OFFlowMod fm =
202 (OFFlowMod) floodlightProvider.getOFMessageFactory()
203 .getMessage(OFType.FLOW_MOD);
204 long cookie = flowEntryId.value();
205
206 short flowModCommand = OFFlowMod.OFPFC_ADD;
207 if (userState.equals("FE_USER_ADD")) {
208 flowModCommand = OFFlowMod.OFPFC_ADD;
209 } else if (userState.equals("FE_USER_MODIFY")) {
210 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
211 } else if (userState.equals("FE_USER_DELETE")) {
212 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
213 } else {
214 // Unknown user state. Ignore the entry
Pavlin Radoslavov2f9d6332013-03-18 23:05:48 -0700215 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
216 flowEntryId.toString(), userState);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800217 continue;
218 }
219
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700220 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700221 // Fetch the match conditions.
222 //
223 // NOTE: The Flow matching conditions common for all
224 // Flow Entries are used ONLY if a Flow Entry does NOT
225 // have the corresponding matching condition set.
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700226 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800227 OFMatch match = new OFMatch();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700228 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700229 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700230 Short matchInPort = flowEntryObj.getMatchInPort();
231 if (matchInPort != null) {
232 match.setInputPort(matchInPort);
233 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
234 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700235 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700236 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700237 if (matchEthernetFrameType == null)
238 matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700239 if (matchEthernetFrameType != null) {
240 match.setDataLayerType(matchEthernetFrameType);
241 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
242 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700243 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700244 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700245 if (matchSrcIPv4Net == null)
246 matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700247 if (matchSrcIPv4Net != null) {
248 match.setFromCIDR(matchSrcIPv4Net, OFMatch.STR_NW_SRC);
249 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700250 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700251 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700252 if (matchDstIPv4Net == null)
253 matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700254 if (matchDstIPv4Net != null) {
255 match.setFromCIDR(matchDstIPv4Net, OFMatch.STR_NW_DST);
256 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700257 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700258 String matchSrcMac = flowEntryObj.getMatchSrcMac();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700259 if (matchSrcMac == null)
260 matchSrcMac = flowObj.getMatchSrcMac();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700261 if (matchSrcMac != null) {
262 match.setDataLayerSource(matchSrcMac);
263 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
264 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700265 //
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700266 String matchDstMac = flowEntryObj.getMatchDstMac();
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700267 if (matchDstMac == null)
268 matchDstMac = flowObj.getMatchDstMac();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700269 if (matchDstMac != null) {
270 match.setDataLayerDestination(matchDstMac);
271 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
272 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700273
274 //
275 // Fetch the actions
276 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800277 List<OFAction> actions = new ArrayList<OFAction>();
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700278 Short actionOutputPort = flowEntryObj.getActionOutput();
279 if (actionOutputPort != null) {
280 OFActionOutput action = new OFActionOutput();
281 // XXX: The max length is hard-coded for now
282 action.setMaxLength((short)0xffff);
283 action.setPort(actionOutputPort);
284 actions.add(action);
285 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800286
287 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
288 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -0700289 .setPriority(PRIORITY_DEFAULT)
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800290 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
291 .setCookie(cookie)
292 .setCommand(flowModCommand)
293 .setMatch(match)
294 .setActions(actions)
295 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
Pavlin Radoslavov78c4e492013-03-12 17:17:48 -0700296 fm.setOutPort(OFPort.OFPP_NONE.getValue());
297 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
298 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
299 if (actionOutputPort != null)
300 fm.setOutPort(actionOutputPort);
301 }
302
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800303 //
304 // TODO: Set the following flag
305 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
306 // See method ForwardingBase::pushRoute()
307 //
308 try {
309 messageDamper.write(mySwitch, fm, null);
310 mySwitch.flush();
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000311 //
312 // TODO: We should use the OpenFlow Barrier mechanism
313 // to check for errors, and update the SwitchState
314 // for a flow entry after the Barrier message is
315 // is received.
316 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800317 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
318 if (userState.equals("FE_USER_DELETE")) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000319 // An entry that needs to be deleted.
320 deleteFlowEntries.add(flowEntryObj);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800321 }
322 } catch (IOException e) {
323 log.error("Failure writing flow mod from network map", e);
324 }
325 }
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000326
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700327 log.debug("MEASUREMENT: Found {} Flow Entries to delete",
328 deleteFlowEntries.size());
329
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000330 //
331 // Delete all entries marked for deletion
332 //
333 // TODO: We should use the OpenFlow Barrier mechanism
334 // to check for errors, and delete the Flow Entries after the
335 // Barrier message is received.
336 //
337 while (! deleteFlowEntries.isEmpty()) {
338 IFlowEntry flowEntryObj = deleteFlowEntries.poll();
339 IFlowPath flowObj =
340 conn.utils().getFlowPathByFlowEntry(conn, flowEntryObj);
341 if (flowObj == null) {
342 log.debug("Did not find FlowPath to be deleted");
343 continue;
344 }
345 flowObj.removeFlowEntry(flowEntryObj);
346 conn.utils().removeFlowEntry(conn, flowEntryObj);
347
348 // Test whether the last flow entry
349 Iterable<IFlowEntry> tmpflowEntries =
350 flowObj.getFlowEntries();
351 boolean found = false;
352 for (IFlowEntry tmpflowEntryObj : tmpflowEntries) {
353 found = true;
354 break;
355 }
356 if (! found) {
357 // Remove the Flow Path as well
358 conn.utils().removeFlowPath(conn, flowObj);
359 }
360 }
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700361
362
363 //
364 // Fetch and recompute the Shortest Path for those
365 // Flow Paths this controller is responsible for.
366 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700367 topoRouteService.prepareShortestPathTopo();
368 Iterable<IFlowPath> allFlowPaths = conn.utils().getAllFlowPaths(conn);
369 HashSet<IFlowPath> flowObjSet = new HashSet<IFlowPath>();
370 for (IFlowPath flowPathObj : allFlowPaths) {
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700371 counterAllFlowPaths++;
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700372 if (flowPathObj == null)
373 continue;
374 String dataPathSummaryStr = flowPathObj.getDataPathSummary();
375 if (dataPathSummaryStr == null)
376 continue; // Could be invalid entry?
377 if (dataPathSummaryStr.isEmpty())
378 continue; // No need to maintain this flow
379
380 // Fetch the fields needed to recompute the shortest path
381 String flowIdStr = flowPathObj.getFlowId();
382 String srcDpidStr = flowPathObj.getSrcSwitch();
383 Short srcPortShort = flowPathObj.getSrcPort();
384 String dstDpidStr = flowPathObj.getDstSwitch();
385 Short dstPortShort = flowPathObj.getDstPort();
386 if ((flowIdStr == null) ||
387 (srcDpidStr == null) ||
388 (srcPortShort == null) ||
389 (dstDpidStr == null) ||
390 (dstPortShort == null)) {
391 log.debug("IGNORING Flow Path entry with null fields");
392 continue;
393 }
394
395 FlowId flowId = new FlowId(flowIdStr);
396 Dpid srcDpid = new Dpid(srcDpidStr);
397 Port srcPort = new Port(srcPortShort);
398 Dpid dstDpid = new Dpid(dstDpidStr);
399 Port dstPort = new Port(dstPortShort);
400 SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort);
401 SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort);
Pavlin Radoslavov2659a0b2013-04-03 20:30:40 -0700402
403 //
404 // Use the source DPID as a heuristic to decide
405 // which controller is responsible for maintaining the
406 // shortest path.
407 // NOTE: This heuristic is error-prone: if the switch
408 // goes away and no controller is responsible for that
409 // switch, then the original Flow Path is not cleaned-up
410 //
411 IOFSwitch mySwitch = mySwitches.get(srcDpid.value());
412 if (mySwitch == null)
413 continue; // Ignore: not my responsibility
414
415 counterMyFlowPaths++;
416
Pavlin Radoslavov832aa652013-03-29 16:21:59 -0700417 //
418 // NOTE: Using here the regular getShortestPath() method
419 // won't work here, because that method calls internally
420 // "conn.endTx(Transaction.COMMIT)", and that will
421 // invalidate all handlers to the Titan database.
422 // If we want to experiment with calling here
423 // getShortestPath(), we need to refactor that code
424 // to avoid closing the transaction.
425 //
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700426 DataPath dataPath =
427 topoRouteService.getTopoShortestPath(srcSwitchPort,
428 dstSwitchPort);
Pavlin Radoslavov4a325822013-04-02 22:16:59 +0000429 if (dataPath == null) {
430 // We need the DataPath to compare the paths
431 dataPath = new DataPath();
432 dataPath.setSrcPort(srcSwitchPort);
433 dataPath.setDstPort(dstSwitchPort);
434 }
435
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700436 String newDataPathSummaryStr = dataPath.dataPathSummary();
437 if (dataPathSummaryStr.equals(newDataPathSummaryStr))
438 continue; // Nothing changed
439
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700440 log.debug("RECONCILE: Need to Reconcile Shortest Path for FlowID {}",
441 flowId.toString());
442 flowObjSet.add(flowPathObj);
443 }
Pavlin Radoslavov53a3a8c2013-04-04 04:34:50 -0700444 log.debug("MEASUREMENT: Found {} Flows to reconcile",
Pavlin Radoslavove87f94e2013-04-04 04:31:09 -0700445 flowObjSet.size());
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700446 reconcileFlows(flowObjSet);
447 topoRouteService.dropShortestPathTopo();
Pavlin Radoslavov0391b9d2013-03-29 11:54:25 -0700448
449
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800450 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700451
452 if (processed_measurement_flow) {
453 long estimatedTime = System.nanoTime() - modifiedMeasurementFlowTime;
454 String logMsg = "MEASUREMENT: Pushed Flow delay: " +
455 (double)estimatedTime / 1000000000 + " sec";
456 log.debug(logMsg);
457 }
Pavlin Radoslavov42f02ba2013-04-03 20:07:30 -0700458
459 long estimatedTime = System.nanoTime() - startTime;
460 double rate = (estimatedTime > 0)? ((double)counterAllFlowPaths * 1000000000) / estimatedTime: 0.0;
461 String logMsg = "MEASUREMENT: Processed AllFlowEntries: " + counterAllFlowEntries + " MyNotUpdatedFlowEntries: " + counterMyNotUpdatedFlowEntries + " AllFlowPaths: " + counterAllFlowPaths + " MyFlowPaths: " + counterMyFlowPaths + " in " + (double)estimatedTime / 1000000000 + " sec: " + rate + " paths/s";
462 log.debug(logMsg);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800463 }
464 };
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700465
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700466 final ScheduledFuture<?> mapReaderHandle =
467 mapReaderScheduler.scheduleAtFixedRate(mapReader, 3, 3, TimeUnit.SECONDS);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800468
469 @Override
470 public void init(String conf) {
471 conn = GraphDBConnection.getInstance(conf);
472 }
473
474 public void finalize() {
475 close();
476 }
477
478 @Override
479 public void close() {
480 conn.close();
481 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800482
483 @Override
484 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
485 Collection<Class<? extends IFloodlightService>> l =
486 new ArrayList<Class<? extends IFloodlightService>>();
487 l.add(IFlowService.class);
488 return l;
489 }
490
491 @Override
492 public Map<Class<? extends IFloodlightService>, IFloodlightService>
493 getServiceImpls() {
494 Map<Class<? extends IFloodlightService>,
495 IFloodlightService> m =
496 new HashMap<Class<? extends IFloodlightService>,
497 IFloodlightService>();
498 m.put(IFlowService.class, this);
499 return m;
500 }
501
502 @Override
503 public Collection<Class<? extends IFloodlightService>>
504 getModuleDependencies() {
505 Collection<Class<? extends IFloodlightService>> l =
506 new ArrayList<Class<? extends IFloodlightService>>();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800507 l.add(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700508 l.add(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800509 l.add(IRestApiService.class);
510 return l;
511 }
512
513 @Override
514 public void init(FloodlightModuleContext context)
515 throws FloodlightModuleException {
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700516 this.context = context;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800517 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -0700518 topoRouteService = context.getServiceImpl(ITopoRouteService.class);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800519 restApi = context.getServiceImpl(IRestApiService.class);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800520 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
521 EnumSet.of(OFType.FLOW_MOD),
522 OFMESSAGE_DAMPER_TIMEOUT);
523 // TODO: An ugly hack!
524 String conf = "/tmp/cassandra.titan";
525 this.init(conf);
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800526 }
527
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000528 private long getNextFlowEntryId() {
529 //
530 // Generate the next Flow Entry ID.
531 // NOTE: For now, the higher 32 bits are random, and
532 // the lower 32 bits are sequential.
533 // In the future, we need a better allocation mechanism.
534 //
535 if ((nextFlowEntryIdSuffix & 0xffffffffL) == 0xffffffffL) {
536 nextFlowEntryIdPrefix = randomGenerator.nextInt();
537 nextFlowEntryIdSuffix = 0;
538 } else {
539 nextFlowEntryIdSuffix++;
540 }
541 long result = (long)nextFlowEntryIdPrefix << 32;
542 result = result | (0xffffffffL & nextFlowEntryIdSuffix);
543 return result;
544 }
545
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800546 @Override
547 public void startUp(FloodlightModuleContext context) {
548 restApi.addRestletRoutable(new FlowWebRoutable());
Pavlin Radoslavov80ca6302013-03-20 02:08:09 -0700549
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000550 // Initialize the Flow Entry ID generator
551 nextFlowEntryIdPrefix = randomGenerator.nextInt();
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800552 }
553
554 /**
555 * Add a flow.
556 *
557 * Internally, ONOS will automatically register the installer for
558 * receiving Flow Path Notifications for that path.
559 *
560 * @param flowPath the Flow Path to install.
561 * @param flowId the return-by-reference Flow ID as assigned internally.
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700562 * @param dataPathSummaryStr the data path summary string if the added
563 * flow will be maintained internally, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800564 * @return true on success, otherwise false.
565 */
566 @Override
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700567 public boolean addFlow(FlowPath flowPath, FlowId flowId,
568 String dataPathSummaryStr) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700569 /*
570 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700571 if (flowPath.flowId().value() == measurementFlowId) {
572 modifiedMeasurementFlowTime = System.nanoTime();
573 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700574 */
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800575
576 //
577 // Assign the FlowEntry IDs
Pavlin Radoslavov01391c92013-03-14 17:13:21 -0700578 // Right now every new flow entry gets a new flow entry ID
579 // TODO: This needs to be redesigned!
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800580 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800581 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
Pavlin Radoslavov0b22d0e2013-04-02 01:12:46 +0000582 long id = getNextFlowEntryId();
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800583 flowEntry.setFlowEntryId(new FlowEntryId(id));
584 }
585
586 IFlowPath flowObj = null;
587 try {
588 if ((flowObj = conn.utils().searchFlowPath(conn, flowPath.flowId()))
589 != null) {
590 log.debug("Adding FlowPath with FlowId {}: found existing FlowPath",
591 flowPath.flowId().toString());
592 } else {
593 flowObj = conn.utils().newFlowPath(conn);
594 log.debug("Adding FlowPath with FlowId {}: creating new FlowPath",
595 flowPath.flowId().toString());
596 }
597 } catch (Exception e) {
598 // TODO: handle exceptions
599 conn.endTx(Transaction.ROLLBACK);
600 log.error(":addFlow FlowId:{} failed",
601 flowPath.flowId().toString());
602 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700603 if (flowObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000604 log.error(":addFlow FlowId:{} failed: Flow object not created",
605 flowPath.flowId().toString());
606 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800607 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700608 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800609
610 //
611 // Set the Flow key:
612 // - flowId
613 //
614 flowObj.setFlowId(flowPath.flowId().toString());
615 flowObj.setType("flow");
616
617 //
618 // Set the Flow attributes:
619 // - flowPath.installerId()
620 // - flowPath.dataPath().srcPort()
621 // - flowPath.dataPath().dstPort()
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700622 // - flowPath.matchEthernetFrameType()
623 // - flowPath.matchSrcIPv4Net()
624 // - flowPath.matchDstIPv4Net()
625 // - flowPath.matchSrcMac()
626 // - flowPath.matchDstMac()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800627 //
628 flowObj.setInstallerId(flowPath.installerId().toString());
629 flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString());
630 flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value());
631 flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString());
632 flowObj.setDstPort(flowPath.dataPath().dstPort().port().value());
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700633 if (flowPath.flowEntryMatch().matchEthernetFrameType()) {
634 flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType());
635 }
636 if (flowPath.flowEntryMatch().matchSrcIPv4Net()) {
637 flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString());
638 }
639 if (flowPath.flowEntryMatch().matchDstIPv4Net()) {
640 flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString());
641 }
642 if (flowPath.flowEntryMatch().matchSrcMac()) {
643 flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString());
644 }
645 if (flowPath.flowEntryMatch().matchDstMac()) {
646 flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString());
647 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800648
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -0700649 if (dataPathSummaryStr != null) {
650 flowObj.setDataPathSummary(dataPathSummaryStr);
651 } else {
652 flowObj.setDataPathSummary("");
653 }
654
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800655 // Flow edges:
656 // HeadFE
657
658
659 //
660 // Flow Entries:
661 // flowPath.dataPath().flowEntries()
662 //
663 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
664 IFlowEntry flowEntryObj = null;
665 boolean found = false;
666 try {
667 if ((flowEntryObj = conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId())) != null) {
668 log.debug("Adding FlowEntry with FlowEntryId {}: found existing FlowEntry",
669 flowEntry.flowEntryId().toString());
670 found = true;
671 } else {
672 flowEntryObj = conn.utils().newFlowEntry(conn);
673 log.debug("Adding FlowEntry with FlowEntryId {}: creating new FlowEntry",
674 flowEntry.flowEntryId().toString());
675 }
676 } catch (Exception e) {
677 // TODO: handle exceptions
678 conn.endTx(Transaction.ROLLBACK);
679 log.error(":addFlow FlowEntryId:{} failed",
680 flowEntry.flowEntryId().toString());
681 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700682 if (flowEntryObj == null) {
Masayoshi Kobayashic9da09e2013-03-26 20:52:02 +0000683 log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created",
684 flowEntry.flowEntryId().toString());
685 conn.endTx(Transaction.ROLLBACK);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800686 return false;
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700687 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800688
689 //
690 // Set the Flow Entry key:
691 // - flowEntry.flowEntryId()
692 //
693 flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString());
694 flowEntryObj.setType("flow_entry");
695
696 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700697 // Set the Flow Entry Edges and attributes:
698 // - Switch edge
699 // - InPort edge
700 // - OutPort edge
701 //
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800702 // - flowEntry.flowEntryMatch()
703 // - flowEntry.flowEntryActions()
704 // - flowEntry.dpid()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800705 // - flowEntry.flowEntryUserState()
706 // - flowEntry.flowEntrySwitchState()
707 // - flowEntry.flowEntryErrorState()
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700708 // - flowEntry.matchInPort()
709 // - flowEntry.matchEthernetFrameType()
710 // - flowEntry.matchSrcIPv4Net()
711 // - flowEntry.matchDstIPv4Net()
712 // - flowEntry.matchSrcMac()
713 // - flowEntry.matchDstMac()
714 // - flowEntry.actionOutput()
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800715 //
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700716 ISwitchObject sw =
717 conn.utils().searchSwitch(conn, flowEntry.dpid().toString());
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800718 flowEntryObj.setSwitchDpid(flowEntry.dpid().toString());
Pankaj Berded0079742013-03-27 17:53:25 -0700719 flowEntryObj.setSwitch(sw);
720 if (flowEntry.flowEntryMatch().matchInPort()) {
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700721 IPortObject inport =
722 conn.utils().searchPort(conn, flowEntry.dpid().toString(),
723 flowEntry.flowEntryMatch().inPort().value());
Pankaj Berded0079742013-03-27 17:53:25 -0700724 flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value());
725 flowEntryObj.setInPort(inport);
726 }
727 if (flowEntry.flowEntryMatch().matchEthernetFrameType()) {
728 flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType());
729 }
730 if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) {
731 flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString());
732 }
733 if (flowEntry.flowEntryMatch().matchDstIPv4Net()) {
734 flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString());
735 }
736 if (flowEntry.flowEntryMatch().matchSrcMac()) {
737 flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString());
738 }
739 if (flowEntry.flowEntryMatch().matchDstMac()) {
740 flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString());
741 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700742
743 for (FlowEntryAction fa : flowEntry.flowEntryActions()) {
Pankaj Berded0079742013-03-27 17:53:25 -0700744 if (fa.actionOutput() != null) {
Pavlin Radoslavovb6d1e362013-03-28 03:15:41 -0700745 IPortObject outport =
746 conn.utils().searchPort(conn,
747 flowEntry.dpid().toString(),
748 fa.actionOutput().port().value());
749 flowEntryObj.setActionOutput(fa.actionOutput().port().value());
750 flowEntryObj.setOutPort(outport);
Pankaj Berded0079742013-03-27 17:53:25 -0700751 }
Pavlin Radoslavove2f0de82013-03-12 01:39:30 -0700752 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800753 // TODO: Hacks with hard-coded state names!
754 if (found)
755 flowEntryObj.setUserState("FE_USER_MODIFY");
756 else
757 flowEntryObj.setUserState("FE_USER_ADD");
758 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
759 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700760 // TODO: Take care of the FlowEntryErrorState.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800761 //
762
763 // Flow Entries edges:
764 // Flow
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700765 // NextFE (TODO)
766 if (! found) {
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800767 flowObj.addFlowEntry(flowEntryObj);
Pavlin Radoslavove0575292013-03-28 05:35:25 -0700768 flowEntryObj.setFlow(flowObj);
769 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800770 }
771 conn.endTx(Transaction.COMMIT);
772
773 //
774 // TODO: We need a proper Flow ID allocation mechanism.
775 //
776 flowId.setValue(flowPath.flowId().value());
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700777
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800778 return true;
779 }
780
781 /**
782 * Delete a previously added flow.
783 *
784 * @param flowId the Flow ID of the flow to delete.
785 * @return true on success, otherwise false.
786 */
787 @Override
788 public boolean deleteFlow(FlowId flowId) {
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700789 /*
790 * TODO: Commented-out for now
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700791 if (flowId.value() == measurementFlowId) {
792 modifiedMeasurementFlowTime = System.nanoTime();
793 }
Pavlin Radoslavov7e154fd2013-04-04 11:15:37 -0700794 */
Pavlin Radoslavov571cff92013-03-20 02:01:32 -0700795
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800796 IFlowPath flowObj = null;
797 //
798 // We just mark the entries for deletion,
799 // and let the switches remove each individual entry after
800 // it has been removed from the switches.
801 //
802 try {
803 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
804 != null) {
805 log.debug("Deleting FlowPath with FlowId {}: found existing FlowPath",
806 flowId.toString());
807 } else {
808 log.debug("Deleting FlowPath with FlowId {}: FlowPath not found",
809 flowId.toString());
810 }
811 } catch (Exception e) {
812 // TODO: handle exceptions
813 conn.endTx(Transaction.ROLLBACK);
814 log.error(":deleteFlow FlowId:{} failed", flowId.toString());
815 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700816 if (flowObj == null) {
817 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800818 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700819 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800820
821 //
822 // Find and mark for deletion all Flow Entries
823 //
824 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
825 boolean empty = true; // TODO: an ugly hack
826 for (IFlowEntry flowEntryObj : flowEntries) {
827 empty = false;
828 // flowObj.removeFlowEntry(flowEntryObj);
829 // conn.utils().removeFlowEntry(conn, flowEntryObj);
830 flowEntryObj.setUserState("FE_USER_DELETE");
831 flowEntryObj.setSwitchState("FE_SWITCH_NOT_UPDATED");
832 }
833 // Remove from the database empty flows
834 if (empty)
835 conn.utils().removeFlowPath(conn, flowObj);
836 conn.endTx(Transaction.COMMIT);
837
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800838 return true;
839 }
840
841 /**
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700842 * Clear the state for a previously added flow.
843 *
844 * @param flowId the Flow ID of the flow to clear.
845 * @return true on success, otherwise false.
846 */
847 @Override
848 public boolean clearFlow(FlowId flowId) {
849 IFlowPath flowObj = null;
850 try {
851 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
852 != null) {
853 log.debug("Clearing FlowPath with FlowId {}: found existing FlowPath",
854 flowId.toString());
855 } else {
856 log.debug("Clearing FlowPath with FlowId {}: FlowPath not found",
857 flowId.toString());
858 }
859 } catch (Exception e) {
860 // TODO: handle exceptions
861 conn.endTx(Transaction.ROLLBACK);
862 log.error(":clearFlow FlowId:{} failed", flowId.toString());
863 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700864 if (flowObj == null) {
865 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700866 return true; // OK: No such flow
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700867 }
Pavlin Radoslavov916832f2013-03-14 17:48:41 -0700868
869 //
870 // Remove all Flow Entries
871 //
872 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
873 for (IFlowEntry flowEntryObj : flowEntries) {
874 flowObj.removeFlowEntry(flowEntryObj);
875 conn.utils().removeFlowEntry(conn, flowEntryObj);
876 }
877 // Remove the Flow itself
878 conn.utils().removeFlowPath(conn, flowObj);
879 conn.endTx(Transaction.COMMIT);
880
881 return true;
882 }
883
884 /**
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800885 * Get a previously added flow.
886 *
887 * @param flowId the Flow ID of the flow to get.
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800888 * @return the Flow Path if found, otherwise null.
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -0800889 */
890 @Override
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800891 public FlowPath getFlow(FlowId flowId) {
892 IFlowPath flowObj = null;
893 try {
894 if ((flowObj = conn.utils().searchFlowPath(conn, flowId))
895 != null) {
896 log.debug("Get FlowPath with FlowId {}: found existing FlowPath",
897 flowId.toString());
898 } else {
899 log.debug("Get FlowPath with FlowId {}: FlowPath not found",
900 flowId.toString());
901 }
902 } catch (Exception e) {
903 // TODO: handle exceptions
904 conn.endTx(Transaction.ROLLBACK);
905 log.error(":getFlow FlowId:{} failed", flowId.toString());
906 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700907 if (flowObj == null) {
908 conn.endTx(Transaction.COMMIT);
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800909 return null; // Flow not found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -0700910 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -0800911
912 //
913 // Extract the Flow state
914 //
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800915 FlowPath flowPath = extractFlowPath(flowObj);
916 conn.endTx(Transaction.COMMIT);
917
918 return flowPath;
919 }
920
921 /**
922 * Get all previously added flows by a specific installer for a given
923 * data path endpoints.
924 *
925 * @param installerId the Caller ID of the installer of the flow to get.
926 * @param dataPathEndpoints the data path endpoints of the flow to get.
927 * @return the Flow Paths if found, otherwise null.
928 */
929 @Override
930 public ArrayList<FlowPath> getAllFlows(CallerId installerId,
931 DataPathEndpoints dataPathEndpoints) {
932 //
933 // TODO: The implementation below is not optimal:
934 // We fetch all flows, and then return only the subset that match
935 // the query conditions.
936 // We should use the appropriate Titan/Gremlin query to filter-out
937 // the flows as appropriate.
938 //
939 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700940 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800941
942 if (allFlows == null) {
943 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700944 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800945 }
946
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800947 for (FlowPath flow : allFlows) {
948 //
949 // TODO: String-based comparison is sub-optimal.
950 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -0800951 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800952 //
953 if (! flow.installerId().toString().equals(installerId.toString()))
954 continue;
955 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
956 continue;
957 }
958 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
959 continue;
960 }
961 flowPaths.add(flow);
962 }
963
964 if (flowPaths.isEmpty()) {
965 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: no FlowPaths found", installerId, dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800966 } else {
967 log.debug("Get FlowPaths for installerId{} and dataPathEndpoints{}: FlowPaths are found", installerId, dataPathEndpoints);
968 }
969
970 return flowPaths;
971 }
972
973 /**
974 * Get all installed flows by all installers for given data path endpoints.
975 *
976 * @param dataPathEndpoints the data path endpoints of the flows to get.
977 * @return the Flow Paths if found, otherwise null.
978 */
979 @Override
980 public ArrayList<FlowPath> getAllFlows(DataPathEndpoints dataPathEndpoints) {
981 //
982 // TODO: The implementation below is not optimal:
983 // We fetch all flows, and then return only the subset that match
984 // the query conditions.
985 // We should use the appropriate Titan/Gremlin query to filter-out
986 // the flows as appropriate.
987 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700988 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
989 ArrayList<FlowPath> allFlows = getAllFlows();
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800990
991 if (allFlows == null) {
992 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -0700993 return flowPaths;
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800994 }
995
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800996 for (FlowPath flow : allFlows) {
997 //
998 // TODO: String-based comparison is sub-optimal.
999 // We are using it for now to save us the extra work of
Pavlin Radoslavovc4e76a62013-03-06 10:52:41 -08001000 // implementing the "equals()" and "hashCode()" methods.
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001001 //
1002 if (! flow.dataPath().srcPort().toString().equals(dataPathEndpoints.srcPort().toString())) {
1003 continue;
1004 }
1005 if (! flow.dataPath().dstPort().toString().equals(dataPathEndpoints.dstPort().toString())) {
1006 continue;
1007 }
1008 flowPaths.add(flow);
1009 }
1010
1011 if (flowPaths.isEmpty()) {
1012 log.debug("Get FlowPaths for dataPathEndpoints{}: no FlowPaths found", dataPathEndpoints);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001013 } else {
1014 log.debug("Get FlowPaths for dataPathEndpoints{}: FlowPaths are found", dataPathEndpoints);
1015 }
1016
1017 return flowPaths;
1018 }
1019
1020 /**
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001021 * Get summary of all installed flows by all installers in a given range
1022 *
1023 * @param flowId the data path endpoints of the flows to get.
1024 * @param maxFlows: the maximum number of flows to be returned
1025 * @return the Flow Paths if found, otherwise null.
1026 */
1027 @Override
1028 public ArrayList<FlowPath> getAllFlowsSummary(FlowId flowId, int maxFlows) {
1029 //
1030 // TODO: The implementation below is not optimal:
1031 // We fetch all flows, and then return only the subset that match
1032 // the query conditions.
1033 // We should use the appropriate Titan/Gremlin query to filter-out
1034 // the flows as appropriate.
1035 //
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001036 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
1037
1038 ArrayList<FlowPath> allFlows = getAllFlows();
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001039
1040 if (allFlows == null) {
1041 log.debug("Get FlowPathsSummary for {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001042 return flowPaths;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001043 }
1044
Umesh Krishnaswamy244b4ae2013-03-29 12:05:15 -07001045 Collections.sort(allFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001046
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001047 for (FlowPath flow : allFlows) {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001048 flow.setFlowEntryMatch(null);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001049
1050 // start from desired flowId
Umesh Krishnaswamy2a82c642013-03-29 08:27:17 -07001051 //if (flow.flowId().value() < flowId.value()) {
1052 // continue;
1053 //}
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001054
1055 // Summarize by making null flow entry fields that are not relevant to report
1056 for (FlowEntry flowEntry : flow.dataPath().flowEntries()) {
1057 flowEntry.setFlowEntryActions(null);
1058 flowEntry.setFlowEntryMatch(null);
1059 }
1060
1061 flowPaths.add(flow);
1062 if (maxFlows != 0 && flowPaths.size() >= maxFlows) {
1063 break;
1064 }
1065 }
1066
1067 if (flowPaths.isEmpty()) {
1068 log.debug("Get FlowPathsSummary {} {}: no FlowPaths found", flowId, maxFlows);
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001069 } else {
1070 log.debug("Get FlowPathsSummary for {} {}: FlowPaths were found", flowId, maxFlows);
1071 }
1072
1073 return flowPaths;
1074 }
1075
1076 /**
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001077 * Get all installed flows by all installers.
1078 *
1079 * @return the Flow Paths if found, otherwise null.
1080 */
1081 @Override
1082 public ArrayList<FlowPath> getAllFlows() {
1083 Iterable<IFlowPath> flowPathsObj = null;
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001084 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001085
1086 try {
1087 if ((flowPathsObj = conn.utils().getAllFlowPaths(conn)) != null) {
1088 log.debug("Get all FlowPaths: found FlowPaths");
1089 } else {
1090 log.debug("Get all FlowPaths: no FlowPaths found");
1091 }
1092 } catch (Exception e) {
1093 // TODO: handle exceptions
1094 conn.endTx(Transaction.ROLLBACK);
1095 log.error(":getAllFlowPaths failed");
1096 }
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001097 if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) {
1098 conn.endTx(Transaction.COMMIT);
Umesh Krishnaswamyea0f4ab2013-03-26 18:49:35 -07001099 return flowPaths; // No Flows found
Pavlin Radoslavov89c8f432013-03-15 18:50:46 -07001100 }
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001101
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001102 for (IFlowPath flowObj : flowPathsObj) {
1103 //
1104 // Extract the Flow state
1105 //
1106 FlowPath flowPath = extractFlowPath(flowObj);
Pavlin Radoslavov3f2af732013-03-29 15:29:35 -07001107 if (flowPath != null)
1108 flowPaths.add(flowPath);
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001109 }
1110
1111 conn.endTx(Transaction.COMMIT);
1112
1113 return flowPaths;
1114 }
1115
1116 /**
1117 * Extract Flow Path State from a Titan Database Object @ref IFlowPath.
1118 *
1119 * @param flowObj the object to extract the Flow Path State from.
1120 * @return the extracted Flow Path State.
1121 */
1122 private FlowPath extractFlowPath(IFlowPath flowObj) {
Pavlin Radoslavov706df052013-03-06 10:49:07 -08001123 //
1124 // Extract the Flow state
1125 //
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001126 String flowIdStr = flowObj.getFlowId();
1127 String installerIdStr = flowObj.getInstallerId();
1128 String srcSwitchStr = flowObj.getSrcSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001129 Short srcPortShort = flowObj.getSrcPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001130 String dstSwitchStr = flowObj.getDstSwitch();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001131 Short dstPortShort = flowObj.getDstPort();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001132
1133 if ((flowIdStr == null) ||
1134 (installerIdStr == null) ||
1135 (srcSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001136 (srcPortShort == null) ||
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001137 (dstSwitchStr == null) ||
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001138 (dstPortShort == null)) {
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001139 // TODO: A work-around, becauuse of some bogus database objects
1140 return null;
1141 }
1142
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001143 FlowPath flowPath = new FlowPath();
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001144 flowPath.setFlowId(new FlowId(flowIdStr));
1145 flowPath.setInstallerId(new CallerId(installerIdStr));
1146 flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001147 flowPath.dataPath().srcPort().setPort(new Port(srcPortShort));
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001148 flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr));
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001149 flowPath.dataPath().dstPort().setPort(new Port(dstPortShort));
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001150 //
1151 // Extract the match conditions common for all Flow Entries
1152 //
1153 {
1154 FlowEntryMatch match = new FlowEntryMatch();
1155 Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType();
1156 if (matchEthernetFrameType != null)
1157 match.enableEthernetFrameType(matchEthernetFrameType);
1158 String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net();
1159 if (matchSrcIPv4Net != null)
1160 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1161 String matchDstIPv4Net = flowObj.getMatchDstIPv4Net();
1162 if (matchDstIPv4Net != null)
1163 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1164 String matchSrcMac = flowObj.getMatchSrcMac();
1165 if (matchSrcMac != null)
1166 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1167 String matchDstMac = flowObj.getMatchDstMac();
1168 if (matchDstMac != null)
1169 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1170 flowPath.setFlowEntryMatch(match);
1171 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001172
1173 //
1174 // Extract all Flow Entries
1175 //
1176 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
1177 for (IFlowEntry flowEntryObj : flowEntries) {
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001178 FlowEntry flowEntry = extractFlowEntry(flowEntryObj);
1179 if (flowEntry == null)
Pavlin Radoslavovc2877682013-03-27 16:40:07 -07001180 continue;
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001181 flowPath.dataPath().flowEntries().add(flowEntry);
1182 }
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -08001183
1184 return flowPath;
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001185 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001186
1187 /**
Pavlin Radoslavov99b12752013-04-04 17:28:06 -07001188 * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry.
1189 *
1190 * @param flowEntryObj the object to extract the Flow Entry State from.
1191 * @return the extracted Flow Entry State.
1192 */
1193 private FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) {
1194 String flowEntryIdStr = flowEntryObj.getFlowEntryId();
1195 String switchDpidStr = flowEntryObj.getSwitchDpid();
1196 String userState = flowEntryObj.getUserState();
1197 String switchState = flowEntryObj.getSwitchState();
1198
1199 if ((flowEntryIdStr == null) ||
1200 (switchDpidStr == null) ||
1201 (userState == null) ||
1202 (switchState == null)) {
1203 // TODO: A work-around, becauuse of some bogus database objects
1204 return null;
1205 }
1206
1207 FlowEntry flowEntry = new FlowEntry();
1208 flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr));
1209 flowEntry.setDpid(new Dpid(switchDpidStr));
1210
1211 //
1212 // Extract the match conditions
1213 //
1214 FlowEntryMatch match = new FlowEntryMatch();
1215 Short matchInPort = flowEntryObj.getMatchInPort();
1216 if (matchInPort != null)
1217 match.enableInPort(new Port(matchInPort));
1218 Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType();
1219 if (matchEthernetFrameType != null)
1220 match.enableEthernetFrameType(matchEthernetFrameType);
1221 String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net();
1222 if (matchSrcIPv4Net != null)
1223 match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net));
1224 String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net();
1225 if (matchDstIPv4Net != null)
1226 match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net));
1227 String matchSrcMac = flowEntryObj.getMatchSrcMac();
1228 if (matchSrcMac != null)
1229 match.enableSrcMac(MACAddress.valueOf(matchSrcMac));
1230 String matchDstMac = flowEntryObj.getMatchDstMac();
1231 if (matchDstMac != null)
1232 match.enableDstMac(MACAddress.valueOf(matchDstMac));
1233 flowEntry.setFlowEntryMatch(match);
1234
1235 //
1236 // Extract the actions
1237 //
1238 ArrayList<FlowEntryAction> actions = new ArrayList<FlowEntryAction>();
1239 Short actionOutputPort = flowEntryObj.getActionOutput();
1240 if (actionOutputPort != null) {
1241 FlowEntryAction action = new FlowEntryAction();
1242 action.setActionOutput(new Port(actionOutputPort));
1243 actions.add(action);
1244 }
1245 flowEntry.setFlowEntryActions(actions);
1246 flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState));
1247 flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState));
1248 //
1249 // TODO: Take care of the FlowEntryMatch, FlowEntryAction set,
1250 // and FlowEntryErrorState.
1251 //
1252 return flowEntry;
1253 }
1254
1255 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001256 * Add and maintain a shortest-path flow.
1257 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001258 * NOTE: The Flow Path argument does NOT contain flow entries.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001259 *
1260 * @param flowPath the Flow Path with the endpoints and the match
1261 * conditions to install.
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001262 * @return the added shortest-path flow on success, otherwise null.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001263 */
1264 @Override
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001265 public FlowPath addAndMaintainShortestPathFlow(FlowPath flowPath) {
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001266 String dataPathSummaryStr = null;
1267
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001268 //
1269 // Do the shortest path computation
1270 //
1271 DataPath dataPath =
1272 topoRouteService.getShortestPath(flowPath.dataPath().srcPort(),
1273 flowPath.dataPath().dstPort());
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001274 if (dataPath == null) {
1275 // We need the DataPath to populate the Network MAP
1276 dataPath = new DataPath();
1277 dataPath.setSrcPort(flowPath.dataPath().srcPort());
1278 dataPath.setDstPort(flowPath.dataPath().dstPort());
1279 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001280
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001281 // Compute the Data Path summary
1282 dataPathSummaryStr = dataPath.dataPathSummary();
1283
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001284 //
1285 // Set the incoming port matching and the outgoing port output
1286 // actions for each flow entry.
1287 //
1288 for (FlowEntry flowEntry : dataPath.flowEntries()) {
1289 // Set the incoming port matching
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001290 FlowEntryMatch flowEntryMatch = new FlowEntryMatch();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001291 flowEntry.setFlowEntryMatch(flowEntryMatch);
1292 flowEntryMatch.enableInPort(flowEntry.inPort());
1293
1294 // Set the outgoing port output action
1295 ArrayList<FlowEntryAction> flowEntryActions = flowEntry.flowEntryActions();
1296 if (flowEntryActions == null) {
1297 flowEntryActions = new ArrayList<FlowEntryAction>();
1298 flowEntry.setFlowEntryActions(flowEntryActions);
1299 }
1300 FlowEntryAction flowEntryAction = new FlowEntryAction();
1301 flowEntryAction.setActionOutput(flowEntry.outPort());
1302 flowEntryActions.add(flowEntryAction);
1303 }
1304
1305 //
1306 // Prepare the computed Flow Path
1307 //
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001308 FlowPath computedFlowPath = new FlowPath();
1309 computedFlowPath.setFlowId(new FlowId(flowPath.flowId().value()));
1310 computedFlowPath.setInstallerId(new CallerId(flowPath.installerId().value()));
1311 computedFlowPath.setDataPath(dataPath);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001312 computedFlowPath.setFlowEntryMatch(new FlowEntryMatch(flowPath.flowEntryMatch()));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001313
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001314 FlowId flowId = new FlowId();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001315 if (! addFlow(computedFlowPath, flowId, dataPathSummaryStr))
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001316 return null;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001317
1318 // TODO: Mark the flow for maintenance purpose
1319
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001320 return (computedFlowPath);
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001321 }
1322
1323 /**
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001324 * Reconcile all flows in a set.
1325 *
1326 * @param flowObjSet the set of flows that need to be reconciliated.
1327 */
1328 public void reconcileFlows(Iterable<IFlowPath> flowObjSet) {
1329 if (! flowObjSet.iterator().hasNext())
1330 return;
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001331
1332 //
1333 // Remove the old Flow Entries, and add the new Flow Entries
1334 //
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001335
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001336 Map<Long, IOFSwitch> mySwitches = floodlightProvider.getSwitches();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001337 LinkedList<FlowPath> flowPaths = new LinkedList<FlowPath>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001338 for (IFlowPath flowObj : flowObjSet) {
1339 FlowPath flowPath = extractFlowPath(flowObj);
1340 if (flowPath == null)
1341 continue;
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001342 flowPaths.add(flowPath);
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001343
1344 //
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001345 // Remove the Flow Entries from the Network MAP
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001346 //
1347 Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries();
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001348 LinkedList<IFlowEntry> deleteFlowEntries = new LinkedList<IFlowEntry>();
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001349 for (IFlowEntry flowEntryObj : flowEntries) {
1350 String dpidStr = flowEntryObj.getSwitchDpid();
1351 if (dpidStr == null)
1352 continue;
1353 Dpid dpid = new Dpid(dpidStr);
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001354 /*
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001355 IOFSwitch mySwitch = mySwitches.get(dpid.value());
1356 if (mySwitch == null)
1357 continue; // Ignore the entry: not my switch
Pavlin Radoslavovb2b6d4f2013-04-01 21:27:31 +00001358 */
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001359 deleteFlowEntries.add(flowEntryObj);
1360 }
1361 for (IFlowEntry flowEntryObj : deleteFlowEntries) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001362 flowObj.removeFlowEntry(flowEntryObj);
1363 conn.utils().removeFlowEntry(conn, flowEntryObj);
1364 }
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001365 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001366
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -07001367 for (FlowPath flowPath : flowPaths) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001368 //
1369 // Delete the flow entries from the switches
1370 //
1371 for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) {
1372 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_DELETE);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001373 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1374 if (mySwitch == null) {
1375 // Not my switch
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001376 installRemoteFlowEntry(flowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001377 } else {
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001378 installFlowEntry(mySwitch, flowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001379 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001380 }
1381
1382 //
1383 // Calculate the new shortest path and install it in the
1384 // Network MAP.
1385 //
1386 FlowPath addedFlowPath = addAndMaintainShortestPathFlow(flowPath);
1387 if (addedFlowPath == null) {
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001388 log.error("Cannot add Shortest Path Flow from {} to {}: path not found?",
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001389 flowPath.dataPath().srcPort().toString(),
1390 flowPath.dataPath().dstPort().toString());
1391 continue;
1392 }
1393
1394 //
1395 // Add the flow entries to the switches
1396 //
1397 for (FlowEntry flowEntry : addedFlowPath.dataPath().flowEntries()) {
1398 flowEntry.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001399 IOFSwitch mySwitch = mySwitches.get(flowEntry.dpid().value());
1400 if (mySwitch == null) {
1401 // Not my switch
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001402 installRemoteFlowEntry(addedFlowPath, flowEntry);
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001403 continue;
1404 }
1405
1406 IFlowEntry flowEntryObj =
1407 conn.utils().searchFlowEntry(conn, flowEntry.flowEntryId());
1408 if (flowEntryObj == null) {
1409 //
1410 // TODO: Remove the "new Object[] wrapper in the statement
1411 // below after the SLF4J logger is upgraded to
1412 // Version 1.7.5
1413 //
1414 log.error("Cannot add Flow Entry to switch {} for Path Flow from {} to {} : Flow Entry not in the Network MAP",
1415 new Object[] {
1416 flowEntry.dpid(),
1417 flowPath.dataPath().srcPort(),
1418 flowPath.dataPath().dstPort()
1419 });
1420 continue;
1421 }
1422 // Update the Flow Entry Switch State in the Network MAP
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001423 if (installFlowEntry(mySwitch, addedFlowPath, flowEntry)) {
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001424 flowEntryObj.setSwitchState("FE_SWITCH_UPDATED");
1425 }
Pavlin Radoslavove0575292013-03-28 05:35:25 -07001426 }
1427 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001428 }
1429
1430 /**
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001431 * Install a Flow Entry on a switch.
1432 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001433 * @param mySwitch the switch to install the Flow Entry into.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001434 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001435 * @param flowEntry the flow entry to install.
1436 * @return true on success, otherwise false.
1437 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001438 public boolean installFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1439 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001440 //
1441 // Create the OpenFlow Flow Modification Entry to push
1442 //
1443 OFFlowMod fm = (OFFlowMod) floodlightProvider.getOFMessageFactory()
1444 .getMessage(OFType.FLOW_MOD);
1445 long cookie = flowEntry.flowEntryId().value();
1446
1447 short flowModCommand = OFFlowMod.OFPFC_ADD;
1448 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
1449 flowModCommand = OFFlowMod.OFPFC_ADD;
1450 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
1451 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
1452 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
1453 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
1454 } else {
1455 // Unknown user state. Ignore the entry
1456 log.debug("Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
1457 flowEntry.flowEntryId().toString(),
1458 flowEntry.flowEntryUserState());
1459 return false;
1460 }
1461
1462 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001463 // Fetch the match conditions.
1464 //
1465 // NOTE: The Flow matching conditions common for all Flow Entries are
1466 // used ONLY if a Flow Entry does NOT have the corresponding matching
1467 // condition set.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001468 //
1469 OFMatch match = new OFMatch();
1470 match.setWildcards(OFMatch.OFPFW_ALL);
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001471 FlowEntryMatch flowPathMatch = flowPath.flowEntryMatch();
1472 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
1473
1474 // Match the Incoming Port
1475 Port matchInPort = flowEntryMatch.inPort();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001476 if (matchInPort != null) {
1477 match.setInputPort(matchInPort.value());
1478 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
1479 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001480
1481 // Match the Ethernet Frame Type
1482 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
1483 if ((matchEthernetFrameType == null) && (flowPathMatch != null)) {
1484 matchEthernetFrameType = flowPathMatch.ethernetFrameType();
1485 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001486 if (matchEthernetFrameType != null) {
1487 match.setDataLayerType(matchEthernetFrameType);
1488 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
1489 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001490
1491 // Match the Source IPv4 Network prefix
1492 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
1493 if ((matchSrcIPv4Net == null) && (flowPathMatch != null)) {
1494 matchSrcIPv4Net = flowPathMatch.srcIPv4Net();
1495 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001496 if (matchSrcIPv4Net != null) {
1497 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
1498 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001499
1500 // Natch the Destination IPv4 Network prefix
1501 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
1502 if ((matchDstIPv4Net == null) && (flowPathMatch != null)) {
1503 matchDstIPv4Net = flowPathMatch.dstIPv4Net();
1504 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001505 if (matchDstIPv4Net != null) {
1506 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
1507 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001508
1509 // Match the Source MAC address
1510 MACAddress matchSrcMac = flowEntryMatch.srcMac();
1511 if ((matchSrcMac == null) && (flowPathMatch != null)) {
1512 matchSrcMac = flowPathMatch.srcMac();
1513 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001514 if (matchSrcMac != null) {
1515 match.setDataLayerSource(matchSrcMac.toString());
1516 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
1517 }
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001518
1519 // Match the Destination MAC address
1520 MACAddress matchDstMac = flowEntryMatch.dstMac();
1521 if ((matchDstMac == null) && (flowPathMatch != null)) {
1522 matchDstMac = flowPathMatch.dstMac();
1523 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001524 if (matchDstMac != null) {
1525 match.setDataLayerDestination(matchDstMac.toString());
1526 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
1527 }
1528
1529 //
1530 // Fetch the actions
1531 //
1532 // TODO: For now we support only the "OUTPUT" actions.
1533 //
1534 fm.setOutPort(OFPort.OFPP_NONE.getValue());
1535 List<OFAction> actions = new ArrayList<OFAction>();
1536 ArrayList<FlowEntryAction> flowEntryActions =
1537 flowEntry.flowEntryActions();
1538 for (FlowEntryAction flowEntryAction : flowEntryActions) {
1539 FlowEntryAction.ActionOutput actionOutput =
1540 flowEntryAction.actionOutput();
1541 if (actionOutput != null) {
1542 short actionOutputPort = actionOutput.port().value();
1543 OFActionOutput action = new OFActionOutput();
1544 // XXX: The max length is hard-coded for now
1545 action.setMaxLength((short)0xffff);
1546 action.setPort(actionOutputPort);
1547 actions.add(action);
1548 if ((flowModCommand == OFFlowMod.OFPFC_DELETE) ||
1549 (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
1550 fm.setOutPort(actionOutputPort);
1551 }
1552 }
1553 }
1554
1555 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
1556 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
1557 .setPriority(PRIORITY_DEFAULT)
1558 .setBufferId(OFPacketOut.BUFFER_ID_NONE)
1559 .setCookie(cookie)
1560 .setCommand(flowModCommand)
1561 .setMatch(match)
1562 .setActions(actions)
1563 .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH);
1564
1565 //
1566 // TODO: Set the following flag
1567 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
1568 // See method ForwardingBase::pushRoute()
1569 //
1570
1571 //
1572 // Write the message to the switch
1573 //
1574 try {
1575 messageDamper.write(mySwitch, fm, null);
1576 mySwitch.flush();
1577 } catch (IOException e) {
1578 log.error("Failure writing flow mod from network map", e);
1579 return false;
1580 }
1581 return true;
1582 }
1583
1584 /**
1585 * Remove a Flow Entry from a switch.
1586 *
Pavlin Radoslavov2b858f82013-03-28 11:37:37 -07001587 * @param mySwitch the switch to remove the Flow Entry from.
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001588 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001589 * @param flowEntry the flow entry to remove.
1590 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001591 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001592 public boolean removeFlowEntry(IOFSwitch mySwitch, FlowPath flowPath,
1593 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001594 //
1595 // The installFlowEntry() method implements both installation
1596 // and removal of flow entries.
1597 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001598 return (installFlowEntry(mySwitch, flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001599 }
1600
1601 /**
1602 * Install a Flow Entry on a remote controller.
1603 *
1604 * TODO: We need it now: Jono
1605 * - For now it will make a REST call to the remote controller.
1606 * - Internally, it needs to know the name of the remote controller.
1607 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001608 * @param flowPath the flow path for the flow entry to install.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001609 * @param flowEntry the flow entry to install.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001610 * @return true on success, otherwise false.
1611 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001612 public boolean installRemoteFlowEntry(FlowPath flowPath,
1613 FlowEntry flowEntry) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001614 // TODO: We need it now: Jono
1615 // - For now it will make a REST call to the remote controller.
1616 // - Internally, it needs to know the name of the remote controller.
1617 return true;
1618 }
1619
1620 /**
1621 * Remove a flow entry on a remote controller.
1622 *
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001623 * @param flowPath the flow path for the flow entry to remove.
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001624 * @param flowEntry the flow entry to remove.
1625 * @return true on success, otherwise false.
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001626 */
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001627 public boolean removeRemoteFlowEntry(FlowPath flowPath,
1628 FlowEntry flowEntry) {
Pavlin Radoslavov6b6f4a82013-03-28 03:30:00 -07001629 //
1630 // The installRemoteFlowEntry() method implements both installation
1631 // and removal of flow entries.
1632 //
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -07001633 return (installRemoteFlowEntry(flowPath, flowEntry));
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07001634 }
Pavlin Radoslavov9e5344c2013-02-18 09:58:30 -08001635}