sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
| 18 | import org.onosproject.net.DefaultLink; |
| 19 | import org.onosproject.net.DefaultPath; |
| 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.Path; |
| 24 | import org.onosproject.net.provider.ProviderId; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 27 | |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.LinkedList; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 33 | import java.util.Set; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 36 | * This class creates breadth-first-search (BFS) tree for a given root device |
| 37 | * and returns paths from the root Device to leaf Devices (target devices). |
| 38 | * The paths are snapshot paths at the point of the class instantiation. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 39 | */ |
Shashikanth VH | 013a7bc | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 40 | public class EcmpShortestPathGraph { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | LinkedList<DeviceId> deviceQueue = new LinkedList<>(); |
| 42 | LinkedList<Integer> distanceQueue = new LinkedList<>(); |
| 43 | HashMap<DeviceId, Integer> deviceSearched = new HashMap<>(); |
| 44 | HashMap<DeviceId, ArrayList<Link>> upstreamLinks = new HashMap<>(); |
| 45 | HashMap<DeviceId, ArrayList<Path>> paths = new HashMap<>(); |
| 46 | HashMap<Integer, ArrayList<DeviceId>> distanceDeviceMap = new HashMap<>(); |
| 47 | DeviceId rootDevice; |
| 48 | private SegmentRoutingManager srManager; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 49 | private static final Logger log = LoggerFactory.getLogger(EcmpShortestPathGraph.class); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Constructor. |
| 53 | * |
| 54 | * @param rootDevice root of the BFS tree |
| 55 | * @param srManager SegmentRoutingManager object |
| 56 | */ |
Shashikanth VH | 013a7bc | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 57 | public EcmpShortestPathGraph(DeviceId rootDevice, SegmentRoutingManager srManager) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 58 | this.rootDevice = rootDevice; |
| 59 | this.srManager = srManager; |
| 60 | calcECMPShortestPathGraph(); |
| 61 | } |
| 62 | |
| 63 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 64 | * Calculates the BFS tree. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 66 | private void calcECMPShortestPathGraph() { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 67 | deviceQueue.add(rootDevice); |
| 68 | int currDistance = 0; |
| 69 | distanceQueue.add(currDistance); |
| 70 | deviceSearched.put(rootDevice, currDistance); |
| 71 | while (!deviceQueue.isEmpty()) { |
| 72 | DeviceId sw = deviceQueue.poll(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 73 | Set<DeviceId> prevSw = Sets.newHashSet(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 74 | currDistance = distanceQueue.poll(); |
| 75 | |
| 76 | for (Link link : srManager.linkService.getDeviceEgressLinks(sw)) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 77 | if (srManager.avoidLink(link)) { |
| 78 | continue; |
| 79 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 80 | DeviceId reachedDevice = link.dst().deviceId(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 81 | if (prevSw.contains(reachedDevice)) { |
| 82 | // Ignore LAG links between the same set of Devices |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 83 | continue; |
| 84 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 85 | prevSw.add(reachedDevice); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Integer distance = deviceSearched.get(reachedDevice); |
Sho SHIMIZU | af97343 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 89 | if ((distance != null) && (distance < (currDistance + 1))) { |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 90 | continue; |
| 91 | } |
| 92 | if (distance == null) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 93 | // First time visiting this Device node |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 94 | deviceQueue.add(reachedDevice); |
| 95 | distanceQueue.add(currDistance + 1); |
| 96 | deviceSearched.put(reachedDevice, currDistance + 1); |
| 97 | |
| 98 | ArrayList<DeviceId> distanceSwArray = distanceDeviceMap |
| 99 | .get(currDistance + 1); |
| 100 | if (distanceSwArray == null) { |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 101 | distanceSwArray = new ArrayList<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 102 | distanceSwArray.add(reachedDevice); |
| 103 | distanceDeviceMap.put(currDistance + 1, distanceSwArray); |
| 104 | } else { |
| 105 | distanceSwArray.add(reachedDevice); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ArrayList<Link> upstreamLinkArray = |
| 110 | upstreamLinks.get(reachedDevice); |
| 111 | if (upstreamLinkArray == null) { |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 112 | upstreamLinkArray = new ArrayList<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 113 | upstreamLinkArray.add(copyDefaultLink(link)); |
| 114 | //upstreamLinkArray.add(link); |
| 115 | upstreamLinks.put(reachedDevice, upstreamLinkArray); |
| 116 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 117 | // ECMP links |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 118 | upstreamLinkArray.add(copyDefaultLink(link)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 124 | private void getDFSPaths(DeviceId dstDeviceDeviceId, Path path, ArrayList<Path> paths) { |
| 125 | DeviceId rootDeviceDeviceId = rootDevice; |
| 126 | for (Link upstreamLink : upstreamLinks.get(dstDeviceDeviceId)) { |
| 127 | /* Deep clone the path object */ |
| 128 | Path sofarPath; |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 129 | ArrayList<Link> sofarLinks = new ArrayList<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 130 | if (path != null && !path.links().isEmpty()) { |
| 131 | sofarLinks.addAll(path.links()); |
| 132 | } |
| 133 | sofarLinks.add(upstreamLink); |
| 134 | sofarPath = new DefaultPath(ProviderId.NONE, sofarLinks, 0); |
| 135 | if (upstreamLink.src().deviceId().equals(rootDeviceDeviceId)) { |
| 136 | paths.add(sofarPath); |
| 137 | return; |
| 138 | } else { |
| 139 | getDFSPaths(upstreamLink.src().deviceId(), sofarPath, paths); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Return root Device for the graph. |
| 146 | * |
| 147 | * @return root Device |
| 148 | */ |
| 149 | public DeviceId getRootDevice() { |
| 150 | return rootDevice; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Return the computed ECMP paths from the root Device to a given Device in |
| 155 | * the network. |
| 156 | * |
| 157 | * @param targetDevice the target Device |
| 158 | * @return the list of ECMP Paths from the root Device to the target Device |
| 159 | */ |
| 160 | public ArrayList<Path> getECMPPaths(DeviceId targetDevice) { |
| 161 | ArrayList<Path> pathArray = paths.get(targetDevice); |
| 162 | if (pathArray == null && deviceSearched.containsKey( |
| 163 | targetDevice)) { |
| 164 | pathArray = new ArrayList<>(); |
| 165 | DeviceId sw = targetDevice; |
| 166 | getDFSPaths(sw, null, pathArray); |
| 167 | paths.put(targetDevice, pathArray); |
| 168 | } |
| 169 | return pathArray; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return the complete info of the computed ECMP paths for each Device |
| 174 | * learned in multiple iterations from the root Device. |
| 175 | * |
Saurav Das | a07f203 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 176 | * @return the hash table of Devices learned in multiple Dijkstra |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 177 | * iterations and corresponding ECMP paths to it from the root |
| 178 | * Device |
| 179 | */ |
| 180 | public HashMap<Integer, HashMap<DeviceId, |
| 181 | ArrayList<Path>>> getCompleteLearnedDeviceesAndPaths() { |
| 182 | |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 183 | HashMap<Integer, HashMap<DeviceId, ArrayList<Path>>> pathGraph = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 184 | |
| 185 | for (Integer itrIndx : distanceDeviceMap.keySet()) { |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 186 | HashMap<DeviceId, ArrayList<Path>> swMap = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 187 | for (DeviceId sw : distanceDeviceMap.get(itrIndx)) { |
| 188 | swMap.put(sw, getECMPPaths(sw)); |
| 189 | } |
| 190 | pathGraph.put(itrIndx, swMap); |
| 191 | } |
| 192 | |
| 193 | return pathGraph; |
| 194 | } |
| 195 | |
| 196 | /** |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 197 | * Returns the complete info of the computed ECMP paths for each target device |
Saurav Das | 4e3224f | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 198 | * learned in multiple iterations from the root Device. The computed info |
| 199 | * returned is per iteration (Integer key of outer HashMap). In each |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 200 | * iteration, for the target devices reached (DeviceId key of inner HashMap), |
Saurav Das | 4e3224f | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 201 | * the ECMP paths are detailed (2D array). |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 202 | * |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 203 | * @return the hash table of target Devices learned in multiple Dijkstra |
Saurav Das | a07f203 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 204 | * iterations and corresponding ECMP paths in terms of Devices to |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 205 | * be traversed (via) from the root Device to the target Device |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 206 | */ |
| 207 | public HashMap<Integer, HashMap<DeviceId, |
| 208 | ArrayList<ArrayList<DeviceId>>>> getAllLearnedSwitchesAndVia() { |
| 209 | |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 210 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> deviceViaMap = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 211 | |
| 212 | for (Integer itrIndx : distanceDeviceMap.keySet()) { |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 213 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swMap = new HashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 214 | |
| 215 | for (DeviceId sw : distanceDeviceMap.get(itrIndx)) { |
| 216 | ArrayList<ArrayList<DeviceId>> swViaArray = new ArrayList<>(); |
| 217 | for (Path path : getECMPPaths(sw)) { |
| 218 | ArrayList<DeviceId> swVia = new ArrayList<>(); |
| 219 | for (Link link : path.links()) { |
| 220 | if (link.src().deviceId().equals(rootDevice)) { |
| 221 | /* No need to add the root Device again in |
| 222 | * the Via list |
| 223 | */ |
| 224 | continue; |
| 225 | } |
| 226 | swVia.add(link.src().deviceId()); |
| 227 | } |
| 228 | swViaArray.add(swVia); |
| 229 | } |
| 230 | swMap.put(sw, swViaArray); |
| 231 | } |
| 232 | deviceViaMap.put(itrIndx, swMap); |
| 233 | } |
| 234 | return deviceViaMap; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | private Link copyDefaultLink(Link link) { |
| 239 | DefaultLink src = (DefaultLink) link; |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 240 | DefaultLink defaultLink = DefaultLink.builder() |
| 241 | .providerId(src.providerId()) |
| 242 | .src(src.src()) |
| 243 | .dst(src.dst()) |
| 244 | .type(src.type()) |
| 245 | .annotations(src.annotations()) |
| 246 | .build(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 247 | |
| 248 | return defaultLink; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public String toString() { |
| 253 | StringBuilder sBuilder = new StringBuilder(); |
| 254 | for (Device device: srManager.deviceService.getDevices()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 255 | if (!device.id().equals(rootDevice)) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 256 | sBuilder.append("\r\n Paths from " + rootDevice + " to " |
| 257 | + device.id()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 258 | ArrayList<Path> paths = getECMPPaths(device.id()); |
| 259 | if (paths != null) { |
| 260 | for (Path path : paths) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 261 | sBuilder.append("\r\n == "); // equal cost paths delimiter |
| 262 | for (int i = path.links().size() - 1; i >= 0; i--) { |
| 263 | Link link = path.links().get(i); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 264 | sBuilder.append(" : " + link.src() + " -> " + link.dst()); |
| 265 | } |
| 266 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 267 | } else { |
| 268 | sBuilder.append("\r\n == no paths"); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | return sBuilder.toString(); |
| 273 | } |
| 274 | } |
| 275 | |