Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 25 | import org.onlab.graph.DijkstraGraphSearch; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 26 | import org.onlab.graph.DisjointPathPair; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 27 | import org.onlab.graph.GraphPathSearch; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 28 | import org.onlab.graph.GraphPathSearch.Result; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 29 | import org.onlab.graph.SrlgGraphSearch; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 30 | import org.onlab.graph.SuurballeGraphSearch; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 31 | import org.onlab.graph.TarjanGraphSearch; |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 32 | import org.onlab.graph.TarjanGraphSearch.SccResult; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.AbstractModel; |
| 34 | import org.onosproject.net.ConnectPoint; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DefaultDisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.DefaultPath; |
| 37 | import org.onosproject.net.DeviceId; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 38 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.Path; |
| 41 | import org.onosproject.net.provider.ProviderId; |
| 42 | import org.onosproject.net.topology.ClusterId; |
| 43 | import org.onosproject.net.topology.DefaultTopologyCluster; |
| 44 | import org.onosproject.net.topology.DefaultTopologyVertex; |
| 45 | import org.onosproject.net.topology.GraphDescription; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 46 | import org.onosproject.net.topology.HopCountLinkWeight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import org.onosproject.net.topology.LinkWeight; |
| 48 | import org.onosproject.net.topology.Topology; |
| 49 | import org.onosproject.net.topology.TopologyCluster; |
| 50 | import org.onosproject.net.topology.TopologyEdge; |
| 51 | import org.onosproject.net.topology.TopologyGraph; |
| 52 | import org.onosproject.net.topology.TopologyVertex; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 55 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 56 | import java.util.HashMap; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Set; |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 61 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 62 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 63 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 64 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 65 | import static org.onlab.util.Tools.isNullOrEmpty; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 66 | import static org.onosproject.core.CoreService.CORE_PROVIDER_ID; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 67 | import static org.onosproject.net.Link.State.INACTIVE; |
| 68 | import static org.onosproject.net.Link.Type.INDIRECT; |
| 69 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 70 | /** |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 71 | * Default implementation of the topology descriptor. This carries the backing |
| 72 | * topology data. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 73 | */ |
| 74 | public class DefaultTopology extends AbstractModel implements Topology { |
| 75 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 76 | private static final Logger log = LoggerFactory.getLogger(DefaultTopology.class); |
| 77 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 78 | private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA = new DijkstraGraphSearch<>(); |
| 79 | private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN = new TarjanGraphSearch<>(); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 80 | private static final SuurballeGraphSearch<TopologyVertex, TopologyEdge> SUURBALLE = new SuurballeGraphSearch<>(); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 81 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 82 | private static LinkWeight defaultLinkWeight = null; |
| 83 | private static GraphPathSearch<TopologyVertex, TopologyEdge> defaultGraphPathSearch = null; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 84 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 85 | private final long time; |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 86 | private final long creationTime; |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 87 | private final long computeCost; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 88 | private final TopologyGraph graph; |
| 89 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 90 | private final LinkWeight hopCountWeight; |
| 91 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 92 | private final Supplier<SccResult<TopologyVertex, TopologyEdge>> clusterResults; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 93 | private final Supplier<ImmutableMap<ClusterId, TopologyCluster>> clusters; |
| 94 | private final Supplier<ImmutableSet<ConnectPoint>> infrastructurePoints; |
| 95 | private final Supplier<ImmutableSetMultimap<ClusterId, ConnectPoint>> broadcastSets; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 96 | private final Function<ConnectPoint, Boolean> broadcastFunction; |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 97 | private final Supplier<ClusterIndexes> clusterIndexes; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 100 | * Sets the default link-weight to be used when computing paths. If null is |
| 101 | * specified, the builtin default link-weight measuring hop-counts will be |
| 102 | * used. |
| 103 | * |
| 104 | * @param linkWeight new default link-weight |
| 105 | */ |
| 106 | public static void setDefaultLinkWeight(LinkWeight linkWeight) { |
| 107 | log.info("Setting new default link-weight function to {}", linkWeight); |
| 108 | defaultLinkWeight = linkWeight; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets the default lpath search algorighm to be used when computing paths. |
| 113 | * If null is specified, the builtin default Dijkstra will be used. |
| 114 | * |
| 115 | * @param graphPathSearch new default algorithm |
| 116 | */ |
| 117 | public static void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) { |
| 118 | log.info("Setting new default graph path algorithm to {}", graphPathSearch); |
| 119 | defaultGraphPathSearch = graphPathSearch; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 124 | * Creates a topology descriptor attributed to the specified provider. |
| 125 | * |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 126 | * @param providerId identity of the provider |
| 127 | * @param description data describing the new topology |
| 128 | * @param broadcastFunction broadcast point function |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 129 | */ |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 130 | public DefaultTopology(ProviderId providerId, GraphDescription description, |
| 131 | Function<ConnectPoint, Boolean> broadcastFunction) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 132 | super(providerId); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 133 | this.broadcastFunction = broadcastFunction; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 134 | this.time = description.timestamp(); |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 135 | this.creationTime = description.creationTime(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 136 | |
| 137 | // Build the graph |
| 138 | this.graph = new DefaultTopologyGraph(description.vertexes(), |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 139 | description.edges()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 140 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 141 | this.clusterResults = Suppliers.memoize(() -> searchForClusters()); |
| 142 | this.clusters = Suppliers.memoize(() -> buildTopologyClusters()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 143 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 144 | this.clusterIndexes = Suppliers.memoize(() -> buildIndexes()); |
| 145 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 146 | this.hopCountWeight = new HopCountLinkWeight(graph.getVertexes().size()); |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 147 | this.broadcastSets = Suppliers.memoize(() -> buildBroadcastSets()); |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 148 | this.infrastructurePoints = Suppliers.memoize(() -> findInfrastructurePoints()); |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 149 | this.computeCost = Math.max(0, System.nanoTime() - time); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Creates a topology descriptor attributed to the specified provider. |
| 154 | * |
| 155 | * @param providerId identity of the provider |
| 156 | * @param description data describing the new topology |
| 157 | */ |
| 158 | public DefaultTopology(ProviderId providerId, GraphDescription description) { |
| 159 | this(providerId, description, null); |
| 160 | } |
| 161 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public long time() { |
| 164 | return time; |
| 165 | } |
| 166 | |
| 167 | @Override |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 168 | public long creationTime() { |
| 169 | return creationTime; |
| 170 | } |
| 171 | |
| 172 | @Override |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 173 | public long computeCost() { |
| 174 | return computeCost; |
| 175 | } |
| 176 | |
| 177 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 178 | public int clusterCount() { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 179 | return clusters.get().size(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public int deviceCount() { |
| 184 | return graph.getVertexes().size(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int linkCount() { |
| 189 | return graph.getEdges().size(); |
| 190 | } |
| 191 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 192 | private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice() { |
| 193 | return clusterIndexes.get().clustersByDevice; |
| 194 | } |
| 195 | |
| 196 | private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster() { |
| 197 | return clusterIndexes.get().devicesByCluster; |
| 198 | } |
| 199 | |
| 200 | private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster() { |
| 201 | return clusterIndexes.get().linksByCluster; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns the backing topology graph. |
| 206 | * |
| 207 | * @return topology graph |
| 208 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 209 | public TopologyGraph getGraph() { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 210 | return graph; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns the set of topology clusters. |
| 215 | * |
| 216 | * @return set of clusters |
| 217 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 218 | public Set<TopologyCluster> getClusters() { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 219 | return ImmutableSet.copyOf(clusters.get().values()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns the specified topology cluster. |
| 224 | * |
| 225 | * @param clusterId cluster identifier |
| 226 | * @return topology cluster |
| 227 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 228 | public TopologyCluster getCluster(ClusterId clusterId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 229 | return clusters.get().get(clusterId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns the topology cluster that contains the given device. |
| 234 | * |
| 235 | * @param deviceId device identifier |
| 236 | * @return topology cluster |
| 237 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 238 | public TopologyCluster getCluster(DeviceId deviceId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 239 | return clustersByDevice().get(deviceId); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns the set of cluster devices. |
| 244 | * |
| 245 | * @param cluster topology cluster |
| 246 | * @return cluster devices |
| 247 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 248 | public Set<DeviceId> getClusterDevices(TopologyCluster cluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 249 | return devicesByCluster().get(cluster); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Returns the set of cluster links. |
| 254 | * |
| 255 | * @param cluster topology cluster |
| 256 | * @return cluster links |
| 257 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 258 | public Set<Link> getClusterLinks(TopologyCluster cluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 259 | return linksByCluster().get(cluster); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Indicates whether the given point is an infrastructure link end-point. |
| 264 | * |
| 265 | * @param connectPoint connection point |
| 266 | * @return true if infrastructure |
| 267 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 268 | public boolean isInfrastructure(ConnectPoint connectPoint) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 269 | return infrastructurePoints.get().contains(connectPoint); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Indicates whether the given point is part of a broadcast set. |
| 274 | * |
| 275 | * @param connectPoint connection point |
| 276 | * @return true if in broadcast set |
| 277 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 278 | public boolean isBroadcastPoint(ConnectPoint connectPoint) { |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 279 | if (broadcastFunction != null) { |
| 280 | return broadcastFunction.apply(connectPoint); |
| 281 | } |
| 282 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 283 | // Any non-infrastructure, i.e. edge points are assumed to be OK. |
| 284 | if (!isInfrastructure(connectPoint)) { |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | // Find the cluster to which the device belongs. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 289 | TopologyCluster cluster = clustersByDevice().get(connectPoint.deviceId()); |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 290 | checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 291 | |
| 292 | // 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] | 293 | // belongs to it, return true. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 294 | Set<ConnectPoint> points = broadcastSets.get().get(cluster.id()); |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 295 | return isNullOrEmpty(points) || points.contains(connectPoint); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns the size of the cluster broadcast set. |
| 300 | * |
| 301 | * @param clusterId cluster identifier |
| 302 | * @return size of the cluster broadcast set |
| 303 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 304 | public int broadcastSetSize(ClusterId clusterId) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 305 | return broadcastSets.get().get(clusterId).size(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /** |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 309 | * Returns the set of the cluster broadcast points. |
| 310 | * |
| 311 | * @param clusterId cluster identifier |
| 312 | * @return set of cluster broadcast points |
| 313 | */ |
| 314 | public Set<ConnectPoint> broadcastPoints(ClusterId clusterId) { |
| 315 | return broadcastSets.get().get(clusterId); |
| 316 | } |
| 317 | |
| 318 | /** |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 319 | * Returns the set of pre-computed shortest paths between source and |
| 320 | * destination devices. |
| 321 | * |
| 322 | * @param src source device |
| 323 | * @param dst destination device |
| 324 | * @return set of shortest paths |
| 325 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 326 | public Set<Path> getPaths(DeviceId src, DeviceId dst) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 327 | return getPaths(src, dst, linkWeight()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Computes on-demand the set of shortest paths between source and |
| 332 | * destination devices. |
| 333 | * |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 334 | * @param src source device |
| 335 | * @param dst destination device |
Thomas Vachuska | b14c77a | 2014-11-04 18:08:01 -0800 | [diff] [blame] | 336 | * @param weight link weight function |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 337 | * @return set of shortest paths |
| 338 | */ |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 339 | public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 340 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 341 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 342 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 343 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 344 | // src or dst not part of the current graph |
| 345 | return ImmutableSet.of(); |
| 346 | } |
| 347 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 348 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 349 | graphPathSearch().search(graph, srcV, dstV, weight, ALL_PATHS); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 350 | ImmutableSet.Builder<Path> builder = ImmutableSet.builder(); |
| 351 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 352 | builder.add(networkPath(path)); |
| 353 | } |
| 354 | return builder.build(); |
| 355 | } |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 356 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 357 | /** |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 358 | * /** |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 359 | * Returns the set of pre-computed shortest disjoint path pairs between source and |
| 360 | * destination devices. |
| 361 | * |
| 362 | * @param src source device |
| 363 | * @param dst destination device |
| 364 | * @return set of shortest disjoint path pairs |
| 365 | */ |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 366 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 367 | return getDisjointPaths(src, dst, linkWeight()); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Computes on-demand the set of shortest disjoint path pairs between source and |
| 372 | * destination devices. |
| 373 | * |
| 374 | * @param src source device |
| 375 | * @param dst destination device |
| 376 | * @param weight link weight function |
| 377 | * @return set of disjoint shortest path pairs |
| 378 | */ |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 379 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, LinkWeight weight) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 380 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 381 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [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 | |
| 388 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
| 389 | SUURBALLE.search(graph, srcV, dstV, weight, ALL_PATHS); |
| 390 | ImmutableSet.Builder<DisjointPath> builder = ImmutableSet.builder(); |
| 391 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 392 | builder.add(networkDisjointPath((org.onlab.graph.DisjointPathPair<TopologyVertex, TopologyEdge>) path)); |
| 393 | } |
| 394 | return builder.build(); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Computes on-demand the set of shortest disjoint risk groups path pairs between source and |
| 399 | * destination devices. |
| 400 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 401 | * @param src source device |
| 402 | * @param dst destination device |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 403 | * @param weight edge weight object |
| 404 | * @param riskProfile map representing risk groups for each edge |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 405 | * @return set of shortest disjoint paths |
| 406 | */ |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 407 | private Set<DisjointPath> disjointPaths(DeviceId src, DeviceId dst, LinkWeight weight, |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 408 | Map<TopologyEdge, Object> riskProfile) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 409 | DefaultTopologyVertex srcV = new DefaultTopologyVertex(src); |
| 410 | DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst); |
| 411 | |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 412 | Set<TopologyVertex> vertices = graph.getVertexes(); |
| 413 | if (!vertices.contains(srcV) || !vertices.contains(dstV)) { |
| 414 | // src or dst not part of the current graph |
| 415 | return ImmutableSet.of(); |
| 416 | } |
| 417 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 418 | SrlgGraphSearch<TopologyVertex, TopologyEdge> srlg = new SrlgGraphSearch<>(riskProfile); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 419 | GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = |
| 420 | srlg.search(graph, srcV, dstV, weight, ALL_PATHS); |
| 421 | ImmutableSet.Builder<DisjointPath> builder = ImmutableSet.builder(); |
| 422 | for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) { |
| 423 | builder.add(networkDisjointPath((org.onlab.graph.DisjointPathPair<TopologyVertex, TopologyEdge>) path)); |
| 424 | } |
| 425 | return builder.build(); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Computes on-demand the set of shortest disjoint risk groups path pairs between source and |
| 430 | * destination devices. |
| 431 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 432 | * @param src source device |
| 433 | * @param dst destination device |
| 434 | * @param weight edge weight object |
| 435 | * @param riskProfile map representing risk groups for each link |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 436 | * @return set of shortest disjoint paths |
| 437 | */ |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 438 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, LinkWeight weight, |
| 439 | Map<Link, Object> riskProfile) { |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 440 | Map<TopologyEdge, Object> riskProfile2 = new HashMap<>(); |
| 441 | for (Link l : riskProfile.keySet()) { |
| 442 | riskProfile2.put(new TopologyEdge() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 443 | Link cur = l; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 444 | |
| 445 | public Link link() { |
| 446 | return cur; |
| 447 | } |
| 448 | |
| 449 | public TopologyVertex src() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 450 | return () -> src; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | public TopologyVertex dst() { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 454 | return () -> dst; |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 455 | } |
| 456 | }, riskProfile.get(l)); |
| 457 | } |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 458 | return disjointPaths(src, dst, weight, riskProfile2); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Computes on-demand the set of shortest disjoint risk groups path pairs between source and |
| 463 | * destination devices. |
| 464 | * |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 465 | * @param src source device |
| 466 | * @param dst destination device |
| 467 | * @param riskProfile map representing risk groups for each link |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 468 | * @return set of shortest disjoint paths |
| 469 | */ |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 470 | public Set<DisjointPath> getDisjointPaths(DeviceId src, DeviceId dst, Map<Link, Object> riskProfile) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 471 | return getDisjointPaths(src, dst, linkWeight(), riskProfile); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 472 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 473 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 474 | // Converts graph path to a network path with the same cost. |
| 475 | private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) { |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 476 | List<Link> links = path.edges().stream().map(TopologyEdge::link).collect(Collectors.toList()); |
Thomas Vachuska | 6acd3bb | 2014-11-09 23:44:22 -0800 | [diff] [blame] | 477 | return new DefaultPath(CORE_PROVIDER_ID, links, path.cost()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 480 | private DisjointPath networkDisjointPath(DisjointPathPair<TopologyVertex, TopologyEdge> path) { |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 481 | return new DefaultDisjointPath(CORE_PROVIDER_ID, |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 482 | (DefaultPath) networkPath(path.primary()), |
| 483 | (DefaultPath) networkPath(path.secondary())); |
Nikhil Cheerla | 1cf0f9a | 2015-07-09 12:26:48 -0700 | [diff] [blame] | 484 | } |
| 485 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 486 | // Searches for SCC clusters in the network topology graph using Tarjan |
| 487 | // algorithm. |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 488 | private SccResult<TopologyVertex, TopologyEdge> searchForClusters() { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 489 | return TARJAN.search(graph, new NoIndirectLinksWeight()); |
| 490 | } |
| 491 | |
| 492 | // Builds the topology clusters and returns the id-cluster bindings. |
| 493 | private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() { |
| 494 | ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder(); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 495 | SccResult<TopologyVertex, TopologyEdge> results = clusterResults.get(); |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 496 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 497 | // Extract both vertexes and edges from the results; the lists form |
| 498 | // pairs along the same index. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 499 | List<Set<TopologyVertex>> clusterVertexes = results.clusterVertexes(); |
| 500 | List<Set<TopologyEdge>> clusterEdges = results.clusterEdges(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 501 | |
| 502 | // Scan over the lists and create a cluster from the results. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 503 | for (int i = 0, n = results.clusterCount(); i < n; i++) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 504 | Set<TopologyVertex> vertexSet = clusterVertexes.get(i); |
| 505 | Set<TopologyEdge> edgeSet = clusterEdges.get(i); |
| 506 | |
| 507 | ClusterId cid = ClusterId.clusterId(i); |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 508 | DefaultTopologyCluster cluster = new DefaultTopologyCluster(cid, |
| 509 | vertexSet.size(), |
| 510 | edgeSet.size(), |
| 511 | findRoot(vertexSet)); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 512 | clusterBuilder.put(cid, cluster); |
| 513 | } |
| 514 | return clusterBuilder.build(); |
| 515 | } |
| 516 | |
| 517 | // Finds the vertex whose device id is the lexicographical minimum in the |
| 518 | // specified set. |
| 519 | private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) { |
| 520 | TopologyVertex minVertex = null; |
| 521 | for (TopologyVertex vertex : vertexSet) { |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 522 | if ((minVertex == null) || (minVertex.deviceId() |
| 523 | .toString().compareTo(minVertex.deviceId().toString()) < 0)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 524 | minVertex = vertex; |
| 525 | } |
| 526 | } |
| 527 | return minVertex; |
| 528 | } |
| 529 | |
| 530 | // Processes a map of broadcast sets for each cluster. |
| 531 | private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 532 | Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap.builder(); |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 533 | for (TopologyCluster cluster : clusters.get().values()) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 534 | addClusterBroadcastSet(cluster, builder); |
| 535 | } |
| 536 | return builder.build(); |
| 537 | } |
| 538 | |
| 539 | // Finds all broadcast points for the cluster. These are those connection |
| 540 | // points which lie along the shortest paths between the cluster root and |
| 541 | // all other devices within the cluster. |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 542 | private void addClusterBroadcastSet(TopologyCluster cluster, Builder<ClusterId, ConnectPoint> builder) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 543 | // Use the graph root search results to build the broadcast set. |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 544 | Result<TopologyVertex, TopologyEdge> result = DIJKSTRA.search(graph, cluster.root(), null, hopCountWeight, 1); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 545 | for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) { |
| 546 | TopologyVertex vertex = entry.getKey(); |
| 547 | |
| 548 | // Ignore any parents that lead outside the cluster. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 549 | if (clustersByDevice().get(vertex.deviceId()) != cluster) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | |
| 553 | // Ignore any back-link sets that are empty. |
| 554 | Set<TopologyEdge> parents = entry.getValue(); |
| 555 | if (parents.isEmpty()) { |
| 556 | continue; |
| 557 | } |
| 558 | |
| 559 | // Use the first back-link source and destinations to add to the |
| 560 | // broadcast set. |
| 561 | Link link = parents.iterator().next().link(); |
| 562 | builder.put(cluster.id(), link.src()); |
| 563 | builder.put(cluster.id(), link.dst()); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // Collects and returns an set of all infrastructure link end-points. |
| 568 | private ImmutableSet<ConnectPoint> findInfrastructurePoints() { |
| 569 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
| 570 | for (TopologyEdge edge : graph.getEdges()) { |
| 571 | builder.add(edge.link().src()); |
| 572 | builder.add(edge.link().dst()); |
| 573 | } |
| 574 | return builder.build(); |
| 575 | } |
| 576 | |
| 577 | // Builds cluster-devices, cluster-links and device-cluster indexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 578 | private ClusterIndexes buildIndexes() { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 579 | // Prepare the index builders |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 580 | ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder = |
| 581 | ImmutableMap.builder(); |
| 582 | ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder = |
| 583 | ImmutableSetMultimap.builder(); |
| 584 | ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder = |
| 585 | ImmutableSetMultimap.builder(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 586 | |
| 587 | // Now scan through all the clusters |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 588 | for (TopologyCluster cluster : clusters.get().values()) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 589 | int i = cluster.id().index(); |
| 590 | |
| 591 | // Scan through all the cluster vertexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 592 | for (TopologyVertex vertex : clusterResults.get().clusterVertexes().get(i)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 593 | devicesBuilder.put(cluster, vertex.deviceId()); |
| 594 | clusterBuilder.put(vertex.deviceId(), cluster); |
| 595 | } |
| 596 | |
| 597 | // Scan through all the cluster edges. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 598 | for (TopologyEdge edge : clusterResults.get().clusterEdges().get(i)) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 599 | linksBuilder.put(cluster, edge.link()); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Finalize all indexes. |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 604 | return new ClusterIndexes(clusterBuilder.build(), |
| 605 | devicesBuilder.build(), |
| 606 | linksBuilder.build()); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 609 | private GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch() { |
| 610 | return defaultGraphPathSearch != null ? defaultGraphPathSearch : DIJKSTRA; |
| 611 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 612 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 613 | private LinkWeight linkWeight() { |
| 614 | return defaultLinkWeight != null ? defaultLinkWeight : hopCountWeight; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Link weight for preventing traversal over indirect links. |
| 618 | private static class NoIndirectLinksWeight implements LinkWeight { |
| 619 | @Override |
| 620 | public double weight(TopologyEdge edge) { |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 621 | return (edge.link().state() == INACTIVE) |
| 622 | || (edge.link().type() == INDIRECT) ? -1 : 1; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 626 | static final class ClusterIndexes { |
| 627 | final ImmutableMap<DeviceId, TopologyCluster> clustersByDevice; |
| 628 | final ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster; |
| 629 | final ImmutableSetMultimap<TopologyCluster, Link> linksByCluster; |
| 630 | |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 631 | public ClusterIndexes(ImmutableMap<DeviceId, TopologyCluster> clustersByDevice, |
| 632 | ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster, |
| 633 | ImmutableSetMultimap<TopologyCluster, Link> linksByCluster) { |
Yuta HIGUCHI | b2e74a0 | 2014-11-30 13:54:38 -0800 | [diff] [blame] | 634 | this.clustersByDevice = clustersByDevice; |
| 635 | this.devicesByCluster = devicesByCluster; |
| 636 | this.linksByCluster = linksByCluster; |
| 637 | } |
| 638 | } |
| 639 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 640 | @Override |
| 641 | public String toString() { |
| 642 | return toStringHelper(this) |
| 643 | .add("time", time) |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 644 | .add("creationTime", creationTime) |
Thomas Vachuska | 6b7920d | 2014-11-25 19:48:39 -0800 | [diff] [blame] | 645 | .add("computeCost", computeCost) |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 646 | .add("clusters", clusterCount()) |
| 647 | .add("devices", deviceCount()) |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 648 | .add("links", linkCount()).toString(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 649 | } |
| 650 | } |