blob: 0907643fc19f095d206469794a0fb53c0e76c269 [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;
jccd8697232015-05-05 14:42:23 +080057import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
58import org.onosproject.store.serializers.KryoNamespaces;
59import org.onosproject.store.service.EventuallyConsistentMap;
Satish Kd8adcef2016-04-26 16:31:14 +053060import org.onosproject.store.service.EventuallyConsistentMapEvent;
61import org.onosproject.store.service.EventuallyConsistentMapListener;
jccd8697232015-05-05 14:42:23 +080062import org.onosproject.store.service.MultiValuedTimestamp;
63import org.onosproject.store.service.StorageService;
Madan Jampanibcf1a482015-06-24 19:05:56 -070064import org.onosproject.store.service.WallClockTimestamp;
jccd8697232015-05-05 14:42:23 +080065import org.slf4j.Logger;
66
67import com.google.common.base.MoreObjects;
68import com.google.common.collect.ImmutableSet;
69
Satish Kd8adcef2016-04-26 16:31:14 +053070import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
71import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
72
jccd8697232015-05-05 14:42:23 +080073/**
74 * Manages inventory of tunnel in distributed data store that uses optimistic
75 * replication and gossip based techniques.
76 */
77@Component(immediate = true)
78@Service
79public class DistributedTunnelStore
80 extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
81 implements TunnelStore {
82
83 private final Logger log = getLogger(getClass());
84
85 /**
86 * The topic used for obtaining globally unique ids.
87 */
Satish Kfc1a6312016-02-23 13:33:04 +053088 private String tunnelOpTopic = "tunnel-ops-ids";
jccd8697232015-05-05 14:42:23 +080089
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected ClusterCommunicationService clusterCommunicator;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected ClusterService clusterService;
95
96 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected CoreService coreService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected StorageService storageService;
101
102 // tunnel identity as map key in the store.
103 private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore;
jccd8697232015-05-05 14:42:23 +0800104 // maintains records that app subscribes tunnel.
105 private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship;
106
Satish Kd8adcef2016-04-26 16:31:14 +0530107 // tunnel name as map key.
108 private final Map<TunnelName, Set<TunnelId>> tunnelNameAsKeyMap = Maps.newConcurrentMap();
109 // maintains all the tunnels between source and destination.
110 private final Map<TunnelKey, Set<TunnelId>> srcAndDstKeyMap = Maps.newConcurrentMap();
111 // maintains all the tunnels by tunnel type.
112 private final Map<Tunnel.Type, Set<TunnelId>> typeKeyMap = Maps.newConcurrentMap();
113
jccd8697232015-05-05 14:42:23 +0800114 private IdGenerator idGenerator;
115
Satish Kd8adcef2016-04-26 16:31:14 +0530116 private EventuallyConsistentMapListener<TunnelId, Tunnel> tunnelUpdateListener =
117 new InternalTunnelChangeEventListener();
118
jccd8697232015-05-05 14:42:23 +0800119 @Activate
120 public void activate() {
121 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
122 .register(KryoNamespaces.API)
Madan Jampani6c02d9e2016-05-24 15:09:47 -0700123 .register(MultiValuedTimestamp.class);
jccd8697232015-05-05 14:42:23 +0800124 tunnelIdAsKeyStore = storageService
125 .<TunnelId, Tunnel>eventuallyConsistentMapBuilder()
126 .withName("all_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700127 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
samuel7a5691a2015-05-23 00:36:32 +0800128 orderRelationship = storageService
129 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
130 .withName("type_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700131 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Satish Kfc1a6312016-02-23 13:33:04 +0530132 idGenerator = coreService.getIdGenerator(tunnelOpTopic);
Satish Kd8adcef2016-04-26 16:31:14 +0530133 tunnelIdAsKeyStore.addListener(tunnelUpdateListener);
jccd8697232015-05-05 14:42:23 +0800134 log.info("Started");
135 }
136
137 @Deactivate
138 public void deactivate() {
Satish Kd8adcef2016-04-26 16:31:14 +0530139 tunnelIdAsKeyStore.removeListener(tunnelUpdateListener);
Madan Jampani632f16b2015-08-11 12:42:59 -0700140 orderRelationship.destroy();
jccd8697232015-05-05 14:42:23 +0800141 tunnelIdAsKeyStore.destroy();
Satish Kd8adcef2016-04-26 16:31:14 +0530142 srcAndDstKeyMap.clear();
143 typeKeyMap.clear();
144 tunnelNameAsKeyMap.clear();
jccd8697232015-05-05 14:42:23 +0800145 log.info("Stopped");
146 }
147
148 @Override
149 public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530150 return handleCreateOrUpdateTunnel(tunnel, null);
151 }
152
153 @Override
154 public TunnelId createOrUpdateTunnel(Tunnel tunnel, State state) {
155 return handleCreateOrUpdateTunnel(tunnel, state);
156 }
157
158 private TunnelId handleCreateOrUpdateTunnel(Tunnel tunnel, State state) {
jccd8697232015-05-05 14:42:23 +0800159 // tunnelIdAsKeyStore.
Satish Kf1b38bc2015-11-22 17:13:51 +0530160 if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId().toString())) {
jccd8697232015-05-05 14:42:23 +0800161 Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
162 if (old == null) {
163 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
164 return tunnel.tunnelId();
165 }
samuel7a5691a2015-05-23 00:36:32 +0800166 DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
167 SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530168 State newTunnelState = (state != null) ? state : old.state();
samuel7a5691a2015-05-23 00:36:32 +0800169 Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
170 old.dst(), old.type(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530171 newTunnelState, old.groupId(),
jccd8697232015-05-05 14:42:23 +0800172 old.tunnelId(),
samuel7a5691a2015-05-23 00:36:32 +0800173 old.tunnelName(),
174 old.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530175 old.resource(),
samuel7a5691a2015-05-23 00:36:32 +0800176 DefaultAnnotations.merge(oldAnno, newAnno));
jccd8697232015-05-05 14:42:23 +0800177 tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT);
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530178 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED,
jccd8697232015-05-05 14:42:23 +0800179 tunnel);
180 notifyDelegate(event);
181 return tunnel.tunnelId();
182 } else {
Brian Stanke9a108972016-04-11 15:25:17 -0400183 TunnelId tunnelId = TunnelId.valueOf(String.valueOf(idGenerator.getNewId()));
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530184 State tunnelState = (state != null) ? state : tunnel.state();
jccd8697232015-05-05 14:42:23 +0800185 Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
186 tunnel.dst(), tunnel.type(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530187 tunnelState, tunnel.groupId(),
jccd8697232015-05-05 14:42:23 +0800188 tunnelId,
189 tunnel.tunnelName(),
samuel7a5691a2015-05-23 00:36:32 +0800190 tunnel.path(),
Satish Kd70e9572016-04-28 13:58:51 +0530191 tunnel.resource(),
jccd8697232015-05-05 14:42:23 +0800192 tunnel.annotations());
jccd8697232015-05-05 14:42:23 +0800193 tunnelIdAsKeyStore.put(tunnelId, newT);
Satish Kd8adcef2016-04-26 16:31:14 +0530194
jccd8697232015-05-05 14:42:23 +0800195 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
196 tunnel);
197 notifyDelegate(event);
198 return tunnelId;
199 }
200 }
201
202 @Override
203 public void deleteTunnel(TunnelId tunnelId) {
204 Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
205 if (deletedTunnel == null) {
206 return;
207 }
Satish Kd8adcef2016-04-26 16:31:14 +0530208
jccd8697232015-05-05 14:42:23 +0800209 tunnelIdAsKeyStore.remove(tunnelId);
Satish Kd8adcef2016-04-26 16:31:14 +0530210
jccd8697232015-05-05 14:42:23 +0800211 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
212 deletedTunnel);
213 notifyDelegate(event);
214 }
215
216 @Override
217 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
218 ProviderId producerName) {
219 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530220 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800221 if (idSet == null) {
222 return;
223 }
224 Tunnel deletedTunnel = null;
225 TunnelEvent event = null;
226 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
227 for (TunnelId id : idSet) {
228 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530229
230 if (producerName == null || (producerName != null
231 && producerName.equals(deletedTunnel.providerId()))) {
jccd8697232015-05-05 14:42:23 +0800232 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530233
Satish Kfc1a6312016-02-23 13:33:04 +0530234 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
235 deletedTunnel);
236 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800237 }
238 }
Satish Kfc1a6312016-02-23 13:33:04 +0530239
240 if (!ls.isEmpty()) {
241 notifyDelegate(ls);
242 }
jccd8697232015-05-05 14:42:23 +0800243 }
244
245 @Override
246 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
247 ProviderId producerName) {
248 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530249 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800250 if (idSet == null) {
251 return;
252 }
253 Tunnel deletedTunnel = null;
254 TunnelEvent event = null;
255 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
256 for (TunnelId id : idSet) {
257 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530258
259 if (type.equals(deletedTunnel.type()) && (producerName == null || (producerName != null
260 && producerName.equals(deletedTunnel.providerId())))) {
jccd8697232015-05-05 14:42:23 +0800261 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530262
Satish Kfc1a6312016-02-23 13:33:04 +0530263 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
264 deletedTunnel);
265 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800266 }
267 }
Satish Kfc1a6312016-02-23 13:33:04 +0530268
269 if (!ls.isEmpty()) {
270 notifyDelegate(ls);
271 }
jccd8697232015-05-05 14:42:23 +0800272 }
273
274 @Override
275 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
276 Annotations... annotations) {
277 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
278 if (orderSet == null) {
279 orderSet = new HashSet<TunnelSubscription>();
280 }
281 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
282 annotations);
283 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
Satish Kd9722352015-11-20 23:19:52 +0530284 if (result == null || Tunnel.State.INACTIVE.equals(result.state())) {
jccd8697232015-05-05 14:42:23 +0800285 return null;
286 }
Satish Kd9722352015-11-20 23:19:52 +0530287
jccd8697232015-05-05 14:42:23 +0800288 orderSet.add(order);
289 orderRelationship.put(appId, orderSet);
290 return result;
291 }
292
293 @Override
294 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
295 TunnelEndPoint src,
296 TunnelEndPoint dst,
297 Annotations... annotations) {
298 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
299 if (orderSet == null) {
300 orderSet = new HashSet<TunnelSubscription>();
301 }
302 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
303 boolean isExist = orderSet.contains(order);
304 if (!isExist) {
305 orderSet.add(order);
306 }
307 orderRelationship.put(appId, orderSet);
308 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530309 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
Jon Hallcbd1b392017-01-18 20:15:44 -0800310 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800311 return Collections.emptySet();
312 }
313 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
314 for (TunnelId tunnelId : idSet) {
315 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
316 if (Tunnel.State.ACTIVE.equals(result.state())) {
317 tunnelSet.add(result);
318 }
319 }
320 return tunnelSet;
321 }
322
323 @Override
324 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
325 TunnelEndPoint src,
326 TunnelEndPoint dst, Type type,
327 Annotations... annotations) {
328 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
329 if (orderSet == null) {
330 orderSet = new HashSet<TunnelSubscription>();
331 }
332 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
333 boolean isExist = orderSet.contains(order);
334 if (!isExist) {
335 orderSet.add(order);
336 }
337 orderRelationship.put(appId, orderSet);
338 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530339 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
Jon Hallcbd1b392017-01-18 20:15:44 -0800340 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800341 return Collections.emptySet();
342 }
343 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
344 for (TunnelId tunnelId : idSet) {
345 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
346 if (type.equals(result.type())
347 && Tunnel.State.ACTIVE.equals(result.state())) {
348 tunnelSet.add(result);
349 }
350 }
351 return tunnelSet;
352 }
353
354 @Override
355 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
356 TunnelName tunnelName,
357 Annotations... annotations) {
358 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
359 if (orderSet == null) {
360 orderSet = new HashSet<TunnelSubscription>();
361 }
362 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
363 annotations);
364 boolean isExist = orderSet.contains(order);
Satish Kd8adcef2016-04-26 16:31:14 +0530365
366 Set<TunnelId> idSet = tunnelNameAsKeyMap.get(tunnelName);
Jon Hallcbd1b392017-01-18 20:15:44 -0800367 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800368 return Collections.emptySet();
369 }
370 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
371 for (TunnelId tunnelId : idSet) {
372 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
373 if (Tunnel.State.ACTIVE.equals(result.state())) {
374 tunnelSet.add(result);
375 }
376 }
Satish Kd8adcef2016-04-26 16:31:14 +0530377
378 if (!tunnelSet.isEmpty() && !isExist) {
379 orderSet.add(order);
380 orderRelationship.put(appId, orderSet);
381 }
382
jccd8697232015-05-05 14:42:23 +0800383 return tunnelSet;
384 }
385
386 @Override
387 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
388 Annotations... annotations) {
389 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
390 annotations);
391 return deleteOrder(order);
392 }
393
394 @Override
395 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
396 Annotations... annotations) {
397 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
398 annotations);
399 return deleteOrder(order);
400 }
401
402 @Override
403 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
404 TunnelEndPoint dst, Type type,
405 Annotations... annotations) {
406 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
407 return deleteOrder(order);
408 }
409
410 @Override
411 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
412 TunnelEndPoint dst, Annotations... annotations) {
413 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
414 return deleteOrder(order);
415 }
416
417 private boolean deleteOrder(TunnelSubscription order) {
418 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
419 if (orderSet == null) {
420 return true;
421 }
422 if (orderSet.contains(order)) {
423 orderSet.remove(order);
424 return true;
425 }
426 return false;
427 }
428
429 @Override
430 public Tunnel queryTunnel(TunnelId tunnelId) {
431 return tunnelIdAsKeyStore.get(tunnelId);
432 }
433
434 @Override
435 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
436 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
437 .get(appId)) : Collections.emptySet();
438 }
439
440 @Override
441 public Collection<Tunnel> queryTunnel(Type type) {
442 Collection<Tunnel> result = new HashSet<Tunnel>();
Satish Kd8adcef2016-04-26 16:31:14 +0530443 Set<TunnelId> tunnelIds = typeKeyMap.get(type);
jccd8697232015-05-05 14:42:23 +0800444 if (tunnelIds == null) {
445 return Collections.emptySet();
446 }
447 for (TunnelId id : tunnelIds) {
448 result.add(tunnelIdAsKeyStore.get(id));
449 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800450 return result.isEmpty() ? Collections.emptySet() : ImmutableSet
jccd8697232015-05-05 14:42:23 +0800451 .copyOf(result);
452 }
453
454 @Override
455 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
456 Collection<Tunnel> result = new HashSet<Tunnel>();
457 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530458 Set<TunnelId> tunnelIds = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800459 if (tunnelIds == null) {
460 return Collections.emptySet();
461 }
462 for (TunnelId id : tunnelIds) {
463 result.add(tunnelIdAsKeyStore.get(id));
464 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800465 return result.isEmpty() ? Collections.emptySet() : ImmutableSet
jccd8697232015-05-05 14:42:23 +0800466 .copyOf(result);
467 }
468
469 @Override
samuel7a5691a2015-05-23 00:36:32 +0800470 public Collection<Tunnel> queryAllTunnels() {
471 return tunnelIdAsKeyStore.values();
472 }
473
474 @Override
jccd8697232015-05-05 14:42:23 +0800475 public int tunnelCount() {
476 return tunnelIdAsKeyStore.size();
477 }
478
479 /**
480 * Uses source TunnelPoint and destination TunnelPoint as map key.
481 */
482 private static final class TunnelKey {
483 private final TunnelEndPoint src;
484 private final TunnelEndPoint dst;
485
486 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
487 this.src = src;
488 this.dst = dst;
489
490 }
491
492 /**
493 * create a map key.
494 *
495 * @param src
496 * @param dst
497 * @return a key using source ip and destination ip
498 */
499 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
500 return new TunnelKey(src, dst);
501 }
502
503 @Override
504 public int hashCode() {
505 return Objects.hash(src, dst);
506 }
507
508 @Override
509 public boolean equals(Object obj) {
510 if (this == obj) {
511 return true;
512 }
513 if (obj instanceof TunnelKey) {
514 final TunnelKey other = (TunnelKey) obj;
515 return Objects.equals(this.src, other.src)
516 && Objects.equals(this.dst, other.dst);
517 }
518 return false;
519 }
520
521 @Override
522 public String toString() {
523 return MoreObjects.toStringHelper(getClass()).add("src", src)
524 .add("dst", dst).toString();
525 }
526 }
Satish Kd8adcef2016-04-26 16:31:14 +0530527
528 /**
529 * Eventually consistent map listener for tunnel change event which updated the local map based on event.
530 */
531 private class InternalTunnelChangeEventListener
532 implements EventuallyConsistentMapListener<TunnelId, Tunnel> {
533 @Override
534 public void event(EventuallyConsistentMapEvent<TunnelId, Tunnel> event) {
535 TunnelId tunnelId = event.key();
536 Tunnel tunnel = event.value();
537
538 if (event.type() == PUT) {
539
540 // Update tunnel name map
541 Set<TunnelId> tunnelNameSet = tunnelNameAsKeyMap.get(tunnel
542 .tunnelName());
543 if (tunnelNameSet == null) {
544 tunnelNameSet = new HashSet<TunnelId>();
545 }
546 tunnelNameSet.add(tunnelId);
547 tunnelNameAsKeyMap.put(tunnel.tunnelName(), tunnelNameSet);
548
549 // Update tunnel source and destination map
550 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
551 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyMap.get(key);
552 if (srcAndDstKeySet == null) {
553 srcAndDstKeySet = new HashSet<TunnelId>();
554 }
555 srcAndDstKeySet.add(tunnelId);
556 srcAndDstKeyMap.put(key, srcAndDstKeySet);
557
558 // Update tunnel type map
559 Set<TunnelId> typeKeySet = typeKeyMap.get(tunnel.type());
560 if (typeKeySet == null) {
561 typeKeySet = new HashSet<TunnelId>();
562 }
563 typeKeySet.add(tunnelId);
564 typeKeyMap.put(tunnel.type(), typeKeySet);
565 } else if (event.type() == REMOVE) {
566
567 // Update tunnel name map
568 tunnelNameAsKeyMap.get(tunnel.tunnelName()).remove(tunnelId);
569 if (tunnelNameAsKeyMap.get(tunnel.tunnelName()).isEmpty()) {
570 tunnelNameAsKeyMap.remove(tunnel.tunnelName());
571 }
572
573 // Update tunnel source and destination map
574 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
575 srcAndDstKeyMap.get(key).remove(tunnelId);
576 if (srcAndDstKeyMap.get(key).isEmpty()) {
577 srcAndDstKeyMap.remove(key);
578 }
579
580 // Update tunnel type map
581 typeKeyMap.get(tunnel.type()).remove(tunnelId);
582 if (typeKeyMap.get(tunnel.type()).isEmpty()) {
583 typeKeyMap.remove(tunnel.type());
584 }
585 }
586 }
587 }
jccd8697232015-05-05 14:42:23 +0800588}