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