Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame^] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.HashSet; |
| 5 | |
| 6 | import net.onrc.onos.intent.ConstrainedBFSTree; |
| 7 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
| 8 | import net.onrc.onos.intent.Intent; |
| 9 | import net.onrc.onos.intent.PathIntent; |
| 10 | import net.onrc.onos.intent.PathIntents; |
| 11 | import net.onrc.onos.intent.ShortestPathIntent; |
| 12 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 13 | import net.onrc.onos.ofcontroller.networkgraph.Path; |
| 14 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 15 | |
| 16 | /** |
| 17 | * @author Toshio Koide (t-koide@onlab.us) |
| 18 | */ |
| 19 | public class PathCalcRuntime { |
| 20 | NetworkGraph graph; |
| 21 | HashSet<Intent> inputIntents = new HashSet<Intent>(); |
| 22 | PathIntents outputIntents = new PathIntents(); |
| 23 | |
| 24 | public PathCalcRuntime(NetworkGraph g) { |
| 25 | this.graph = g; |
| 26 | } |
| 27 | |
| 28 | public Collection<Intent> getInputIntents() { |
| 29 | return inputIntents; |
| 30 | } |
| 31 | |
| 32 | public PathIntents getOutputIntents() { |
| 33 | return outputIntents; |
| 34 | } |
| 35 | |
| 36 | public void addInputIntents(Collection<Intent> inputIntents) { |
| 37 | this.inputIntents.addAll(inputIntents); |
| 38 | this.outputIntents = calcPathIntents(inputIntents); |
| 39 | } |
| 40 | |
| 41 | protected PathIntents calcPathIntents(Collection<Intent> originalIntents) { |
| 42 | PathIntents pathIntents = new PathIntents(); |
| 43 | |
| 44 | for (Intent intent: originalIntents) { |
| 45 | if (!(intent instanceof ShortestPathIntent)) { |
| 46 | // unsupported intent type. |
| 47 | // TODO should push back the intent to caller |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | ShortestPathIntent spIntent = (ShortestPathIntent) intent; |
| 52 | Switch srcSwitch = spIntent.getSourcePort().getSwitch(); |
| 53 | Switch dstSwitch = spIntent.getDestinationPort().getSwitch(); |
| 54 | if (srcSwitch == null || dstSwitch == null) { |
| 55 | // incomplete intent. |
| 56 | // TODO should push back the intent to caller |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | Double bandwidth = null; |
| 61 | ConstrainedBFSTree tree = null; |
| 62 | if (intent instanceof ConstrainedShortestPathIntent) { |
| 63 | bandwidth = ((ConstrainedShortestPathIntent) intent).getBandwidth(); |
| 64 | tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth); |
| 65 | } |
| 66 | else { |
| 67 | tree = new ConstrainedBFSTree(srcSwitch); |
| 68 | } |
| 69 | Path path = tree.getPath(dstSwitch); |
| 70 | if (path == null) { |
| 71 | // path not found. |
| 72 | // TODO should push back the intent to caller |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | pathIntents.addIntent(new PathIntent(path, bandwidth, intent)); |
| 77 | } |
| 78 | return pathIntents; |
| 79 | } |
| 80 | } |