Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 16 | package org.onosproject.common; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 18 | import com.google.common.base.Function; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 19 | import com.google.common.base.Supplier; |
| 20 | import com.google.common.base.Suppliers; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.ImmutableSetMultimap; |
| 24 | import com.google.common.collect.ImmutableSetMultimap.Builder; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 25 | import org.onlab.graph.DefaultEdgeWeigher; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 26 | import org.onlab.graph.DijkstraGraphSearch; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 27 | import org.onlab.graph.DisjointPathPair; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 28 | import org.onlab.graph.GraphPathSearch; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 29 | import org.onlab.graph.GraphPathSearch.Result; |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 30 | import org.onlab.graph.KShortestPathsSearch; |
| 31 | import org.onlab.graph.LazyKShortestPathsSearch; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 32 | import org.onlab.graph.ScalarWeight; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 33 | import org.onlab.graph.SrlgGraphSearch; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 34 | import org.onlab.graph.SuurballeGraphSearch; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 35 | import org.onlab.graph.TarjanGraphSearch; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 36 | import org.onlab.graph.TarjanGraphSearch.SccResult; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 37 | import org.onlab.graph.Weight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.AbstractModel; |
| 39 | import org.onosproject.net.ConnectPoint; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 40 | import org.onosproject.net.DefaultDisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.DefaultPath; |
| 42 | import org.onosproject.net.DeviceId; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 43 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.Link; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 45 | import org.onosproject.net.Link.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.Path; |
| 47 | import org.onosproject.net.provider.ProviderId; |
| 48 | import org.onosproject.net.topology.ClusterId; |
| 49 | import org.onosproject.net.topology.DefaultTopologyCluster; |
| 50 | import org.onosproject.net.topology.DefaultTopologyVertex; |
| 51 | import org.onosproject.net.topology.GraphDescription; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 52 | import org.onosproject.net.topology.HopCountLinkWeigher; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 53 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.topology.Topology; |
| 55 | import org.onosproject.net.topology.TopologyCluster; |
| 56 | import org.onosproject.net.topology.TopologyEdge; |
| 57 | import org.onosproject.net.topology.TopologyGraph; |
| 58 | import org.onosproject.net.topology.TopologyVertex; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 61 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 62 | import java.util.HashMap; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 63 | import java.util.List; |
| 64 | import java.util.Map; |
| 65 | import java.util.Set; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 67 | import java.util.stream.Stream; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 68 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 69 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 70 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 71 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 72 | import static org.onlab.util.Tools.isNullOrEmpty; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 73 | import static org.onosproject.core.CoreService.CORE_PROVIDER_ID; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 74 | import static org.onosproject.net.Link.State.INACTIVE; |
| 75 | import static org.onosproject.net.Link.Type.INDIRECT; |
| 76 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 77 | /** |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 78 | * Default implementation of the topology descriptor. This carries the backing |
| 79 | * topology data. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 80 | */ |
| 81 | public class DefaultTopology extends AbstractModel implements Topology { |
| 82 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 83 | private static final Logger log = LoggerFactory.getLogger(DefaultTopology.class); |
| 84 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 85 | private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA = |
| 86 | new DijkstraGraphSearch<>(); |
| 87 | private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN = |
| 88 | new TarjanGraphSearch<>(); |
| 89 | private static final SuurballeGraphSearch<TopologyVertex, TopologyEdge> SUURBALLE = |
| 90 | new SuurballeGraphSearch<>(); |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 91 | private static final KShortestPathsSearch<TopologyVertex, TopologyEdge> KSHORTEST = |
| 92 | new KShortestPathsSearch<>(); |
| 93 | private static final LazyKShortestPathsSearch<TopologyVertex, TopologyEdge> LAZY_KSHORTEST = |
| 94 | new LazyKShortestPathsSearch<>(); |
| 95 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 96 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 97 | private static LinkWeigher defaultLinkWeigher = null; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 98 | private static GraphPathSearch<TopologyVertex, TopologyEdge> defaultGraphPathSearch = null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 99 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 100 | private final long time; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 101 | private final long creationTime; |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 102 | private final long computeCost; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 103 | private final TopologyGraph graph; |
| 104 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 105 | private final LinkWeigher hopCountWeigher; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 106 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 107 | private final Supplier<SccResult<TopologyVertex, TopologyEdge>> clusterResults; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 108 | private final Supplier<ImmutableMap<ClusterId, TopologyCluster>> clusters; |
| 109 | private final Supplier<ImmutableSet<ConnectPoint>> infrastructurePoints; |
| 110 | private final Supplier<ImmutableSetMultimap<ClusterId, ConnectPoint>> broadcastSets; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 111 | private final Function<ConnectPoint, Boolean> broadcastFunction; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 112 | private final Supplier<ClusterIndexes> clusterIndexes; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 115 | * Sets the default link-weight to be used when computing paths. If null is |
| 116 | * specified, the builtin default link-weight measuring hop-counts will be |
| 117 | * used. |
| 118 | * |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 119 | * @param linkWeigher new default link-weight |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 120 | */ |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 121 | public static synchronized void setDefaultLinkWeigher(LinkWeigher linkWeigher) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 122 | log.info("Setting new default link-weight function to {}", linkWeigher); |
| 123 | defaultLinkWeigher = linkWeigher; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets the default lpath search algorighm to be used when computing paths. |
| 128 | * If null is specified, the builtin default Dijkstra will be used. |
| 129 | * |
| 130 | * @param graphPathSearch new default algorithm |
| 131 | */ |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 132 | public static synchronized void setDefaultGraphPathSearch( |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 133 | GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 134 | log.info("Setting new default graph path algorithm to {}", graphPathSearch); |
| 135 | defaultGraphPathSearch = graphPathSearch; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 140 | * Creates a topology descriptor attributed to the specified provider. |
| 141 | * |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 142 | * @param providerId identity of the provider |
| 143 | * @param description data describing the new topology |
| 144 | * @param broadcastFunction broadcast point function |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 145 | */ |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 146 | public DefaultTopology(ProviderId providerId, GraphDescription description, |
| 147 | Function<ConnectPoint, Boolean> broadcastFunction) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 148 | super(providerId); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 149 | this.broadcastFunction = broadcastFunction; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 150 | this.time = description.timestamp(); |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 151 | this.creationTime = description.creationTime(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 152 | |
| 153 | // Build the graph |
| 154 | this.graph = new DefaultTopologyGraph(description.vertexes(), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 155 | description.edges()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 156 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 157 | this.clusterResults = Suppliers.memoize(this::searchForClusters); |
| 158 | this.clusters = Suppliers.memoize(this::buildTopologyClusters); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 159 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 160 | this.clusterIndexes = Suppliers.memoize(this::buildIndexes); |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 161 | |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 162 | this.hopCountWeigher = new HopCountLinkWeigher(graph.getVertexes().size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 163 | this.broadcastSets = Suppliers.memoize(this::buildBroadcastSets); |
| 164 | this.infrastructurePoints = Suppliers.memoize(this::findInfrastructurePoints); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 165 | this.computeCost = Math.max(0, System.nanoTime() - time); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Creates a topology descriptor attributed to the specified provider. |
| 170 | * |
| 171 | * @param providerId identity of the provider |
| 172 | * @param description data describing the new topology |
| 173 | */ |
| 174 | public DefaultTopology(ProviderId providerId, GraphDescription description) { |
| 175 | this(providerId, description, null); |
| 176 | } |
| 177 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 178 | @Override |
| 179 | public long time() { |
| 180 | return time; |
| 181 | } |
| 182 | |
| 183 | @Override |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 184 | public long creationTime() { |
| 185 | return creationTime; |
| 186 | } |
| 187 | |
| 188 | @Override |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 189 | public long computeCost() { |
| 190 | return computeCost; |
| 191 | } |
| 192 | |
| 193 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 194 | public int clusterCount() { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 195 | return clusters.get().size(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public int deviceCount() { |
| 200 | return graph.getVertexes().size(); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int linkCount() { |
| 205 | return graph.getEdges().size(); |
| 206 | } |
| 207 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 208 | private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice() { |
| 209 | return clusterIndexes.get().clustersByDevice; |
| 210 | } |
| 211 | |
| 212 | private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster() { |
| 213 | return clusterIndexes.get().devicesByCluster; |
| 214 | } |
| 215 | |
| 216 | private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster() { |
| 217 | return clusterIndexes.get().linksByCluster; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns the backing topology graph. |
| 222 | * |
| 223 | * @return topology graph |
| 224 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 225 | public TopologyGraph getGraph() { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 226 | return graph; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns the set of topology clusters. |
| 231 | * |
| 232 | * @return set of clusters |
| 233 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 234 | public Set<TopologyCluster> getClusters() { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 235 | return ImmutableSet.copyOf(clusters.get().values()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Returns the specified topology cluster. |
| 240 | * |
| 241 | * @param clusterId cluster identifier |
| 242 | * @return topology cluster |
| 243 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 244 | public TopologyCluster getCluster(ClusterId clusterId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 245 | return clusters.get().get(clusterId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns the topology cluster that contains the given device. |
| 250 | * |
| 251 | * @param deviceId device identifier |
| 252 | * @return topology cluster |
| 253 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 254 | public TopologyCluster getCluster(DeviceId deviceId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 255 | return clustersByDevice().get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns the set of cluster devices. |
| 260 | * |
| 261 | * @param cluster topology cluster |
| 262 | * @return cluster devices |
| 263 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 264 | public Set<DeviceId> getClusterDevices(TopologyCluster cluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 265 | return devicesByCluster().get(cluster); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns the set of cluster links. |
| 270 | * |
| 271 | * @param cluster topology cluster |
| 272 | * @return cluster links |
| 273 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 274 | public Set<Link> getClusterLinks(TopologyCluster cluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 275 | return linksByCluster().get(cluster); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Indicates whether the given point is an infrastructure link end-point. |
| 280 | * |
| 281 | * @param connectPoint connection point |
| 282 | * @return true if infrastructure |
| 283 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 284 | public boolean isInfrastructure(ConnectPoint connectPoint) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 285 | return infrastructurePoints.get().contains(connectPoint); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Indicates whether the given point is part of a broadcast set. |
| 290 | * |
| 291 | * @param connectPoint connection point |
| 292 | * @return true if in broadcast set |
| 293 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 294 | public boolean isBroadcastPoint(ConnectPoint connectPoint) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 295 | if (broadcastFunction != null) { |
| 296 | return broadcastFunction.apply(connectPoint); |
| 297 | } |
| 298 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 299 | // Any non-infrastructure, i.e. edge points are assumed to be OK. |
| 300 | if (!isInfrastructure(connectPoint)) { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | // Find the cluster to which the device belongs. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 305 | TopologyCluster cluster = clustersByDevice().get(connectPoint.deviceId()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 306 | checkArgument(cluster != null, |
| 307 | "No cluster found for device %s", connectPoint.deviceId()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 308 | |
| 309 | // If the broadcast set is null or empty, or if the point explicitly |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 310 | // belongs to it, return true. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 311 | Set<ConnectPoint> points = broadcastSets.get().get(cluster.id()); |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 312 | return isNullOrEmpty(points) || points.contains(connectPoint); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Returns the size of the cluster broadcast set. |
| 317 | * |
| 318 | * @param clusterId cluster identifier |
| 319 | * @return size of the cluster broadcast set |
| 320 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 321 | public int broadcastSetSize(ClusterId clusterId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 322 | return broadcastSets.get().get(clusterId).size(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 326 | * Returns the set of the cluster broadcast points. |
| 327 | * |
| 328 | * @param clusterId cluster identifier |
| 329 | * @return set of cluster broadcast points |
| 330 | */ |
| 331 | public Set<ConnectPoint> broadcastPoints(ClusterId clusterId) { |
| 332 | return broadcastSets.get().get(clusterId); |
| 333 | } |
| 334 | |
| 335 | /** |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 336 | * Returns the set of pre-computed shortest paths between source and |
| 337 | * destination devices. |
| 338 | * |
| 339 | * @param src source device |
| 340 | * @param dst destination device |
| 341 | * @return set of shortest paths |
| 342 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 343 | public Set<Path> getPaths(DeviceId src, DeviceId dst) { |
Aaron Kruglikov | f6aed99 | 2016-11-08 15:16:03 -0800 | [diff] [blame] | 344 | return getPaths(src, dst, linkWeight(), ALL_PATHS); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Computes on-demand the set of shortest paths between source and |
| 349 | * destination devices. |
| 350 | * |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 351 | * @param src source device |
| 352 | * @param dst destination device |
| 353 | * @param weigher link weight function |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 354 | * @return set of shortest paths |
| 355 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 356 | public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeigher weigher) { |
| 357 | return getPaths(src, dst, weigher, ALL_PATHS); |
Aaron Kruglikov | f6aed99 | 2016-11-08 15:16:03 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Computes on-demand the set of shortest paths between source and |
| 362 | * destination devices, the set of returned paths will be no more than, |
| 363 | * maxPaths in size. The first {@code maxPaths} paths will be returned |
| 364 | * maintaining any ordering guarantees provided by the underlying |
| 365 | * (default or if no default is specified {@link DijkstraGraphSearch}) |
| 366 | * search. If returning all paths of a given length would exceed |
| 367 | * {@code maxPaths} a subset of paths of that length will be returned, |
| 368 | * which paths will be returned depends on the currently specified |
| 369 | * {@code GraphPathSearch}. See {@link #setDefaultGraphPathSearch}. |
| 370 | * |
| 371 | * @param src source device |
| 372 | * @param dst destination device |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 373 | * @param weigher link weight function |
Aaron Kruglikov | f6aed99 | 2016-11-08 15:16:03 -0800 | [diff] [blame] | 374 | * @param maxPaths maximum number of paths |
| 375 | * @return set of shortest paths |
| 376 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 377 | public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeigher weigher, |
Aaron Kruglikov | f6aed99 | 2016-11-08 15:16:03 -0800 | [diff] [blame] | 378 | int maxPaths) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 379 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 380 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 381 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 382 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 383 | // src or dst not part of the current graph |
| 384 | return ImmutableSet.of(); |
| 385 | } |
| 386 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 387 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 388 | graphPathSearch().search(graph, srcV, dstV, weigher, maxPaths); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 389 | ImmutableSet.Builder<Path> builder = ImmutableSet.builder(); |
| 390 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 391 | builder.add(networkPath(path)); |
| 392 | } |
| 393 | return builder.build(); |
| 394 | } |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 395 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 396 | /** |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 397 | * Computes on-demand the k-shortest paths between source and |
| 398 | * destination devices. |
| 399 | * |
| 400 | * @param src source device |
| 401 | * @param dst destination device |
| 402 | * @param maxPaths maximum number of paths (k) |
| 403 | * @return set of k-shortest paths |
| 404 | */ |
| 405 | public Set<Path> getKShortestPaths(DeviceId src, DeviceId dst, |
| 406 | int maxPaths) { |
| 407 | |
| 408 | return getKShortestPaths(src, dst, linkWeight(), maxPaths); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Computes on-demand the k-shortest paths between source and |
| 413 | * destination devices. |
| 414 | * |
| 415 | * The first {@code maxPaths} paths will be returned |
| 416 | * in ascending order according to the provided {@code weigher} |
| 417 | * |
| 418 | * @param src source device |
| 419 | * @param dst destination device |
| 420 | * @param weigher link weight function |
| 421 | * @param maxPaths maximum number of paths (k) |
| 422 | * @return set of k-shortest paths |
| 423 | */ |
| 424 | public Set<Path> getKShortestPaths(DeviceId src, DeviceId dst, |
| 425 | LinkWeigher weigher, |
| 426 | int maxPaths) { |
| 427 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 428 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
| 429 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 430 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 431 | // src or dst not part of the current graph |
| 432 | return ImmutableSet.of(); |
| 433 | } |
| 434 | |
| 435 | return KSHORTEST.search(graph, srcV, dstV, weigher, maxPaths) |
| 436 | .paths().stream() |
| 437 | .map(this::networkPath) |
Yuta HIGUCHI | 498fa1d | 2017-05-17 16:08:40 -0700 | [diff] [blame] | 438 | .collect(ImmutableSet.toImmutableSet()); |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Lazily computes on-demand the k-shortest paths between source and |
| 443 | * destination devices. |
| 444 | * |
| 445 | * |
| 446 | * @param src source device |
| 447 | * @param dst destination device |
| 448 | * @return stream of k-shortest paths |
| 449 | */ |
| 450 | public Stream<Path> getKShortestPaths(DeviceId src, DeviceId dst) { |
| 451 | return getKShortestPaths(src, dst, linkWeight()); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Lazily computes on-demand the k-shortest paths between source and |
| 456 | * destination devices. |
| 457 | * |
| 458 | * |
| 459 | * @param src source device |
| 460 | * @param dst destination device |
| 461 | * @param weigher link weight function |
| 462 | * @return stream of k-shortest paths |
| 463 | */ |
| 464 | public Stream<Path> getKShortestPaths(DeviceId src, DeviceId dst, |
| 465 | LinkWeigher weigher) { |
| 466 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 467 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
| 468 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 469 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 470 | // src or dst not part of the current graph |
| 471 | return Stream.empty(); |
| 472 | } |
| 473 | |
| 474 | return LAZY_KSHORTEST.lazyPathSearch(graph, srcV, dstV, weigher) |
| 475 | .map(this::networkPath); |
| 476 | } |
| 477 | |
| 478 | /** |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 479 | * Returns the set of pre-computed shortest disjoint path pairs between |
| 480 | * source and destination devices. |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 481 | * |
| 482 | * @param src source device |
| 483 | * @param dst destination device |
| 484 | * @return set of shortest disjoint path pairs |
| 485 | */ |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 486 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 487 | return getDisjointPaths(src, dst, linkWeight()); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /** |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 491 | * Computes on-demand the set of shortest disjoint path pairs between |
| 492 | * source and destination devices. |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 493 | * |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 494 | * @param src source device |
| 495 | * @param dst destination device |
| 496 | * @param weigher link weight function |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 497 | * @return set of disjoint shortest path pairs |
| 498 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 499 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, |
| 500 | LinkWeigher weigher) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 501 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 502 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 503 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 504 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 505 | // src or dst not part of the current graph |
| 506 | return ImmutableSet.of(); |
| 507 | } |
| 508 | |
| 509 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 510 | SUURBALLE.search(graph, srcV, dstV, weigher, ALL_PATHS); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 511 | ImmutableSet.Builder<DisjointPath> builder = ImmutableSet.builder(); |
| 512 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
helenyrwu | e3c4334 | 2016-08-11 10:33:12 -0700 | [diff] [blame] | 513 | DisjointPath disjointPath = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 514 | networkDisjointPath((DisjointPathPair<TopologyVertex, TopologyEdge>) path); |
helenyrwu | e3c4334 | 2016-08-11 10:33:12 -0700 | [diff] [blame] | 515 | if (disjointPath.backup() != null) { |
| 516 | builder.add(disjointPath); |
| 517 | } |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 518 | } |
| 519 | return builder.build(); |
| 520 | } |
| 521 | |
| 522 | /** |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 523 | * Computes on-demand the set of shortest disjoint risk groups path pairs |
| 524 | * between source and destination devices. |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 525 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 526 | * @param src source device |
| 527 | * @param dst destination device |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 528 | * @param weigher edge weight object |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 529 | * @param riskProfile map representing risk groups for each edge |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 530 | * @return set of shortest disjoint paths |
| 531 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 532 | private Set<DisjointPath> disjointPaths(DeviceId src, DeviceId dst, |
| 533 | LinkWeigher weigher, |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 534 | Map<TopologyEdge, Object> riskProfile) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 535 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 536 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
| 537 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 538 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 539 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 540 | // src or dst not part of the current graph |
| 541 | return ImmutableSet.of(); |
| 542 | } |
| 543 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 544 | SrlgGraphSearch<TopologyVertex, TopologyEdge> srlg = |
| 545 | new SrlgGraphSearch<>(riskProfile); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 546 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 547 | srlg.search(graph, srcV, dstV, weigher, ALL_PATHS); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 548 | ImmutableSet.Builder<DisjointPath> builder = ImmutableSet.builder(); |
| 549 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
helenyrwu | e3c4334 | 2016-08-11 10:33:12 -0700 | [diff] [blame] | 550 | DisjointPath disjointPath = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 551 | networkDisjointPath((DisjointPathPair<TopologyVertex, TopologyEdge>) path); |
helenyrwu | e3c4334 | 2016-08-11 10:33:12 -0700 | [diff] [blame] | 552 | if (disjointPath.backup() != null) { |
| 553 | builder.add(disjointPath); |
| 554 | } |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 555 | } |
| 556 | return builder.build(); |
| 557 | } |
| 558 | |
| 559 | /** |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 560 | * Computes on-demand the set of shortest disjoint risk groups path pairs |
| 561 | * between source and destination devices. |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 562 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 563 | * @param src source device |
| 564 | * @param dst destination device |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 565 | * @param weigher edge weight object |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 566 | * @param riskProfile map representing risk groups for each link |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 567 | * @return set of shortest disjoint paths |
| 568 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 569 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, |
| 570 | LinkWeigher weigher, |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 571 | Map<Link, Object> riskProfile) { |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 572 | Map<TopologyEdge, Object> riskProfile2 = new HashMap<>(); |
| 573 | for (Link l : riskProfile.keySet()) { |
| 574 | riskProfile2.put(new TopologyEdge() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 575 | Link cur = l; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 576 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 577 | @Override |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 578 | public Link link() { |
| 579 | return cur; |
| 580 | } |
| 581 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 582 | @Override |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 583 | public TopologyVertex src() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 584 | return () -> src; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 587 | @Override |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 588 | public TopologyVertex dst() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 589 | return () -> dst; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 590 | } |
| 591 | }, riskProfile.get(l)); |
| 592 | } |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 593 | return disjointPaths(src, dst, weigher, riskProfile2); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 597 | * Computes on-demand the set of shortest disjoint risk groups path pairs |
| 598 | * between source and destination devices. |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 599 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 600 | * @param src source device |
| 601 | * @param dst destination device |
| 602 | * @param riskProfile map representing risk groups for each link |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 603 | * @return set of shortest disjoint paths |
| 604 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 605 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, |
| 606 | Map<Link, Object> riskProfile) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 607 | return getDisjointPaths(src, dst, linkWeight(), riskProfile); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 608 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 609 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 610 | // Converts graph path to a network path with the same cost. |
| 611 | private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 612 | List<Link> links = path.edges().stream().map(TopologyEdge::link) |
| 613 | .collect(Collectors.toList()); |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 614 | return new DefaultPath(CORE_PROVIDER_ID, links, path.cost()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 617 | private DisjointPath networkDisjointPath( |
| 618 | DisjointPathPair<TopologyVertex, TopologyEdge> path) { |
Yuta HIGUCHI | c3d69f5 | 2016-08-08 11:52:52 -0700 | [diff] [blame] | 619 | if (!path.hasBackup()) { |
| 620 | // There was no secondary path available. |
| 621 | return new DefaultDisjointPath(CORE_PROVIDER_ID, |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 622 | (DefaultPath) networkPath(path.primary()), |
| 623 | null); |
Yuta HIGUCHI | c3d69f5 | 2016-08-08 11:52:52 -0700 | [diff] [blame] | 624 | } |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 625 | return new DefaultDisjointPath(CORE_PROVIDER_ID, |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 626 | (DefaultPath) networkPath(path.primary()), |
| 627 | (DefaultPath) networkPath(path.secondary())); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 628 | } |
| 629 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 630 | // Searches for SCC clusters in the network topology graph using Tarjan |
| 631 | // algorithm. |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 632 | private SccResult<TopologyVertex, TopologyEdge> searchForClusters() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 633 | return TARJAN.search(graph, new NoIndirectLinksWeigher()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Builds the topology clusters and returns the id-cluster bindings. |
| 637 | private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 638 | ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = |
| 639 | ImmutableMap.builder(); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 640 | SccResult<TopologyVertex, TopologyEdge> results = clusterResults.get(); |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 641 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 642 | // Extract both vertexes and edges from the results; the lists form |
| 643 | // pairs along the same index. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 644 | List<Set<TopologyVertex>> clusterVertexes = results.clusterVertexes(); |
| 645 | List<Set<TopologyEdge>> clusterEdges = results.clusterEdges(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 646 | |
| 647 | // Scan over the lists and create a cluster from the results. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 648 | for (int i = 0, n = results.clusterCount(); i < n; i++) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 649 | Set<TopologyVertex> vertexSet = clusterVertexes.get(i); |
| 650 | Set<TopologyEdge> edgeSet = clusterEdges.get(i); |
| 651 | |
| 652 | ClusterId cid = ClusterId.clusterId(i); |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 653 | DefaultTopologyCluster cluster = new DefaultTopologyCluster(cid, |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 654 | vertexSet.size(), |
| 655 | edgeSet.size(), |
| 656 | findRoot(vertexSet)); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 657 | clusterBuilder.put(cid, cluster); |
| 658 | } |
| 659 | return clusterBuilder.build(); |
| 660 | } |
| 661 | |
| 662 | // Finds the vertex whose device id is the lexicographical minimum in the |
| 663 | // specified set. |
| 664 | private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) { |
| 665 | TopologyVertex minVertex = null; |
| 666 | for (TopologyVertex vertex : vertexSet) { |
Jon Hall | e753963 | 2016-05-11 15:44:31 -0700 | [diff] [blame] | 667 | if ((minVertex == null) || (vertex.deviceId() |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 668 | .toString().compareTo(minVertex.deviceId().toString()) < 0)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 669 | minVertex = vertex; |
| 670 | } |
| 671 | } |
| 672 | return minVertex; |
| 673 | } |
| 674 | |
| 675 | // Processes a map of broadcast sets for each cluster. |
| 676 | private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 677 | Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder(); |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 678 | for (TopologyCluster cluster : clusters.get().values()) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 679 | addClusterBroadcastSet(cluster, builder); |
| 680 | } |
| 681 | return builder.build(); |
| 682 | } |
| 683 | |
| 684 | // Finds all broadcast points for the cluster. These are those connection |
| 685 | // points which lie along the shortest paths between the cluster root and |
| 686 | // all other devices within the cluster. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 687 | private void addClusterBroadcastSet(TopologyCluster cluster, |
| 688 | Builder<ClusterId, ConnectPoint> builder) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 689 | // Use the graph root search results to build the broadcast set. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 690 | Result<TopologyVertex, TopologyEdge> result = |
| 691 | DIJKSTRA.search(graph, cluster.root(), null, hopCountWeigher, 1); |
| 692 | for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : |
| 693 | result.parents().entrySet()) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 694 | TopologyVertex vertex = entry.getKey(); |
| 695 | |
| 696 | // Ignore any parents that lead outside the cluster. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 697 | if (clustersByDevice().get(vertex.deviceId()) != cluster) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 698 | continue; |
| 699 | } |
| 700 | |
| 701 | // Ignore any back-link sets that are empty. |
| 702 | Set<TopologyEdge> parents = entry.getValue(); |
| 703 | if (parents.isEmpty()) { |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | // Use the first back-link source and destinations to add to the |
| 708 | // broadcast set. |
| 709 | Link link = parents.iterator().next().link(); |
| 710 | builder.put(cluster.id(), link.src()); |
| 711 | builder.put(cluster.id(), link.dst()); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // Collects and returns an set of all infrastructure link end-points. |
| 716 | private ImmutableSet<ConnectPoint> findInfrastructurePoints() { |
| 717 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 718 | for (TopologyEdge edge : graph.getEdges()) { |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 719 | if (edge.link().type() == Type.EDGE) { |
| 720 | // exclude EDGE link from infrastructure link |
| 721 | // - Device <-> Host |
| 722 | // - Device <-> remote domain Device |
| 723 | continue; |
| 724 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 725 | builder.add(edge.link().src()); |
| 726 | builder.add(edge.link().dst()); |
| 727 | } |
| 728 | return builder.build(); |
| 729 | } |
| 730 | |
| 731 | // Builds cluster-devices, cluster-links and device-cluster indexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 732 | private ClusterIndexes buildIndexes() { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 733 | // Prepare the index builders |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 734 | ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = |
| 735 | ImmutableMap.builder(); |
| 736 | ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = |
| 737 | ImmutableSetMultimap.builder(); |
| 738 | ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = |
| 739 | ImmutableSetMultimap.builder(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 740 | |
| 741 | // Now scan through all the clusters |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 742 | for (TopologyCluster cluster : clusters.get().values()) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 743 | int i = cluster.id().index(); |
| 744 | |
| 745 | // Scan through all the cluster vertexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 746 | for (TopologyVertex vertex : clusterResults.get().clusterVertexes().get(i)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 747 | devicesBuilder.put(cluster, vertex.deviceId()); |
| 748 | clusterBuilder.put(vertex.deviceId(), cluster); |
| 749 | } |
| 750 | |
| 751 | // Scan through all the cluster edges. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 752 | for (TopologyEdge edge : clusterResults.get().clusterEdges().get(i)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 753 | linksBuilder.put(cluster, edge.link()); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | // Finalize all indexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 758 | return new ClusterIndexes(clusterBuilder.build(), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 759 | devicesBuilder.build(), |
| 760 | linksBuilder.build()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 763 | private GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch() { |
| 764 | return defaultGraphPathSearch != null ? defaultGraphPathSearch : DIJKSTRA; |
| 765 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 766 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 767 | private LinkWeigher linkWeight() { |
| 768 | return defaultLinkWeigher != null ? defaultLinkWeigher : hopCountWeigher; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | // Link weight for preventing traversal over indirect links. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 772 | private static class NoIndirectLinksWeigher |
| 773 | extends DefaultEdgeWeigher<TopologyVertex, TopologyEdge> |
| 774 | implements LinkWeigher { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 775 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 776 | public Weight weight(TopologyEdge edge) { |
| 777 | return (edge.link().state() == INACTIVE) || |
| 778 | (edge.link().type() == INDIRECT) ? |
| 779 | getNonViableWeight() : new ScalarWeight(HOP_WEIGHT_VALUE); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 783 | static final class ClusterIndexes { |
| 784 | final ImmutableMap<DeviceId, TopologyCluster> clustersByDevice; |
| 785 | final ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster; |
| 786 | final ImmutableSetMultimap<TopologyCluster, Link> linksByCluster; |
| 787 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 788 | public ClusterIndexes(ImmutableMap<DeviceId, TopologyCluster> clustersByDevice, |
| 789 | ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster, |
| 790 | ImmutableSetMultimap<TopologyCluster, Link> linksByCluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 791 | this.clustersByDevice = clustersByDevice; |
| 792 | this.devicesByCluster = devicesByCluster; |
| 793 | this.linksByCluster = linksByCluster; |
| 794 | } |
| 795 | } |
| 796 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 797 | @Override |
| 798 | public String toString() { |
| 799 | return toStringHelper(this) |
| 800 | .add("time", time) |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 801 | .add("creationTime", creationTime) |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 802 | .add("computeCost", computeCost) |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 803 | .add("clusters", clusterCount()) |
| 804 | .add("devices", deviceCount()) |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 805 | .add("links", linkCount()).toString(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 806 | } |
| 807 | } |