blob: 4eeed7cc5cde108b4f8ea63209b408275ef7520d [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;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070024import java.util.Map;
alshabib339a3d92014-09-26 17:54:32 -070025import java.util.List;
26import java.util.Set;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070027import java.util.stream.Collectors;
alshabib339a3d92014-09-26 17:54:32 -070028
29import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib339a3d92014-09-26 17:54:32 -070034import org.apache.felix.scr.annotations.Service;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070035import org.onlab.util.KryoNamespace;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070036import org.onosproject.common.DefaultTopology;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.event.Event;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070038import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.Link;
43import org.onosproject.net.Path;
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -070044import org.onosproject.net.DisjointPath;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.provider.ProviderId;
46import org.onosproject.net.topology.ClusterId;
47import org.onosproject.net.topology.DefaultGraphDescription;
48import org.onosproject.net.topology.GraphDescription;
49import org.onosproject.net.topology.LinkWeight;
50import org.onosproject.net.topology.Topology;
51import org.onosproject.net.topology.TopologyCluster;
52import org.onosproject.net.topology.TopologyEvent;
53import org.onosproject.net.topology.TopologyGraph;
54import org.onosproject.net.topology.TopologyStore;
55import org.onosproject.net.topology.TopologyStoreDelegate;
56import org.onosproject.store.AbstractStore;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070057import org.onosproject.store.serializers.KryoNamespaces;
58import org.onosproject.store.service.EventuallyConsistentMap;
59import org.onosproject.store.service.EventuallyConsistentMapEvent;
60import org.onosproject.store.service.EventuallyConsistentMapListener;
61import org.onosproject.store.service.LogicalClockService;
62import org.onosproject.store.service.StorageService;
alshabib339a3d92014-09-26 17:54:32 -070063import org.slf4j.Logger;
64
65/**
66 * Manages inventory of topology snapshots using trivial in-memory
67 * structures implementation.
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070068 * <p>
Brian O'Connor44008532014-12-04 16:41:36 -080069 * Note: This component is not distributed per-se. It runs on every
70 * instance and feeds off of other distributed stores.
alshabib339a3d92014-09-26 17:54:32 -070071 */
alshabib339a3d92014-09-26 17:54:32 -070072@Component(immediate = true)
73@Service
74public class DistributedTopologyStore
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070075 extends AbstractStore<TopologyEvent, TopologyStoreDelegate>
76 implements TopologyStore {
alshabib339a3d92014-09-26 17:54:32 -070077
78 private final Logger log = getLogger(getClass());
Jonathan Hart29858af2014-10-29 15:33:18 -070079 private volatile DefaultTopology current =
80 new DefaultTopology(ProviderId.NONE,
Thomas Vachuska320c58f2015-08-05 10:42:32 -070081 new DefaultGraphDescription(0L, System.currentTimeMillis(),
Thomas Vachuska930a8ee2015-08-04 18:49:36 -070082 Collections.<Device>emptyList(),
83 Collections.<Link>emptyList()));
alshabib339a3d92014-09-26 17:54:32 -070084
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected StorageService storageService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected LogicalClockService clockService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected MastershipService mastershipService;
93
94 // Cluster root to broadcast points bindings to allow convergence to
95 // a shared broadcast tree; node that is the master of the cluster root
96 // is the primary.
97 private EventuallyConsistentMap<DeviceId, Set<ConnectPoint>> broadcastPoints;
98
99 private EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> listener =
100 new InternalBroadcastPointListener();
101
alshabib339a3d92014-09-26 17:54:32 -0700102 @Activate
103 public void activate() {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700104 KryoNamespace.Builder hostSerializer = KryoNamespace.newBuilder()
105 .register(KryoNamespaces.API);
106
107 broadcastPoints = storageService.<DeviceId, Set<ConnectPoint>>eventuallyConsistentMapBuilder()
108 .withName("onos-broadcast-trees")
109 .withSerializer(hostSerializer)
110 .withTimestampProvider((k, v) -> clockService.getTimestamp())
111 .build();
112 broadcastPoints.addListener(listener);
alshabib339a3d92014-09-26 17:54:32 -0700113 log.info("Started");
114 }
115
116 @Deactivate
117 public void deactivate() {
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700118 broadcastPoints.removeListener(listener);
119 broadcastPoints.destroy();
alshabib339a3d92014-09-26 17:54:32 -0700120 log.info("Stopped");
121 }
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700122
alshabib339a3d92014-09-26 17:54:32 -0700123 @Override
124 public Topology currentTopology() {
125 return current;
126 }
127
128 @Override
129 public boolean isLatest(Topology topology) {
130 // Topology is current only if it is the same as our current topology
131 return topology == current;
132 }
133
134 @Override
135 public TopologyGraph getGraph(Topology topology) {
136 return defaultTopology(topology).getGraph();
137 }
138
139 @Override
140 public Set<TopologyCluster> getClusters(Topology topology) {
141 return defaultTopology(topology).getClusters();
142 }
143
144 @Override
145 public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
146 return defaultTopology(topology).getCluster(clusterId);
147 }
148
149 @Override
150 public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
151 return defaultTopology(topology).getClusterDevices(cluster);
152 }
153
154 @Override
155 public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
156 return defaultTopology(topology).getClusterLinks(cluster);
157 }
158
159 @Override
160 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
161 return defaultTopology(topology).getPaths(src, dst);
162 }
163
164 @Override
165 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700166 LinkWeight weight) {
alshabib339a3d92014-09-26 17:54:32 -0700167 return defaultTopology(topology).getPaths(src, dst, weight);
168 }
169
170 @Override
Nikhil Cheerla2ec191f2015-07-09 12:34:54 -0700171 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
172 return defaultTopology(topology).getDisjointPaths(src, dst);
173 }
174
175 @Override
176 public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
177 LinkWeight weight) {
178 return defaultTopology(topology).getDisjointPaths(src, dst, weight);
179 }
180
181 @Override
182 public Set<DisjointPath> getSRLGDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
183 Map<Link, Object> riskProfile) {
184 return defaultTopology(topology).getSRLGDisjointPaths(src, dst, riskProfile);
185 }
186
187 @Override
188 public Set<DisjointPath> getSRLGDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
189 LinkWeight weight, Map<Link, Object> riskProfile) {
190 return defaultTopology(topology).getSRLGDisjointPaths(src, dst, weight, riskProfile);
191 }
192
193 @Override
alshabib339a3d92014-09-26 17:54:32 -0700194 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
195 return defaultTopology(topology).isInfrastructure(connectPoint);
196 }
197
198 @Override
199 public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
200 return defaultTopology(topology).isBroadcastPoint(connectPoint);
201 }
202
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700203 private boolean isBroadcastPoint(ConnectPoint connectPoint) {
204 // Any non-infrastructure, i.e. edge points are assumed to be OK.
205 if (!current.isInfrastructure(connectPoint)) {
206 return true;
207 }
208
209 // Find the cluster to which the device belongs.
210 TopologyCluster cluster = current.getCluster(connectPoint.deviceId());
211 checkArgument(cluster != null, "No cluster found for device %s", connectPoint.deviceId());
212
213 // If the broadcast set is null or empty, or if the point explicitly
214 // belongs to it, return true;
215 Set<ConnectPoint> points = broadcastPoints.get(cluster.root().deviceId());
216 return isNullOrEmpty(points) || points.contains(connectPoint);
217 }
218
alshabib339a3d92014-09-26 17:54:32 -0700219 @Override
220 public TopologyEvent updateTopology(ProviderId providerId,
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700221 GraphDescription graphDescription,
222 List<Event> reasons) {
alshabib339a3d92014-09-26 17:54:32 -0700223 // First off, make sure that what we're given is indeed newer than
224 // what we already have.
225 if (current != null && graphDescription.timestamp() < current.time()) {
226 return null;
227 }
228
229 // Have the default topology construct self from the description data.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700230 DefaultTopology newTopology =
231 new DefaultTopology(providerId, graphDescription, this::isBroadcastPoint);
232 updateBroadcastPoints(newTopology);
alshabib339a3d92014-09-26 17:54:32 -0700233
234 // Promote the new topology to current and return a ready-to-send event.
235 synchronized (this) {
236 current = newTopology;
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700237 return new TopologyEvent(TOPOLOGY_CHANGED, current, reasons);
alshabib339a3d92014-09-26 17:54:32 -0700238 }
239 }
240
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700241 private void updateBroadcastPoints(DefaultTopology topology) {
242 // Remove any broadcast trees rooted by devices for which we are master.
243 Set<DeviceId> toRemove = broadcastPoints.keySet().stream()
244 .filter(mastershipService::isLocalMaster)
245 .collect(Collectors.toSet());
246
247 // Update the broadcast trees rooted by devices for which we are master.
248 topology.getClusters().forEach(c -> {
249 toRemove.remove(c.root().deviceId());
250 if (mastershipService.isLocalMaster(c.root().deviceId())) {
251 broadcastPoints.put(c.root().deviceId(),
252 topology.broadcastPoints(c.id()));
253 }
254 });
255
256 toRemove.forEach(broadcastPoints::remove);
257 }
258
alshabib339a3d92014-09-26 17:54:32 -0700259 // Validates the specified topology and returns it as a default
260 private DefaultTopology defaultTopology(Topology topology) {
Thomas Vachuska930a8ee2015-08-04 18:49:36 -0700261 checkArgument(topology instanceof DefaultTopology,
262 "Topology class %s not supported", topology.getClass());
263 return (DefaultTopology) topology;
alshabib339a3d92014-09-26 17:54:32 -0700264 }
265
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700266 private class InternalBroadcastPointListener
267 implements EventuallyConsistentMapListener<DeviceId, Set<ConnectPoint>> {
268 @Override
269 public void event(EventuallyConsistentMapEvent<DeviceId, Set<ConnectPoint>> event) {
270 if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
271 if (!event.value().isEmpty()) {
272 log.info("Cluster rooted at {} has {} broadcast-points; #{}",
273 event.key(), event.value().size(), event.value().hashCode());
274 }
275 }
276 }
277 }
alshabib339a3d92014-09-26 17:54:32 -0700278}