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