Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 4 | import java.util.HashMap; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 5 | import java.util.LinkedList; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 6 | |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.intent.Path; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.topology.Link; |
| 9 | import net.onrc.onos.core.topology.LinkData; |
| 10 | import net.onrc.onos.core.topology.Switch; |
| 11 | import net.onrc.onos.core.util.Dpid; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 12 | |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 13 | import org.projectfloodlight.openflow.util.HexString; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 17 | /** |
| 18 | * This class creates bandwidth constrained breadth first tree and returns paths |
| 19 | * from root switch to leaf switches which satisfies the bandwidth condition. If |
| 20 | * bandwidth parameter is not specified, the normal breadth first tree will be |
| 21 | * calculated. The paths are snapshot paths at the point of the class |
| 22 | * instantiation. |
| 23 | */ |
| 24 | public class ECMPShortestPathGraph { |
| 25 | LinkedList<Switch> switchQueue = new LinkedList<>(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 26 | LinkedList<Integer> distanceQueue = new LinkedList<>(); |
| 27 | HashMap<Dpid, Integer> switchSearched = new HashMap<>(); |
| 28 | HashMap<Dpid, ArrayList<LinkData>> upstreamLinks = new HashMap<>(); |
| 29 | HashMap<Dpid, ArrayList<Path>> paths = new HashMap<>(); |
| 30 | HashMap<Integer, ArrayList<Switch>> distanceSwitchMap = new HashMap<>(); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 31 | Switch rootSwitch; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 32 | private static final Logger log = LoggerFactory |
| 33 | .getLogger(SegmentRoutingManager.class); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 34 | |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 35 | /** |
Saurav Das | c0aa04c | 2014-09-22 10:22:23 -0700 | [diff] [blame] | 36 | * Constructor |
| 37 | * |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 38 | * @param rootSwitch root of the BFS tree |
| 39 | */ |
| 40 | public ECMPShortestPathGraph(Switch rootSwitch) { |
| 41 | this.rootSwitch = rootSwitch; |
| 42 | calcECMPShortestPathGraph(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Calculates the BFS tree using any provided constraints and Intents. |
| 47 | */ |
| 48 | private void calcECMPShortestPathGraph() { |
| 49 | switchQueue.add(rootSwitch); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 50 | int currDistance = 0; |
| 51 | distanceQueue.add(currDistance); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 52 | switchSearched.put(rootSwitch.getDpid(), currDistance); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 53 | while (!switchQueue.isEmpty()) { |
| 54 | Switch sw = switchQueue.poll(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 55 | Switch prevSw = null; |
| 56 | currDistance = distanceQueue.poll(); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 57 | for (Link link : sw.getOutgoingLinks()) { |
| 58 | Switch reachedSwitch = link.getDstPort().getSwitch(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 59 | if ((prevSw != null) |
| 60 | && (prevSw.getDpid().equals(reachedSwitch.getDpid()))) |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 61 | { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 62 | /* Ignore LAG links between the same set of switches */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 63 | continue; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 64 | } |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 65 | else |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 66 | { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 67 | prevSw = reachedSwitch; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 68 | } |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 69 | |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 70 | Integer distance = switchSearched.get(reachedSwitch.getDpid()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 71 | if ((distance != null) && (distance.intValue() < (currDistance + 1))) { |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 72 | continue; |
| 73 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 74 | if (distance == null) { |
| 75 | /* First time visiting this switch node */ |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 76 | switchQueue.add(reachedSwitch); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 77 | distanceQueue.add(currDistance + 1); |
| 78 | switchSearched.put(reachedSwitch.getDpid(), currDistance + 1); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 79 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 80 | ArrayList<Switch> distanceSwArray = distanceSwitchMap |
| 81 | .get(currDistance + 1); |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 82 | if (distanceSwArray == null) |
| 83 | { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 84 | distanceSwArray = new ArrayList<Switch>(); |
| 85 | distanceSwArray.add(reachedSwitch); |
| 86 | distanceSwitchMap.put(currDistance + 1, distanceSwArray); |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 87 | } |
| 88 | else |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 89 | distanceSwArray.add(reachedSwitch); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 90 | } |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 91 | |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 92 | ArrayList<LinkData> upstreamLinkArray = |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 93 | upstreamLinks.get(reachedSwitch.getDpid()); |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 94 | if (upstreamLinkArray == null) |
| 95 | { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 96 | upstreamLinkArray = new ArrayList<LinkData>(); |
| 97 | upstreamLinkArray.add(new LinkData(link)); |
| 98 | upstreamLinks.put(reachedSwitch.getDpid(), upstreamLinkArray); |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 99 | } |
| 100 | else |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 101 | /* ECMP links */ |
| 102 | upstreamLinkArray.add(new LinkData(link)); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 105 | |
| 106 | log.debug("ECMPShortestPathGraph:switchSearched for switch {} is {}", |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 107 | HexString.toHexString(rootSwitch.getDpid().value()), switchSearched); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 108 | log.debug("ECMPShortestPathGraph:upstreamLinks for switch {} is {}", |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 109 | HexString.toHexString(rootSwitch.getDpid().value()), upstreamLinks); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 110 | log.debug("ECMPShortestPathGraph:distanceSwitchMap for switch {} is {}", |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 111 | HexString.toHexString(rootSwitch.getDpid().value()), distanceSwitchMap); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 112 | /* |
| 113 | for (Integer distance: distanceSwitchMap.keySet()){ |
| 114 | for (Switch sw: distanceSwitchMap.get(distance)){ |
| 115 | ArrayList<Path> path = getPath(sw); |
| 116 | log.debug("ECMPShortestPathGraph:Paths in Pass{} from switch {} to switch {} is {}", |
| 117 | distance, |
| 118 | HexString.toHexString(rootSwitch.getDpid().value()), |
| 119 | HexString.toHexString(sw.getDpid().value()), path); |
| 120 | } |
| 121 | }*/ |
| 122 | } |
| 123 | |
| 124 | private void getDFSPaths(Dpid dstSwitchDpid, Path path, ArrayList<Path> paths) { |
| 125 | Dpid rootSwitchDpid = rootSwitch.getDpid(); |
| 126 | for (LinkData upstreamLink : upstreamLinks.get(dstSwitchDpid)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 127 | /* Deep clone the path object */ |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 128 | Path sofarPath = new Path(); |
| 129 | if (!path.isEmpty()) |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 130 | sofarPath.addAll(path.subList(0, path.size())); |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 131 | sofarPath.add(upstreamLink); |
| 132 | if (upstreamLink.getSrc().getDpid().equals(rootSwitchDpid)) |
| 133 | { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 134 | paths.add(sofarPath); |
| 135 | return; |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 136 | } |
| 137 | else |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 138 | getDFSPaths(upstreamLink.getSrc().getDpid(), sofarPath, paths); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 139 | } |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 143 | * Return the computed ECMP paths from the root switch to a given switch in |
| 144 | * the network |
| 145 | * |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 146 | * @param targetSwitch the target switch |
| 147 | * @return the list of ECMP Paths from the root switch to the target switch |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 148 | */ |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 149 | public ArrayList<Path> getECMPPaths(Switch targetSwitch) { |
| 150 | ArrayList<Path> pathArray = paths.get(targetSwitch.getDpid()); |
| 151 | if (pathArray == null && switchSearched.containsKey( |
| 152 | targetSwitch.getDpid())) { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 153 | pathArray = new ArrayList<>(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 154 | Path path = new Path(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 155 | Dpid sw = targetSwitch.getDpid(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 156 | getDFSPaths(sw, path, pathArray); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 157 | paths.put(targetSwitch.getDpid(), pathArray); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 158 | } |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 159 | return pathArray; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 160 | } |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Return the complete info of the computed ECMP paths for each switch |
| 164 | * learned in multiple iterations from the root switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 165 | * |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 166 | * @return the hash table of Switches learned in multiple Dijkstra |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 167 | * iterations and corresponding ECMP paths to it from the root |
| 168 | * switch |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 169 | */ |
| 170 | public HashMap<Integer, HashMap<Switch, |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 171 | ArrayList<Path>>> getCompleteLearnedSwitchesAndPaths() { |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 172 | |
| 173 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> pathGraph = new |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 174 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 175 | |
| 176 | for (Integer itrIndx : distanceSwitchMap.keySet()) { |
| 177 | HashMap<Switch, ArrayList<Path>> swMap = new |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 178 | HashMap<Switch, ArrayList<Path>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 179 | for (Switch sw : distanceSwitchMap.get(itrIndx)) { |
| 180 | swMap.put(sw, getECMPPaths(sw)); |
| 181 | } |
| 182 | pathGraph.put(itrIndx, swMap); |
| 183 | } |
| 184 | |
| 185 | return pathGraph; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Return the complete info of the computed ECMP paths for each switch |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 190 | * learned in multiple iterations from the root switch in the form of { |
| 191 | * Iteration1, Switch<> via {Switch<>, Switch<>} Switch<> via {Switch<>, |
| 192 | * Switch<>} Iteration2, Switch<> via {Switch<>, Switch<>, Switch<>} |
| 193 | * {Switch<>, Switch<>, Switch<>} Switch<> via {Switch<>, Switch<>, |
| 194 | * Switch<>} } |
| 195 | * |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 196 | * @return the hash table of Switches learned in multiple Dijkstra |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 197 | * iterations and corresponding ECMP paths in terms of Switches to |
| 198 | * be traversed to it from the root switch |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 199 | */ |
| 200 | public HashMap<Integer, HashMap<Switch, |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 201 | ArrayList<ArrayList<Dpid>>>> getAllLearnedSwitchesAndVia() { |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 202 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 203 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchViaMap = new HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 204 | |
| 205 | for (Integer itrIndx : distanceSwitchMap.keySet()) { |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 206 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swMap = |
| 207 | new HashMap<Switch, ArrayList<ArrayList<Dpid>>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 208 | |
| 209 | for (Switch sw : distanceSwitchMap.get(itrIndx)) { |
| 210 | ArrayList<ArrayList<Dpid>> swViaArray = new ArrayList<>(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 211 | for (Path path : getECMPPaths(sw)) { |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 212 | ArrayList<Dpid> swVia = new ArrayList<>(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 213 | for (LinkData link : path.subList(0, path.size())) { |
| 214 | if (link.getSrc().getDpid().equals(rootSwitch.getDpid())) { |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 215 | /* No need to add the root switch again in |
| 216 | * the Via list |
| 217 | */ |
| 218 | continue; |
| 219 | } |
| 220 | swVia.add(link.getSrc().getDpid()); |
| 221 | } |
| 222 | swViaArray.add(swVia); |
| 223 | } |
| 224 | swMap.put(sw, swViaArray); |
| 225 | } |
| 226 | switchViaMap.put(itrIndx, swMap); |
| 227 | } |
| 228 | return switchViaMap; |
| 229 | } |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 230 | } |