blob: b6e6d0437d3b89d7f71eeec17d113a91f311dd4d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.topology.impl;
alshabib339a3d92014-09-26 17:54:32 -070017
alshabib339a3d92014-09-26 17:54:32 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib339a3d92014-09-26 17:54:32 -070025import org.apache.felix.scr.annotations.Service;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080026import org.onlab.graph.GraphPathSearch;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070027import org.onlab.util.KryoNamespace;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080028import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070029import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.event.Event;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070031import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.DeviceId;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080034import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.Link;
36import org.onosproject.net.Path;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080037import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.provider.ProviderId;
39import org.onosproject.net.topology.ClusterId;
40import org.onosproject.net.topology.DefaultGraphDescription;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080041import org.onosproject.net.topology.GeoDistanceLinkWeight;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030043import org.onosproject.net.topology.LinkWeigher;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080044import org.onosproject.net.topology.MetricLinkWeight;
45import org.onosproject.net.topology.PathAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.topology.Topology;
47import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080048import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.topology.TopologyEvent;
50import org.onosproject.net.topology.TopologyGraph;
51import org.onosproject.net.topology.TopologyStore;
52import org.onosproject.net.topology.TopologyStoreDelegate;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080053import org.onosproject.net.topology.TopologyVertex;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.store.AbstractStore;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070055import org.onosproject.store.serializers.KryoNamespaces;
56import org.onosproject.store.service.EventuallyConsistentMap;
57import org.onosproject.store.service.EventuallyConsistentMapEvent;
58import org.onosproject.store.service.EventuallyConsistentMapListener;
59import org.onosproject.store.service.LogicalClockService;
60import org.onosproject.store.service.StorageService;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080061import org.osgi.service.component.ComponentContext;
alshabib339a3d92014-09-26 17:54:32 -070062import org.slf4j.Logger;
63
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080064import java.util.Collections;
65import java.util.Dictionary;
66import java.util.List;
67import java.util.Map;
68import java.util.Objects;
69import java.util.Set;
70import java.util.stream.Collectors;
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -070071import java.util.stream.Stream;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080072
73import static com.google.common.base.Preconditions.checkArgument;
74import static org.onlab.util.Tools.get;
75import static org.onlab.util.Tools.isNullOrEmpty;
76import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
77import static org.slf4j.LoggerFactory.getLogger;
78
alshabib339a3d92014-09-26 17:54:32 -070079/**
80 * Manages inventory of topology snapshots using trivial in-memory
81 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070082 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080083 * Note: This component is not distributed per-se. It runs on every
84 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070085 */
alshabib339a3d92014-09-26 17:54:32 -070086@Component(immediate = true)
87@Service
88public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070089 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080090 implements TopologyStore, PathAdminService {
alshabib339a3d92014-09-26 17:54:32 -070091
92 private final Logger log = getLogger(getClass());
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080093
94 private static final String FORMAT = "Settings: linkWeightFunction={}";
95
Jonathan Hart29858af2014-10-29 15:33:18 -070096 private volatile DefaultTopology current =
97 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -070098 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Sho SHIMIZU21d00692016-08-15 11:15:28 -070099 Collections.emptyList(),
100 Collections.emptyList()));
alshabib339a3d92014-09-26 17:54:32 -0700101
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected StorageService storageService;
104
105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected LogicalClockService clockService;
107
108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected MastershipService mastershipService;
110
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected ComponentConfigService configService;
113
114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
115 protected DeviceService deviceService;
116
117 private static final String HOP_COUNT = "hopCount";
118 private static final String LINK_METRIC = "linkMetric";
119 private static final String GEO_DISTANCE = "geoDistance";
120
121 private static final String DEFAULT_LINK_WEIGHT_FUNCTION = "hopCount";
122 @Property(name = "linkWeightFunction", value = DEFAULT_LINK_WEIGHT_FUNCTION,
123 label = "Default link-weight function: hopCount, linkMetric, geoDistance")
124 private String linkWeightFunction = DEFAULT_LINK_WEIGHT_FUNCTION;
125
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700126 // Cluster root to broadcast points bindings to allow convergence to
127 // a shared broadcast tree; node that is the master of the cluster root
128 // is the primary.
129 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
130
131 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
132 new InternalBroadcastPointListener();
133
alshabib339a3d92014-09-26 17:54:32 -0700134 @Activate
sisubram1a100a92017-08-09 10:26:00 +0000135 protected void activate(ComponentContext context) {
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800136 configService.registerProperties(getClass());
sisubram1a100a92017-08-09 10:26:00 +0000137 modified(context);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700138 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
139 .register(KryoNamespaces.API);
140
141 broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder()
142 .withName("onos-broadcast-trees")
143 .withSerializer(hostSerializer)
144 .withTimestampProvider((k, v) -> clockService.getTimestamp())
145 .build();
146 broadcastPoints.addListener(listener);
alshabib339a3d92014-09-26 17:54:32 -0700147 log.info("Started");
148 }
149
150 @Deactivate
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800151 protected void deactivate() {
152 configService.unregisterProperties(getClass(), false);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700153 broadcastPoints.removeListener(listener);
154 broadcastPoints.destroy();
alshabib339a3d92014-09-26 17:54:32 -0700155 log.info("Stopped");
156 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700157
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800158 @Modified
159 protected void modified(ComponentContext context) {
160 Dictionary<?, ?> properties = context.getProperties();
161
162 String newLinkWeightFunction = get(properties, "linkWeightFunction");
163 if (newLinkWeightFunction != null &&
164 !Objects.equals(newLinkWeightFunction, linkWeightFunction)) {
165 linkWeightFunction = newLinkWeightFunction;
Ray Milkey7483e1b2018-02-07 15:43:01 -0800166 LinkWeigher weight = linkWeightFunction.equals(LINK_METRIC) ?
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800167 new MetricLinkWeight() :
168 linkWeightFunction.equals(GEO_DISTANCE) ?
169 new GeoDistanceLinkWeight(deviceService) : null;
Ray Milkey7483e1b2018-02-07 15:43:01 -0800170 setDefaultLinkWeigher(weight);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800171 }
172 log.info(FORMAT, linkWeightFunction);
173 }
174
alshabib339a3d92014-09-26 17:54:32 -0700175 @Override
176 public Topology currentTopology() {
177 return current;
178 }
179
180 @Override
181 public boolean isLatest(Topology topology) {
182 // Topology is current only if it is the same as our current topology
183 return topology == current;
184 }
185
186 @Override
187 public TopologyGraph getGraph(Topology topology) {
188 return defaultTopology(topology).getGraph();
189 }
190
191 @Override
192 public Set<TopologyCluster> getClusters(Topology topology) {
193 return defaultTopology(topology).getClusters();
194 }
195
196 @Override
197 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
198 return defaultTopology(topology).getCluster(clusterId);
199 }
200
201 @Override
202 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
203 return defaultTopology(topology).getClusterDevices(cluster);
204 }
205
206 @Override
207 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
208 return defaultTopology(topology).getClusterLinks(cluster);
209 }
210
211 @Override
212 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
213 return defaultTopology(topology).getPaths(src, dst);
214 }
215
Andrey Komarov2398d962016-09-26 15:11:23 +0300216
217 @Override
218 public Set<Path> getPaths(Topology topology, DeviceId src,
219 DeviceId dst, LinkWeigher weigher) {
220 return defaultTopology(topology).getPaths(src, dst, weigher);
alshabib339a3d92014-09-26 17:54:32 -0700221 }
222
223 @Override
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700224 public Set<Path> getKShortestPaths(Topology topology,
225 DeviceId src, DeviceId dst,
226 LinkWeigher weigher,
227 int maxPaths) {
228 return defaultTopology(topology).getKShortestPaths(src, dst, weigher, maxPaths);
229 }
230
231 @Override
232 public Stream<Path> getKShortestPaths(Topology topology,
233 DeviceId src,
234 DeviceId dst,
235 LinkWeigher weigher) {
236 return defaultTopology(topology).getKShortestPaths(src, dst, weigher);
237 }
238
239 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700240 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
241 return defaultTopology(topology).getDisjointPaths(src, dst);
242 }
243
244 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300245 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
246 DeviceId dst, LinkWeigher weigher) {
247 return defaultTopology(topology).getDisjointPaths(src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700248 }
249
250 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700251 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
252 Map<Link, Object> riskProfile) {
253 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700254 }
255
256 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300257 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
258 DeviceId dst, LinkWeigher weigher,
259 Map<Link, Object> riskProfile) {
260 return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700261 }
262
263 @Override
alshabib339a3d92014-09-26 17:54:32 -0700264 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
265 return defaultTopology(topology).isInfrastructure(connectPoint);
266 }
267
268 @Override
269 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
270 return defaultTopology(topology).isBroadcastPoint(connectPoint);
271 }
272
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700273 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
274 // Any non-infrastructure, i.e. edge points are assumed to be OK.
275 if (!current.isInfrastructure(connectPoint)) {
276 return true;
277 }
278
279 // Find the cluster to which the device belongs.
280 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
281 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
282
283 // If the broadcast set is null or empty, or if the point explicitly
284 // belongs to it, return true;
285 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
286 return isNullOrEmpty(points) || points.contains(connectPoint);
287 }
288
alshabib339a3d92014-09-26 17:54:32 -0700289 @Override
290 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700291 GraphDescription graphDescription,
292 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700293 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700294 DefaultTopology newTopology =
295 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
296 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700297
298 // Promote the new topology to current and return a ready-to-send event.
299 synchronized (this) {
You Wang66b77fa2017-02-06 16:51:10 -0800300 // Make sure that what we're given is indeed newer than what we
301 // already have.
302 if (current != null && newTopology.time() < current.time()) {
303 return null;
304 }
alshabib339a3d92014-09-26 17:54:32 -0700305 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700306 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700307 }
308 }
309
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700310 private void updateBroadcastPoints(DefaultTopology topology) {
311 // Remove any broadcast trees rooted by devices for which we are master.
312 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
313 .filter(mastershipService::isLocalMaster)
314 .collect(Collectors.toSet());
315
316 // Update the broadcast trees rooted by devices for which we are master.
317 topology.getClusters().forEach(c -> {
318 toRemove.remove(c.root().deviceId());
319 if (mastershipService.isLocalMaster(c.root().deviceId())) {
320 broadcastPoints.put(c.root().deviceId(),
321 topology.broadcastPoints(c.id()));
322 }
323 });
324
325 toRemove.forEach(broadcastPoints::remove);
326 }
327
alshabib339a3d92014-09-26 17:54:32 -0700328 // Validates the specified topology and returns it as a default
329 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700330 checkArgument(topology instanceof DefaultTopology,
331 "Topology class %s not supported", topology.getClass());
332 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700333 }
334
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800335 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300336 public void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
337 DefaultTopology.setDefaultLinkWeigher(linkWeigher);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800338 }
339
340 @Override
341 public void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) {
342 DefaultTopology.setDefaultGraphPathSearch(graphPathSearch);
343 }
344
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700345 private class InternalBroadcastPointListener
346 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
347 @Override
348 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
349 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
350 if (!event.value().isEmpty()) {
Madan Jampanib6e884b2016-06-23 01:34:15 -0700351 log.debug("Cluster rooted at {} has {} broadcast-points; #{}",
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700352 event.key(), event.value().size(), event.value().hashCode());
353 }
354 }
355 }
356 }
alshabib339a3d92014-09-26 17:54:32 -0700357}