Toshio Koide | 25ce96a | 2014-01-30 17:52:09 -0800 | [diff] [blame^] | 1 | package net.onrc.onos.ofcontroller.app; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.HashSet; |
| 5 | import java.util.LinkedList; |
| 6 | |
| 7 | public class ShortestPathFlow extends Flow { |
| 8 | |
| 9 | public ShortestPathFlow(NetworkGraph graph, String name, SwitchPort srcPort, SwitchPort dstPort) { |
| 10 | super(graph, name, srcPort, dstPort); |
| 11 | // TODO Auto-generated constructor stub |
| 12 | } |
| 13 | |
| 14 | @Override |
| 15 | boolean calcPath() { |
| 16 | LinkedList<Switch> switchQueue = new LinkedList<Switch>(); |
| 17 | HashSet<Switch> switchSearched = new HashSet<Switch>(); |
| 18 | HashMap<Switch, Link> upstreamLinks = new HashMap<Switch, Link>(); |
| 19 | |
| 20 | Switch srcSwitch = srcPort.getSwitch(); |
| 21 | Switch dstSwitch = dstPort.getSwitch(); |
| 22 | |
| 23 | switchQueue.add(srcSwitch); |
| 24 | switchSearched.add(srcSwitch); |
| 25 | |
| 26 | while (!switchQueue.isEmpty()) { |
| 27 | Switch sw = switchQueue.poll(); |
| 28 | if (sw == dstSwitch) { |
| 29 | // path has been searched. |
| 30 | // store path into itself |
| 31 | path.clear(); |
| 32 | while (sw != srcSwitch) { |
| 33 | Link upstreamLink = upstreamLinks.get(sw); |
| 34 | path.add(0, upstreamLink); |
| 35 | sw = upstreamLink.getSrcPort().getSwitch(); |
| 36 | } |
| 37 | state = FlowState.PathCalculated; |
| 38 | return true; |
| 39 | } |
| 40 | for (Link link: sw.getAdjLinks()) { |
| 41 | Switch reachedSwitch = link.getDstPort().getSwitch(); |
| 42 | if (switchSearched.contains(reachedSwitch)) continue; |
| 43 | switchQueue.add(reachedSwitch); |
| 44 | switchSearched.add(reachedSwitch); |
| 45 | upstreamLinks.put(reachedSwitch, link); |
| 46 | } |
| 47 | } |
| 48 | state = FlowState.PathCalculationFailed; |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | void calcFlowEntries() { |
| 54 | // TODO Auto-generated method stub |
| 55 | // not implemented yet |
| 56 | } |
| 57 | |
| 58 | } |