blob: 51857c7e04d1635d0349ac38abe35bde24c85213 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 Vachuska930a8ee2015-08-04 18:49:36 -070016package org.onosproject.common;
alshabib339a3d92014-09-26 17:54:32 -070017
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070018import com.google.common.base.Supplier;
19import com.google.common.base.Suppliers;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.collect.ImmutableSetMultimap;
23import com.google.common.collect.ImmutableSetMultimap.Builder;
alshabib339a3d92014-09-26 17:54:32 -070024import org.onlab.graph.DijkstraGraphSearch;
25import org.onlab.graph.GraphPathSearch;
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080026import org.onlab.graph.GraphPathSearch.Result;
alshabib339a3d92014-09-26 17:54:32 -070027import org.onlab.graph.TarjanGraphSearch;
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080028import org.onlab.graph.TarjanGraphSearch.SCCResult;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.AbstractModel;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DefaultPath;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Link;
34import org.onosproject.net.Path;
35import org.onosproject.net.provider.ProviderId;
36import org.onosproject.net.topology.ClusterId;
37import org.onosproject.net.topology.DefaultTopologyCluster;
38import org.onosproject.net.topology.DefaultTopologyVertex;
39import org.onosproject.net.topology.GraphDescription;
40import org.onosproject.net.topology.LinkWeight;
41import org.onosproject.net.topology.Topology;
42import org.onosproject.net.topology.TopologyCluster;
43import org.onosproject.net.topology.TopologyEdge;
44import org.onosproject.net.topology.TopologyGraph;
45import org.onosproject.net.topology.TopologyVertex;
alshabib339a3d92014-09-26 17:54:32 -070046
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070047import java.util.ArrayList;
48import java.util.List;
49import java.util.Map;
50import java.util.Set;
alshabib339a3d92014-09-26 17:54:32 -070051
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070052import static com.google.common.base.MoreObjects.toStringHelper;
53import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
54import static org.onosproject.core.CoreService.CORE_PROVIDER_ID;
55import static org.onosproject.net.Link.State.ACTIVE;
56import static org.onosproject.net.Link.State.INACTIVE;
57import static org.onosproject.net.Link.Type.INDIRECT;
58
alshabib339a3d92014-09-26 17:54:32 -070059/**
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050060 * Default implementation of the topology descriptor. This carries the backing
61 * topology data.
alshabib339a3d92014-09-26 17:54:32 -070062 */
63public class DefaultTopology extends AbstractModel implements Topology {
64
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050065 private static final DijkstraGraphSearch<TopologyVertex, TopologyEdge> DIJKSTRA = new DijkstraGraphSearch<>();
66 private static final TarjanGraphSearch<TopologyVertex, TopologyEdge> TARJAN = new TarjanGraphSearch<>();
alshabib339a3d92014-09-26 17:54:32 -070067
alshabib339a3d92014-09-26 17:54:32 -070068 private final long time;
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050069 private final long creationTime;
Thomas Vachuska6b7920d2014-11-25 19:48:39 -080070 private final long computeCost;
alshabib339a3d92014-09-26 17:54:32 -070071 private final TopologyGraph graph;
72
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080073 private final LinkWeight weight;
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080074 private final Supplier<SCCResult<TopologyVertex, TopologyEdge>> clusterResults;
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080075 private final Supplier<ImmutableMap<ClusterId, TopologyCluster>> clusters;
76 private final Supplier<ImmutableSet<ConnectPoint>> infrastructurePoints;
77 private final Supplier<ImmutableSetMultimap<ClusterId, ConnectPoint>> broadcastSets;
alshabib339a3d92014-09-26 17:54:32 -070078
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080079 private final Supplier<ClusterIndexes> clusterIndexes;
alshabib339a3d92014-09-26 17:54:32 -070080
81 /**
82 * Creates a topology descriptor attributed to the specified provider.
83 *
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070084 * @param providerId identity of the provider
85 * @param description data describing the new topology
alshabib339a3d92014-09-26 17:54:32 -070086 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070087 public DefaultTopology(ProviderId providerId, GraphDescription description) {
alshabib339a3d92014-09-26 17:54:32 -070088 super(providerId);
89 this.time = description.timestamp();
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050090 this.creationTime = description.creationTime();
alshabib339a3d92014-09-26 17:54:32 -070091
92 // Build the graph
93 this.graph = new DefaultTopologyGraph(description.vertexes(),
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070094 description.edges());
alshabib339a3d92014-09-26 17:54:32 -070095
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080096 this.clusterResults = Suppliers.memoize(() -> searchForClusters());
97 this.clusters = Suppliers.memoize(() -> buildTopologyClusters());
alshabib339a3d92014-09-26 17:54:32 -070098
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -080099 this.clusterIndexes = Suppliers.memoize(() -> buildIndexes());
100
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800101 this.weight = new HopCountLinkWeight(graph.getVertexes().size());
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800102 this.broadcastSets = Suppliers.memoize(() -> buildBroadcastSets());
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700103 this.infrastructurePoints = Suppliers.memoize(() -> findInfrastructurePoints());
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800104 this.computeCost = Math.max(0, System.nanoTime() - time);
alshabib339a3d92014-09-26 17:54:32 -0700105 }
106
107 @Override
108 public long time() {
109 return time;
110 }
111
112 @Override
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500113 public long creationTime() {
114 return creationTime;
115 }
116
117 @Override
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800118 public long computeCost() {
119 return computeCost;
120 }
121
122 @Override
alshabib339a3d92014-09-26 17:54:32 -0700123 public int clusterCount() {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800124 return clusters.get().size();
alshabib339a3d92014-09-26 17:54:32 -0700125 }
126
127 @Override
128 public int deviceCount() {
129 return graph.getVertexes().size();
130 }
131
132 @Override
133 public int linkCount() {
134 return graph.getEdges().size();
135 }
136
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800137 private ImmutableMap<DeviceId, TopologyCluster> clustersByDevice() {
138 return clusterIndexes.get().clustersByDevice;
139 }
140
141 private ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster() {
142 return clusterIndexes.get().devicesByCluster;
143 }
144
145 private ImmutableSetMultimap<TopologyCluster, Link> linksByCluster() {
146 return clusterIndexes.get().linksByCluster;
alshabib339a3d92014-09-26 17:54:32 -0700147 }
148
149 /**
150 * Returns the backing topology graph.
151 *
152 * @return topology graph
153 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700154 public TopologyGraph getGraph() {
alshabib339a3d92014-09-26 17:54:32 -0700155 return graph;
156 }
157
158 /**
159 * Returns the set of topology clusters.
160 *
161 * @return set of clusters
162 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700163 public Set<TopologyCluster> getClusters() {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800164 return ImmutableSet.copyOf(clusters.get().values());
alshabib339a3d92014-09-26 17:54:32 -0700165 }
166
167 /**
168 * Returns the specified topology cluster.
169 *
170 * @param clusterId cluster identifier
171 * @return topology cluster
172 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700173 public TopologyCluster getCluster(ClusterId clusterId) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800174 return clusters.get().get(clusterId);
alshabib339a3d92014-09-26 17:54:32 -0700175 }
176
177 /**
178 * Returns the topology cluster that contains the given device.
179 *
180 * @param deviceId device identifier
181 * @return topology cluster
182 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700183 public TopologyCluster getCluster(DeviceId deviceId) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800184 return clustersByDevice().get(deviceId);
alshabib339a3d92014-09-26 17:54:32 -0700185 }
186
187 /**
188 * Returns the set of cluster devices.
189 *
190 * @param cluster topology cluster
191 * @return cluster devices
192 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700193 public Set<DeviceId> getClusterDevices(TopologyCluster cluster) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800194 return devicesByCluster().get(cluster);
alshabib339a3d92014-09-26 17:54:32 -0700195 }
196
197 /**
198 * Returns the set of cluster links.
199 *
200 * @param cluster topology cluster
201 * @return cluster links
202 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700203 public Set<Link> getClusterLinks(TopologyCluster cluster) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800204 return linksByCluster().get(cluster);
alshabib339a3d92014-09-26 17:54:32 -0700205 }
206
207 /**
208 * Indicates whether the given point is an infrastructure link end-point.
209 *
210 * @param connectPoint connection point
211 * @return true if infrastructure
212 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700213 public boolean isInfrastructure(ConnectPoint connectPoint) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800214 return infrastructurePoints.get().contains(connectPoint);
alshabib339a3d92014-09-26 17:54:32 -0700215 }
216
217 /**
218 * Indicates whether the given point is part of a broadcast set.
219 *
220 * @param connectPoint connection point
221 * @return true if in broadcast set
222 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700223 public boolean isBroadcastPoint(ConnectPoint connectPoint) {
alshabib339a3d92014-09-26 17:54:32 -0700224 // Any non-infrastructure, i.e. edge points are assumed to be OK.
225 if (!isInfrastructure(connectPoint)) {
226 return true;
227 }
228
229 // Find the cluster to which the device belongs.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800230 TopologyCluster cluster = clustersByDevice().get(connectPoint.deviceId());
alshabib339a3d92014-09-26 17:54:32 -0700231 if (cluster == null) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500232 throw new IllegalArgumentException("No cluster found for device "
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700233 + connectPoint.deviceId());
alshabib339a3d92014-09-26 17:54:32 -0700234 }
235
236 // If the broadcast set is null or empty, or if the point explicitly
237 // belongs to it, return true;
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800238 Set<ConnectPoint> points = broadcastSets.get().get(cluster.id());
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500239 return (points == null) || points.isEmpty() || points.contains(connectPoint);
alshabib339a3d92014-09-26 17:54:32 -0700240 }
241
242 /**
243 * Returns the size of the cluster broadcast set.
244 *
245 * @param clusterId cluster identifier
246 * @return size of the cluster broadcast set
247 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700248 public int broadcastSetSize(ClusterId clusterId) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800249 return broadcastSets.get().get(clusterId).size();
alshabib339a3d92014-09-26 17:54:32 -0700250 }
251
252 /**
253 * Returns the set of pre-computed shortest paths between source and
254 * destination devices.
255 *
256 * @param src source device
257 * @param dst destination device
258 * @return set of shortest paths
259 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700260 public Set<Path> getPaths(DeviceId src, DeviceId dst) {
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800261 return getPaths(src, dst, null);
alshabib339a3d92014-09-26 17:54:32 -0700262 }
263
264 /**
265 * Computes on-demand the set of shortest paths between source and
266 * destination devices.
267 *
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700268 * @param src source device
269 * @param dst destination device
Thomas Vachuskab14c77a2014-11-04 18:08:01 -0800270 * @param weight link weight function
alshabib339a3d92014-09-26 17:54:32 -0700271 * @return set of shortest paths
272 */
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700273 public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800274 final DefaultTopologyVertex srcV = new DefaultTopologyVertex(src);
275 final DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst);
276 Set<TopologyVertex> vertices = graph.getVertexes();
277 if (!vertices.contains(srcV) || !vertices.contains(dstV)) {
278 // src or dst not part of the current graph
279 return ImmutableSet.of();
280 }
281
alshabib339a3d92014-09-26 17:54:32 -0700282 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result =
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800283 DIJKSTRA.search(graph, srcV, dstV, weight, ALL_PATHS);
alshabib339a3d92014-09-26 17:54:32 -0700284 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
285 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
286 builder.add(networkPath(path));
287 }
288 return builder.build();
289 }
290
alshabib339a3d92014-09-26 17:54:32 -0700291 // Converts graph path to a network path with the same cost.
292 private Path networkPath(org.onlab.graph.Path<TopologyVertex, TopologyEdge> path) {
293 List<Link> links = new ArrayList<>();
294 for (TopologyEdge edge : path.edges()) {
295 links.add(edge.link());
296 }
Thomas Vachuska6acd3bb2014-11-09 23:44:22 -0800297 return new DefaultPath(CORE_PROVIDER_ID, links, path.cost());
alshabib339a3d92014-09-26 17:54:32 -0700298 }
299
alshabib339a3d92014-09-26 17:54:32 -0700300 // Searches for SCC clusters in the network topology graph using Tarjan
301 // algorithm.
302 private SCCResult<TopologyVertex, TopologyEdge> searchForClusters() {
303 return TARJAN.search(graph, new NoIndirectLinksWeight());
304 }
305
306 // Builds the topology clusters and returns the id-cluster bindings.
307 private ImmutableMap<ClusterId, TopologyCluster> buildTopologyClusters() {
308 ImmutableMap.Builder<ClusterId, TopologyCluster> clusterBuilder = ImmutableMap.builder();
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800309 SCCResult<TopologyVertex, TopologyEdge> results = clusterResults.get();
alshabib339a3d92014-09-26 17:54:32 -0700310 // Extract both vertexes and edges from the results; the lists form
311 // pairs along the same index.
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800312 List<Set<TopologyVertex>> clusterVertexes = results.clusterVertexes();
313 List<Set<TopologyEdge>> clusterEdges = results.clusterEdges();
alshabib339a3d92014-09-26 17:54:32 -0700314
315 // Scan over the lists and create a cluster from the results.
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800316 for (int i = 0, n = results.clusterCount(); i < n; i++) {
alshabib339a3d92014-09-26 17:54:32 -0700317 Set<TopologyVertex> vertexSet = clusterVertexes.get(i);
318 Set<TopologyEdge> edgeSet = clusterEdges.get(i);
319
320 ClusterId cid = ClusterId.clusterId(i);
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500321 DefaultTopologyCluster cluster = new DefaultTopologyCluster(cid,
322 vertexSet.size(),
323 edgeSet.size(),
324 findRoot(vertexSet));
alshabib339a3d92014-09-26 17:54:32 -0700325 clusterBuilder.put(cid, cluster);
326 }
327 return clusterBuilder.build();
328 }
329
330 // Finds the vertex whose device id is the lexicographical minimum in the
331 // specified set.
332 private TopologyVertex findRoot(Set<TopologyVertex> vertexSet) {
333 TopologyVertex minVertex = null;
334 for (TopologyVertex vertex : vertexSet) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500335 if ((minVertex == null) || (minVertex.deviceId()
336 .toString().compareTo(minVertex.deviceId().toString()) < 0)) {
alshabib339a3d92014-09-26 17:54:32 -0700337 minVertex = vertex;
338 }
339 }
340 return minVertex;
341 }
342
343 // Processes a map of broadcast sets for each cluster.
344 private ImmutableSetMultimap<ClusterId, ConnectPoint> buildBroadcastSets() {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500345 Builder<ClusterId, ConnectPoint> builder = ImmutableSetMultimap
346 .builder();
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800347 for (TopologyCluster cluster : clusters.get().values()) {
alshabib339a3d92014-09-26 17:54:32 -0700348 addClusterBroadcastSet(cluster, builder);
349 }
350 return builder.build();
351 }
352
353 // Finds all broadcast points for the cluster. These are those connection
354 // points which lie along the shortest paths between the cluster root and
355 // all other devices within the cluster.
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500356 private void addClusterBroadcastSet(TopologyCluster cluster, Builder<ClusterId, ConnectPoint> builder) {
alshabib339a3d92014-09-26 17:54:32 -0700357 // Use the graph root search results to build the broadcast set.
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500358 Result<TopologyVertex, TopologyEdge> result = DIJKSTRA.search(graph, cluster.root(), null, weight, 1);
alshabib339a3d92014-09-26 17:54:32 -0700359 for (Map.Entry<TopologyVertex, Set<TopologyEdge>> entry : result.parents().entrySet()) {
360 TopologyVertex vertex = entry.getKey();
361
362 // Ignore any parents that lead outside the cluster.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800363 if (clustersByDevice().get(vertex.deviceId()) != cluster) {
alshabib339a3d92014-09-26 17:54:32 -0700364 continue;
365 }
366
367 // Ignore any back-link sets that are empty.
368 Set<TopologyEdge> parents = entry.getValue();
369 if (parents.isEmpty()) {
370 continue;
371 }
372
373 // Use the first back-link source and destinations to add to the
374 // broadcast set.
375 Link link = parents.iterator().next().link();
376 builder.put(cluster.id(), link.src());
377 builder.put(cluster.id(), link.dst());
378 }
379 }
380
381 // Collects and returns an set of all infrastructure link end-points.
382 private ImmutableSet<ConnectPoint> findInfrastructurePoints() {
383 ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder();
384 for (TopologyEdge edge : graph.getEdges()) {
385 builder.add(edge.link().src());
386 builder.add(edge.link().dst());
387 }
388 return builder.build();
389 }
390
391 // Builds cluster-devices, cluster-links and device-cluster indexes.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800392 private ClusterIndexes buildIndexes() {
alshabib339a3d92014-09-26 17:54:32 -0700393 // Prepare the index builders
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500394 ImmutableMap.Builder<DeviceId, TopologyCluster> clusterBuilder =
395 ImmutableMap.builder();
396 ImmutableSetMultimap.Builder<TopologyCluster, DeviceId> devicesBuilder =
397 ImmutableSetMultimap.builder();
398 ImmutableSetMultimap.Builder<TopologyCluster, Link> linksBuilder =
399 ImmutableSetMultimap.builder();
alshabib339a3d92014-09-26 17:54:32 -0700400
401 // Now scan through all the clusters
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800402 for (TopologyCluster cluster : clusters.get().values()) {
alshabib339a3d92014-09-26 17:54:32 -0700403 int i = cluster.id().index();
404
405 // Scan through all the cluster vertexes.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800406 for (TopologyVertex vertex : clusterResults.get().clusterVertexes().get(i)) {
alshabib339a3d92014-09-26 17:54:32 -0700407 devicesBuilder.put(cluster, vertex.deviceId());
408 clusterBuilder.put(vertex.deviceId(), cluster);
409 }
410
411 // Scan through all the cluster edges.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800412 for (TopologyEdge edge : clusterResults.get().clusterEdges().get(i)) {
alshabib339a3d92014-09-26 17:54:32 -0700413 linksBuilder.put(cluster, edge.link());
414 }
415 }
416
417 // Finalize all indexes.
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800418 return new ClusterIndexes(clusterBuilder.build(),
419 devicesBuilder.build(),
420 linksBuilder.build());
alshabib339a3d92014-09-26 17:54:32 -0700421 }
422
423 // Link weight for measuring link cost as hop count with indirect links
424 // being as expensive as traversing the entire graph to assume the worst.
425 private static class HopCountLinkWeight implements LinkWeight {
426 private final int indirectLinkCost;
427
428 HopCountLinkWeight(int indirectLinkCost) {
429 this.indirectLinkCost = indirectLinkCost;
430 }
431
432 @Override
433 public double weight(TopologyEdge edge) {
434 // To force preference to use direct paths first, make indirect
435 // links as expensive as the linear vertex traversal.
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500436 return edge.link().state() ==
437 ACTIVE ? (edge.link().type() ==
438 INDIRECT ? indirectLinkCost : 1) : -1;
alshabib339a3d92014-09-26 17:54:32 -0700439 }
440 }
441
442 // Link weight for preventing traversal over indirect links.
443 private static class NoIndirectLinksWeight implements LinkWeight {
444 @Override
445 public double weight(TopologyEdge edge) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500446 return (edge.link().state() == INACTIVE)
447 || (edge.link().type() == INDIRECT) ? -1 : 1;
alshabib339a3d92014-09-26 17:54:32 -0700448 }
449 }
450
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800451 static final class ClusterIndexes {
452 final ImmutableMap<DeviceId, TopologyCluster> clustersByDevice;
453 final ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster;
454 final ImmutableSetMultimap<TopologyCluster, Link> linksByCluster;
455
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700456 public ClusterIndexes(ImmutableMap<DeviceId, TopologyCluster> clustersByDevice,
457 ImmutableSetMultimap<TopologyCluster, DeviceId> devicesByCluster,
458 ImmutableSetMultimap<TopologyCluster, Link> linksByCluster) {
Yuta HIGUCHIb2e74a02014-11-30 13:54:38 -0800459 this.clustersByDevice = clustersByDevice;
460 this.devicesByCluster = devicesByCluster;
461 this.linksByCluster = linksByCluster;
462 }
463 }
464
alshabib339a3d92014-09-26 17:54:32 -0700465 @Override
466 public String toString() {
467 return toStringHelper(this)
468 .add("time", time)
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500469 .add("creationTime", creationTime)
Thomas Vachuska6b7920d2014-11-25 19:48:39 -0800470 .add("computeCost", computeCost)
alshabib339a3d92014-09-26 17:54:32 -0700471 .add("clusters", clusterCount())
472 .add("devices", deviceCount())
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -0500473 .add("links", linkCount()).toString();
alshabib339a3d92014-09-26 17:54:32 -0700474 }
475}