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 | 59bc8ea | 2014-02-21 17:48:46 -0800 | [diff] [blame^] | 45 | // TODO optimize locking of NetworkGraph |
| 46 | graph.acquireReadLock(); |
| 47 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 48 | for (IntentOperation intentOp: intentOpList) { |
| 49 | switch (intentOp.operator) { |
| 50 | case ADD: |
| 51 | if (!(intentOp.intent instanceof ShortestPathIntent)) { |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 52 | log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName()); |
| 53 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 54 | ErrorType.UNSUPPORTED_INTENT, |
| 55 | "Unsupported intent type.", |
| 56 | intentOp.intent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 57 | continue; |
| 58 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 59 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 60 | ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent; |
| 61 | Switch srcSwitch = graph.getSwitch(spIntent.getSrcSwitchDpid()); |
| 62 | Switch dstSwitch = graph.getSwitch(spIntent.getDstSwitchDpid()); |
| 63 | if (srcSwitch == null || dstSwitch == null) { |
Toshio Koide | b39c9d3 | 2014-02-20 01:21:47 -0800 | [diff] [blame] | 64 | log.error("Switch not found: {}, {}", |
| 65 | spIntent.getSrcSwitchDpid(), |
| 66 | spIntent.getDstSwitchDpid()); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 67 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 68 | ErrorType.SWITCH_NOT_FOUND, |
| 69 | "Switch not found.", |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 70 | spIntent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 71 | continue; |
| 72 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 73 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 74 | double bandwidth = 0.0; |
| 75 | ConstrainedBFSTree tree = null; |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 76 | if (spIntent instanceof ConstrainedShortestPathIntent) { |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 77 | bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth(); |
| 78 | tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth); |
| 79 | } |
| 80 | else { |
| 81 | tree = spfTrees.get(srcSwitch); |
| 82 | if (tree == null) { |
| 83 | tree = new ConstrainedBFSTree(srcSwitch); |
| 84 | spfTrees.put(srcSwitch, tree); |
| 85 | } |
| 86 | } |
| 87 | Path path = tree.getPath(dstSwitch); |
| 88 | if (path == null) { |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 89 | log.error("Path not found: {}", spIntent.toString()); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 90 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 91 | ErrorType.PATH_NOT_FOUND, |
| 92 | "Path not found.", |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 93 | spIntent)); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 94 | continue; |
| 95 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 96 | |
| 97 | // generate new path-intent ID |
| 98 | String oldPathIntentId = spIntent.getPathIntentId(); |
| 99 | String newPathIntentId; |
| 100 | if (oldPathIntentId == null) |
| 101 | newPathIntentId = PathIntent.createFirstId(spIntent.getId()); |
| 102 | else { |
| 103 | newPathIntentId = PathIntent.createNextId(oldPathIntentId); |
| 104 | |
| 105 | // Request removal of low-level intent if it exists. |
| 106 | pathIntentOpList.add(Operator.REMOVE, new Intent(oldPathIntentId)); |
| 107 | } |
| 108 | |
| 109 | // create new path-intent |
| 110 | PathIntent pathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent); |
| 111 | pathIntent.setState(IntentState.INST_REQ); |
| 112 | spIntent.setPathIntent(pathIntent); |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 113 | pathIntentOpList.add(Operator.ADD, pathIntent); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 114 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 115 | break; |
| 116 | case REMOVE: |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 117 | pathIntentOpList.add(Operator.REMOVE, new Intent( |
| 118 | ((ShortestPathIntent) intentOp.intent).getPathIntentId())); |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 119 | break; |
Toshio Koide | df2eab9 | 2014-02-20 11:24:59 -0800 | [diff] [blame] | 120 | case ERROR: |
| 121 | // just ignore |
| 122 | break; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 123 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 124 | } |
Toshio Koide | 59bc8ea | 2014-02-21 17:48:46 -0800 | [diff] [blame^] | 125 | // TODO optimize locking of NetworkGraph |
| 126 | graph.releaseReadLock(); |
| 127 | |
Toshio Koide | 27ffd41 | 2014-02-18 19:15:27 -0800 | [diff] [blame] | 128 | return pathIntentOpList; |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 129 | } |
| 130 | } |