blob: d754fa1b69e417e8fb7b0b01a697dc87ce3ae5a0 [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 }
Toshio Koide25ce96a2014-01-30 17:52:09 -080074 state = FlowState.PathCalculated;
75 return true;
Toshio Koide5f260652014-01-30 14:41:12 -080076 }
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 Koide25ce96a2014-01-30 17:52:09 -080086 state = FlowState.PathCalculationFailed;
Toshio Koide5f260652014-01-30 14:41:12 -080087 return false;
88 }
Toshio Koide25ce96a2014-01-30 17:52:09 -080089
90 @Override
91 void calcFlowEntries() {
92 // TODO Auto-generated method stub
93 // not implemented yet
94 }
Toshio Koide5f260652014-01-30 14:41:12 -080095}