blob: 585ed14d962c62ee631c4e186a024caa33345d59 [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;
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -070072import java.util.stream.Stream;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080073
74import static com.google.common.base.Preconditions.checkArgument;
75import static org.onlab.util.Tools.get;
76import static org.onlab.util.Tools.isNullOrEmpty;
Andrey Komarov2398d962016-09-26 15:11:23 +030077import static org.onosproject.net.topology.AdapterLinkWeigher.adapt;
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080078import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
79import static org.slf4j.LoggerFactory.getLogger;
80
alshabib339a3d92014-09-26 17:54:32 -070081/**
82 * Manages inventory of topology snapshots using trivial in-memory
83 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070084 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080085 * Note: This component is not distributed per-se. It runs on every
86 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070087 */
alshabib339a3d92014-09-26 17:54:32 -070088@Component(immediate = true)
89@Service
90public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070091 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080092 implements TopologyStore, PathAdminService {
alshabib339a3d92014-09-26 17:54:32 -070093
94 private final Logger log = getLogger(getClass());
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080095
96 private static final String FORMAT = "Settings: linkWeightFunction={}";
97
Jonathan Hart29858af2014-10-29 15:33:18 -070098 private volatile DefaultTopology current =
99 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700100 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700101 Collections.emptyList(),
102 Collections.emptyList()));
alshabib339a3d92014-09-26 17:54:32 -0700103
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected StorageService storageService;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected LogicalClockService clockService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected MastershipService mastershipService;
112
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected ComponentConfigService configService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected DeviceService deviceService;
118
119 private static final String HOP_COUNT = "hopCount";
120 private static final String LINK_METRIC = "linkMetric";
121 private static final String GEO_DISTANCE = "geoDistance";
122
123 private static final String DEFAULT_LINK_WEIGHT_FUNCTION = "hopCount";
124 @Property(name = "linkWeightFunction", value = DEFAULT_LINK_WEIGHT_FUNCTION,
125 label = "Default link-weight function: hopCount, linkMetric, geoDistance")
126 private String linkWeightFunction = DEFAULT_LINK_WEIGHT_FUNCTION;
127
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700128 // Cluster root to broadcast points bindings to allow convergence to
129 // a shared broadcast tree; node that is the master of the cluster root
130 // is the primary.
131 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
132
133 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
134 new InternalBroadcastPointListener();
135
alshabib339a3d92014-09-26 17:54:32 -0700136 @Activate
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800137 protected void activate() {
138 configService.registerProperties(getClass());
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700139 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
140 .register(KryoNamespaces.API);
141
142 broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder()
143 .withName("onos-broadcast-trees")
144 .withSerializer(hostSerializer)
145 .withTimestampProvider((k, v) -> clockService.getTimestamp())
146 .build();
147 broadcastPoints.addListener(listener);
alshabib339a3d92014-09-26 17:54:32 -0700148 log.info("Started");
149 }
150
151 @Deactivate
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800152 protected void deactivate() {
153 configService.unregisterProperties(getClass(), false);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700154 broadcastPoints.removeListener(listener);
155 broadcastPoints.destroy();
alshabib339a3d92014-09-26 17:54:32 -0700156 log.info("Stopped");
157 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700158
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800159 @Modified
160 protected void modified(ComponentContext context) {
161 Dictionary<?, ?> properties = context.getProperties();
162
163 String newLinkWeightFunction = get(properties, "linkWeightFunction");
164 if (newLinkWeightFunction != null &&
165 !Objects.equals(newLinkWeightFunction, linkWeightFunction)) {
166 linkWeightFunction = newLinkWeightFunction;
167 LinkWeight weight = linkWeightFunction.equals(LINK_METRIC) ?
168 new MetricLinkWeight() :
169 linkWeightFunction.equals(GEO_DISTANCE) ?
170 new GeoDistanceLinkWeight(deviceService) : null;
171 setDefaultLinkWeight(weight);
172 }
173 log.info(FORMAT, linkWeightFunction);
174 }
175
alshabib339a3d92014-09-26 17:54:32 -0700176 @Override
177 public Topology currentTopology() {
178 return current;
179 }
180
181 @Override
182 public boolean isLatest(Topology topology) {
183 // Topology is current only if it is the same as our current topology
184 return topology == current;
185 }
186
187 @Override
188 public TopologyGraph getGraph(Topology topology) {
189 return defaultTopology(topology).getGraph();
190 }
191
192 @Override
193 public Set<TopologyCluster> getClusters(Topology topology) {
194 return defaultTopology(topology).getClusters();
195 }
196
197 @Override
198 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
199 return defaultTopology(topology).getCluster(clusterId);
200 }
201
202 @Override
203 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
204 return defaultTopology(topology).getClusterDevices(cluster);
205 }
206
207 @Override
208 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
209 return defaultTopology(topology).getClusterLinks(cluster);
210 }
211
212 @Override
213 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
214 return defaultTopology(topology).getPaths(src, dst);
215 }
216
217 @Override
218 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700219 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300220 return getPaths(topology, src, dst, adapt(weight));
221 }
222
223 @Override
224 public Set<Path> getPaths(Topology topology, DeviceId src,
225 DeviceId dst, LinkWeigher weigher) {
226 return defaultTopology(topology).getPaths(src, dst, weigher);
alshabib339a3d92014-09-26 17:54:32 -0700227 }
228
229 @Override
Yuta HIGUCHIac8b2292017-03-30 19:21:57 -0700230 public Set<Path> getKShortestPaths(Topology topology,
231 DeviceId src, DeviceId dst,
232 LinkWeigher weigher,
233 int maxPaths) {
234 return defaultTopology(topology).getKShortestPaths(src, dst, weigher, maxPaths);
235 }
236
237 @Override
238 public Stream<Path> getKShortestPaths(Topology topology,
239 DeviceId src,
240 DeviceId dst,
241 LinkWeigher weigher) {
242 return defaultTopology(topology).getKShortestPaths(src, dst, weigher);
243 }
244
245 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700246 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
247 return defaultTopology(topology).getDisjointPaths(src, dst);
248 }
249
250 @Override
251 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
252 LinkWeight weight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300253 return getDisjointPaths(topology, src, dst, adapt(weight));
254 }
255
256 @Override
257 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
258 DeviceId dst, LinkWeigher weigher) {
259 return defaultTopology(topology).getDisjointPaths(src, dst, weigher);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700260 }
261
262 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700263 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
264 Map<Link, Object> riskProfile) {
265 return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700266 }
267
268 @Override
Thomas Vachuska48e64e42015-09-22 15:32:55 -0700269 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
270 LinkWeight weight, Map<Link, Object> riskProfile) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300271 return getDisjointPaths(topology, src, dst, adapt(weight), riskProfile);
272 }
273
274 @Override
275 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src,
276 DeviceId dst, LinkWeigher weigher,
277 Map<Link, Object> riskProfile) {
278 return defaultTopology(topology).getDisjointPaths(src, dst, weigher, riskProfile);
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700279 }
280
281 @Override
alshabib339a3d92014-09-26 17:54:32 -0700282 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
283 return defaultTopology(topology).isInfrastructure(connectPoint);
284 }
285
286 @Override
287 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
288 return defaultTopology(topology).isBroadcastPoint(connectPoint);
289 }
290
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700291 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
292 // Any non-infrastructure, i.e. edge points are assumed to be OK.
293 if (!current.isInfrastructure(connectPoint)) {
294 return true;
295 }
296
297 // Find the cluster to which the device belongs.
298 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
299 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
300
301 // If the broadcast set is null or empty, or if the point explicitly
302 // belongs to it, return true;
303 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
304 return isNullOrEmpty(points) || points.contains(connectPoint);
305 }
306
alshabib339a3d92014-09-26 17:54:32 -0700307 @Override
308 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700309 GraphDescription graphDescription,
310 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700311 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700312 DefaultTopology newTopology =
313 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
314 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700315
316 // Promote the new topology to current and return a ready-to-send event.
317 synchronized (this) {
You Wang66b77fa2017-02-06 16:51:10 -0800318 // Make sure that what we're given is indeed newer than what we
319 // already have.
320 if (current != null && newTopology.time() < current.time()) {
321 return null;
322 }
alshabib339a3d92014-09-26 17:54:32 -0700323 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700324 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700325 }
326 }
327
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700328 private void updateBroadcastPoints(DefaultTopology topology) {
329 // Remove any broadcast trees rooted by devices for which we are master.
330 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
331 .filter(mastershipService::isLocalMaster)
332 .collect(Collectors.toSet());
333
334 // Update the broadcast trees rooted by devices for which we are master.
335 topology.getClusters().forEach(c -> {
336 toRemove.remove(c.root().deviceId());
337 if (mastershipService.isLocalMaster(c.root().deviceId())) {
338 broadcastPoints.put(c.root().deviceId(),
339 topology.broadcastPoints(c.id()));
340 }
341 });
342
343 toRemove.forEach(broadcastPoints::remove);
344 }
345
alshabib339a3d92014-09-26 17:54:32 -0700346 // Validates the specified topology and returns it as a default
347 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700348 checkArgument(topology instanceof DefaultTopology,
349 "Topology class %s not supported", topology.getClass());
350 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700351 }
352
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800353 @Override
354 public void setDefaultLinkWeight(LinkWeight linkWeight) {
Andrey Komarov2398d962016-09-26 15:11:23 +0300355 DefaultTopology.setDefaultLinkWeigher(adapt(linkWeight));
356 }
357
358 @Override
359 public void setDefaultLinkWeigher(LinkWeigher linkWeigher) {
360 DefaultTopology.setDefaultLinkWeigher(linkWeigher);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -0800361 }
362
363 @Override
364 public void setDefaultGraphPathSearch(GraphPathSearch<TopologyVertex, TopologyEdge> graphPathSearch) {
365 DefaultTopology.setDefaultGraphPathSearch(graphPathSearch);
366 }
367
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700368 private class InternalBroadcastPointListener
369 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
370 @Override
371 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
372 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
373 if (!event.value().isEmpty()) {
Madan Jampanib6e884b2016-06-23 01:34:15 -0700374 log.debug("Cluster rooted at {} has {} broadcast-points; #{}",
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700375 event.key(), event.value().size(), event.value().hashCode());
376 }
377 }
378 }
379 }
alshabib339a3d92014-09-26 17:54:32 -0700380}