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