blob: 7d995863879564253fb5f25c61563ab6e5fb8ef6 [file] [log] [blame]
Thomas Vachuskabf916ea2015-05-20 18:24:34 -07001/*
samuel7a5691a2015-05-23 00:36:32 +08002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuskabf916ea2015-05-20 18:24:34 -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 */
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070016package org.onosproject.incubator.store.tunnel.impl;
jccd8697232015-05-05 14:42:23 +080017
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.Collections;
23import java.util.HashSet;
24import java.util.List;
25import java.util.Objects;
26import java.util.Set;
27
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.apache.felix.scr.annotations.Service;
34import org.onlab.util.KryoNamespace;
35import org.onosproject.cluster.ClusterService;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
38import org.onosproject.core.IdGenerator;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070039import org.onosproject.incubator.net.tunnel.DefaultTunnel;
40import org.onosproject.incubator.net.tunnel.Tunnel;
41import org.onosproject.incubator.net.tunnel.Tunnel.Type;
42import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
43import org.onosproject.incubator.net.tunnel.TunnelEvent;
44import org.onosproject.incubator.net.tunnel.TunnelId;
45import org.onosproject.incubator.net.tunnel.TunnelName;
46import org.onosproject.incubator.net.tunnel.TunnelStore;
47import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
48import org.onosproject.incubator.net.tunnel.TunnelSubscription;
samuel7a5691a2015-05-23 00:36:32 +080049import org.onosproject.net.Annotations;
50import org.onosproject.net.DefaultAnnotations;
51import org.onosproject.net.SparseAnnotations;
52import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080053import org.onosproject.store.AbstractStore;
54import org.onosproject.store.app.GossipApplicationStore.InternalState;
55import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
56import org.onosproject.store.serializers.KryoNamespaces;
57import org.onosproject.store.service.EventuallyConsistentMap;
58import org.onosproject.store.service.MultiValuedTimestamp;
59import org.onosproject.store.service.StorageService;
60import org.onosproject.store.service.WallclockClockManager;
61import org.slf4j.Logger;
62
63import com.google.common.base.MoreObjects;
64import com.google.common.collect.ImmutableSet;
65
66/**
67 * Manages inventory of tunnel in distributed data store that uses optimistic
68 * replication and gossip based techniques.
69 */
70@Component(immediate = true)
71@Service
72public class DistributedTunnelStore
73 extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
74 implements TunnelStore {
75
76 private final Logger log = getLogger(getClass());
77
78 /**
79 * The topic used for obtaining globally unique ids.
80 */
81 private String runnelOpTopoic = "tunnel-ops-ids";
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected ClusterCommunicationService clusterCommunicator;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected ClusterService clusterService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected StorageService storageService;
94
95 // tunnel identity as map key in the store.
96 private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore;
97 // tunnel name as map key in the store.
98 private EventuallyConsistentMap<TunnelName, Set<TunnelId>> tunnelNameAsKeyStore;
99 // maintains all the tunnels between source and destination.
100 private EventuallyConsistentMap<TunnelKey, Set<TunnelId>> srcAndDstKeyStore;
101 // maintains all the tunnels by tunnel type.
102 private EventuallyConsistentMap<Tunnel.Type, Set<TunnelId>> typeKeyStore;
103 // maintains records that app subscribes tunnel.
104 private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship;
105
106 private IdGenerator idGenerator;
107
108 @Activate
109 public void activate() {
110 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
111 .register(KryoNamespaces.API)
112 .register(MultiValuedTimestamp.class)
113 .register(InternalState.class);
114 tunnelIdAsKeyStore = storageService
115 .<TunnelId, Tunnel>eventuallyConsistentMapBuilder()
116 .withName("all_tunnel").withSerializer(serializer)
117 .withClockService(new WallclockClockManager<>()).build();
118 tunnelNameAsKeyStore = storageService
119 .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder()
120 .withName("tunnel_name_tunnel").withSerializer(serializer)
121 .withClockService(new WallclockClockManager<>()).build();
122 srcAndDstKeyStore = storageService
123 .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder()
124 .withName("src_dst_tunnel").withSerializer(serializer)
125 .withClockService(new WallclockClockManager<>()).build();
126 typeKeyStore = storageService
127 .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder()
128 .withName("type_tunnel").withSerializer(serializer)
129 .withClockService(new WallclockClockManager<>()).build();
samuel7a5691a2015-05-23 00:36:32 +0800130 orderRelationship = storageService
131 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
132 .withName("type_tunnel").withSerializer(serializer)
133 .withClockService(new WallclockClockManager<>()).build();
jccd8697232015-05-05 14:42:23 +0800134 idGenerator = coreService.getIdGenerator(runnelOpTopoic);
135 log.info("Started");
136 }
137
138 @Deactivate
139 public void deactivate() {
140 tunnelIdAsKeyStore.destroy();
141 srcAndDstKeyStore.destroy();
142 typeKeyStore.destroy();
143 tunnelNameAsKeyStore.destroy();
144 log.info("Stopped");
145 }
146
147 @Override
148 public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
149 // tunnelIdAsKeyStore.
150 if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId())) {
151 Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
152 if (old == null) {
153 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
154 return tunnel.tunnelId();
155 }
samuel7a5691a2015-05-23 00:36:32 +0800156 DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
157 SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
158 Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
159 old.dst(), old.type(),
160 old.state(), old.groupId(),
jccd8697232015-05-05 14:42:23 +0800161 old.tunnelId(),
samuel7a5691a2015-05-23 00:36:32 +0800162 old.tunnelName(),
163 old.path(),
164 DefaultAnnotations.merge(oldAnno, newAnno));
jccd8697232015-05-05 14:42:23 +0800165 tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT);
166 TunnelEvent event = new TunnelEvent(
167 TunnelEvent.Type.TUNNEL_UPDATED,
168 tunnel);
169 notifyDelegate(event);
170 return tunnel.tunnelId();
171 } else {
172 TunnelId tunnelId = TunnelId.valueOf(idGenerator.getNewId());
173 Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
174 tunnel.dst(), tunnel.type(),
175 tunnel.state(), tunnel.groupId(),
176 tunnelId,
177 tunnel.tunnelName(),
samuel7a5691a2015-05-23 00:36:32 +0800178 tunnel.path(),
jccd8697232015-05-05 14:42:23 +0800179 tunnel.annotations());
180 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
181 tunnelIdAsKeyStore.put(tunnelId, newT);
182 Set<TunnelId> tunnelnameSet = tunnelNameAsKeyStore.get(tunnel
183 .tunnelName());
184 if (tunnelnameSet == null) {
185 tunnelnameSet = new HashSet<TunnelId>();
186 }
187 tunnelnameSet.add(tunnelId);
188 tunnelNameAsKeyStore.put(tunnel
189 .tunnelName(), tunnelnameSet);
190 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyStore.get(key);
191 if (srcAndDstKeySet == null) {
192 srcAndDstKeySet = new HashSet<TunnelId>();
193 }
194 srcAndDstKeySet.add(tunnelId);
195 srcAndDstKeyStore.put(key, srcAndDstKeySet);
196 Set<TunnelId> typeKeySet = typeKeyStore.get(tunnel.type());
197 if (typeKeySet == null) {
198 typeKeySet = new HashSet<TunnelId>();
199 }
200 typeKeySet.add(tunnelId);
201 typeKeyStore.put(tunnel.type(), typeKeySet);
202 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
203 tunnel);
204 notifyDelegate(event);
205 return tunnelId;
206 }
207 }
208
209 @Override
210 public void deleteTunnel(TunnelId tunnelId) {
211 Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
212 if (deletedTunnel == null) {
213 return;
214 }
215 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()).remove(tunnelId);
216 tunnelIdAsKeyStore.remove(tunnelId);
217 TunnelKey key = new TunnelKey(deletedTunnel.src(), deletedTunnel.dst());
218 srcAndDstKeyStore.get(key).remove(tunnelId);
219 typeKeyStore.get(deletedTunnel.type()).remove(tunnelId);
220 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
221 deletedTunnel);
222 notifyDelegate(event);
223 }
224
225 @Override
226 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
227 ProviderId producerName) {
228 TunnelKey key = TunnelKey.tunnelKey(src, dst);
229 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
230 if (idSet == null) {
231 return;
232 }
233 Tunnel deletedTunnel = null;
234 TunnelEvent event = null;
235 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
236 for (TunnelId id : idSet) {
237 deletedTunnel = tunnelIdAsKeyStore.get(id);
238 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
239 deletedTunnel);
240 ls.add(event);
241 if (producerName.equals(deletedTunnel.providerId())) {
242 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
243 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
244 .remove(deletedTunnel.tunnelId());
245 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
246 typeKeyStore.get(deletedTunnel.type())
247 .remove(deletedTunnel.tunnelId());
248 }
249 }
250 notifyDelegate(ls);
251 }
252
253 @Override
254 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
255 ProviderId producerName) {
256 TunnelKey key = TunnelKey.tunnelKey(src, dst);
257 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
258 if (idSet == null) {
259 return;
260 }
261 Tunnel deletedTunnel = null;
262 TunnelEvent event = null;
263 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
264 for (TunnelId id : idSet) {
265 deletedTunnel = tunnelIdAsKeyStore.get(id);
266 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
267 deletedTunnel);
268 ls.add(event);
269 if (producerName.equals(deletedTunnel.providerId())
270 && type.equals(deletedTunnel.type())) {
271 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
272 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
273 .remove(deletedTunnel.tunnelId());
274 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
275 typeKeyStore.get(deletedTunnel.type())
276 .remove(deletedTunnel.tunnelId());
277 }
278 }
279 notifyDelegate(ls);
280 }
281
282 @Override
283 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
284 Annotations... annotations) {
285 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
286 if (orderSet == null) {
287 orderSet = new HashSet<TunnelSubscription>();
288 }
289 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
290 annotations);
291 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
292 if (result != null || Tunnel.State.INACTIVE.equals(result.state())) {
293 return null;
294 }
295 orderSet.add(order);
296 orderRelationship.put(appId, orderSet);
297 return result;
298 }
299
300 @Override
301 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
302 TunnelEndPoint src,
303 TunnelEndPoint dst,
304 Annotations... annotations) {
305 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
306 if (orderSet == null) {
307 orderSet = new HashSet<TunnelSubscription>();
308 }
309 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
310 boolean isExist = orderSet.contains(order);
311 if (!isExist) {
312 orderSet.add(order);
313 }
314 orderRelationship.put(appId, orderSet);
315 TunnelKey key = TunnelKey.tunnelKey(src, dst);
316 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
317 if (idSet == null || idSet.size() == 0) {
318 return Collections.emptySet();
319 }
320 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
321 for (TunnelId tunnelId : idSet) {
322 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
323 if (Tunnel.State.ACTIVE.equals(result.state())) {
324 tunnelSet.add(result);
325 }
326 }
327 return tunnelSet;
328 }
329
330 @Override
331 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
332 TunnelEndPoint src,
333 TunnelEndPoint dst, Type type,
334 Annotations... annotations) {
335 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
336 if (orderSet == null) {
337 orderSet = new HashSet<TunnelSubscription>();
338 }
339 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
340 boolean isExist = orderSet.contains(order);
341 if (!isExist) {
342 orderSet.add(order);
343 }
344 orderRelationship.put(appId, orderSet);
345 TunnelKey key = TunnelKey.tunnelKey(src, dst);
346 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
347 if (idSet == null || idSet.size() == 0) {
348 return Collections.emptySet();
349 }
350 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
351 for (TunnelId tunnelId : idSet) {
352 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
353 if (type.equals(result.type())
354 && Tunnel.State.ACTIVE.equals(result.state())) {
355 tunnelSet.add(result);
356 }
357 }
358 return tunnelSet;
359 }
360
361 @Override
362 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
363 TunnelName tunnelName,
364 Annotations... annotations) {
365 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
366 if (orderSet == null) {
367 orderSet = new HashSet<TunnelSubscription>();
368 }
369 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
370 annotations);
371 boolean isExist = orderSet.contains(order);
372 if (!isExist) {
373 orderSet.add(order);
374 }
375 orderRelationship.put(appId, orderSet);
376 Set<TunnelId> idSet = tunnelNameAsKeyStore.get(tunnelName);
377 if (idSet == null || idSet.size() == 0) {
378 return Collections.emptySet();
379 }
380 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
381 for (TunnelId tunnelId : idSet) {
382 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
383 if (Tunnel.State.ACTIVE.equals(result.state())) {
384 tunnelSet.add(result);
385 }
386 }
387 return tunnelSet;
388 }
389
390 @Override
391 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
392 Annotations... annotations) {
393 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
394 annotations);
395 return deleteOrder(order);
396 }
397
398 @Override
399 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
400 Annotations... annotations) {
401 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
402 annotations);
403 return deleteOrder(order);
404 }
405
406 @Override
407 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
408 TunnelEndPoint dst, Type type,
409 Annotations... annotations) {
410 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
411 return deleteOrder(order);
412 }
413
414 @Override
415 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
416 TunnelEndPoint dst, Annotations... annotations) {
417 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
418 return deleteOrder(order);
419 }
420
421 private boolean deleteOrder(TunnelSubscription order) {
422 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
423 if (orderSet == null) {
424 return true;
425 }
426 if (orderSet.contains(order)) {
427 orderSet.remove(order);
428 return true;
429 }
430 return false;
431 }
432
433 @Override
434 public Tunnel queryTunnel(TunnelId tunnelId) {
435 return tunnelIdAsKeyStore.get(tunnelId);
436 }
437
438 @Override
439 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
440 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
441 .get(appId)) : Collections.emptySet();
442 }
443
444 @Override
445 public Collection<Tunnel> queryTunnel(Type type) {
446 Collection<Tunnel> result = new HashSet<Tunnel>();
447 Set<TunnelId> tunnelIds = typeKeyStore.get(type);
448 if (tunnelIds == null) {
449 return Collections.emptySet();
450 }
451 for (TunnelId id : tunnelIds) {
452 result.add(tunnelIdAsKeyStore.get(id));
453 }
454 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
455 .copyOf(result);
456 }
457
458 @Override
459 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
460 Collection<Tunnel> result = new HashSet<Tunnel>();
461 TunnelKey key = TunnelKey.tunnelKey(src, dst);
462 Set<TunnelId> tunnelIds = srcAndDstKeyStore.get(key);
463 if (tunnelIds == null) {
464 return Collections.emptySet();
465 }
466 for (TunnelId id : tunnelIds) {
467 result.add(tunnelIdAsKeyStore.get(id));
468 }
469 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
470 .copyOf(result);
471 }
472
473 @Override
samuel7a5691a2015-05-23 00:36:32 +0800474 public Collection<Tunnel> queryAllTunnels() {
475 return tunnelIdAsKeyStore.values();
476 }
477
478 @Override
jccd8697232015-05-05 14:42:23 +0800479 public int tunnelCount() {
480 return tunnelIdAsKeyStore.size();
481 }
482
483 /**
484 * Uses source TunnelPoint and destination TunnelPoint as map key.
485 */
486 private static final class TunnelKey {
487 private final TunnelEndPoint src;
488 private final TunnelEndPoint dst;
489
490 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
491 this.src = src;
492 this.dst = dst;
493
494 }
495
496 /**
497 * create a map key.
498 *
499 * @param src
500 * @param dst
501 * @return a key using source ip and destination ip
502 */
503 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
504 return new TunnelKey(src, dst);
505 }
506
507 @Override
508 public int hashCode() {
509 return Objects.hash(src, dst);
510 }
511
512 @Override
513 public boolean equals(Object obj) {
514 if (this == obj) {
515 return true;
516 }
517 if (obj instanceof TunnelKey) {
518 final TunnelKey other = (TunnelKey) obj;
519 return Objects.equals(this.src, other.src)
520 && Objects.equals(this.dst, other.dst);
521 }
522 return false;
523 }
524
525 @Override
526 public String toString() {
527 return MoreObjects.toStringHelper(getClass()).add("src", src)
528 .add("dst", dst).toString();
529 }
530 }
jccd8697232015-05-05 14:42:23 +0800531}