Toshio Koide | 5f26065 | 2014-01-30 14:41:12 -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 | /** |
| 8 | * This code is valid for the architectural study purpose only. |
| 9 | * @author Toshio Koide (t-koide@onlab.us) |
| 10 | */ |
| 11 | public class ConstrainedFlow extends Flow { |
| 12 | public double bandwidth; |
| 13 | |
| 14 | /** |
| 15 | * |
| 16 | * @param srcPort |
| 17 | * @param dstPort |
| 18 | * @param bandwidth |
| 19 | */ |
| 20 | public ConstrainedFlow(NetworkGraph graph, String name, SwitchPort srcPort, SwitchPort dstPort, double bandwidth) { |
| 21 | super(graph, name, srcPort, dstPort); |
| 22 | this.bandwidth = bandwidth; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * calculate available bandwidth of specified link |
| 27 | * @param link |
| 28 | * @return |
| 29 | */ |
| 30 | protected Double getAvailableBandwidth(Link link) { |
| 31 | Double capacity = link.getCapacity(); |
| 32 | if (capacity.isInfinite()) { |
| 33 | return capacity; |
| 34 | } |
| 35 | Double bandwidth = capacity; |
| 36 | for (Flow flow: link.getFlows()) { |
| 37 | if (flow instanceof ConstrainedFlow) { |
| 38 | bandwidth -= ((ConstrainedFlow)flow).getBandwidth(); |
| 39 | } |
| 40 | } |
| 41 | return bandwidth; |
| 42 | } |
| 43 | |
| 44 | public Double getBandwidth() { |
| 45 | return bandwidth; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * calculate path by creating BFS tree satisfying the bandwidth condition |
| 50 | */ |
| 51 | @Override |
| 52 | public boolean calcPath() { |
| 53 | LinkedList<Switch> switchQueue = new LinkedList<Switch>(); |
| 54 | HashSet<Switch> switchSearched = new HashSet<Switch>(); |
| 55 | HashMap<Switch, Link> upstreamLinks = new HashMap<Switch, Link>(); |
| 56 | |
| 57 | Switch srcSwitch = srcPort.getSwitch(); |
| 58 | Switch dstSwitch = dstPort.getSwitch(); |
| 59 | |
| 60 | switchQueue.add(srcSwitch); |
| 61 | switchSearched.add(srcSwitch); |
| 62 | |
| 63 | while (!switchQueue.isEmpty()) { |
| 64 | Switch sw = switchQueue.poll(); |
| 65 | if (sw == dstSwitch) { |
| 66 | // path has been searched. |
| 67 | // store path into itself |
| 68 | path.clear(); |
| 69 | while (sw != srcSwitch) { |
| 70 | Link upstreamLink = upstreamLinks.get(sw); |
| 71 | path.add(0, upstreamLink); |
| 72 | sw = upstreamLink.getSrcPort().getSwitch(); |
| 73 | } |
Toshio Koide | 25ce96a | 2014-01-30 17:52:09 -0800 | [diff] [blame] | 74 | state = FlowState.PathCalculated; |
| 75 | return true; |
Toshio Koide | 5f26065 | 2014-01-30 14:41:12 -0800 | [diff] [blame] | 76 | } |
| 77 | for (Link link: sw.getAdjLinks()) { |
| 78 | Switch reachedSwitch = link.getDstPort().getSwitch(); |
| 79 | Double availableBandwidth = getAvailableBandwidth(link); |
| 80 | if (availableBandwidth < bandwidth || switchSearched.contains(reachedSwitch)) continue; |
| 81 | switchQueue.add(reachedSwitch); |
| 82 | switchSearched.add(reachedSwitch); |
| 83 | upstreamLinks.put(reachedSwitch, link); |
| 84 | } |
| 85 | } |
Toshio Koide | 25ce96a | 2014-01-30 17:52:09 -0800 | [diff] [blame] | 86 | state = FlowState.PathCalculationFailed; |
Toshio Koide | 5f26065 | 2014-01-30 14:41:12 -0800 | [diff] [blame] | 87 | return false; |
| 88 | } |
Toshio Koide | 25ce96a | 2014-01-30 17:52:09 -0800 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | void calcFlowEntries() { |
| 92 | // TODO Auto-generated method stub |
| 93 | // not implemented yet |
| 94 | } |
Toshio Koide | 5f26065 | 2014-01-30 14:41:12 -0800 | [diff] [blame] | 95 | } |