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; |
Yuta HIGUCHI | 1fc395e | 2014-05-13 14:06:28 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.intent.Path; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.intent.PathIntent; |
| 18 | import net.onrc.onos.core.intent.PathIntentMap; |
| 19 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.topology.Switch; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.topology.Topology; |
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 { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 30 | private Topology topology; |
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 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 33 | public PathCalcRuntime(Topology topology) { |
| 34 | this.topology = topology; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | */ |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 44 | public IntentOperationList calcPathIntents(final IntentOperationList intentOpList, |
| 45 | final IntentMap appIntents, final PathIntentMap pathIntents) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | IntentOperationList pathIntentOpList = new IntentOperationList(); |
| 47 | HashMap<Switch, ConstrainedBFSTree> spfTrees = new HashMap<>(); |
Toshio Koide | 59bc8ea | 2014-02-21 17:48:46 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 49 | // TODO optimize locking of Topology |
| 50 | topology.acquireReadLock(); |
| 51 | log.debug("Topology: {}", topology.getLinks()); |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 52 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | for (IntentOperation intentOp : intentOpList) { |
| 54 | switch (intentOp.operator) { |
| 55 | case ADD: |
| 56 | if (!(intentOp.intent instanceof ShortestPathIntent)) { |
| 57 | log.error("Unsupported intent type: {}", intentOp.intent.getClass().getName()); |
| 58 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 59 | ErrorType.UNSUPPORTED_INTENT, |
| 60 | "Unsupported intent type.", |
| 61 | intentOp.intent)); |
| 62 | continue; |
| 63 | } |
Toshio Koide | c87810e | 2014-02-11 13:03:21 -0800 | [diff] [blame] | 64 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | ShortestPathIntent spIntent = (ShortestPathIntent) intentOp.intent; |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 66 | Switch srcSwitch = topology.getSwitch(spIntent.getSrcSwitchDpid()); |
| 67 | Switch dstSwitch = topology.getSwitch(spIntent.getDstSwitchDpid()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | if (srcSwitch == null || dstSwitch == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 69 | log.error("Switch not found. src:{}, dst:{}, Topology:{}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | spIntent.getSrcSwitchDpid(), |
| 71 | spIntent.getDstSwitchDpid(), |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 72 | topology.getLinks()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 74 | ErrorType.SWITCH_NOT_FOUND, |
| 75 | "Switch not found.", |
| 76 | spIntent)); |
| 77 | continue; |
| 78 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | double bandwidth = 0.0; |
| 81 | ConstrainedBFSTree tree = null; |
| 82 | if (spIntent instanceof ConstrainedShortestPathIntent) { |
| 83 | bandwidth = ((ConstrainedShortestPathIntent) intentOp.intent).getBandwidth(); |
| 84 | tree = new ConstrainedBFSTree(srcSwitch, pathIntents, bandwidth); |
| 85 | } else { |
| 86 | tree = spfTrees.get(srcSwitch); |
| 87 | if (tree == null) { |
| 88 | tree = new ConstrainedBFSTree(srcSwitch); |
| 89 | spfTrees.put(srcSwitch, tree); |
| 90 | } |
| 91 | } |
| 92 | Path path = tree.getPath(dstSwitch); |
| 93 | if (path == null) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 94 | log.error("Path not found. Intent: {}, Topology: {}", spIntent.toString(), topology.getLinks()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | pathIntentOpList.add(Operator.ERROR, new ErrorIntent( |
| 96 | ErrorType.PATH_NOT_FOUND, |
| 97 | "Path not found.", |
| 98 | spIntent)); |
| 99 | continue; |
| 100 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 101 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 102 | // generate new path-intent ID |
| 103 | String oldPathIntentId = spIntent.getPathIntentId(); |
| 104 | String newPathIntentId; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 105 | if (oldPathIntentId == null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | newPathIntentId = PathIntent.createFirstId(spIntent.getId()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 107 | } else { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | newPathIntentId = PathIntent.createNextId(oldPathIntentId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | } |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 110 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | // create new path-intent |
Toshio Koide | 7d3cee0 | 2014-06-05 18:56:19 -0700 | [diff] [blame] | 112 | PathIntent newPathIntent = new PathIntent(newPathIntentId, path, bandwidth, spIntent); |
| 113 | newPathIntent.setState(IntentState.INST_REQ); |
| 114 | |
| 115 | // create and add operation(s) |
| 116 | if (oldPathIntentId == null) { |
| 117 | // operation for new path-intent |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame^] | 118 | spIntent.setPathIntentId(newPathIntent); |
Toshio Koide | 7d3cee0 | 2014-06-05 18:56:19 -0700 | [diff] [blame] | 119 | pathIntentOpList.add(Operator.ADD, newPathIntent); |
| 120 | log.debug("new intent:{}", newPathIntent); |
| 121 | } else { |
| 122 | PathIntent oldPathIntent = (PathIntent) pathIntents.getIntent(oldPathIntentId); |
| 123 | if (newPathIntent.hasSameFields(oldPathIntent)) { |
| 124 | // skip the same operation (reroute) |
| 125 | spIntent.setState(IntentState.INST_ACK); |
| 126 | log.debug("skip intent:{}", newPathIntent); |
| 127 | } else { |
| 128 | // update existing path-intent (reroute) |
Brian O'Connor | a581b9d | 2014-06-15 23:32:36 -0700 | [diff] [blame^] | 129 | spIntent.setPathIntentId(newPathIntent); |
Toshio Koide | 7d3cee0 | 2014-06-05 18:56:19 -0700 | [diff] [blame] | 130 | pathIntentOpList.add(Operator.REMOVE, oldPathIntent); |
| 131 | pathIntentOpList.add(Operator.ADD, newPathIntent); |
| 132 | log.debug("update intent:{} -> {}", oldPathIntent, newPathIntent); |
| 133 | } |
| 134 | } |
Toshio Koide | 59bc8ea | 2014-02-21 17:48:46 -0800 | [diff] [blame] | 135 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | break; |
| 137 | case REMOVE: |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 138 | ShortestPathIntent targetAppIntent = |
| 139 | (ShortestPathIntent) appIntents.getIntent( |
| 140 | intentOp.intent.getId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | if (targetAppIntent != null) { |
| 142 | String pathIntentId = targetAppIntent.getPathIntentId(); |
| 143 | if (pathIntentId != null) { |
| 144 | Intent targetPathIntent = pathIntents.getIntent(pathIntentId); |
| 145 | if (targetPathIntent != null) { |
| 146 | pathIntentOpList.add(Operator.REMOVE, targetPathIntent); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | break; |
| 151 | case ERROR: |
| 152 | // just ignore |
| 153 | break; |
Ray Milkey | 0b122ed | 2014-04-14 10:06:03 -0700 | [diff] [blame] | 154 | default: |
| 155 | log.error("Unknown intent operator {}", intentOp.operator); |
| 156 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 159 | // TODO optimize locking of Topology |
| 160 | topology.releaseReadLock(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | |
| 162 | return pathIntentOpList; |
| 163 | } |
Ray Milkey | 0f913a0 | 2014-04-07 20:58:17 -0700 | [diff] [blame] | 164 | } |