blob: 82f0140ad10a04e6ab8cff40e1e15abc842f2b32 [file] [log] [blame]
Thomas Vachuskabf916ea2015-05-20 18:24:34 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyfe0e0852018-01-18 11:14:05 -0800230 if (producerName == null || producerName.equals(deletedTunnel.providerId())) {
jccd8697232015-05-05 14:42:23 +0800231 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530232
Satish Kfc1a6312016-02-23 13:33:04 +0530233 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
234 deletedTunnel);
235 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800236 }
237 }
Satish Kfc1a6312016-02-23 13:33:04 +0530238
239 if (!ls.isEmpty()) {
240 notifyDelegate(ls);
241 }
jccd8697232015-05-05 14:42:23 +0800242 }
243
244 @Override
245 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
246 ProviderId producerName) {
247 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530248 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800249 if (idSet == null) {
250 return;
251 }
252 Tunnel deletedTunnel = null;
253 TunnelEvent event = null;
254 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
255 for (TunnelId id : idSet) {
256 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530257
Ray Milkeyfe0e0852018-01-18 11:14:05 -0800258 if (type.equals(deletedTunnel.type()) && (producerName == null ||
259 producerName.equals(deletedTunnel.providerId()))) {
jccd8697232015-05-05 14:42:23 +0800260 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
Satish Kd8adcef2016-04-26 16:31:14 +0530261
Satish Kfc1a6312016-02-23 13:33:04 +0530262 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
263 deletedTunnel);
264 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800265 }
266 }
Satish Kfc1a6312016-02-23 13:33:04 +0530267
268 if (!ls.isEmpty()) {
269 notifyDelegate(ls);
270 }
jccd8697232015-05-05 14:42:23 +0800271 }
272
273 @Override
274 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
275 Annotations... annotations) {
276 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
277 if (orderSet == null) {
278 orderSet = new HashSet<TunnelSubscription>();
279 }
280 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
281 annotations);
282 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
Satish Kd9722352015-11-20 23:19:52 +0530283 if (result == null || Tunnel.State.INACTIVE.equals(result.state())) {
jccd8697232015-05-05 14:42:23 +0800284 return null;
285 }
Satish Kd9722352015-11-20 23:19:52 +0530286
jccd8697232015-05-05 14:42:23 +0800287 orderSet.add(order);
288 orderRelationship.put(appId, orderSet);
289 return result;
290 }
291
292 @Override
293 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
294 TunnelEndPoint src,
295 TunnelEndPoint dst,
296 Annotations... annotations) {
297 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
298 if (orderSet == null) {
299 orderSet = new HashSet<TunnelSubscription>();
300 }
301 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
302 boolean isExist = orderSet.contains(order);
303 if (!isExist) {
304 orderSet.add(order);
305 }
306 orderRelationship.put(appId, orderSet);
307 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530308 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
Jon Hallcbd1b392017-01-18 20:15:44 -0800309 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800310 return Collections.emptySet();
311 }
312 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
313 for (TunnelId tunnelId : idSet) {
314 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
315 if (Tunnel.State.ACTIVE.equals(result.state())) {
316 tunnelSet.add(result);
317 }
318 }
319 return tunnelSet;
320 }
321
322 @Override
323 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
324 TunnelEndPoint src,
325 TunnelEndPoint dst, Type type,
326 Annotations... annotations) {
327 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
328 if (orderSet == null) {
329 orderSet = new HashSet<TunnelSubscription>();
330 }
331 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
332 boolean isExist = orderSet.contains(order);
333 if (!isExist) {
334 orderSet.add(order);
335 }
336 orderRelationship.put(appId, orderSet);
337 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530338 Set<TunnelId> idSet = srcAndDstKeyMap.get(key);
Jon Hallcbd1b392017-01-18 20:15:44 -0800339 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800340 return Collections.emptySet();
341 }
342 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
343 for (TunnelId tunnelId : idSet) {
344 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
345 if (type.equals(result.type())
346 && Tunnel.State.ACTIVE.equals(result.state())) {
347 tunnelSet.add(result);
348 }
349 }
350 return tunnelSet;
351 }
352
353 @Override
354 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
355 TunnelName tunnelName,
356 Annotations... annotations) {
357 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
358 if (orderSet == null) {
359 orderSet = new HashSet<TunnelSubscription>();
360 }
361 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
362 annotations);
363 boolean isExist = orderSet.contains(order);
Satish Kd8adcef2016-04-26 16:31:14 +0530364
365 Set<TunnelId> idSet = tunnelNameAsKeyMap.get(tunnelName);
Jon Hallcbd1b392017-01-18 20:15:44 -0800366 if (idSet == null || idSet.isEmpty()) {
jccd8697232015-05-05 14:42:23 +0800367 return Collections.emptySet();
368 }
369 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
370 for (TunnelId tunnelId : idSet) {
371 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
372 if (Tunnel.State.ACTIVE.equals(result.state())) {
373 tunnelSet.add(result);
374 }
375 }
Satish Kd8adcef2016-04-26 16:31:14 +0530376
377 if (!tunnelSet.isEmpty() && !isExist) {
378 orderSet.add(order);
379 orderRelationship.put(appId, orderSet);
380 }
381
jccd8697232015-05-05 14:42:23 +0800382 return tunnelSet;
383 }
384
385 @Override
386 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
387 Annotations... annotations) {
388 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
389 annotations);
390 return deleteOrder(order);
391 }
392
393 @Override
394 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
395 Annotations... annotations) {
396 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
397 annotations);
398 return deleteOrder(order);
399 }
400
401 @Override
402 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
403 TunnelEndPoint dst, Type type,
404 Annotations... annotations) {
405 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
406 return deleteOrder(order);
407 }
408
409 @Override
410 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
411 TunnelEndPoint dst, Annotations... annotations) {
412 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
413 return deleteOrder(order);
414 }
415
416 private boolean deleteOrder(TunnelSubscription order) {
417 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
418 if (orderSet == null) {
419 return true;
420 }
421 if (orderSet.contains(order)) {
422 orderSet.remove(order);
423 return true;
424 }
425 return false;
426 }
427
428 @Override
429 public Tunnel queryTunnel(TunnelId tunnelId) {
430 return tunnelIdAsKeyStore.get(tunnelId);
431 }
432
433 @Override
434 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
435 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
436 .get(appId)) : Collections.emptySet();
437 }
438
439 @Override
440 public Collection<Tunnel> queryTunnel(Type type) {
441 Collection<Tunnel> result = new HashSet<Tunnel>();
Satish Kd8adcef2016-04-26 16:31:14 +0530442 Set<TunnelId> tunnelIds = typeKeyMap.get(type);
jccd8697232015-05-05 14:42:23 +0800443 if (tunnelIds == null) {
444 return Collections.emptySet();
445 }
446 for (TunnelId id : tunnelIds) {
447 result.add(tunnelIdAsKeyStore.get(id));
448 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800449 return result.isEmpty() ? Collections.emptySet() : ImmutableSet
jccd8697232015-05-05 14:42:23 +0800450 .copyOf(result);
451 }
452
453 @Override
454 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
455 Collection<Tunnel> result = new HashSet<Tunnel>();
456 TunnelKey key = TunnelKey.tunnelKey(src, dst);
Satish Kd8adcef2016-04-26 16:31:14 +0530457 Set<TunnelId> tunnelIds = srcAndDstKeyMap.get(key);
jccd8697232015-05-05 14:42:23 +0800458 if (tunnelIds == null) {
459 return Collections.emptySet();
460 }
461 for (TunnelId id : tunnelIds) {
462 result.add(tunnelIdAsKeyStore.get(id));
463 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800464 return result.isEmpty() ? Collections.emptySet() : ImmutableSet
jccd8697232015-05-05 14:42:23 +0800465 .copyOf(result);
466 }
467
468 @Override
samuel7a5691a2015-05-23 00:36:32 +0800469 public Collection<Tunnel> queryAllTunnels() {
470 return tunnelIdAsKeyStore.values();
471 }
472
473 @Override
jccd8697232015-05-05 14:42:23 +0800474 public int tunnelCount() {
475 return tunnelIdAsKeyStore.size();
476 }
477
478 /**
479 * Uses source TunnelPoint and destination TunnelPoint as map key.
480 */
481 private static final class TunnelKey {
482 private final TunnelEndPoint src;
483 private final TunnelEndPoint dst;
484
485 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
486 this.src = src;
487 this.dst = dst;
488
489 }
490
491 /**
492 * create a map key.
493 *
494 * @param src
495 * @param dst
496 * @return a key using source ip and destination ip
497 */
498 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
499 return new TunnelKey(src, dst);
500 }
501
502 @Override
503 public int hashCode() {
504 return Objects.hash(src, dst);
505 }
506
507 @Override
508 public boolean equals(Object obj) {
509 if (this == obj) {
510 return true;
511 }
512 if (obj instanceof TunnelKey) {
513 final TunnelKey other = (TunnelKey) obj;
514 return Objects.equals(this.src, other.src)
515 && Objects.equals(this.dst, other.dst);
516 }
517 return false;
518 }
519
520 @Override
521 public String toString() {
522 return MoreObjects.toStringHelper(getClass()).add("src", src)
523 .add("dst", dst).toString();
524 }
525 }
Satish Kd8adcef2016-04-26 16:31:14 +0530526
527 /**
528 * Eventually consistent map listener for tunnel change event which updated the local map based on event.
529 */
530 private class InternalTunnelChangeEventListener
531 implements EventuallyConsistentMapListener<TunnelId, Tunnel> {
532 @Override
533 public void event(EventuallyConsistentMapEvent<TunnelId, Tunnel> event) {
534 TunnelId tunnelId = event.key();
535 Tunnel tunnel = event.value();
536
537 if (event.type() == PUT) {
538
539 // Update tunnel name map
540 Set<TunnelId> tunnelNameSet = tunnelNameAsKeyMap.get(tunnel
541 .tunnelName());
542 if (tunnelNameSet == null) {
543 tunnelNameSet = new HashSet<TunnelId>();
544 }
545 tunnelNameSet.add(tunnelId);
546 tunnelNameAsKeyMap.put(tunnel.tunnelName(), tunnelNameSet);
547
548 // Update tunnel source and destination map
549 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
550 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyMap.get(key);
551 if (srcAndDstKeySet == null) {
552 srcAndDstKeySet = new HashSet<TunnelId>();
553 }
554 srcAndDstKeySet.add(tunnelId);
555 srcAndDstKeyMap.put(key, srcAndDstKeySet);
556
557 // Update tunnel type map
558 Set<TunnelId> typeKeySet = typeKeyMap.get(tunnel.type());
559 if (typeKeySet == null) {
560 typeKeySet = new HashSet<TunnelId>();
561 }
562 typeKeySet.add(tunnelId);
563 typeKeyMap.put(tunnel.type(), typeKeySet);
564 } else if (event.type() == REMOVE) {
565
566 // Update tunnel name map
567 tunnelNameAsKeyMap.get(tunnel.tunnelName()).remove(tunnelId);
568 if (tunnelNameAsKeyMap.get(tunnel.tunnelName()).isEmpty()) {
569 tunnelNameAsKeyMap.remove(tunnel.tunnelName());
570 }
571
572 // Update tunnel source and destination map
573 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
574 srcAndDstKeyMap.get(key).remove(tunnelId);
575 if (srcAndDstKeyMap.get(key).isEmpty()) {
576 srcAndDstKeyMap.remove(key);
577 }
578
579 // Update tunnel type map
580 typeKeyMap.get(tunnel.type()).remove(tunnelId);
581 if (typeKeyMap.get(tunnel.type()).isEmpty()) {
582 typeKeyMap.remove(tunnel.type());
583 }
584 }
585 }
586 }
jccd8697232015-05-05 14:42:23 +0800587}