blob: 45e83b25975cf02bf08bb25cfe48938388178db8 [file] [log] [blame]
Thomas Vachuskabf916ea2015-05-20 18:24:34 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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;
Satish Kd8adcef2016-04-26 16:31:14 +053025import java.util.Map;
jccd8697232015-05-05 14:42:23 +080026import java.util.Objects;
27import java.util.Set;
28
Satish Kd8adcef2016-04-26 16:31:14 +053029import com.google.common.collect.Maps;
jccd8697232015-05-05 14:42:23 +080030import org.apache.felix.scr.annotations.Activate;
31import org.apache.felix.scr.annotations.Component;
32import org.apache.felix.scr.annotations.Deactivate;
33import org.apache.felix.scr.annotations.Reference;
34import org.apache.felix.scr.annotations.ReferenceCardinality;
35import org.apache.felix.scr.annotations.Service;
36import org.onlab.util.KryoNamespace;
37import org.onosproject.cluster.ClusterService;
38import org.onosproject.core.ApplicationId;
39import org.onosproject.core.CoreService;
40import org.onosproject.core.IdGenerator;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070041import org.onosproject.incubator.net.tunnel.DefaultTunnel;
42import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei4c3c9972016-04-05 14:51:46 +053043import org.onosproject.incubator.net.tunnel.Tunnel.State;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070044import org.onosproject.incubator.net.tunnel.Tunnel.Type;
45import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
46import org.onosproject.incubator.net.tunnel.TunnelEvent;
47import org.onosproject.incubator.net.tunnel.TunnelId;
48import org.onosproject.incubator.net.tunnel.TunnelName;
49import org.onosproject.incubator.net.tunnel.TunnelStore;
50import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
51import org.onosproject.incubator.net.tunnel.TunnelSubscription;
samuel7a5691a2015-05-23 00:36:32 +080052import org.onosproject.net.Annotations;
53import org.onosproject.net.DefaultAnnotations;
54import org.onosproject.net.SparseAnnotations;
55import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080056import org.onosproject.store.AbstractStore;
57import org.onosproject.store.app.GossipApplicationStore.InternalState;
58import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
59import org.onosproject.store.serializers.KryoNamespaces;
60import org.onosproject.store.service.EventuallyConsistentMap;
Satish Kd8adcef2016-04-26 16:31:14 +053061import org.onosproject.store.service.EventuallyConsistentMapEvent;
62import org.onosproject.store.service.EventuallyConsistentMapListener;
jccd8697232015-05-05 14:42:23 +080063import org.onosproject.store.service.MultiValuedTimestamp;
64import org.onosproject.store.service.StorageService;
Madan Jampanibcf1a482015-06-24 19:05:56 -070065import org.onosproject.store.service.WallClockTimestamp;
jccd8697232015-05-05 14:42:23 +080066import org.slf4j.Logger;
67
68import com.google.common.base.MoreObjects;
69import com.google.common.collect.ImmutableSet;
70
Satish Kd8adcef2016-04-26 16:31:14 +053071import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
72import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
73
jccd8697232015-05-05 14:42:23 +080074/**
75 * Manages inventory of tunnel in distributed data store that uses optimistic
76 * replication and gossip based techniques.
77 */
78@Component(immediate = true)
79@Service
80public class DistributedTunnelStore
81 extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
82 implements TunnelStore {
83
84 private final Logger log = getLogger(getClass());
85
86 /**
87 * The topic used for obtaining globally unique ids.
88 */
Satish Kfc1a6312016-02-23 13:33:04 +053089 private String tunnelOpTopic = "tunnel-ops-ids";
jccd8697232015-05-05 14:42:23 +080090
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected ClusterCommunicationService clusterCommunicator;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected ClusterService clusterService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected CoreService coreService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected StorageService storageService;
102
103 // tunnel identity as map key in the store.
104 private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore;
jccd8697232015-05-05 14:42:23 +0800105 // maintains records that app subscribes tunnel.
106 private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship;
107
Satish Kd8adcef2016-04-26 16:31:14 +0530108 // tunnel name as map key.
109 private final Map<TunnelName, Set<TunnelId>> tunnelNameAsKeyMap = Maps.newConcurrentMap();
110 // maintains all the tunnels between source and destination.
111 private final Map<TunnelKey, Set<TunnelId>> srcAndDstKeyMap = Maps.newConcurrentMap();
112 // maintains all the tunnels by tunnel type.
113 private final Map<Tunnel.Type, Set<TunnelId>> typeKeyMap = Maps.newConcurrentMap();
114
jccd8697232015-05-05 14:42:23 +0800115 private IdGenerator idGenerator;
116
Satish Kd8adcef2016-04-26 16:31:14 +0530117 private EventuallyConsistentMapListener<TunnelId, Tunnel> tunnelUpdateListener =
118 new InternalTunnelChangeEventListener();
119
jccd8697232015-05-05 14:42:23 +0800120 @Activate
121 public void activate() {
122 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
123 .register(KryoNamespaces.API)
124 .register(MultiValuedTimestamp.class)
125 .register(InternalState.class);
126 tunnelIdAsKeyStore = storageService
127 .<TunnelId, Tunnel>eventuallyConsistentMapBuilder()
128 .withName("all_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700129 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
samuel7a5691a2015-05-23 00:36:32 +0800130 orderRelationship = storageService
131 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
132 .withName("type_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700133 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Satish Kfc1a6312016-02-23 13:33:04 +0530134 idGenerator = coreService.getIdGenerator(tunnelOpTopic);
Satish Kd8adcef2016-04-26 16:31:14 +0530135 tunnelIdAsKeyStore.addListener(tunnelUpdateListener);
jccd8697232015-05-05 14:42:23 +0800136 log.info("Started");
137 }
138
139 @Deactivate
140 public void deactivate() {
Satish Kd8adcef2016-04-26 16:31:14 +0530141 tunnelIdAsKeyStore.removeListener(tunnelUpdateListener);
Madan Jampani632f16b2015-08-11 12:42:59 -0700142 orderRelationship.destroy();
jccd8697232015-05-05 14:42:23 +0800143 tunnelIdAsKeyStore.destroy();
Satish Kd8adcef2016-04-26 16:31:14 +0530144 srcAndDstKeyMap.clear();
145 typeKeyMap.clear();
146 tunnelNameAsKeyMap.clear();
jccd8697232015-05-05 14:42:23 +0800147 log.info("Stopped");
148 }
149
150 @Override
151 public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530152 return handleCreateOrUpdateTunnel(tunnel, null);
153 }
154
155 @Override
156 public TunnelId createOrUpdateTunnel(Tunnel tunnel, State state) {
157 return handleCreateOrUpdateTunnel(tunnel, state);
158 }
159
160 private TunnelId handleCreateOrUpdateTunnel(Tunnel tunnel, State state) {
jccd8697232015-05-05 14:42:23 +0800161 // tunnelIdAsKeyStore.
Satish Kf1b38bc2015-11-22 17:13:51 +0530162 if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId().toString())) {
jccd8697232015-05-05 14:42:23 +0800163 Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
164 if (old == null) {
165 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
166 return tunnel.tunnelId();
167 }
samuel7a5691a2015-05-23 00:36:32 +0800168 DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
169 SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530170 State newTunnelState = (state != null) ? state : old.state();
samuel7a5691a2015-05-23 00:36:32 +0800171 Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
172 old.dst(), old.type(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530173 newTunnelState, old.groupId(),
jccd8697232015-05-05 14:42:23 +0800174 old.tunnelId(),
samuel7a5691a2015-05-23 00:36:32 +0800175 old.tunnelName(),
176 old.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530177 old.resource(),
samuel7a5691a2015-05-23 00:36:32 +0800178 DefaultAnnotations.merge(oldAnno, newAnno));
jccd8697232015-05-05 14:42:23 +0800179 tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT);
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530180 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED,
jccd8697232015-05-05 14:42:23 +0800181 tunnel);
182 notifyDelegate(event);
183 return tunnel.tunnelId();
184 } else {
Brian Stanke9a108972016-04-11 15:25:17 -0400185 TunnelId tunnelId = TunnelId.valueOf(String.valueOf(idGenerator.getNewId()));
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530186 State tunnelState = (state != null) ? state : tunnel.state();
jccd8697232015-05-05 14:42:23 +0800187 Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
188 tunnel.dst(), tunnel.type(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530189 tunnelState, tunnel.groupId(),
jccd8697232015-05-05 14:42:23 +0800190 tunnelId,
191 tunnel.tunnelName(),
samuel7a5691a2015-05-23 00:36:32 +0800192 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530193 tunnel.resource(),
jccd8697232015-05-05 14:42:23 +0800194 tunnel.annotations());
jccd8697232015-05-05 14:42:23 +0800195 tunnelIdAsKeyStore.put(tunnelId, newT);
Satish Kd8adcef2016-04-26 16:31:14 +0530196
jccd8697232015-05-05 14:42:23 +0800197 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
198 tunnel);
199 notifyDelegate(event);
200 return tunnelId;
201 }
202 }
203
204 @Override
205 public void deleteTunnel(TunnelId tunnelId) {
206 Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
207 if (deletedTunnel == null) {
208 return;
209 }
Satish Kd8adcef2016-04-26 16:31:14 +0530210
jccd8697232015-05-05 14:42:23 +0800211 tunnelIdAsKeyStore.remove(tunnelId);
Satish Kd8adcef2016-04-26 16:31:14 +0530212
jccd8697232015-05-05 14:42:23 +0800213 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
214 deletedTunnel);
215 notifyDelegate(event);
216 }
217
218 @Override
219 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
220 ProviderId producerName) {
221 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530222 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800223 if (idSet == null) {
224 return;
225 }
226 Tunnel deletedTunnel = null;
227 TunnelEvent event = null;
228 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
229 for (TunnelId id : idSet) {
230 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530231
232 if (producerName == null || (producerName != null
233 && producerName.equals(deletedTunnel.providerId()))) {
jccd8697232015-05-05 14:42:23 +0800234 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530235
Satish Kfc1a6312016-02-23 13:33:04 +0530236 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
237 deletedTunnel);
238 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800239 }
240 }
Satish Kfc1a6312016-02-23 13:33:04 +0530241
242 if (!ls.isEmpty()) {
243 notifyDelegate(ls);
244 }
jccd8697232015-05-05 14:42:23 +0800245 }
246
247 @Override
248 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
249 ProviderId producerName) {
250 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530251 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800252 if (idSet == null) {
253 return;
254 }
255 Tunnel deletedTunnel = null;
256 TunnelEvent event = null;
257 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
258 for (TunnelId id : idSet) {
259 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530260
261 if (type.equals(deletedTunnel.type()) && (producerName == null || (producerName != null
262 && producerName.equals(deletedTunnel.providerId())))) {
jccd8697232015-05-05 14:42:23 +0800263 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530264
Satish Kfc1a6312016-02-23 13:33:04 +0530265 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
266 deletedTunnel);
267 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800268 }
269 }
Satish Kfc1a6312016-02-23 13:33:04 +0530270
271 if (!ls.isEmpty()) {
272 notifyDelegate(ls);
273 }
jccd8697232015-05-05 14:42:23 +0800274 }
275
276 @Override
277 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
278 Annotations... annotations) {
279 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
280 if (orderSet == null) {
281 orderSet = new HashSet<TunnelSubscription>();
282 }
283 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
284 annotations);
285 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
Satish Kd9722352015-11-20 23:19:52 +0530286 if (result == null || Tunnel.State.INACTIVE.equals(result.state())) {
jccd8697232015-05-05 14:42:23 +0800287 return null;
288 }
Satish Kd9722352015-11-20 23:19:52 +0530289
jccd8697232015-05-05 14:42:23 +0800290 orderSet.add(order);
291 orderRelationship.put(appId, orderSet);
292 return result;
293 }
294
295 @Override
296 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
297 TunnelEndPoint src,
298 TunnelEndPoint dst,
299 Annotations... annotations) {
300 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
301 if (orderSet == null) {
302 orderSet = new HashSet<TunnelSubscription>();
303 }
304 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
305 boolean isExist = orderSet.contains(order);
306 if (!isExist) {
307 orderSet.add(order);
308 }
309 orderRelationship.put(appId, orderSet);
310 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530311 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800312 if (idSet == null || idSet.size() == 0) {
313 return Collections.emptySet();
314 }
315 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
316 for (TunnelId tunnelId : idSet) {
317 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
318 if (Tunnel.State.ACTIVE.equals(result.state())) {
319 tunnelSet.add(result);
320 }
321 }
322 return tunnelSet;
323 }
324
325 @Override
326 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
327 TunnelEndPoint src,
328 TunnelEndPoint dst, Type type,
329 Annotations... annotations) {
330 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
331 if (orderSet == null) {
332 orderSet = new HashSet<TunnelSubscription>();
333 }
334 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
335 boolean isExist = orderSet.contains(order);
336 if (!isExist) {
337 orderSet.add(order);
338 }
339 orderRelationship.put(appId, orderSet);
340 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530341 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800342 if (idSet == null || idSet.size() == 0) {
343 return Collections.emptySet();
344 }
345 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
346 for (TunnelId tunnelId : idSet) {
347 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
348 if (type.equals(result.type())
349 && Tunnel.State.ACTIVE.equals(result.state())) {
350 tunnelSet.add(result);
351 }
352 }
353 return tunnelSet;
354 }
355
356 @Override
357 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
358 TunnelName tunnelName,
359 Annotations... annotations) {
360 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
361 if (orderSet == null) {
362 orderSet = new HashSet<TunnelSubscription>();
363 }
364 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
365 annotations);
366 boolean isExist = orderSet.contains(order);
Satish Kd8adcef2016-04-26 16:31:14 +0530367
368 Set<TunnelId> idSet = tunnelNameAsKeyMap.get(tunnelName);
jccd8697232015-05-05 14:42:23 +0800369 if (idSet == null || idSet.size() == 0) {
370 return Collections.emptySet();
371 }
372 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
373 for (TunnelId tunnelId : idSet) {
374 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
375 if (Tunnel.State.ACTIVE.equals(result.state())) {
376 tunnelSet.add(result);
377 }
378 }
Satish Kd8adcef2016-04-26 16:31:14 +0530379
380 if (!tunnelSet.isEmpty() && !isExist) {
381 orderSet.add(order);
382 orderRelationship.put(appId, orderSet);
383 }
384
jccd8697232015-05-05 14:42:23 +0800385 return tunnelSet;
386 }
387
388 @Override
389 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
390 Annotations... annotations) {
391 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
392 annotations);
393 return deleteOrder(order);
394 }
395
396 @Override
397 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
398 Annotations... annotations) {
399 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
400 annotations);
401 return deleteOrder(order);
402 }
403
404 @Override
405 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
406 TunnelEndPoint dst, Type type,
407 Annotations... annotations) {
408 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
409 return deleteOrder(order);
410 }
411
412 @Override
413 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
414 TunnelEndPoint dst, Annotations... annotations) {
415 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
416 return deleteOrder(order);
417 }
418
419 private boolean deleteOrder(TunnelSubscription order) {
420 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
421 if (orderSet == null) {
422 return true;
423 }
424 if (orderSet.contains(order)) {
425 orderSet.remove(order);
426 return true;
427 }
428 return false;
429 }
430
431 @Override
432 public Tunnel queryTunnel(TunnelId tunnelId) {
433 return tunnelIdAsKeyStore.get(tunnelId);
434 }
435
436 @Override
437 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
438 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
439 .get(appId)) : Collections.emptySet();
440 }
441
442 @Override
443 public Collection<Tunnel> queryTunnel(Type type) {
444 Collection<Tunnel> result = new HashSet<Tunnel>();
Satish Kd8adcef2016-04-26 16:31:14 +0530445 Set<TunnelId> tunnelIds = typeKeyMap.get(type);
jccd8697232015-05-05 14:42:23 +0800446 if (tunnelIds == null) {
447 return Collections.emptySet();
448 }
449 for (TunnelId id : tunnelIds) {
450 result.add(tunnelIdAsKeyStore.get(id));
451 }
452 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
453 .copyOf(result);
454 }
455
456 @Override
457 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
458 Collection<Tunnel> result = new HashSet<Tunnel>();
459 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530460 Set<TunnelId> tunnelIds = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800461 if (tunnelIds == null) {
462 return Collections.emptySet();
463 }
464 for (TunnelId id : tunnelIds) {
465 result.add(tunnelIdAsKeyStore.get(id));
466 }
467 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
468 .copyOf(result);
469 }
470
471 @Override
samuel7a5691a2015-05-23 00:36:32 +0800472 public Collection<Tunnel> queryAllTunnels() {
473 return tunnelIdAsKeyStore.values();
474 }
475
476 @Override
jccd8697232015-05-05 14:42:23 +0800477 public int tunnelCount() {
478 return tunnelIdAsKeyStore.size();
479 }
480
481 /**
482 * Uses source TunnelPoint and destination TunnelPoint as map key.
483 */
484 private static final class TunnelKey {
485 private final TunnelEndPoint src;
486 private final TunnelEndPoint dst;
487
488 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
489 this.src = src;
490 this.dst = dst;
491
492 }
493
494 /**
495 * create a map key.
496 *
497 * @param src
498 * @param dst
499 * @return a key using source ip and destination ip
500 */
501 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
502 return new TunnelKey(src, dst);
503 }
504
505 @Override
506 public int hashCode() {
507 return Objects.hash(src, dst);
508 }
509
510 @Override
511 public boolean equals(Object obj) {
512 if (this == obj) {
513 return true;
514 }
515 if (obj instanceof TunnelKey) {
516 final TunnelKey other = (TunnelKey) obj;
517 return Objects.equals(this.src, other.src)
518 && Objects.equals(this.dst, other.dst);
519 }
520 return false;
521 }
522
523 @Override
524 public String toString() {
525 return MoreObjects.toStringHelper(getClass()).add("src", src)
526 .add("dst", dst).toString();
527 }
528 }
Satish Kd8adcef2016-04-26 16:31:14 +0530529
530 /**
531 * Eventually consistent map listener for tunnel change event which updated the local map based on event.
532 */
533 private class InternalTunnelChangeEventListener
534 implements EventuallyConsistentMapListener<TunnelId, Tunnel> {
535 @Override
536 public void event(EventuallyConsistentMapEvent<TunnelId, Tunnel> event) {
537 TunnelId tunnelId = event.key();
538 Tunnel tunnel = event.value();
539
540 if (event.type() == PUT) {
541
542 // Update tunnel name map
543 Set<TunnelId> tunnelNameSet = tunnelNameAsKeyMap.get(tunnel
544 .tunnelName());
545 if (tunnelNameSet == null) {
546 tunnelNameSet = new HashSet<TunnelId>();
547 }
548 tunnelNameSet.add(tunnelId);
549 tunnelNameAsKeyMap.put(tunnel.tunnelName(), tunnelNameSet);
550
551 // Update tunnel source and destination map
552 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
553 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyMap.get(key);
554 if (srcAndDstKeySet == null) {
555 srcAndDstKeySet = new HashSet<TunnelId>();
556 }
557 srcAndDstKeySet.add(tunnelId);
558 srcAndDstKeyMap.put(key, srcAndDstKeySet);
559
560 // Update tunnel type map
561 Set<TunnelId> typeKeySet = typeKeyMap.get(tunnel.type());
562 if (typeKeySet == null) {
563 typeKeySet = new HashSet<TunnelId>();
564 }
565 typeKeySet.add(tunnelId);
566 typeKeyMap.put(tunnel.type(), typeKeySet);
567 } else if (event.type() == REMOVE) {
568
569 // Update tunnel name map
570 tunnelNameAsKeyMap.get(tunnel.tunnelName()).remove(tunnelId);
571 if (tunnelNameAsKeyMap.get(tunnel.tunnelName()).isEmpty()) {
572 tunnelNameAsKeyMap.remove(tunnel.tunnelName());
573 }
574
575 // Update tunnel source and destination map
576 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
577 srcAndDstKeyMap.get(key).remove(tunnelId);
578 if (srcAndDstKeyMap.get(key).isEmpty()) {
579 srcAndDstKeyMap.remove(key);
580 }
581
582 // Update tunnel type map
583 typeKeyMap.get(tunnel.type()).remove(tunnelId);
584 if (typeKeyMap.get(tunnel.type()).isEmpty()) {
585 typeKeyMap.remove(tunnel.type());
586 }
587 }
588 }
589 }
jccd8697232015-05-05 14:42:23 +0800590}