Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.topology.impl; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 18 | import org.onlab.graph.GraphPathSearch; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 19 | import org.onlab.util.KryoNamespace; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 20 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 21 | import org.onosproject.common.DefaultTopology; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.event.Event; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 23 | import org.onosproject.mastership.MastershipService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 26 | import org.onosproject.net.DisjointPath; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 29 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.provider.ProviderId; |
| 31 | import org.onosproject.net.topology.ClusterId; |
| 32 | import org.onosproject.net.topology.DefaultGraphDescription; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 33 | import org.onosproject.net.topology.GeoDistanceLinkWeight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.topology.GraphDescription; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 35 | import org.onosproject.net.topology.LinkWeigher; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 36 | import org.onosproject.net.topology.MetricLinkWeight; |
| 37 | import org.onosproject.net.topology.PathAdminService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.topology.Topology; |
| 39 | import org.onosproject.net.topology.TopologyCluster; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 40 | import org.onosproject.net.topology.TopologyEdge; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.topology.TopologyEvent; |
| 42 | import org.onosproject.net.topology.TopologyGraph; |
| 43 | import org.onosproject.net.topology.TopologyStore; |
| 44 | import org.onosproject.net.topology.TopologyStoreDelegate; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 45 | import org.onosproject.net.topology.TopologyVertex; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.store.AbstractStore; |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 47 | import org.onosproject.store.serializers.KryoNamespaces; |
| 48 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 49 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 50 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 51 | import org.onosproject.store.service.LogicalClockService; |
| 52 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 53 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 54 | import org.osgi.service.component.annotations.Activate; |
| 55 | import org.osgi.service.component.annotations.Component; |
| 56 | import org.osgi.service.component.annotations.Deactivate; |
| 57 | import org.osgi.service.component.annotations.Modified; |
| 58 | import org.osgi.service.component.annotations.Reference; |
| 59 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 62 | import java.util.Collections; |
| 63 | import java.util.Dictionary; |
| 64 | import java.util.List; |
| 65 | import java.util.Map; |
| 66 | import java.util.Objects; |
| 67 | import java.util.Set; |
| 68 | import java.util.stream.Collectors; |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 69 | import java.util.stream.Stream; |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 70 | |
| 71 | import static com.google.common.base.Preconditions.checkArgument; |
| 72 | import static org.onlab.util.Tools.get; |
| 73 | import static org.onlab.util.Tools.isNullOrEmpty; |
| 74 | import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED; |
| 75 | import static org.slf4j.LoggerFactory.getLogger; |
| 76 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Manages inventory of topology snapshots using trivial in-memory |
| 79 | * structures implementation. |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 80 | * <p> |
Brian O'Connor | 4400853 | 2014-12-04 16:41:36 -0800 | [diff] [blame] | 81 | * Note: This component is not distributed per-se. It runs on every |
| 82 | * instance and feeds off of other distributed stores. |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 83 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 84 | @Component(immediate = true, service = { TopologyStore.class, PathAdminService.class }) |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 85 | public class DistributedTopologyStore |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 86 | extends AbstractStore<TopologyEvent, TopologyStoreDelegate> |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 87 | implements TopologyStore, PathAdminService { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 88 | |
| 89 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 90 | |
| 91 | private static final String FORMAT = "Settings: linkWeightFunction={}"; |
| 92 | |
Jonathan Hart | 29858af | 2014-10-29 15:33:18 -0700 | [diff] [blame] | 93 | private volatile DefaultTopology current = |
| 94 | new DefaultTopology(ProviderId.NONE, |
Thomas Vachuska | 320c58f | 2015-08-05 10:42:32 -0700 | [diff] [blame] | 95 | new DefaultGraphDescription(0L, System.currentTimeMillis(), |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 96 | Collections.emptyList(), |
| 97 | Collections.emptyList())); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 98 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 100 | protected StorageService storageService; |
| 101 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 103 | protected LogicalClockService clockService; |
| 104 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 106 | protected MastershipService mastershipService; |
| 107 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 109 | protected ComponentConfigService configService; |
| 110 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 112 | protected DeviceService deviceService; |
| 113 | |
| 114 | private static final String HOP_COUNT = "hopCount"; |
| 115 | private static final String LINK_METRIC = "linkMetric"; |
| 116 | private static final String GEO_DISTANCE = "geoDistance"; |
| 117 | |
| 118 | private static final String DEFAULT_LINK_WEIGHT_FUNCTION = "hopCount"; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 119 | //@Property(name = "linkWeightFunction", value = DEFAULT_LINK_WEIGHT_FUNCTION, |
| 120 | // label = "Default link-weight function: hopCount, linkMetric, geoDistance") |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 121 | private String linkWeightFunction = DEFAULT_LINK_WEIGHT_FUNCTION; |
| 122 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 123 | // Cluster root to broadcast points bindings to allow convergence to |
| 124 | // a shared broadcast tree; node that is the master of the cluster root |
| 125 | // is the primary. |
| 126 | private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints; |
| 127 | |
| 128 | private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener = |
| 129 | new InternalBroadcastPointListener(); |
| 130 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 131 | @Activate |
sisubram | 1a100a9 | 2017-08-09 10:26:00 +0000 | [diff] [blame] | 132 | protected void activate(ComponentContext context) { |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 133 | configService.registerProperties(getClass()); |
sisubram | 1a100a9 | 2017-08-09 10:26:00 +0000 | [diff] [blame] | 134 | modified(context); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 135 | KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder() |
| 136 | .register(KryoNamespaces.API); |
| 137 | |
| 138 | broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder() |
| 139 | .withName("onos-broadcast-trees") |
| 140 | .withSerializer(hostSerializer) |
| 141 | .withTimestampProvider((k, v) -> clockService.getTimestamp()) |
| 142 | .build(); |
| 143 | broadcastPoints.addListener(listener); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 144 | log.info("Started"); |
| 145 | } |
| 146 | |
| 147 | @Deactivate |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 148 | protected void deactivate() { |
| 149 | configService.unregisterProperties(getClass(), false); |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 150 | broadcastPoints.removeListener(listener); |
| 151 | broadcastPoints.destroy(); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 152 | log.info("Stopped"); |
| 153 | } |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 154 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 155 | @Modified |
| 156 | protected void modified(ComponentContext context) { |
| 157 | Dictionary<?, ?> properties = context.getProperties(); |
| 158 | |
| 159 | String newLinkWeightFunction = get(properties, "linkWeightFunction"); |
| 160 | if (newLinkWeightFunction != null && |
| 161 | !Objects.equals(newLinkWeightFunction, linkWeightFunction)) { |
| 162 | linkWeightFunction = newLinkWeightFunction; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 163 | LinkWeigher weight = linkWeightFunction.equals(LINK_METRIC) ? |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 164 | new MetricLinkWeight() : |
| 165 | linkWeightFunction.equals(GEO_DISTANCE) ? |
| 166 | new GeoDistanceLinkWeight(deviceService) : null; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 167 | setDefaultLinkWeigher(weight); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 168 | } |
| 169 | log.info(FORMAT, linkWeightFunction); |
| 170 | } |
| 171 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 172 | @Override |
| 173 | public Topology currentTopology() { |
| 174 | return current; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public boolean isLatest(Topology topology) { |
| 179 | // Topology is current only if it is the same as our current topology |
| 180 | return topology == current; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public TopologyGraph getGraph(Topology topology) { |
| 185 | return defaultTopology(topology).getGraph(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public Set<TopologyCluster> getClusters(Topology topology) { |
| 190 | return defaultTopology(topology).getClusters(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { |
| 195 | return defaultTopology(topology).getCluster(clusterId); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { |
| 200 | return defaultTopology(topology).getClusterDevices(cluster); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { |
| 205 | return defaultTopology(topology).getClusterLinks(cluster); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 210 | return defaultTopology(topology).getPaths(src, dst); |
| 211 | } |
| 212 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 213 | |
| 214 | @Override |
| 215 | public Set<Path> getPaths(Topology topology, DeviceId src, |
| 216 | DeviceId dst, LinkWeigher weigher) { |
| 217 | return defaultTopology(topology).getPaths(src, dst, weigher); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
Yuta HIGUCHI | ac8b229 | 2017-03-30 19:21:57 -0700 | [diff] [blame] | 221 | public Set<Path> getKShortestPaths(Topology topology, |
| 222 | DeviceId src, DeviceId dst, |
| 223 | LinkWeigher weigher, |
| 224 | int maxPaths) { |
| 225 | return defaultTopology(topology).getKShortestPaths(src, dst, weigher, maxPaths); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Stream<Path> getKShortestPaths(Topology topology, |
| 230 | DeviceId src, |
| 231 | DeviceId dst, |
| 232 | LinkWeigher weigher) { |
| 233 | return defaultTopology(topology).getKShortestPaths(src, dst, weigher); |
| 234 | } |
| 235 | |
| 236 | @Override |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 237 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) { |
| 238 | return defaultTopology(topology).getDisjointPaths(src, dst); |
| 239 | } |
| 240 | |
| 241 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 242 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 243 | DeviceId dst, LinkWeigher weigher) { |
| 244 | return defaultTopology(topology).getDisjointPaths(src, dst, weigher); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
Thomas Vachuska | 48e64e4 | 2015-09-22 15:32:55 -0700 | [diff] [blame] | 248 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, |
| 249 | Map<Link, Object> riskProfile) { |
| 250 | return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 254 | public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, |
| 255 | DeviceId dst, LinkWeigher weigher, |
| 256 | Map<Link, Object> riskProfile) { |
| 257 | return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile); |
Nikhil Cheerla | 2ec191f | 2015-07-09 12:34:54 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 261 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 262 | return defaultTopology(topology).isInfrastructure(connectPoint); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { |
| 267 | return defaultTopology(topology).isBroadcastPoint(connectPoint); |
| 268 | } |
| 269 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 270 | private boolean isBroadcastPoint(ConnectPoint connectPoint) { |
| 271 | // Any non-infrastructure, i.e. edge points are assumed to be OK. |
| 272 | if (!current.isInfrastructure(connectPoint)) { |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | // Find the cluster to which the device belongs. |
| 277 | TopologyCluster cluster = current.getCluster(connectPoint.deviceId()); |
| 278 | checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId()); |
| 279 | |
| 280 | // If the broadcast set is null or empty, or if the point explicitly |
| 281 | // belongs to it, return true; |
| 282 | Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId()); |
| 283 | return isNullOrEmpty(points) || points.contains(connectPoint); |
| 284 | } |
| 285 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 286 | @Override |
| 287 | public TopologyEvent updateTopology(ProviderId providerId, |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 288 | GraphDescription graphDescription, |
| 289 | List<Event> reasons) { |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 290 | // Have the default topology construct self from the description data. |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 291 | DefaultTopology newTopology = |
| 292 | new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint); |
| 293 | updateBroadcastPoints(newTopology); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 294 | |
| 295 | // Promote the new topology to current and return a ready-to-send event. |
| 296 | synchronized (this) { |
You Wang | 66b77fa | 2017-02-06 16:51:10 -0800 | [diff] [blame] | 297 | // Make sure that what we're given is indeed newer than what we |
| 298 | // already have. |
| 299 | if (current != null && newTopology.time() < current.time()) { |
| 300 | return null; |
| 301 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 302 | current = newTopology; |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 303 | return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons); |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 307 | private void updateBroadcastPoints(DefaultTopology topology) { |
| 308 | // Remove any broadcast trees rooted by devices for which we are master. |
| 309 | Set<DeviceId> toRemove = broadcastPoints.keySet().stream() |
| 310 | .filter(mastershipService::isLocalMaster) |
| 311 | .collect(Collectors.toSet()); |
| 312 | |
| 313 | // Update the broadcast trees rooted by devices for which we are master. |
| 314 | topology.getClusters().forEach(c -> { |
| 315 | toRemove.remove(c.root().deviceId()); |
| 316 | if (mastershipService.isLocalMaster(c.root().deviceId())) { |
| 317 | broadcastPoints.put(c.root().deviceId(), |
| 318 | topology.broadcastPoints(c.id())); |
| 319 | } |
| 320 | }); |
| 321 | |
| 322 | toRemove.forEach(broadcastPoints::remove); |
| 323 | } |
| 324 | |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 325 | // Validates the specified topology and returns it as a default |
| 326 | private DefaultTopology defaultTopology(Topology topology) { |
Thomas Vachuska | 930a8ee | 2015-08-04 18:49:36 -0700 | [diff] [blame] | 327 | checkArgument(topology instanceof DefaultTopology, |
| 328 | "Topology class %s not supported", topology.getClass()); |
| 329 | return (DefaultTopology) topology; |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 332 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 333 | public void setDefaultLinkWeigher(LinkWeigher linkWeigher) { |
| 334 | DefaultTopology.setDefaultLinkWeigher(linkWeigher); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) { |
| 339 | DefaultTopology.setDefaultGraphPathSearch(graphPathSearch); |
| 340 | } |
| 341 | |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 342 | private class InternalBroadcastPointListener |
| 343 | implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> { |
| 344 | @Override |
| 345 | public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) { |
| 346 | if (event.type() == EventuallyConsistentMapEvent.Type.PUT) { |
| 347 | if (!event.value().isEmpty()) { |
Madan Jampani | b6e884b | 2016-06-23 01:34:15 -0700 | [diff] [blame] | 348 | log.debug("Cluster rooted at {} has {} broadcast-points; #{}", |
Thomas Vachuska | b2c47a7 | 2015-08-05 14:22:54 -0700 | [diff] [blame] | 349 | event.key(), event.value().size(), event.value().hashCode()); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
alshabib | 339a3d9 | 2014-09-26 17:54:32 -0700 | [diff] [blame] | 354 | } |