2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.incubator.store.tunnel.impl;
18 import static org.slf4j.LoggerFactory.getLogger;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Objects;
28 import org.apache.felix.scr.annotations.Activate;
29 import org.apache.felix.scr.annotations.Component;
30 import org.apache.felix.scr.annotations.Deactivate;
31 import org.apache.felix.scr.annotations.Reference;
32 import org.apache.felix.scr.annotations.ReferenceCardinality;
33 import org.apache.felix.scr.annotations.Service;
34 import org.onlab.util.KryoNamespace;
35 import org.onosproject.cluster.ClusterService;
36 import org.onosproject.core.ApplicationId;
37 import org.onosproject.core.CoreService;
38 import org.onosproject.core.IdGenerator;
39 import org.onosproject.incubator.net.tunnel.DefaultTunnel;
40 import org.onosproject.incubator.net.tunnel.Tunnel;
41 import org.onosproject.incubator.net.tunnel.Tunnel.Type;
42 import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
43 import org.onosproject.incubator.net.tunnel.TunnelEvent;
44 import org.onosproject.incubator.net.tunnel.TunnelId;
45 import org.onosproject.incubator.net.tunnel.TunnelName;
46 import org.onosproject.incubator.net.tunnel.TunnelStore;
47 import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
48 import org.onosproject.incubator.net.tunnel.TunnelSubscription;
49 import org.onosproject.net.Annotations;
50 import org.onosproject.net.DefaultAnnotations;
51 import org.onosproject.net.SparseAnnotations;
52 import org.onosproject.net.provider.ProviderId;
53 import org.onosproject.store.AbstractStore;
54 import org.onosproject.store.app.GossipApplicationStore.InternalState;
55 import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
56 import org.onosproject.store.serializers.KryoNamespaces;
57 import org.onosproject.store.service.EventuallyConsistentMap;
58 import org.onosproject.store.service.MultiValuedTimestamp;
59 import org.onosproject.store.service.StorageService;
60 import org.onosproject.store.service.WallClockTimestamp;
61 import org.slf4j.Logger;
63 import com.google.common.base.MoreObjects;
64 import com.google.common.collect.ImmutableSet;
67 * Manages inventory of tunnel in distributed data store that uses optimistic
68 * replication and gossip based techniques.
70 @Component(immediate = true)
72 public class DistributedTunnelStore
73 extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
74 implements TunnelStore {
76 private final Logger log = getLogger(getClass());
79 * The topic used for obtaining globally unique ids.
81 private String runnelOpTopoic = "tunnel-ops-ids";
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected ClusterCommunicationService clusterCommunicator;
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected ClusterService clusterService;
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected StorageService storageService;
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;
106 private IdGenerator idGenerator;
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 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
118 tunnelNameAsKeyStore = storageService
119 .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder()
120 .withName("tunnel_name_tunnel").withSerializer(serializer)
121 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
122 srcAndDstKeyStore = storageService
123 .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder()
124 .withName("src_dst_tunnel").withSerializer(serializer)
125 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
126 typeKeyStore = storageService
127 .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder()
128 .withName("type_tunnel").withSerializer(serializer)
129 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
130 orderRelationship = storageService
131 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
132 .withName("type_tunnel").withSerializer(serializer)
133 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
134 idGenerator = coreService.getIdGenerator(runnelOpTopoic);
139 public void deactivate() {
140 orderRelationship.destroy();
141 tunnelIdAsKeyStore.destroy();
142 srcAndDstKeyStore.destroy();
143 typeKeyStore.destroy();
144 tunnelNameAsKeyStore.destroy();
149 public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
150 // tunnelIdAsKeyStore.
151 if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId())) {
152 Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
154 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
155 return tunnel.tunnelId();
157 DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
158 SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
159 Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
160 old.dst(), old.type(),
161 old.state(), old.groupId(),
165 DefaultAnnotations.merge(oldAnno, newAnno));
166 tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT);
167 TunnelEvent event = new TunnelEvent(
168 TunnelEvent.Type.TUNNEL_UPDATED,
170 notifyDelegate(event);
171 return tunnel.tunnelId();
173 TunnelId tunnelId = TunnelId.valueOf(idGenerator.getNewId());
174 Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
175 tunnel.dst(), tunnel.type(),
176 tunnel.state(), tunnel.groupId(),
180 tunnel.annotations());
181 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
182 tunnelIdAsKeyStore.put(tunnelId, newT);
183 Set<TunnelId> tunnelnameSet = tunnelNameAsKeyStore.get(tunnel
185 if (tunnelnameSet == null) {
186 tunnelnameSet = new HashSet<TunnelId>();
188 tunnelnameSet.add(tunnelId);
189 tunnelNameAsKeyStore.put(tunnel
190 .tunnelName(), tunnelnameSet);
191 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyStore.get(key);
192 if (srcAndDstKeySet == null) {
193 srcAndDstKeySet = new HashSet<TunnelId>();
195 srcAndDstKeySet.add(tunnelId);
196 srcAndDstKeyStore.put(key, srcAndDstKeySet);
197 Set<TunnelId> typeKeySet = typeKeyStore.get(tunnel.type());
198 if (typeKeySet == null) {
199 typeKeySet = new HashSet<TunnelId>();
201 typeKeySet.add(tunnelId);
202 typeKeyStore.put(tunnel.type(), typeKeySet);
203 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
205 notifyDelegate(event);
211 public void deleteTunnel(TunnelId tunnelId) {
212 Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
213 if (deletedTunnel == null) {
216 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()).remove(tunnelId);
217 tunnelIdAsKeyStore.remove(tunnelId);
218 TunnelKey key = new TunnelKey(deletedTunnel.src(), deletedTunnel.dst());
219 srcAndDstKeyStore.get(key).remove(tunnelId);
220 typeKeyStore.get(deletedTunnel.type()).remove(tunnelId);
221 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
223 notifyDelegate(event);
227 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
228 ProviderId producerName) {
229 TunnelKey key = TunnelKey.tunnelKey(src, dst);
230 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
234 Tunnel deletedTunnel = null;
235 TunnelEvent event = null;
236 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
237 for (TunnelId id : idSet) {
238 deletedTunnel = tunnelIdAsKeyStore.get(id);
239 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
242 if (producerName.equals(deletedTunnel.providerId())) {
243 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
244 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
245 .remove(deletedTunnel.tunnelId());
246 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
247 typeKeyStore.get(deletedTunnel.type())
248 .remove(deletedTunnel.tunnelId());
255 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
256 ProviderId producerName) {
257 TunnelKey key = TunnelKey.tunnelKey(src, dst);
258 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
262 Tunnel deletedTunnel = null;
263 TunnelEvent event = null;
264 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
265 for (TunnelId id : idSet) {
266 deletedTunnel = tunnelIdAsKeyStore.get(id);
267 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
270 if (producerName.equals(deletedTunnel.providerId())
271 && type.equals(deletedTunnel.type())) {
272 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
273 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
274 .remove(deletedTunnel.tunnelId());
275 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
276 typeKeyStore.get(deletedTunnel.type())
277 .remove(deletedTunnel.tunnelId());
284 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
285 Annotations... annotations) {
286 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
287 if (orderSet == null) {
288 orderSet = new HashSet<TunnelSubscription>();
290 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
292 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
293 if (result != null || Tunnel.State.INACTIVE.equals(result.state())) {
297 orderRelationship.put(appId, orderSet);
302 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
305 Annotations... annotations) {
306 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
307 if (orderSet == null) {
308 orderSet = new HashSet<TunnelSubscription>();
310 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
311 boolean isExist = orderSet.contains(order);
315 orderRelationship.put(appId, orderSet);
316 TunnelKey key = TunnelKey.tunnelKey(src, dst);
317 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
318 if (idSet == null || idSet.size() == 0) {
319 return Collections.emptySet();
321 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
322 for (TunnelId tunnelId : idSet) {
323 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
324 if (Tunnel.State.ACTIVE.equals(result.state())) {
325 tunnelSet.add(result);
332 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
334 TunnelEndPoint dst, Type type,
335 Annotations... annotations) {
336 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
337 if (orderSet == null) {
338 orderSet = new HashSet<TunnelSubscription>();
340 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
341 boolean isExist = orderSet.contains(order);
345 orderRelationship.put(appId, orderSet);
346 TunnelKey key = TunnelKey.tunnelKey(src, dst);
347 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
348 if (idSet == null || idSet.size() == 0) {
349 return Collections.emptySet();
351 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
352 for (TunnelId tunnelId : idSet) {
353 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
354 if (type.equals(result.type())
355 && Tunnel.State.ACTIVE.equals(result.state())) {
356 tunnelSet.add(result);
363 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
364 TunnelName tunnelName,
365 Annotations... annotations) {
366 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
367 if (orderSet == null) {
368 orderSet = new HashSet<TunnelSubscription>();
370 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
372 boolean isExist = orderSet.contains(order);
376 orderRelationship.put(appId, orderSet);
377 Set<TunnelId> idSet = tunnelNameAsKeyStore.get(tunnelName);
378 if (idSet == null || idSet.size() == 0) {
379 return Collections.emptySet();
381 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
382 for (TunnelId tunnelId : idSet) {
383 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
384 if (Tunnel.State.ACTIVE.equals(result.state())) {
385 tunnelSet.add(result);
392 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
393 Annotations... annotations) {
394 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
396 return deleteOrder(order);
400 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
401 Annotations... annotations) {
402 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
404 return deleteOrder(order);
408 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
409 TunnelEndPoint dst, Type type,
410 Annotations... annotations) {
411 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
412 return deleteOrder(order);
416 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
417 TunnelEndPoint dst, Annotations... annotations) {
418 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
419 return deleteOrder(order);
422 private boolean deleteOrder(TunnelSubscription order) {
423 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
424 if (orderSet == null) {
427 if (orderSet.contains(order)) {
428 orderSet.remove(order);
435 public Tunnel queryTunnel(TunnelId tunnelId) {
436 return tunnelIdAsKeyStore.get(tunnelId);
440 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
441 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
442 .get(appId)) : Collections.emptySet();
446 public Collection<Tunnel> queryTunnel(Type type) {
447 Collection<Tunnel> result = new HashSet<Tunnel>();
448 Set<TunnelId> tunnelIds = typeKeyStore.get(type);
449 if (tunnelIds == null) {
450 return Collections.emptySet();
452 for (TunnelId id : tunnelIds) {
453 result.add(tunnelIdAsKeyStore.get(id));
455 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
460 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
461 Collection<Tunnel> result = new HashSet<Tunnel>();
462 TunnelKey key = TunnelKey.tunnelKey(src, dst);
463 Set<TunnelId> tunnelIds = srcAndDstKeyStore.get(key);
464 if (tunnelIds == null) {
465 return Collections.emptySet();
467 for (TunnelId id : tunnelIds) {
468 result.add(tunnelIdAsKeyStore.get(id));
470 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
475 public Collection<Tunnel> queryAllTunnels() {
476 return tunnelIdAsKeyStore.values();
480 public int tunnelCount() {
481 return tunnelIdAsKeyStore.size();
485 * Uses source TunnelPoint and destination TunnelPoint as map key.
487 private static final class TunnelKey {
488 private final TunnelEndPoint src;
489 private final TunnelEndPoint dst;
491 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
502 * @return a key using source ip and destination ip
504 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
505 return new TunnelKey(src, dst);
509 public int hashCode() {
510 return Objects.hash(src, dst);
514 public boolean equals(Object obj) {
518 if (obj instanceof TunnelKey) {
519 final TunnelKey other = (TunnelKey) obj;
520 return Objects.equals(this.src, other.src)
521 && Objects.equals(this.dst, other.dst);
527 public String toString() {
528 return MoreObjects.toStringHelper(getClass()).add("src", src)
529 .add("dst", dst).toString();