blob: 7b18c348cca47b56bf9c13b688a08f8bf0adf2d9 [file] [log] [blame]
Toshio Koide5f260652014-01-30 14:41:12 -08001package net.onrc.onos.ofcontroller.app;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import 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 */
11public 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 }
74 return super.calcPath();
75 }
76 for (Link link: sw.getAdjLinks()) {
77 Switch reachedSwitch = link.getDstPort().getSwitch();
78 Double availableBandwidth = getAvailableBandwidth(link);
79 if (availableBandwidth < bandwidth || switchSearched.contains(reachedSwitch)) continue;
80 switchQueue.add(reachedSwitch);
81 switchSearched.add(reachedSwitch);
82 upstreamLinks.put(reachedSwitch, link);
83 }
84 }
85 state = FlowState.PathCalcurationFailed;
86 return false;
87 }
88}