blob: 487fad9b26da3d59e3d0eb6782c8284d46a3df34 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.topology.impl;
alshabib339a3d92014-09-26 17:54:32 -070017
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070018import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070019import static org.onlab.util.Tools.isNullOrEmpty;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070020import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
alshabib339a3d92014-09-26 17:54:32 -070021import static org.slf4j.LoggerFactory.getLogger;
22
Jonathan Hart29858af2014-10-29 15:33:18 -070023import java.util.Collections;
alshabib339a3d92014-09-26 17:54:32 -070024import java.util.List;
25import java.util.Set;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070026import java.util.stream.Collectors;
alshabib339a3d92014-09-26 17:54:32 -070027
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070031import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib339a3d92014-09-26 17:54:32 -070033import org.apache.felix.scr.annotations.Service;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070034import org.onlab.util.KryoNamespace;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070035import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.event.Event;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070037import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.Link;
42import org.onosproject.net.Path;
43import org.onosproject.net.provider.ProviderId;
44import org.onosproject.net.topology.ClusterId;
45import org.onosproject.net.topology.DefaultGraphDescription;
46import org.onosproject.net.topology.GraphDescription;
47import org.onosproject.net.topology.LinkWeight;
48import org.onosproject.net.topology.Topology;
49import org.onosproject.net.topology.TopologyCluster;
50import org.onosproject.net.topology.TopologyEvent;
51import org.onosproject.net.topology.TopologyGraph;
52import org.onosproject.net.topology.TopologyStore;
53import org.onosproject.net.topology.TopologyStoreDelegate;
54import 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;
alshabib339a3d92014-09-26 17:54:32 -070061import org.slf4j.Logger;
62
63/**
64 * Manages inventory of topology snapshots using trivial in-memory
65 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070066 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080067 * Note: This component is not distributed per-se. It runs on every
68 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070069 */
alshabib339a3d92014-09-26 17:54:32 -070070@Component(immediate = true)
71@Service
72public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070073 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
74 implements TopologyStore {
alshabib339a3d92014-09-26 17:54:32 -070075
76 private final Logger log = getLogger(getClass());
77
Jonathan Hart29858af2014-10-29 15:33:18 -070078 private volatile DefaultTopology current =
79 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -070080 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070081 Collections.<Device>emptyList(),
82 Collections.<Link>emptyList()));
alshabib339a3d92014-09-26 17:54:32 -070083
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected StorageService storageService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected LogicalClockService clockService;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected MastershipService mastershipService;
92
93 // Cluster root to broadcast points bindings to allow convergence to
94 // a shared broadcast tree; node that is the master of the cluster root
95 // is the primary.
96 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
97
98 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
99 new InternalBroadcastPointListener();
100
alshabib339a3d92014-09-26 17:54:32 -0700101 @Activate
102 public void activate() {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700103 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
104 .register(KryoNamespaces.API);
105
106 broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder()
107 .withName("onos-broadcast-trees")
108 .withSerializer(hostSerializer)
109 .withTimestampProvider((k, v) -> clockService.getTimestamp())
110 .build();
111 broadcastPoints.addListener(listener);
alshabib339a3d92014-09-26 17:54:32 -0700112 log.info("Started");
113 }
114
115 @Deactivate
116 public void deactivate() {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700117 broadcastPoints.removeListener(listener);
118 broadcastPoints.destroy();
alshabib339a3d92014-09-26 17:54:32 -0700119 log.info("Stopped");
120 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700121
alshabib339a3d92014-09-26 17:54:32 -0700122 @Override
123 public Topology currentTopology() {
124 return current;
125 }
126
127 @Override
128 public boolean isLatest(Topology topology) {
129 // Topology is current only if it is the same as our current topology
130 return topology == current;
131 }
132
133 @Override
134 public TopologyGraph getGraph(Topology topology) {
135 return defaultTopology(topology).getGraph();
136 }
137
138 @Override
139 public Set<TopologyCluster> getClusters(Topology topology) {
140 return defaultTopology(topology).getClusters();
141 }
142
143 @Override
144 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
145 return defaultTopology(topology).getCluster(clusterId);
146 }
147
148 @Override
149 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
150 return defaultTopology(topology).getClusterDevices(cluster);
151 }
152
153 @Override
154 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
155 return defaultTopology(topology).getClusterLinks(cluster);
156 }
157
158 @Override
159 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
160 return defaultTopology(topology).getPaths(src, dst);
161 }
162
163 @Override
164 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700165 LinkWeight weight) {
alshabib339a3d92014-09-26 17:54:32 -0700166 return defaultTopology(topology).getPaths(src, dst, weight);
167 }
168
169 @Override
170 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
171 return defaultTopology(topology).isInfrastructure(connectPoint);
172 }
173
174 @Override
175 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
176 return defaultTopology(topology).isBroadcastPoint(connectPoint);
177 }
178
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700179 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
180 // Any non-infrastructure, i.e. edge points are assumed to be OK.
181 if (!current.isInfrastructure(connectPoint)) {
182 return true;
183 }
184
185 // Find the cluster to which the device belongs.
186 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
187 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
188
189 // If the broadcast set is null or empty, or if the point explicitly
190 // belongs to it, return true;
191 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
192 return isNullOrEmpty(points) || points.contains(connectPoint);
193 }
194
alshabib339a3d92014-09-26 17:54:32 -0700195 @Override
196 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700197 GraphDescription graphDescription,
198 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700199 // First off, make sure that what we're given is indeed newer than
200 // what we already have.
201 if (current != null && graphDescription.timestamp() < current.time()) {
202 return null;
203 }
204
205 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700206 DefaultTopology newTopology =
207 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
208 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700209
210 // Promote the new topology to current and return a ready-to-send event.
211 synchronized (this) {
212 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700213 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700214 }
215 }
216
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700217 private void updateBroadcastPoints(DefaultTopology topology) {
218 // Remove any broadcast trees rooted by devices for which we are master.
219 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
220 .filter(mastershipService::isLocalMaster)
221 .collect(Collectors.toSet());
222
223 // Update the broadcast trees rooted by devices for which we are master.
224 topology.getClusters().forEach(c -> {
225 toRemove.remove(c.root().deviceId());
226 if (mastershipService.isLocalMaster(c.root().deviceId())) {
227 broadcastPoints.put(c.root().deviceId(),
228 topology.broadcastPoints(c.id()));
229 }
230 });
231
232 toRemove.forEach(broadcastPoints::remove);
233 }
234
alshabib339a3d92014-09-26 17:54:32 -0700235 // Validates the specified topology and returns it as a default
236 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700237 checkArgument(topology instanceof DefaultTopology,
238 "Topology class %s not supported", topology.getClass());
239 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700240 }
241
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700242 private class InternalBroadcastPointListener
243 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
244 @Override
245 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
246 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
247 if (!event.value().isEmpty()) {
248 log.info("Cluster rooted at {} has {} broadcast-points; #{}",
249 event.key(), event.value().size(), event.value().hashCode());
250 }
251 }
252 }
253 }
alshabib339a3d92014-09-26 17:54:32 -0700254}