blob: df70dd423cc5d103fae738ac05211848ecbca158 [file] [log] [blame]
Jonathan Hart0961fe82014-04-03 09:56:25 -07001package net.onrc.onos.apps.forwarding;
Jonathan Hart1caaa932013-11-04 15:28:28 -08002
Jonathan Hart41d1e912013-11-24 16:50:25 -08003import java.util.ArrayList;
Jonathan Harte93aed42013-12-05 18:39:50 -08004import java.util.Collection;
Jonathan Hartd33a6cf2013-12-10 14:29:08 -08005import java.util.HashMap;
Jonathan Hart1caaa932013-11-04 15:28:28 -08006import java.util.Iterator;
TeruUf9111652014-05-14 23:10:35 -07007import java.util.LinkedList;
Jonathan Hart41d1e912013-11-24 16:50:25 -08008import java.util.List;
Jonathan Harte93aed42013-12-05 18:39:50 -08009import java.util.Map;
TeruU417fe022014-02-04 12:59:30 -080010import java.util.concurrent.Executors;
11import java.util.concurrent.ScheduledExecutorService;
12import java.util.concurrent.TimeUnit;
Jonathan Hart1caaa932013-11-04 15:28:28 -080013
Jonathan Harte93aed42013-12-05 18:39:50 -080014import net.floodlightcontroller.core.module.FloodlightModuleContext;
15import net.floodlightcontroller.core.module.IFloodlightModule;
16import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080017import net.floodlightcontroller.util.MACAddress;
Jonathan Hartf5bd2582014-04-09 17:43:41 -070018import net.onrc.onos.api.packet.IPacketListener;
19import net.onrc.onos.api.packet.IPacketService;
Jonathan Hart0961fe82014-04-03 09:56:25 -070020import net.onrc.onos.apps.proxyarp.IProxyArpService;
Jonathan Hart23701d12014-04-03 10:45:48 -070021import net.onrc.onos.core.devicemanager.IOnosDeviceService;
Jonathan Hartaa380972014-04-03 10:24:46 -070022import net.onrc.onos.core.intent.Intent;
Jonathan Harta99ec672014-04-03 11:30:34 -070023import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hartaa380972014-04-03 10:24:46 -070024import net.onrc.onos.core.intent.IntentMap;
TeruUf9111652014-05-14 23:10:35 -070025import net.onrc.onos.core.intent.IntentMap.ChangedEvent;
26import net.onrc.onos.core.intent.IntentMap.ChangedListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070027import net.onrc.onos.core.intent.IntentOperation;
28import net.onrc.onos.core.intent.IntentOperationList;
29import net.onrc.onos.core.intent.PathIntent;
30import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hartaa380972014-04-03 10:24:46 -070031import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070032import net.onrc.onos.core.packet.Ethernet;
33import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart472062d2014-04-03 10:56:48 -070034import net.onrc.onos.core.topology.Device;
Jonathan Harte37e4e22014-05-13 19:12:02 -070035import net.onrc.onos.core.topology.ITopologyService;
Jonathan Hart472062d2014-04-03 10:56:48 -070036import net.onrc.onos.core.topology.LinkEvent;
Jonathan Hartf5bd2582014-04-09 17:43:41 -070037import net.onrc.onos.core.topology.Port;
Jonathan Hart472062d2014-04-03 10:56:48 -070038import net.onrc.onos.core.topology.Switch;
TeruUf9111652014-05-14 23:10:35 -070039import net.onrc.onos.core.topology.Topology;
Jonathan Hart23701d12014-04-03 10:45:48 -070040import net.onrc.onos.core.util.Dpid;
41import net.onrc.onos.core.util.FlowPath;
Jonathan Hart23701d12014-04-03 10:45:48 -070042import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080043
Jonathan Hart1caaa932013-11-04 15:28:28 -080044import org.openflow.util.HexString;
45import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
Jonathan Hartd857ad62013-12-14 18:08:17 -080048import com.google.common.collect.LinkedListMultimap;
49import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080050
Jonathan Hartf5bd2582014-04-09 17:43:41 -070051public class Forwarding implements /*IOFMessageListener,*/ IFloodlightModule,
TeruUf9111652014-05-14 23:10:35 -070052 IForwardingService, IPacketListener, ChangedListener {
Ray Milkeyec838942014-04-09 11:28:43 -070053 private static final Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU7feef8a2014-04-03 00:15:49 -070054
Ray Milkey2476cac2014-04-08 11:03:21 -070055 private static final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
Ray Milkeyec838942014-04-09 11:28:43 -070056 private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
TeruU30c0c932014-05-15 16:47:41 -070057 private static final int SRC_SWITCH_TIMEOUT_ADJUST_SECOND = 2;
58 private static final int DEFAULT_IDLE_TIMEOUT = 5;
59 private int idleTimeout = DEFAULT_IDLE_TIMEOUT;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070060
Ray Milkeyec838942014-04-09 11:28:43 -070061 private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Harte93aed42013-12-05 18:39:50 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 private final String callerId = "Forwarding";
Jonathan Harte93aed42013-12-05 18:39:50 -080064
Jonathan Hartf5bd2582014-04-09 17:43:41 -070065 private IPacketService packetService;
Ray Milkey269ffb92014-04-03 14:43:30 -070066 private IControllerRegistryService controllerRegistryService;
67
Jonathan Harte37e4e22014-05-13 19:12:02 -070068 private ITopologyService topologyService;
69 private Topology topology;
Ray Milkey269ffb92014-04-03 14:43:30 -070070 private IPathCalcRuntimeService pathRuntime;
71 private IntentMap intentMap;
72
73 // TODO it seems there is a Guava collection that will time out entries.
74 // We should see if this will work here.
75 private Map<Path, PushedFlow> pendingFlows;
76 private ListMultimap<String, PacketToPush> waitingPackets;
77
78 private final Object lock = new Object();
79
Jonathan Hart8ed69c52014-04-09 13:29:16 -070080 private static class PacketToPush {
Jonathan Hartf5bd2582014-04-09 17:43:41 -070081 public final Ethernet eth;
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public final long dpid;
83
Jonathan Hartf5bd2582014-04-09 17:43:41 -070084 public PacketToPush(Ethernet eth, long dpid) {
85 this.eth = eth;
Ray Milkey269ffb92014-04-03 14:43:30 -070086 this.dpid = dpid;
87 }
88 }
89
Jonathan Hart8ed69c52014-04-09 13:29:16 -070090 private static class PushedFlow {
Ray Milkey269ffb92014-04-03 14:43:30 -070091 public final String intentId;
92 public boolean installed = false;
93 public short firstOutPort;
94
95 public PushedFlow(String flowId) {
96 this.intentId = flowId;
97 }
98 }
99
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700100 private static final class Path {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 public final MACAddress srcMac;
102 public final MACAddress dstMac;
103
104 public Path(MACAddress srcMac, MACAddress dstMac) {
105 this.srcMac = srcMac;
106 this.dstMac = dstMac;
107 }
108
109 @Override
110 public boolean equals(Object other) {
111 if (!(other instanceof Path)) {
112 return false;
113 }
114
115 Path otherPath = (Path) other;
116 return srcMac.equals(otherPath.srcMac) &&
117 dstMac.equals(otherPath.dstMac);
118 }
119
120 @Override
121 public int hashCode() {
122 int hash = 17;
123 hash = 31 * hash + srcMac.hashCode();
124 hash = 31 * hash + dstMac.hashCode();
125 return hash;
126 }
127
128 @Override
129 public String toString() {
130 return "(" + srcMac + ") => (" + dstMac + ")";
131 }
132 }
133
134 @Override
135 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
136 List<Class<? extends IFloodlightService>> services =
137 new ArrayList<Class<? extends IFloodlightService>>(1);
138 services.add(IForwardingService.class);
139 return services;
140 }
141
142 @Override
143 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
144 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
145 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
146 impls.put(IForwardingService.class, this);
147 return impls;
148 }
149
150 @Override
151 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
152 List<Class<? extends IFloodlightService>> dependencies =
153 new ArrayList<Class<? extends IFloodlightService>>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 dependencies.add(IControllerRegistryService.class);
155 dependencies.add(IOnosDeviceService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700156 dependencies.add(ITopologyService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700157 dependencies.add(IPathCalcRuntimeService.class);
158 // We don't use the IProxyArpService directly, but reactive forwarding
159 // requires it to be loaded and answering ARP requests
160 dependencies.add(IProxyArpService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700161 dependencies.add(IPacketService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 return dependencies;
163 }
164
165 @Override
166 public void init(FloodlightModuleContext context) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700168 topologyService = context.getServiceImpl(ITopologyService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700169 pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700170 packetService = context.getServiceImpl(IPacketService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700171
Ray Milkey269ffb92014-04-03 14:43:30 -0700172 pendingFlows = new HashMap<Path, PushedFlow>();
173 waitingPackets = LinkedListMultimap.create();
174 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800175
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 @Override
177 public void startUp(FloodlightModuleContext context) {
TeruU30c0c932014-05-15 16:47:41 -0700178 Map<String, String> configOptions = context.getConfigParams(this);
179
180 try {
181 if (Integer.parseInt(configOptions.get("idletimeout")) > 0) {
182 idleTimeout = Integer.parseInt(configOptions.get("idletimeout"));
183 log.info("idle_timeout for Forwarding is set to {}.", idleTimeout);
184 } else {
185 log.info("idle_timeout for Forwarding is less than 0. Use default {}.", idleTimeout);
186 }
187 } catch (NumberFormatException e) {
188 log.info("idle_timeout related config options were not set. Use default.");
189 }
190
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700191 packetService.registerPacketListener(this);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800192
Jonathan Harte37e4e22014-05-13 19:12:02 -0700193 topology = topologyService.getTopology();
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 intentMap = pathRuntime.getPathIntents();
TeruUf9111652014-05-14 23:10:35 -0700195 intentMap.addChangeListener(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800197
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 @Override
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700199 public void receive(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700200 if (log.isTraceEnabled()) {
201 log.trace("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber());
202 }
TeruU417fe022014-02-04 12:59:30 -0800203
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700204 if (eth.getEtherType() != Ethernet.TYPE_IPV4) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700205 // Only handle IPv4 packets right now
206 return;
Ray Milkey269ffb92014-04-03 14:43:30 -0700207 }
TeruU417fe022014-02-04 12:59:30 -0800208
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 if (eth.isBroadcast() || eth.isMulticast()) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700210 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700211 } else {
212 // Unicast
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700213 handlePacketIn(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 }
TeruU417fe022014-02-04 12:59:30 -0800216
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700217 private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700218 if (log.isTraceEnabled()) {
219 log.trace("Sending broadcast packet to other ONOS instances");
220 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800221
Jonathan Harte3702f22014-04-29 02:56:56 -0700222 packetService.broadcastPacketOutEdge(eth,
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700223 new SwitchPort(sw.getDpid(), inPort.getNumber().shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700224 }
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700225
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700226 private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700227 if (log.isTraceEnabled()) {
228 log.trace("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber());
229 }
TeruU7feef8a2014-04-03 00:15:49 -0700230
Ray Milkey269ffb92014-04-03 14:43:30 -0700231 String destinationMac =
232 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart0444d932014-01-22 15:06:17 -0800233
Ray Milkey269ffb92014-04-03 14:43:30 -0700234 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
Jonathan Harte37e4e22014-05-13 19:12:02 -0700235 Device deviceObject = topology.getDeviceByMac(MACAddress.valueOf(destinationMac));
TeruU6464af02014-02-06 21:38:45 -0800236
Ray Milkey269ffb92014-04-03 14:43:30 -0700237 if (deviceObject == null) {
238 log.debug("No device entry found for {}",
239 destinationMac);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800240
Ray Milkey269ffb92014-04-03 14:43:30 -0700241 //Device is not in the DB, so wait it until the device is added.
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700242 EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, inPort, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
Ray Milkey269ffb92014-04-03 14:43:30 -0700243 return;
244 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800245
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700246 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700247 }
TeruU417fe022014-02-04 12:59:30 -0800248
Ray Milkey269ffb92014-04-03 14:43:30 -0700249 private class WaitDeviceArp implements Runnable {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700250 Switch sw;
251 Port inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700252 Ethernet eth;
TeruU417fe022014-02-04 12:59:30 -0800253
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700254 public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700255 super();
256 this.sw = sw;
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700257 this.inPort = inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700258 this.eth = eth;
259 }
TeruU417fe022014-02-04 12:59:30 -0800260
Ray Milkey269ffb92014-04-03 14:43:30 -0700261 @Override
262 public void run() {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700263 Device deviceObject = topology.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700264 if (deviceObject == null) {
265 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700266 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700267 return;
268 }
269 log.debug("wait {}ms and device {} was found, continue", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700270 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 }
272 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800273
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700274 private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Device deviceObject) {
TeruU7feef8a2014-04-03 00:15:49 -0700275
Ray Milkey269ffb92014-04-03 14:43:30 -0700276 log.debug("Start continuehandlePacketIn");
TeruU7feef8a2014-04-03 00:15:49 -0700277
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
279 Iterator<net.onrc.onos.core.topology.Port> ports = deviceObject.getAttachmentPoints().iterator();
280 if (!ports.hasNext()) {
281 log.debug("No attachment point found for device {} - broadcasting packet",
282 deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700283 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700284 return;
285 }
TeruU7feef8a2014-04-03 00:15:49 -0700286
Ray Milkey269ffb92014-04-03 14:43:30 -0700287 //This code assumes the device has only one port. It should be problem.
288 net.onrc.onos.core.topology.Port portObject = ports.next();
289 short destinationPort = portObject.getNumber().shortValue();
290 Switch switchObject = portObject.getSwitch();
291 long destinationDpid = switchObject.getDpid();
292
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700293 // TODO eliminate cast
Ray Milkey269ffb92014-04-03 14:43:30 -0700294 SwitchPort srcSwitchPort = new SwitchPort(
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700295 new Dpid(sw.getDpid()),
296 new net.onrc.onos.core.util.Port((short) inPort.getNumber().longValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700297 SwitchPort dstSwitchPort = new SwitchPort(
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700298 new Dpid(destinationDpid),
299 new net.onrc.onos.core.util.Port(destinationPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700300
301 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
302 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
303
304 synchronized (lock) {
305 //TODO check concurrency
306 Path pathspec = new Path(srcMacAddress, dstMacAddress);
307 PushedFlow existingFlow = pendingFlows.get(pathspec);
308
309 //A path is installed side by side to reduce a path timeout and a wrong state.
310 if (existingFlow != null) {
311 // We've already start to install a flow for this pair of MAC addresses
312 if (log.isDebugEnabled()) {
313 log.debug("Found existing the same pathspec {}, intent ID is {}",
314 pathspec,
315 existingFlow.intentId);
316 }
317
Ray Milkey269ffb92014-04-03 14:43:30 -0700318 // Find the correct port here. We just assume the PI is from
319 // the first hop switch, but this is definitely not always
320 // the case. We'll have to retrieve the flow from HZ every time
321 // because it could change (be rerouted) sometimes.
322 if (existingFlow.installed) {
323 // Flow has been sent to the switches so it is safe to
324 // send a packet out now
325
326 Intent intent = intentMap.getIntent(existingFlow.intentId);
327 PathIntent pathIntent = null;
328 if (intent instanceof PathIntent) {
329 pathIntent = (PathIntent) intent;
330 } else {
TeruUf9111652014-05-14 23:10:35 -0700331 log.debug("Intent ID {} is not PathIntent or null. return.", existingFlow.intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700332 return;
333 }
334
335 Boolean isflowEntryForThisSwitch = false;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700336 net.onrc.onos.core.intent.Path path = pathIntent.getPath();
Ray Milkey269ffb92014-04-03 14:43:30 -0700337
Ray Milkey7f1567c2014-04-08 13:53:32 -0700338 for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) {
Ray Milkey149693c2014-05-20 14:58:53 -0700339 LinkEvent le = i.next();
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700340 if (le.getSrc().dpid.equals(sw.getDpid())) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700341 log.debug("src {} dst {}", le.getSrc(), le.getDst());
342 isflowEntryForThisSwitch = true;
343 break;
344 }
345 }
346
Ray Milkey6c4f2fe2014-04-11 09:47:23 -0700347 if (!isflowEntryForThisSwitch) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700348 // If we don't find a flow entry for that switch, then we're
349 // in the middle of a rerouting (or something's gone wrong).
350 // This packet will be dropped as a victim of the rerouting.
351 log.debug("Dropping packet on flow {} between {}-{}",
352 existingFlow.intentId,
353 srcMacAddress, dstMacAddress);
354 } else {
355 log.debug("Sending packet out from sw {}, outport{}", sw, existingFlow.firstOutPort);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700356
Jonathan Harte3702f22014-04-29 02:56:56 -0700357 packetService.sendPacket(eth, new SwitchPort(
358 sw.getDpid(), existingFlow.firstOutPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700359 }
360 } else {
361 // Flow path has not yet been installed to switches so save the
362 // packet out for later
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700363 log.debug("Put a packet into the waiting list. flowId {}", existingFlow.intentId);
364 waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700365 }
366 return;
367 }
368
369 log.debug("Adding new flow between {} at {} and {} at {}",
370 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
371
372 String intentId = callerId + ":" + controllerRegistryService.getNextUniqueId();
373 IntentOperationList operations = new IntentOperationList();
374 ShortestPathIntent intent = new ShortestPathIntent(intentId,
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700375 sw.getDpid(), inPort.getNumber(), srcMacAddress.toLong(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700376 destinationDpid, destinationPort, dstMacAddress.toLong());
TeruU30c0c932014-05-15 16:47:41 -0700377 intent.setIdleTimeout(idleTimeout + SRC_SWITCH_TIMEOUT_ADJUST_SECOND);
378 intent.setFirstSwitchIdleTimeout(idleTimeout);
Ray Milkey269ffb92014-04-03 14:43:30 -0700379 IntentOperation.Operator operator = IntentOperation.Operator.ADD;
380 operations.add(operator, intent);
381 pathRuntime.executeIntentOperations(operations);
Ray Milkey269ffb92014-04-03 14:43:30 -0700382 // Add to waiting lists
383 pendingFlows.put(pathspec, new PushedFlow(intentId));
384 log.debug("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700385 waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700386 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
387
388 }
389 }
390
Ray Milkey269ffb92014-04-03 14:43:30 -0700391 @Override
392 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
393 }
394
395 @Override
396 public void flowRemoved(FlowPath removedFlowPath) {
397 }
398
399 public void flowRemoved(PathIntent removedIntent) {
400 if (log.isTraceEnabled()) {
401 log.trace("Path {} was removed", removedIntent.getParentIntent().getId());
402 }
403
404 ShortestPathIntent spfIntent = (ShortestPathIntent) removedIntent.getParentIntent();
405 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
406 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
407 Path removedPath = new Path(srcMacAddress, dstMacAddress);
408
409 synchronized (lock) {
410 // There *shouldn't* be any packets queued if the flow has
411 // just been removed.
412 List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId());
413 if (!packets.isEmpty()) {
414 log.warn("Removed flow {} has packets queued.", spfIntent.getId());
415 }
416 pendingFlows.remove(removedPath);
417 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId());
418 }
419 }
420
421 private void flowInstalled(PathIntent installedPath) {
422 if (log.isTraceEnabled()) {
TeruUf9111652014-05-14 23:10:35 -0700423 log.trace("Installed intent ID {}, path {}", installedPath.getParentIntent().getId(), installedPath.getPath());
Ray Milkey269ffb92014-04-03 14:43:30 -0700424 }
425
426 ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent();
427 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
428 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
429 Path path = new Path(srcMacAddress, dstMacAddress);
430 log.debug("Path spec {}", path);
431
432 // TODO waiting packets should time out. We could request a path that
433 // can't be installed right now because of a network partition. The path
434 // may eventually be installed, but we may have received thousands of
435 // packets in the meantime and probably don't want to send very old packets.
436
437 List<PacketToPush> packets = null;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700438 net.onrc.onos.core.intent.Path graphPath = installedPath.getPath();
Ray Milkey269ffb92014-04-03 14:43:30 -0700439
TeruU220c45e2014-04-10 18:56:26 -0700440 short outPort;
441 if (graphPath.isEmpty()) {
442 outPort = (short) spfIntent.getDstPortNumber();
443 log.debug("Path is empty. Maybe devices on the same switch. outPort {}", outPort);
444 } else {
445 outPort = graphPath.get(0).getSrc().getNumber().shortValue();
446 log.debug("path{}, outPort {}", graphPath, outPort);
447 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700448
449 PushedFlow existingFlow = null;
450
451 synchronized (lock) {
452 existingFlow = pendingFlows.get(path);
453
454 if (existingFlow != null) {
455 existingFlow.installed = true;
456 existingFlow.firstOutPort = outPort;
457 } else {
458 log.debug("ExistingFlow {} is null", path);
459 return;
460 }
461
462 //Check both existing flow are installed status.
463 if (existingFlow.installed) {
464 packets = waitingPackets.removeAll(existingFlow.intentId);
465 if (log.isDebugEnabled()) {
466 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
467 existingFlow.intentId, existingFlow.firstOutPort, packets.size());
468 }
469 } else {
470 log.debug("Forward or reverse flows hasn't been pushed yet. return");
471 return;
472 }
473 }
474
475 for (PacketToPush packet : packets) {
476 log.debug("Start packetToPush to sw {}, outPort {}, path {}", packet.dpid, existingFlow.firstOutPort, path);
Jonathan Harte3702f22014-04-29 02:56:56 -0700477 packetService.sendPacket(packet.eth, new SwitchPort(
478 packet.dpid, existingFlow.firstOutPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700479 }
480 }
481
Ray Milkey269ffb92014-04-03 14:43:30 -0700482 @Override
TeruUf9111652014-05-14 23:10:35 -0700483 public void intentsChange(LinkedList<ChangedEvent> events) {
484 for (ChangedEvent event : events) {
485 log.debug("path intent ID {}, eventType {}", event.intent.getId() , event.eventType);
486 PathIntent pathIntent = (PathIntent) intentMap.getIntent(event.intent.getId());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700487 if (pathIntent == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700488 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700489 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700490
Ray Milkeyb29e6262014-04-09 16:02:14 -0700491 if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700492 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700493 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700494
TeruUf9111652014-05-14 23:10:35 -0700495 switch(event.eventType) {
496 case ADDED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700497 break;
TeruUf9111652014-05-14 23:10:35 -0700498 case REMOVED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700499 break;
TeruUf9111652014-05-14 23:10:35 -0700500 case STATE_CHANGED:
501 IntentState state = pathIntent.getState();
502 switch (state) {
503 case INST_REQ:
504 break;
505 case INST_ACK:
506 flowInstalled(pathIntent);
507 break;
508 case INST_NACK:
509 break;
510 case DEL_REQ:
511 break;
512 case DEL_ACK:
513 flowRemoved(pathIntent);
514 break;
515 case DEL_PENDING:
516 break;
517 default:
518 break;
519 }
Ray Milkey149693c2014-05-20 14:58:53 -0700520 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700521 default:
522 break;
523 }
524 }
525 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800526}