blob: 0ea6845e5b38f757bd377d195cc4476320a8a333 [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
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080018import org.onlab.graph.GraphPathSearch;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070019import org.onlab.util.KryoNamespace;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080020import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070021import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.event.Event;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070023import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.DeviceId;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080026import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Link;
28import org.onosproject.net.Path;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080029import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.provider.ProviderId;
31import org.onosproject.net.topology.ClusterId;
32import org.onosproject.net.topology.DefaultGraphDescription;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080033import org.onosproject.net.topology.GeoDistanceLinkWeight;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.topology.GraphDescription;
Andrey Komarov2398d962016-09-26 15:11:23 +030035import org.onosproject.net.topology.LinkWeigher;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080036import org.onosproject.net.topology.MetricLinkWeight;
37import org.onosproject.net.topology.PathAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.topology.Topology;
39import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080040import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.topology.TopologyEvent;
42import org.onosproject.net.topology.TopologyGraph;
43import org.onosproject.net.topology.TopologyStore;
44import org.onosproject.net.topology.TopologyStoreDelegate;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080045import org.onosproject.net.topology.TopologyVertex;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.store.AbstractStore;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070047import org.onosproject.store.serializers.KryoNamespaces;
48import org.onosproject.store.service.EventuallyConsistentMap;
49import org.onosproject.store.service.EventuallyConsistentMapEvent;
50import org.onosproject.store.service.EventuallyConsistentMapListener;
51import org.onosproject.store.service.LogicalClockService;
52import org.onosproject.store.service.StorageService;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080053import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054import org.osgi.service.component.annotations.Activate;
55import org.osgi.service.component.annotations.Component;
56import org.osgi.service.component.annotations.Deactivate;
57import org.osgi.service.component.annotations.Modified;
58import org.osgi.service.component.annotations.Reference;
59import org.osgi.service.component.annotations.ReferenceCardinality;
alshabib339a3d92014-09-26 17:54:32 -070060import org.slf4j.Logger;
61
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080062import java.util.Collections;
63import java.util.Dictionary;
64import java.util.List;
65import java.util.Map;
66import java.util.Objects;
67import java.util.Set;
68import java.util.stream.Collectors;
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -070069import java.util.stream.Stream;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080070
71import static com.google.common.base.Preconditions.checkArgument;
72import static org.onlab.util.Tools.get;
73import static org.onlab.util.Tools.isNullOrEmpty;
74import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
Ray Milkeyb5646e62018-10-16 11:42:18 -070075import static org.onosproject.store.OsgiPropertyConstants.LINK_WEIGHT_FUNCTION;
76import static org.onosproject.store.OsgiPropertyConstants.LINK_WEIGHT_FUNCTION_DEFAULT;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080077import 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 */
Ray Milkeyb5646e62018-10-16 11:42:18 -070086@Component(
87 immediate = true,
88 service = {
89 TopologyStore.class, PathAdminService.class
90 },
91 property = {
92 LINK_WEIGHT_FUNCTION + "=" + LINK_WEIGHT_FUNCTION_DEFAULT
93 }
94)
alshabib339a3d92014-09-26 17:54:32 -070095public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070096 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080097 implements TopologyStore, PathAdminService {
alshabib339a3d92014-09-26 17:54:32 -070098
99 private final Logger log = getLogger(getClass());
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800100
101 private static final String FORMAT = "Settings: linkWeightFunction={}";
102
Jonathan Hart29858af2014-10-29 15:33:18 -0700103 private volatile DefaultTopology current =
104 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700105 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700106 Collections.emptyList(),
107 Collections.emptyList()));
alshabib339a3d92014-09-26 17:54:32 -0700108
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700110 protected StorageService storageService;
111
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700113 protected LogicalClockService clockService;
114
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700116 protected MastershipService mastershipService;
117
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700118 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800119 protected ComponentConfigService configService;
120
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800122 protected DeviceService deviceService;
123
124 private static final String HOP_COUNT = "hopCount";
125 private static final String LINK_METRIC = "linkMetric";
126 private static final String GEO_DISTANCE = "geoDistance";
127
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700128 /** Default link-weight function: hopCount, linkMetric, geoDistance. */
Ray Milkeyb5646e62018-10-16 11:42:18 -0700129 private String linkWeightFunction = LINK_WEIGHT_FUNCTION_DEFAULT;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800130
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700131 // Cluster root to broadcast points bindings to allow convergence to
132 // a shared broadcast tree; node that is the master of the cluster root
133 // is the primary.
134 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
135
136 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
137 new InternalBroadcastPointListener();
138
alshabib339a3d92014-09-26 17:54:32 -0700139 @Activate
sisubram1a100a92017-08-09 10:26:00 +0000140 protected void activate(ComponentContext context) {
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800141 configService.registerProperties(getClass());
sisubram1a100a92017-08-09 10:26:00 +0000142 modified(context);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700143 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
144 .register(KryoNamespaces.API);
145
146 broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder()
147 .withName("onos-broadcast-trees")
148 .withSerializer(hostSerializer)
149 .withTimestampProvider((k, v) -> clockService.getTimestamp())
150 .build();
151 broadcastPoints.addListener(listener);
alshabib339a3d92014-09-26 17:54:32 -0700152 log.info("Started");
153 }
154
155 @Deactivate
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800156 protected void deactivate() {
157 configService.unregisterProperties(getClass(), false);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700158 broadcastPoints.removeListener(listener);
159 broadcastPoints.destroy();
alshabib339a3d92014-09-26 17:54:32 -0700160 log.info("Stopped");
161 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700162
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800163 @Modified
164 protected void modified(ComponentContext context) {
165 Dictionary<?, ?> properties = context.getProperties();
166
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700167 String newLinkWeightFunction = get(properties, LINK_WEIGHT_FUNCTION);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800168 if (newLinkWeightFunction != null &&
169 !Objects.equals(newLinkWeightFunction, linkWeightFunction)) {
170 linkWeightFunction = newLinkWeightFunction;
Ray Milkey7483e1b2018-02-07 15:43:01 -0800171 LinkWeigher weight = linkWeightFunction.equals(LINK_METRIC) ?
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800172 new MetricLinkWeight() :
173 linkWeightFunction.equals(GEO_DISTANCE) ?
174 new GeoDistanceLinkWeight(deviceService) : null;
Ray Milkey7483e1b2018-02-07 15:43:01 -0800175 setDefaultLinkWeigher(weight);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800176 }
177 log.info(FORMAT, linkWeightFunction);
178 }
179
alshabib339a3d92014-09-26 17:54:32 -0700180 @Override
181 public Topology currentTopology() {
182 return current;
183 }
184
185 @Override
186 public boolean isLatest(Topology topology) {
187 // Topology is current only if it is the same as our current topology
188 return topology == current;
189 }
190
191 @Override
192 public TopologyGraph getGraph(Topology topology) {
193 return defaultTopology(topology).getGraph();
194 }
195
196 @Override
197 public Set<TopologyCluster> getClusters(Topology topology) {
198 return defaultTopology(topology).getClusters();
199 }
200
201 @Override
202 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
203 return defaultTopology(topology).getCluster(clusterId);
204 }
205
206 @Override
207 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
208 return defaultTopology(topology).getClusterDevices(cluster);
209 }
210
211 @Override
212 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
213 return defaultTopology(topology).getClusterLinks(cluster);
214 }
215
216 @Override
217 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
218 return defaultTopology(topology).getPaths(src, dst);
219 }
220
Andrey Komarov2398d962016-09-26 15:11:23 +0300221
222 @Override
223 public Set<Path> getPaths(Topology topology, DeviceId src,
224 DeviceId dst, LinkWeigher weigher) {
225 return defaultTopology(topology).getPaths(src, dst, weigher);
alshabib339a3d92014-09-26 17:54:32 -0700226 }
227
228 @Override
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700229 public Set<Path> getKShortestPaths(Topology topology,
230 DeviceId src, DeviceId dst,
231 LinkWeigher weigher,
232 int maxPaths) {
233 return defaultTopology(topology).getKShortestPaths(src, dst, weigher, maxPaths);
234 }
235
236 @Override
237 public Stream<Path> getKShortestPaths(Topology topology,
238 DeviceId src,
239 DeviceId dst,
240 LinkWeigher weigher) {
241 return defaultTopology(topology).getKShortestPaths(src, dst, weigher);
242 }
243
244 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700245 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
246 return defaultTopology(topology).getDisjointPaths(src, dst);
247 }
248
249 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300250 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
251 DeviceId dst, LinkWeigher weigher) {
252 return defaultTopology(topology).getDisjointPaths(src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700253 }
254
255 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700256 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
257 Map<Link, Object> riskProfile) {
258 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700259 }
260
261 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300262 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
263 DeviceId dst, LinkWeigher weigher,
264 Map<Link, Object> riskProfile) {
265 return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700266 }
267
268 @Override
alshabib339a3d92014-09-26 17:54:32 -0700269 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
270 return defaultTopology(topology).isInfrastructure(connectPoint);
271 }
272
273 @Override
274 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
275 return defaultTopology(topology).isBroadcastPoint(connectPoint);
276 }
277
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700278 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
279 // Any non-infrastructure, i.e. edge points are assumed to be OK.
280 if (!current.isInfrastructure(connectPoint)) {
281 return true;
282 }
283
284 // Find the cluster to which the device belongs.
285 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
286 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
287
288 // If the broadcast set is null or empty, or if the point explicitly
289 // belongs to it, return true;
290 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
291 return isNullOrEmpty(points) || points.contains(connectPoint);
292 }
293
alshabib339a3d92014-09-26 17:54:32 -0700294 @Override
295 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700296 GraphDescription graphDescription,
297 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700298 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700299 DefaultTopology newTopology =
300 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
301 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700302
303 // Promote the new topology to current and return a ready-to-send event.
304 synchronized (this) {
You Wang66b77fa2017-02-06 16:51:10 -0800305 // Make sure that what we're given is indeed newer than what we
306 // already have.
307 if (current != null && newTopology.time() < current.time()) {
308 return null;
309 }
alshabib339a3d92014-09-26 17:54:32 -0700310 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700311 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700312 }
313 }
314
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700315 private void updateBroadcastPoints(DefaultTopology topology) {
316 // Remove any broadcast trees rooted by devices for which we are master.
317 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
318 .filter(mastershipService::isLocalMaster)
319 .collect(Collectors.toSet());
320
321 // Update the broadcast trees rooted by devices for which we are master.
322 topology.getClusters().forEach(c -> {
323 toRemove.remove(c.root().deviceId());
324 if (mastershipService.isLocalMaster(c.root().deviceId())) {
325 broadcastPoints.put(c.root().deviceId(),
326 topology.broadcastPoints(c.id()));
327 }
328 });
329
330 toRemove.forEach(broadcastPoints::remove);
331 }
332
alshabib339a3d92014-09-26 17:54:32 -0700333 // Validates the specified topology and returns it as a default
334 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700335 checkArgument(topology instanceof DefaultTopology,
336 "Topology class %s not supported", topology.getClass());
337 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700338 }
339
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800340 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +0300341 public void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
342 DefaultTopology.setDefaultLinkWeigher(linkWeigher);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800343 }
344
345 @Override
346 public void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) {
347 DefaultTopology.setDefaultGraphPathSearch(graphPathSearch);
348 }
349
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700350 private class InternalBroadcastPointListener
351 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
352 @Override
353 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
354 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
355 if (!event.value().isEmpty()) {
Madan Jampanib6e884b2016-06-23 01:34:15 -0700356 log.debug("Cluster rooted at {} has {} broadcast-points; #{}",
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700357 event.key(), event.value().size(), event.value().hashCode());
358 }
359 }
360 }
361 }
alshabib339a3d92014-09-26 17:54:32 -0700362}