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