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