Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent.runtime; |
| 2 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 3 | import java.util.HashMap; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 4 | |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 5 | import net.floodlightcontroller.core.module.IFloodlightService; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 6 | import net.onrc.onos.intent.ConstrainedBFSTree; |
| 7 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 8 | import net.onrc.onos.intent.ErrorIntent; |
| 9 | import net.onrc.onos.intent.ErrorIntent.ErrorType; |
Toshio Koide | 0c9106d | 2014-02-19 15:26:38 -0800 | [diff] [blame] | 10 | import net.onrc.onos.intent.Intent; |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 11 | import net.onrc.onos.intent.Intent.IntentState; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 12 | import net.onrc.onos.intent.IntentOperation; |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 13 | import net.onrc.onos.intent.IntentOperation.Operator; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 14 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 15 | import net.onrc.onos.intent.PathIntent; |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 16 | import net.onrc.onos.intent.PathIntentMap; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 17 | import net.onrc.onos.intent.ShortestPathIntent; |
| 18 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.Path; |
| 20 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 21 | |
Toshio Koide | b39c9d3 | 2014-02-20 01:21:47 -0800 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 25 | /** |
| 26 | * @author Toshio Koide (t-koide@onlab.us) |
| 27 | */ |
Toshio Koide | 4f30873 | 2014-02-18 15:19:48 -0800 | [diff] [blame] | 28 | public class PathCalcRuntime implements IFloodlightService { |
| 29 | private NetworkGraph graph; |
Toshio Koide | b39c9d3 | 2014-02-20 01:21:47 -0800 | [diff] [blame] | 30 | private final static Logger log = LoggerFactory.getLogger(PathCalcRuntime.class); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 31 | public PathCalcRuntime(NetworkGraph g) { |
| 32 | this.graph = g; |
| 33 | } |
| 34 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 35 | /** |
| 36 | * calculate shortest-path and constrained-shortest-path intents into low-level path intents |
| 37 | * @param intentOpList IntentOperationList having instances of ShortestPathIntent/ConstrainedShortestPathIntent |
| 38 | * @param pathIntents a set of current low-level intents |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 39 | * @return IntentOperationList. PathIntent and/or ErrorIntent instances. |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 40 | */ |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 41 | public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, final PathIntentMap pathIntents) { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 42 | IntentOperationList pathIntentOpList = new IntentOperationList(); |
| 43 | HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>(); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 44 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 45 | for (IntentOperation intentOp: intentOpList) { |
| 46 | switch (intentOp.operator) { |
| 47 | case ADD: |
| 48 | if (!(intentOp.intent instanceof ShortestPathIntent)) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 49 | log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName()); |
| 50 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 51 | ErrorType.UNSUPPORTED_INTENT, |
| 52 | "Unsupported intent type.", |
| 53 | intentOp.intent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 54 | continue; |
| 55 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 56 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 57 | ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent; |
| 58 | Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid()); |
| 59 | Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid()); |
| 60 | if (srcSwitch == null || dstSwitch == null) { |
Toshio Koide | b39c9d3 | 2014-02-20 01:21:47 -0800 | [diff] [blame] | 61 | log.error("Switch not found: {}, {}", |
| 62 | spIntent.getSrcSwitchDpid(), |
| 63 | spIntent.getDstSwitchDpid()); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 64 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 65 | ErrorType.SWITCH_NOT_FOUND, |
| 66 | "Switch not found.", |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 67 | spIntent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 68 | continue; |
| 69 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 70 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 71 | double bandwidth = 0.0; |
| 72 | ConstrainedBFSTree tree = null; |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 73 | if (spIntent instanceof ConstrainedShortestPathIntent) { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 74 | bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth(); |
| 75 | tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth); |
| 76 | } |
| 77 | else { |
| 78 | tree = spfTrees.get(srcSwitch); |
| 79 | if (tree == null) { |
| 80 | tree = new ConstrainedBFSTree(srcSwitch); |
| 81 | spfTrees.put(srcSwitch, tree); |
| 82 | } |
| 83 | } |
| 84 | Path path = tree.getPath(dstSwitch); |
| 85 | if (path == null) { |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 86 | log.error("Path not found: {}", spIntent.toString()); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 87 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 88 | ErrorType.PATH_NOT_FOUND, |
| 89 | "Path not found.", |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 90 | spIntent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 91 | continue; |
| 92 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 93 | |
| 94 | // generate new path-intent ID |
| 95 | String oldPathIntentId = spIntent.getPathIntentId(); |
| 96 | String newPathIntentId; |
| 97 | if (oldPathIntentId == null) |
| 98 | newPathIntentId = PathIntent.createFirstId(spIntent.getId()); |
| 99 | else { |
| 100 | newPathIntentId = PathIntent.createNextId(oldPathIntentId); |
| 101 | |
| 102 | // Request removal of low-level intent if it exists. |
| 103 | pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId)); |
| 104 | } |
| 105 | |
| 106 | // create new path-intent |
| 107 | PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent); |
| 108 | pathIntent.setState(IntentState.INST_REQ); |
| 109 | spIntent.setPathIntent(pathIntent); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 110 | pathIntentOpList.add(Operator.ADD, pathIntent); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 111 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 112 | break; |
| 113 | case REMOVE: |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 114 | pathIntentOpList.add(Operator.REMOVE, new Intent( |
| 115 | ((ShortestPathIntent) intentOp.intent).getPathIntentId())); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 116 | break; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 117 | case ERROR: |
| 118 | // just ignore |
| 119 | break; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 120 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 121 | } |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 122 | return pathIntentOpList; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 123 | } |
| 124 | } |