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