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