blob: 7f5074aa8b786bb2877236306cc05119aa18a7b5 [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;
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;
Avantika-Huawei4c3c9972016-04-05 14:51:46 +053041import org.onosproject.incubator.net.tunnel.Tunnel.State;
Thomas Vachuskabf916ea2015-05-20 18:24:34 -070042import org.onosproject.incubator.net.tunnel.Tunnel.Type;
43import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
44import org.onosproject.incubator.net.tunnel.TunnelEvent;
45import org.onosproject.incubator.net.tunnel.TunnelId;
46import org.onosproject.incubator.net.tunnel.TunnelName;
47import org.onosproject.incubator.net.tunnel.TunnelStore;
48import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate;
49import org.onosproject.incubator.net.tunnel.TunnelSubscription;
samuel7a5691a2015-05-23 00:36:32 +080050import org.onosproject.net.Annotations;
51import org.onosproject.net.DefaultAnnotations;
52import org.onosproject.net.SparseAnnotations;
53import org.onosproject.net.provider.ProviderId;
jccd8697232015-05-05 14:42:23 +080054import org.onosproject.store.AbstractStore;
55import org.onosproject.store.app.GossipApplicationStore.InternalState;
56import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
57import org.onosproject.store.serializers.KryoNamespaces;
58import org.onosproject.store.service.EventuallyConsistentMap;
59import org.onosproject.store.service.MultiValuedTimestamp;
60import org.onosproject.store.service.StorageService;
Madan Jampanibcf1a482015-06-24 19:05:56 -070061import org.onosproject.store.service.WallClockTimestamp;
jccd8697232015-05-05 14:42:23 +080062import org.slf4j.Logger;
63
64import com.google.common.base.MoreObjects;
65import com.google.common.collect.ImmutableSet;
66
67/**
68 * Manages inventory of tunnel in distributed data store that uses optimistic
69 * replication and gossip based techniques.
70 */
71@Component(immediate = true)
72@Service
73public class DistributedTunnelStore
74 extends AbstractStore<TunnelEvent, TunnelStoreDelegate>
75 implements TunnelStore {
76
77 private final Logger log = getLogger(getClass());
78
79 /**
80 * The topic used for obtaining globally unique ids.
81 */
Satish Kfc1a6312016-02-23 13:33:04 +053082 private String tunnelOpTopic = "tunnel-ops-ids";
jccd8697232015-05-05 14:42:23 +080083
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected ClusterCommunicationService clusterCommunicator;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected ClusterService clusterService;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected CoreService coreService;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected StorageService storageService;
95
96 // tunnel identity as map key in the store.
97 private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore;
98 // tunnel name as map key in the store.
99 private EventuallyConsistentMap<TunnelName, Set<TunnelId>> tunnelNameAsKeyStore;
100 // maintains all the tunnels between source and destination.
101 private EventuallyConsistentMap<TunnelKey, Set<TunnelId>> srcAndDstKeyStore;
102 // maintains all the tunnels by tunnel type.
103 private EventuallyConsistentMap<Tunnel.Type, Set<TunnelId>> typeKeyStore;
104 // maintains records that app subscribes tunnel.
105 private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship;
106
107 private IdGenerator idGenerator;
108
109 @Activate
110 public void activate() {
111 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
112 .register(KryoNamespaces.API)
113 .register(MultiValuedTimestamp.class)
114 .register(InternalState.class);
115 tunnelIdAsKeyStore = storageService
116 .<TunnelId, Tunnel>eventuallyConsistentMapBuilder()
117 .withName("all_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700118 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
jccd8697232015-05-05 14:42:23 +0800119 tunnelNameAsKeyStore = storageService
120 .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder()
121 .withName("tunnel_name_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700122 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
jccd8697232015-05-05 14:42:23 +0800123 srcAndDstKeyStore = storageService
124 .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder()
125 .withName("src_dst_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700126 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
jccd8697232015-05-05 14:42:23 +0800127 typeKeyStore = storageService
128 .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder()
129 .withName("type_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700130 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
samuel7a5691a2015-05-23 00:36:32 +0800131 orderRelationship = storageService
132 .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder()
133 .withName("type_tunnel").withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700134 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Satish Kfc1a6312016-02-23 13:33:04 +0530135 idGenerator = coreService.getIdGenerator(tunnelOpTopic);
jccd8697232015-05-05 14:42:23 +0800136 log.info("Started");
137 }
138
139 @Deactivate
140 public void deactivate() {
Madan Jampani632f16b2015-08-11 12:42:59 -0700141 orderRelationship.destroy();
jccd8697232015-05-05 14:42:23 +0800142 tunnelIdAsKeyStore.destroy();
143 srcAndDstKeyStore.destroy();
144 typeKeyStore.destroy();
145 tunnelNameAsKeyStore.destroy();
146 log.info("Stopped");
147 }
148
149 @Override
150 public TunnelId createOrUpdateTunnel(Tunnel tunnel) {
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530151 return handleCreateOrUpdateTunnel(tunnel, null);
152 }
153
154 @Override
155 public TunnelId createOrUpdateTunnel(Tunnel tunnel, State state) {
156 return handleCreateOrUpdateTunnel(tunnel, state);
157 }
158
159 private TunnelId handleCreateOrUpdateTunnel(Tunnel tunnel, State state) {
jccd8697232015-05-05 14:42:23 +0800160 // tunnelIdAsKeyStore.
Satish Kf1b38bc2015-11-22 17:13:51 +0530161 if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId().toString())) {
jccd8697232015-05-05 14:42:23 +0800162 Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId());
163 if (old == null) {
164 log.info("This tunnel[" + tunnel.tunnelId() + "] is not available.");
165 return tunnel.tunnelId();
166 }
samuel7a5691a2015-05-23 00:36:32 +0800167 DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations();
168 SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations();
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530169 State newTunnelState = (state != null) ? state : old.state();
samuel7a5691a2015-05-23 00:36:32 +0800170 Tunnel newT = new DefaultTunnel(old.providerId(), old.src(),
171 old.dst(), old.type(),
Avantika-Huawei4c3c9972016-04-05 14:51:46 +0530172 newTunnelState, old.groupId(),
jccd8697232015-05-05 14:42:23 +0800173 old.tunnelId(),
samuel7a5691a2015-05-23 00:36:32 +0800174 old.tunnelName(),
175 old.path(),
176 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 {
183 TunnelId tunnelId = TunnelId.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(),
jccd8697232015-05-05 14:42:23 +0800191 tunnel.annotations());
192 TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst());
193 tunnelIdAsKeyStore.put(tunnelId, newT);
194 Set<TunnelId> tunnelnameSet = tunnelNameAsKeyStore.get(tunnel
195 .tunnelName());
196 if (tunnelnameSet == null) {
197 tunnelnameSet = new HashSet<TunnelId>();
198 }
199 tunnelnameSet.add(tunnelId);
200 tunnelNameAsKeyStore.put(tunnel
201 .tunnelName(), tunnelnameSet);
202 Set<TunnelId> srcAndDstKeySet = srcAndDstKeyStore.get(key);
203 if (srcAndDstKeySet == null) {
204 srcAndDstKeySet = new HashSet<TunnelId>();
205 }
206 srcAndDstKeySet.add(tunnelId);
207 srcAndDstKeyStore.put(key, srcAndDstKeySet);
208 Set<TunnelId> typeKeySet = typeKeyStore.get(tunnel.type());
209 if (typeKeySet == null) {
210 typeKeySet = new HashSet<TunnelId>();
211 }
212 typeKeySet.add(tunnelId);
213 typeKeyStore.put(tunnel.type(), typeKeySet);
214 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED,
215 tunnel);
216 notifyDelegate(event);
217 return tunnelId;
218 }
219 }
220
221 @Override
222 public void deleteTunnel(TunnelId tunnelId) {
223 Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId);
224 if (deletedTunnel == null) {
225 return;
226 }
227 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()).remove(tunnelId);
228 tunnelIdAsKeyStore.remove(tunnelId);
229 TunnelKey key = new TunnelKey(deletedTunnel.src(), deletedTunnel.dst());
230 srcAndDstKeyStore.get(key).remove(tunnelId);
231 typeKeyStore.get(deletedTunnel.type()).remove(tunnelId);
232 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
233 deletedTunnel);
234 notifyDelegate(event);
235 }
236
237 @Override
238 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst,
239 ProviderId producerName) {
240 TunnelKey key = TunnelKey.tunnelKey(src, dst);
241 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
242 if (idSet == null) {
243 return;
244 }
245 Tunnel deletedTunnel = null;
246 TunnelEvent event = null;
247 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
248 for (TunnelId id : idSet) {
249 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530250
251 if (producerName == null || (producerName != null
252 && producerName.equals(deletedTunnel.providerId()))) {
jccd8697232015-05-05 14:42:23 +0800253 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
254 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
255 .remove(deletedTunnel.tunnelId());
256 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
257 typeKeyStore.get(deletedTunnel.type())
258 .remove(deletedTunnel.tunnelId());
Satish Kfc1a6312016-02-23 13:33:04 +0530259 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
260 deletedTunnel);
261 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800262 }
263 }
Satish Kfc1a6312016-02-23 13:33:04 +0530264
265 if (!ls.isEmpty()) {
266 notifyDelegate(ls);
267 }
jccd8697232015-05-05 14:42:23 +0800268 }
269
270 @Override
271 public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type,
272 ProviderId producerName) {
273 TunnelKey key = TunnelKey.tunnelKey(src, dst);
274 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
275 if (idSet == null) {
276 return;
277 }
278 Tunnel deletedTunnel = null;
279 TunnelEvent event = null;
280 List<TunnelEvent> ls = new ArrayList<TunnelEvent>();
281 for (TunnelId id : idSet) {
282 deletedTunnel = tunnelIdAsKeyStore.get(id);
Satish Kfc1a6312016-02-23 13:33:04 +0530283
284 if (type.equals(deletedTunnel.type()) && (producerName == null || (producerName != null
285 && producerName.equals(deletedTunnel.providerId())))) {
jccd8697232015-05-05 14:42:23 +0800286 tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId());
287 tunnelNameAsKeyStore.get(deletedTunnel.tunnelName())
288 .remove(deletedTunnel.tunnelId());
289 srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId());
290 typeKeyStore.get(deletedTunnel.type())
291 .remove(deletedTunnel.tunnelId());
Satish Kfc1a6312016-02-23 13:33:04 +0530292 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED,
293 deletedTunnel);
294 ls.add(event);
jccd8697232015-05-05 14:42:23 +0800295 }
296 }
Satish Kfc1a6312016-02-23 13:33:04 +0530297
298 if (!ls.isEmpty()) {
299 notifyDelegate(ls);
300 }
jccd8697232015-05-05 14:42:23 +0800301 }
302
303 @Override
304 public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId,
305 Annotations... annotations) {
306 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
307 if (orderSet == null) {
308 orderSet = new HashSet<TunnelSubscription>();
309 }
310 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
311 annotations);
312 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
Satish Kd9722352015-11-20 23:19:52 +0530313 if (result == null || Tunnel.State.INACTIVE.equals(result.state())) {
jccd8697232015-05-05 14:42:23 +0800314 return null;
315 }
Satish Kd9722352015-11-20 23:19:52 +0530316
jccd8697232015-05-05 14:42:23 +0800317 orderSet.add(order);
318 orderRelationship.put(appId, orderSet);
319 return result;
320 }
321
322 @Override
323 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
324 TunnelEndPoint src,
325 TunnelEndPoint dst,
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, null, 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);
338 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
339 if (idSet == null || idSet.size() == 0) {
340 return Collections.emptySet();
341 }
342 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
343 for (TunnelId tunnelId : idSet) {
344 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
345 if (Tunnel.State.ACTIVE.equals(result.state())) {
346 tunnelSet.add(result);
347 }
348 }
349 return tunnelSet;
350 }
351
352 @Override
353 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
354 TunnelEndPoint src,
355 TunnelEndPoint dst, Type type,
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, src, dst, null, type, null, annotations);
362 boolean isExist = orderSet.contains(order);
363 if (!isExist) {
364 orderSet.add(order);
365 }
366 orderRelationship.put(appId, orderSet);
367 TunnelKey key = TunnelKey.tunnelKey(src, dst);
368 Set<TunnelId> idSet = srcAndDstKeyStore.get(key);
369 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 (type.equals(result.type())
376 && Tunnel.State.ACTIVE.equals(result.state())) {
377 tunnelSet.add(result);
378 }
379 }
380 return tunnelSet;
381 }
382
383 @Override
384 public Collection<Tunnel> borrowTunnel(ApplicationId appId,
385 TunnelName tunnelName,
386 Annotations... annotations) {
387 Set<TunnelSubscription> orderSet = orderRelationship.get(appId);
388 if (orderSet == null) {
389 orderSet = new HashSet<TunnelSubscription>();
390 }
391 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
392 annotations);
393 boolean isExist = orderSet.contains(order);
394 if (!isExist) {
395 orderSet.add(order);
396 }
397 orderRelationship.put(appId, orderSet);
398 Set<TunnelId> idSet = tunnelNameAsKeyStore.get(tunnelName);
399 if (idSet == null || idSet.size() == 0) {
400 return Collections.emptySet();
401 }
402 Collection<Tunnel> tunnelSet = new HashSet<Tunnel>();
403 for (TunnelId tunnelId : idSet) {
404 Tunnel result = tunnelIdAsKeyStore.get(tunnelId);
405 if (Tunnel.State.ACTIVE.equals(result.state())) {
406 tunnelSet.add(result);
407 }
408 }
409 return tunnelSet;
410 }
411
412 @Override
413 public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName,
414 Annotations... annotations) {
415 TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName,
416 annotations);
417 return deleteOrder(order);
418 }
419
420 @Override
421 public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId,
422 Annotations... annotations) {
423 TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null,
424 annotations);
425 return deleteOrder(order);
426 }
427
428 @Override
429 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
430 TunnelEndPoint dst, Type type,
431 Annotations... annotations) {
432 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations);
433 return deleteOrder(order);
434 }
435
436 @Override
437 public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src,
438 TunnelEndPoint dst, Annotations... annotations) {
439 TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations);
440 return deleteOrder(order);
441 }
442
443 private boolean deleteOrder(TunnelSubscription order) {
444 Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId());
445 if (orderSet == null) {
446 return true;
447 }
448 if (orderSet.contains(order)) {
449 orderSet.remove(order);
450 return true;
451 }
452 return false;
453 }
454
455 @Override
456 public Tunnel queryTunnel(TunnelId tunnelId) {
457 return tunnelIdAsKeyStore.get(tunnelId);
458 }
459
460 @Override
461 public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) {
462 return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship
463 .get(appId)) : Collections.emptySet();
464 }
465
466 @Override
467 public Collection<Tunnel> queryTunnel(Type type) {
468 Collection<Tunnel> result = new HashSet<Tunnel>();
469 Set<TunnelId> tunnelIds = typeKeyStore.get(type);
470 if (tunnelIds == null) {
471 return Collections.emptySet();
472 }
473 for (TunnelId id : tunnelIds) {
474 result.add(tunnelIdAsKeyStore.get(id));
475 }
476 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
477 .copyOf(result);
478 }
479
480 @Override
481 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
482 Collection<Tunnel> result = new HashSet<Tunnel>();
483 TunnelKey key = TunnelKey.tunnelKey(src, dst);
484 Set<TunnelId> tunnelIds = srcAndDstKeyStore.get(key);
485 if (tunnelIds == null) {
486 return Collections.emptySet();
487 }
488 for (TunnelId id : tunnelIds) {
489 result.add(tunnelIdAsKeyStore.get(id));
490 }
491 return result.size() == 0 ? Collections.emptySet() : ImmutableSet
492 .copyOf(result);
493 }
494
495 @Override
samuel7a5691a2015-05-23 00:36:32 +0800496 public Collection<Tunnel> queryAllTunnels() {
497 return tunnelIdAsKeyStore.values();
498 }
499
500 @Override
jccd8697232015-05-05 14:42:23 +0800501 public int tunnelCount() {
502 return tunnelIdAsKeyStore.size();
503 }
504
505 /**
506 * Uses source TunnelPoint and destination TunnelPoint as map key.
507 */
508 private static final class TunnelKey {
509 private final TunnelEndPoint src;
510 private final TunnelEndPoint dst;
511
512 private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
513 this.src = src;
514 this.dst = dst;
515
516 }
517
518 /**
519 * create a map key.
520 *
521 * @param src
522 * @param dst
523 * @return a key using source ip and destination ip
524 */
525 static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) {
526 return new TunnelKey(src, dst);
527 }
528
529 @Override
530 public int hashCode() {
531 return Objects.hash(src, dst);
532 }
533
534 @Override
535 public boolean equals(Object obj) {
536 if (this == obj) {
537 return true;
538 }
539 if (obj instanceof TunnelKey) {
540 final TunnelKey other = (TunnelKey) obj;
541 return Objects.equals(this.src, other.src)
542 && Objects.equals(this.dst, other.dst);
543 }
544 return false;
545 }
546
547 @Override
548 public String toString() {
549 return MoreObjects.toStringHelper(getClass()).add("src", src)
550 .add("dst", dst).toString();
551 }
552 }
jccd8697232015-05-05 14:42:23 +0800553}