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); |
| 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 | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 59 | if ((prevSw != null) && (prevSw.getDpid().equals(reachedSwitch.getDpid()))) |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 60 | { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 61 | /* Ignore LAG links between the same set of switches */ |
| 62 | continue; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 63 | } |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 64 | else |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 65 | { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 66 | prevSw = reachedSwitch; |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 67 | } |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 68 | |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 69 | Integer distance = switchSearched.get(reachedSwitch.getDpid()); |
| 70 | if ((distance != null) && (distance.intValue() < (currDistance+1))) { |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 71 | continue; |
| 72 | } |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 73 | if (distance == null){ |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 74 | /* First time visiting this switch node */ |
| 75 | switchQueue.add(reachedSwitch); |
| 76 | distanceQueue.add(currDistance+1); |
| 77 | switchSearched.put(reachedSwitch.getDpid(),currDistance+1); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 78 | |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 79 | ArrayList<Switch> distanceSwArray = distanceSwitchMap.get(currDistance+1); |
| 80 | if (distanceSwArray == null) |
| 81 | { |
| 82 | distanceSwArray = new ArrayList<Switch>(); |
| 83 | distanceSwArray.add(reachedSwitch); |
| 84 | distanceSwitchMap.put(currDistance+1, distanceSwArray); |
| 85 | } |
| 86 | else |
| 87 | distanceSwArray.add(reachedSwitch); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 88 | } |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 89 | |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 90 | ArrayList<LinkData> upstreamLinkArray = |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 91 | upstreamLinks.get(reachedSwitch.getDpid()); |
| 92 | if (upstreamLinkArray == null) |
| 93 | { |
| 94 | upstreamLinkArray = new ArrayList<LinkData>(); |
| 95 | upstreamLinkArray.add(new LinkData(link)); |
| 96 | upstreamLinks.put(reachedSwitch.getDpid(), upstreamLinkArray); |
| 97 | } |
| 98 | else |
| 99 | /* ECMP links */ |
| 100 | upstreamLinkArray.add(new LinkData(link)); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 103 | |
| 104 | log.debug("ECMPShortestPathGraph:switchSearched for switch {} is {}", |
| 105 | HexString.toHexString(rootSwitch.getDpid().value()), switchSearched); |
| 106 | log.debug("ECMPShortestPathGraph:upstreamLinks for switch {} is {}", |
| 107 | HexString.toHexString(rootSwitch.getDpid().value()), upstreamLinks); |
| 108 | log.debug("ECMPShortestPathGraph:distanceSwitchMap for switch {} is {}", |
| 109 | HexString.toHexString(rootSwitch.getDpid().value()), distanceSwitchMap); |
| 110 | /* |
| 111 | for (Integer distance: distanceSwitchMap.keySet()){ |
| 112 | for (Switch sw: distanceSwitchMap.get(distance)){ |
| 113 | ArrayList<Path> path = getPath(sw); |
| 114 | log.debug("ECMPShortestPathGraph:Paths in Pass{} from switch {} to switch {} is {}", |
| 115 | distance, |
| 116 | HexString.toHexString(rootSwitch.getDpid().value()), |
| 117 | HexString.toHexString(sw.getDpid().value()), path); |
| 118 | } |
| 119 | }*/ |
| 120 | } |
| 121 | |
| 122 | private void getDFSPaths(Dpid dstSwitchDpid, Path path, ArrayList<Path> paths) { |
| 123 | Dpid rootSwitchDpid = rootSwitch.getDpid(); |
| 124 | for (LinkData upstreamLink : upstreamLinks.get(dstSwitchDpid)) { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 125 | /* Deep clone the path object */ |
| 126 | Path sofarPath = new Path(); |
| 127 | if (!path.isEmpty()) |
| 128 | sofarPath.addAll(path.subList(0, path.size())); |
| 129 | sofarPath.add(upstreamLink); |
| 130 | if (upstreamLink.getSrc().getDpid().equals(rootSwitchDpid)) |
| 131 | { |
| 132 | paths.add(sofarPath); |
| 133 | return; |
| 134 | } |
| 135 | else |
| 136 | getDFSPaths(upstreamLink.getSrc().getDpid(),sofarPath, paths); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 137 | } |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 141 | * Return the computed ECMP paths from the root switch to a given switch |
| 142 | * in the network |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 143 | * |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 144 | * @param targetSwitch the target switch |
| 145 | * @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] | 146 | */ |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 147 | public ArrayList<Path> getECMPPaths(Switch targetSwitch) { |
| 148 | ArrayList<Path> pathArray = paths.get(targetSwitch.getDpid()); |
| 149 | if (pathArray == null && switchSearched.containsKey( |
| 150 | targetSwitch.getDpid())) { |
Srikanth Vavilapalli | 878dad1 | 2014-09-19 21:02:10 -0700 | [diff] [blame] | 151 | pathArray = new ArrayList<>(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 152 | Path path = new Path(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 153 | Dpid sw = targetSwitch.getDpid(); |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 154 | getDFSPaths(sw, path, pathArray); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 155 | paths.put(targetSwitch.getDpid(), pathArray); |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 156 | } |
Srikanth Vavilapalli | 174e67c | 2014-09-19 07:27:17 -0700 | [diff] [blame] | 157 | return pathArray; |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 158 | } |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Return the complete info of the computed ECMP paths for each switch |
| 162 | * learned in multiple iterations from the root switch |
| 163 | * |
| 164 | * @return the hash table of Switches learned in multiple Dijkstra |
| 165 | * iterations and corresponding ECMP paths to it from the root switch |
| 166 | */ |
| 167 | public HashMap<Integer, HashMap<Switch, |
| 168 | ArrayList<Path>>> getCompleteLearnedSwitchesAndPaths() { |
| 169 | |
| 170 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> pathGraph = new |
| 171 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>>(); |
| 172 | |
| 173 | for (Integer itrIndx : distanceSwitchMap.keySet()) { |
| 174 | HashMap<Switch, ArrayList<Path>> swMap = new |
| 175 | HashMap<Switch, ArrayList<Path>>(); |
| 176 | for (Switch sw : distanceSwitchMap.get(itrIndx)) { |
| 177 | swMap.put(sw, getECMPPaths(sw)); |
| 178 | } |
| 179 | pathGraph.put(itrIndx, swMap); |
| 180 | } |
| 181 | |
| 182 | return pathGraph; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Return the complete info of the computed ECMP paths for each switch |
| 187 | * learned in multiple iterations from the root switch in the form of |
| 188 | * { |
| 189 | * Iteration1, |
| 190 | * Switch<> via {Switch<>, Switch<>} |
| 191 | * Switch<> via {Switch<>, Switch<>} |
| 192 | * Iteration2, |
| 193 | * Switch<> via {Switch<>, Switch<>, Switch<>} |
| 194 | * {Switch<>, Switch<>, Switch<>} |
| 195 | * Switch<> via {Switch<>, Switch<>, Switch<>} |
| 196 | * } |
| 197 | * |
| 198 | * @return the hash table of Switches learned in multiple Dijkstra |
| 199 | * iterations and corresponding ECMP paths in terms of Switches to be |
| 200 | * traversed to it from the root switch |
| 201 | */ |
| 202 | public HashMap<Integer, HashMap<Switch, |
| 203 | ArrayList<ArrayList<Dpid>>>> getAllLearnedSwitchesAndVia() { |
| 204 | |
| 205 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 206 | switchViaMap = new HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 207 | |
| 208 | for (Integer itrIndx : distanceSwitchMap.keySet()) { |
Srikanth Vavilapalli | 5ddf8f0 | 2014-09-24 11:02:28 -0700 | [diff] [blame] | 209 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swMap = |
| 210 | new HashMap<Switch, ArrayList<ArrayList<Dpid>>>(); |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 211 | |
| 212 | for (Switch sw : distanceSwitchMap.get(itrIndx)) { |
| 213 | ArrayList<ArrayList<Dpid>> swViaArray = new ArrayList<>(); |
| 214 | for (Path path:getECMPPaths(sw)){ |
| 215 | ArrayList<Dpid> swVia = new ArrayList<>(); |
| 216 | for (LinkData link:path.subList(0, path.size())){ |
| 217 | if (link.getSrc().getDpid().equals(rootSwitch.getDpid())){ |
| 218 | /* No need to add the root switch again in |
| 219 | * the Via list |
| 220 | */ |
| 221 | continue; |
| 222 | } |
| 223 | swVia.add(link.getSrc().getDpid()); |
| 224 | } |
| 225 | swViaArray.add(swVia); |
| 226 | } |
| 227 | swMap.put(sw, swViaArray); |
| 228 | } |
| 229 | switchViaMap.put(itrIndx, swMap); |
| 230 | } |
| 231 | return switchViaMap; |
| 232 | } |
Srikanth Vavilapalli | 9338f9d | 2014-09-18 07:37:09 -0700 | [diff] [blame] | 233 | } |