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