blob: ffb5a950224a36a2fb429bdbf1347e1874e70cc6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.topology.LinkWeight;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080045import org.onosproject.net.topology.MetricLinkWeight;
46import org.onosproject.net.topology.PathAdminService;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.topology.Topology;
48import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080049import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.topology.TopologyEvent;
51import org.onosproject.net.topology.TopologyGraph;
52import org.onosproject.net.topology.TopologyStore;
53import org.onosproject.net.topology.TopologyStoreDelegate;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080054import org.onosproject.net.topology.TopologyVertex;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.store.AbstractStore;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070056import org.onosproject.store.serializers.KryoNamespaces;
57import org.onosproject.store.service.EventuallyConsistentMap;
58import org.onosproject.store.service.EventuallyConsistentMapEvent;
59import org.onosproject.store.service.EventuallyConsistentMapListener;
60import org.onosproject.store.service.LogicalClockService;
61import org.onosproject.store.service.StorageService;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080062import org.osgi.service.component.ComponentContext;
alshabib339a3d92014-09-26 17:54:32 -070063import org.slf4j.Logger;
64
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080065import java.util.Collections;
66import java.util.Dictionary;
67import java.util.List;
68import java.util.Map;
69import java.util.Objects;
70import java.util.Set;
71import java.util.stream.Collectors;
72
73import static com.google.common.base.Preconditions.checkArgument;
74import static org.onlab.util.Tools.get;
75import static org.onlab.util.Tools.isNullOrEmpty;
Andrey Komarov2398d962016-09-26 15:11:23 +030076import static org.onosproject.net.topology.AdapterLinkWeigher.adapt;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080077import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
78import static org.slf4j.LoggerFactory.getLogger;
79
alshabib339a3d92014-09-26 17:54:32 -070080/**
81 * Manages inventory of topology snapshots using trivial in-memory
82 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070083 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080084 * Note: This component is not distributed per-se. It runs on every
85 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070086 */
alshabib339a3d92014-09-26 17:54:32 -070087@Component(immediate = true)
88@Service
89public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070090 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080091 implements TopologyStore, PathAdminService {
alshabib339a3d92014-09-26 17:54:32 -070092
93 private final Logger log = getLogger(getClass());
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080094
95 private static final String FORMAT = "Settings: linkWeightFunction={}";
96
Jonathan Hart29858af2014-10-29 15:33:18 -070097 private volatile DefaultTopology current =
98 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -070099 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700100 Collections.emptyList(),
101 Collections.emptyList()));
alshabib339a3d92014-09-26 17:54:32 -0700102
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected StorageService storageService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected LogicalClockService clockService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected MastershipService mastershipService;
111
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected ComponentConfigService configService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected DeviceService deviceService;
117
118 private static final String HOP_COUNT = "hopCount";
119 private static final String LINK_METRIC = "linkMetric";
120 private static final String GEO_DISTANCE = "geoDistance";
121
122 private static final String DEFAULT_LINK_WEIGHT_FUNCTION = "hopCount";
123 @Property(name = "linkWeightFunction", value = DEFAULT_LINK_WEIGHT_FUNCTION,
124 label = "Default link-weight function: hopCount, linkMetric, geoDistance")
125 private String linkWeightFunction = DEFAULT_LINK_WEIGHT_FUNCTION;
126
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700127 // Cluster root to broadcast points bindings to allow convergence to
128 // a shared broadcast tree; node that is the master of the cluster root
129 // is the primary.
130 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
131
132 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
133 new InternalBroadcastPointListener();
134
alshabib339a3d92014-09-26 17:54:32 -0700135 @Activate
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800136 protected void activate() {
137 configService.registerProperties(getClass());
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;
166 LinkWeight weight = linkWeightFunction.equals(LINK_METRIC) ?
167 new MetricLinkWeight() :
168 linkWeightFunction.equals(GEO_DISTANCE) ?
169 new GeoDistanceLinkWeight(deviceService) : null;
170 setDefaultLinkWeight(weight);
171 }
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
216 @Override
217 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700218 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300219 return getPaths(topology, src, dst, adapt(weight));
220 }
221
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
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700229 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
230 return defaultTopology(topology).getDisjointPaths(src, dst);
231 }
232
233 @Override
234 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
235 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300236 return getDisjointPaths(topology, src, dst, adapt(weight));
237 }
238
239 @Override
240 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
241 DeviceId dst, LinkWeigher weigher) {
242 return defaultTopology(topology).getDisjointPaths(src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700243 }
244
245 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700246 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
247 Map<Link, Object> riskProfile) {
248 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700249 }
250
251 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700252 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
253 LinkWeight weight, Map<Link, Object> riskProfile) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300254 return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile);
255 }
256
257 @Override
258 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
259 DeviceId dst, LinkWeigher weigher,
260 Map<Link, Object> riskProfile) {
261 return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700262 }
263
264 @Override
alshabib339a3d92014-09-26 17:54:32 -0700265 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
266 return defaultTopology(topology).isInfrastructure(connectPoint);
267 }
268
269 @Override
270 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
271 return defaultTopology(topology).isBroadcastPoint(connectPoint);
272 }
273
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700274 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
275 // Any non-infrastructure, i.e. edge points are assumed to be OK.
276 if (!current.isInfrastructure(connectPoint)) {
277 return true;
278 }
279
280 // Find the cluster to which the device belongs.
281 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
282 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
283
284 // If the broadcast set is null or empty, or if the point explicitly
285 // belongs to it, return true;
286 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
287 return isNullOrEmpty(points) || points.contains(connectPoint);
288 }
289
alshabib339a3d92014-09-26 17:54:32 -0700290 @Override
291 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700292 GraphDescription graphDescription,
293 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700294 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700295 DefaultTopology newTopology =
296 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
297 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700298
299 // Promote the new topology to current and return a ready-to-send event.
300 synchronized (this) {
You Wang66b77fa2017-02-06 16:51:10 -0800301 // Make sure that what we're given is indeed newer than what we
302 // already have.
303 if (current != null && newTopology.time() < current.time()) {
304 return null;
305 }
alshabib339a3d92014-09-26 17:54:32 -0700306 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700307 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700308 }
309 }
310
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700311 private void updateBroadcastPoints(DefaultTopology topology) {
312 // Remove any broadcast trees rooted by devices for which we are master.
313 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
314 .filter(mastershipService::isLocalMaster)
315 .collect(Collectors.toSet());
316
317 // Update the broadcast trees rooted by devices for which we are master.
318 topology.getClusters().forEach(c -> {
319 toRemove.remove(c.root().deviceId());
320 if (mastershipService.isLocalMaster(c.root().deviceId())) {
321 broadcastPoints.put(c.root().deviceId(),
322 topology.broadcastPoints(c.id()));
323 }
324 });
325
326 toRemove.forEach(broadcastPoints::remove);
327 }
328
alshabib339a3d92014-09-26 17:54:32 -0700329 // Validates the specified topology and returns it as a default
330 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700331 checkArgument(topology instanceof DefaultTopology,
332 "Topology class %s not supported", topology.getClass());
333 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700334 }
335
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800336 @Override
337 public void setDefaultLinkWeight(LinkWeight linkWeight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300338 DefaultTopology.setDefaultLinkWeigher(adapt(linkWeight));
339 }
340
341 @Override
342 public void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
343 DefaultTopology.setDefaultLinkWeigher(linkWeigher);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800344 }
345
346 @Override
347 public void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) {
348 DefaultTopology.setDefaultGraphPathSearch(graphPathSearch);
349 }
350
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700351 private class InternalBroadcastPointListener
352 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
353 @Override
354 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
355 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
356 if (!event.value().isEmpty()) {
Madan Jampanib6e884b2016-06-23 01:34:15 -0700357 log.debug("Cluster rooted at {} has {} broadcast-points; #{}",
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700358 event.key(), event.value().size(), event.value().hashCode());
359 }
360 }
361 }
362 }
alshabib339a3d92014-09-26 17:54:32 -0700363}