Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.store.tunnel.impl; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 18 | |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.apache.felix.scr.annotations.Service; |
| 35 | import org.onlab.util.KryoNamespace; |
| 36 | import org.onosproject.cluster.ClusterService; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.core.IdGenerator; |
| 40 | import org.onosproject.net.Annotations; |
| 41 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame^] | 42 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 43 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 44 | import org.onosproject.incubator.net.tunnel.Tunnel.Type; |
| 45 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 46 | import org.onosproject.incubator.net.tunnel.TunnelEvent; |
| 47 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 48 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 49 | import org.onosproject.incubator.net.tunnel.TunnelStore; |
| 50 | import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate; |
| 51 | import org.onosproject.incubator.net.tunnel.TunnelSubscription; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 52 | import org.onosproject.store.AbstractStore; |
| 53 | import org.onosproject.store.app.GossipApplicationStore.InternalState; |
| 54 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 55 | import org.onosproject.store.serializers.KryoNamespaces; |
| 56 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 57 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 58 | import org.onosproject.store.service.StorageService; |
| 59 | import org.onosproject.store.service.WallclockClockManager; |
| 60 | import org.slf4j.Logger; |
| 61 | |
| 62 | import com.google.common.base.MoreObjects; |
| 63 | import com.google.common.collect.ImmutableSet; |
| 64 | |
| 65 | /** |
| 66 | * Manages inventory of tunnel in distributed data store that uses optimistic |
| 67 | * replication and gossip based techniques. |
| 68 | */ |
| 69 | @Component(immediate = true) |
| 70 | @Service |
| 71 | public class DistributedTunnelStore |
| 72 | extends AbstractStore<TunnelEvent, TunnelStoreDelegate> |
| 73 | implements TunnelStore { |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
| 77 | /** |
| 78 | * The topic used for obtaining globally unique ids. |
| 79 | */ |
| 80 | private String runnelOpTopoic = "tunnel-ops-ids"; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected ClusterCommunicationService clusterCommunicator; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected ClusterService clusterService; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected CoreService coreService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected StorageService storageService; |
| 93 | |
| 94 | // tunnel identity as map key in the store. |
| 95 | private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore; |
| 96 | // tunnel name as map key in the store. |
| 97 | private EventuallyConsistentMap<TunnelName, Set<TunnelId>> tunnelNameAsKeyStore; |
| 98 | // maintains all the tunnels between source and destination. |
| 99 | private EventuallyConsistentMap<TunnelKey, Set<TunnelId>> srcAndDstKeyStore; |
| 100 | // maintains all the tunnels by tunnel type. |
| 101 | private EventuallyConsistentMap<Tunnel.Type, Set<TunnelId>> typeKeyStore; |
| 102 | // maintains records that app subscribes tunnel. |
| 103 | private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship; |
| 104 | |
| 105 | private IdGenerator idGenerator; |
| 106 | |
| 107 | @Activate |
| 108 | public void activate() { |
| 109 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 110 | .register(KryoNamespaces.API) |
| 111 | .register(MultiValuedTimestamp.class) |
| 112 | .register(InternalState.class); |
| 113 | tunnelIdAsKeyStore = storageService |
| 114 | .<TunnelId, Tunnel>eventuallyConsistentMapBuilder() |
| 115 | .withName("all_tunnel").withSerializer(serializer) |
| 116 | .withClockService(new WallclockClockManager<>()).build(); |
| 117 | tunnelNameAsKeyStore = storageService |
| 118 | .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 119 | .withName("tunnel_name_tunnel").withSerializer(serializer) |
| 120 | .withClockService(new WallclockClockManager<>()).build(); |
| 121 | srcAndDstKeyStore = storageService |
| 122 | .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 123 | .withName("src_dst_tunnel").withSerializer(serializer) |
| 124 | .withClockService(new WallclockClockManager<>()).build(); |
| 125 | typeKeyStore = storageService |
| 126 | .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 127 | .withName("type_tunnel").withSerializer(serializer) |
| 128 | .withClockService(new WallclockClockManager<>()).build(); |
| 129 | idGenerator = coreService.getIdGenerator(runnelOpTopoic); |
| 130 | log.info("Started"); |
| 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | public void deactivate() { |
| 135 | tunnelIdAsKeyStore.destroy(); |
| 136 | srcAndDstKeyStore.destroy(); |
| 137 | typeKeyStore.destroy(); |
| 138 | tunnelNameAsKeyStore.destroy(); |
| 139 | log.info("Stopped"); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public TunnelId createOrUpdateTunnel(Tunnel tunnel) { |
| 144 | // tunnelIdAsKeyStore. |
| 145 | if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId())) { |
| 146 | Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId()); |
| 147 | if (old == null) { |
| 148 | log.info("This tunnel[" + tunnel.tunnelId() + "] is not available."); |
| 149 | return tunnel.tunnelId(); |
| 150 | } |
| 151 | Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(), |
| 152 | tunnel.dst(), tunnel.type(), |
| 153 | tunnel.state(), tunnel.groupId(), |
| 154 | old.tunnelId(), |
| 155 | tunnel.tunnelName(), |
| 156 | tunnel.annotations()); |
| 157 | tunnelIdAsKeyStore.remove(tunnel.tunnelId()); |
| 158 | tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT); |
| 159 | TunnelEvent event = new TunnelEvent( |
| 160 | TunnelEvent.Type.TUNNEL_UPDATED, |
| 161 | tunnel); |
| 162 | notifyDelegate(event); |
| 163 | return tunnel.tunnelId(); |
| 164 | } else { |
| 165 | TunnelId tunnelId = TunnelId.valueOf(idGenerator.getNewId()); |
| 166 | Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(), |
| 167 | tunnel.dst(), tunnel.type(), |
| 168 | tunnel.state(), tunnel.groupId(), |
| 169 | tunnelId, |
| 170 | tunnel.tunnelName(), |
| 171 | tunnel.annotations()); |
| 172 | TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst()); |
| 173 | tunnelIdAsKeyStore.put(tunnelId, newT); |
| 174 | Set<TunnelId> tunnelnameSet = tunnelNameAsKeyStore.get(tunnel |
| 175 | .tunnelName()); |
| 176 | if (tunnelnameSet == null) { |
| 177 | tunnelnameSet = new HashSet<TunnelId>(); |
| 178 | } |
| 179 | tunnelnameSet.add(tunnelId); |
| 180 | tunnelNameAsKeyStore.put(tunnel |
| 181 | .tunnelName(), tunnelnameSet); |
| 182 | Set<TunnelId> srcAndDstKeySet = srcAndDstKeyStore.get(key); |
| 183 | if (srcAndDstKeySet == null) { |
| 184 | srcAndDstKeySet = new HashSet<TunnelId>(); |
| 185 | } |
| 186 | srcAndDstKeySet.add(tunnelId); |
| 187 | srcAndDstKeyStore.put(key, srcAndDstKeySet); |
| 188 | Set<TunnelId> typeKeySet = typeKeyStore.get(tunnel.type()); |
| 189 | if (typeKeySet == null) { |
| 190 | typeKeySet = new HashSet<TunnelId>(); |
| 191 | } |
| 192 | typeKeySet.add(tunnelId); |
| 193 | typeKeyStore.put(tunnel.type(), typeKeySet); |
| 194 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED, |
| 195 | tunnel); |
| 196 | notifyDelegate(event); |
| 197 | return tunnelId; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void deleteTunnel(TunnelId tunnelId) { |
| 203 | Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId); |
| 204 | if (deletedTunnel == null) { |
| 205 | return; |
| 206 | } |
| 207 | tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()).remove(tunnelId); |
| 208 | tunnelIdAsKeyStore.remove(tunnelId); |
| 209 | TunnelKey key = new TunnelKey(deletedTunnel.src(), deletedTunnel.dst()); |
| 210 | srcAndDstKeyStore.get(key).remove(tunnelId); |
| 211 | typeKeyStore.get(deletedTunnel.type()).remove(tunnelId); |
| 212 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 213 | deletedTunnel); |
| 214 | notifyDelegate(event); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, |
| 219 | ProviderId producerName) { |
| 220 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
| 221 | Set<TunnelId> idSet = srcAndDstKeyStore.get(key); |
| 222 | if (idSet == null) { |
| 223 | return; |
| 224 | } |
| 225 | Tunnel deletedTunnel = null; |
| 226 | TunnelEvent event = null; |
| 227 | List<TunnelEvent> ls = new ArrayList<TunnelEvent>(); |
| 228 | for (TunnelId id : idSet) { |
| 229 | deletedTunnel = tunnelIdAsKeyStore.get(id); |
| 230 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 231 | deletedTunnel); |
| 232 | ls.add(event); |
| 233 | if (producerName.equals(deletedTunnel.providerId())) { |
| 234 | tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId()); |
| 235 | tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()) |
| 236 | .remove(deletedTunnel.tunnelId()); |
| 237 | srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId()); |
| 238 | typeKeyStore.get(deletedTunnel.type()) |
| 239 | .remove(deletedTunnel.tunnelId()); |
| 240 | } |
| 241 | } |
| 242 | notifyDelegate(ls); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type, |
| 247 | ProviderId producerName) { |
| 248 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
| 249 | Set<TunnelId> idSet = srcAndDstKeyStore.get(key); |
| 250 | 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); |
| 258 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 259 | deletedTunnel); |
| 260 | ls.add(event); |
| 261 | if (producerName.equals(deletedTunnel.providerId()) |
| 262 | && type.equals(deletedTunnel.type())) { |
| 263 | tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId()); |
| 264 | tunnelNameAsKeyStore.get(deletedTunnel.tunnelName()) |
| 265 | .remove(deletedTunnel.tunnelId()); |
| 266 | srcAndDstKeyStore.get(key).remove(deletedTunnel.tunnelId()); |
| 267 | typeKeyStore.get(deletedTunnel.type()) |
| 268 | .remove(deletedTunnel.tunnelId()); |
| 269 | } |
| 270 | } |
| 271 | notifyDelegate(ls); |
| 272 | } |
| 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); |
| 284 | if (result != null || Tunnel.State.INACTIVE.equals(result.state())) { |
| 285 | return null; |
| 286 | } |
| 287 | 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); |
| 308 | Set<TunnelId> idSet = srcAndDstKeyStore.get(key); |
| 309 | if (idSet == null || idSet.size() == 0) { |
| 310 | 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); |
| 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 (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); |
| 364 | if (!isExist) { |
| 365 | orderSet.add(order); |
| 366 | } |
| 367 | orderRelationship.put(appId, orderSet); |
| 368 | Set<TunnelId> idSet = tunnelNameAsKeyStore.get(tunnelName); |
| 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 (Tunnel.State.ACTIVE.equals(result.state())) { |
| 376 | tunnelSet.add(result); |
| 377 | } |
| 378 | } |
| 379 | return tunnelSet; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName, |
| 384 | Annotations... annotations) { |
| 385 | TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName, |
| 386 | annotations); |
| 387 | return deleteOrder(order); |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId, |
| 392 | Annotations... annotations) { |
| 393 | TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null, |
| 394 | annotations); |
| 395 | return deleteOrder(order); |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src, |
| 400 | TunnelEndPoint dst, Type type, |
| 401 | Annotations... annotations) { |
| 402 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations); |
| 403 | return deleteOrder(order); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src, |
| 408 | TunnelEndPoint dst, Annotations... annotations) { |
| 409 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations); |
| 410 | return deleteOrder(order); |
| 411 | } |
| 412 | |
| 413 | private boolean deleteOrder(TunnelSubscription order) { |
| 414 | Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId()); |
| 415 | if (orderSet == null) { |
| 416 | return true; |
| 417 | } |
| 418 | if (orderSet.contains(order)) { |
| 419 | orderSet.remove(order); |
| 420 | return true; |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public Tunnel queryTunnel(TunnelId tunnelId) { |
| 427 | return tunnelIdAsKeyStore.get(tunnelId); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) { |
| 432 | return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship |
| 433 | .get(appId)) : Collections.emptySet(); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public Collection<Tunnel> queryTunnel(Type type) { |
| 438 | Collection<Tunnel> result = new HashSet<Tunnel>(); |
| 439 | Set<TunnelId> tunnelIds = typeKeyStore.get(type); |
| 440 | if (tunnelIds == null) { |
| 441 | return Collections.emptySet(); |
| 442 | } |
| 443 | for (TunnelId id : tunnelIds) { |
| 444 | result.add(tunnelIdAsKeyStore.get(id)); |
| 445 | } |
| 446 | return result.size() == 0 ? Collections.emptySet() : ImmutableSet |
| 447 | .copyOf(result); |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) { |
| 452 | Collection<Tunnel> result = new HashSet<Tunnel>(); |
| 453 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
| 454 | Set<TunnelId> tunnelIds = srcAndDstKeyStore.get(key); |
| 455 | if (tunnelIds == null) { |
| 456 | return Collections.emptySet(); |
| 457 | } |
| 458 | for (TunnelId id : tunnelIds) { |
| 459 | result.add(tunnelIdAsKeyStore.get(id)); |
| 460 | } |
| 461 | return result.size() == 0 ? Collections.emptySet() : ImmutableSet |
| 462 | .copyOf(result); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public int tunnelCount() { |
| 467 | return tunnelIdAsKeyStore.size(); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Uses source TunnelPoint and destination TunnelPoint as map key. |
| 472 | */ |
| 473 | private static final class TunnelKey { |
| 474 | private final TunnelEndPoint src; |
| 475 | private final TunnelEndPoint dst; |
| 476 | |
| 477 | private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) { |
| 478 | this.src = src; |
| 479 | this.dst = dst; |
| 480 | |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * create a map key. |
| 485 | * |
| 486 | * @param src |
| 487 | * @param dst |
| 488 | * @return a key using source ip and destination ip |
| 489 | */ |
| 490 | static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) { |
| 491 | return new TunnelKey(src, dst); |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public int hashCode() { |
| 496 | return Objects.hash(src, dst); |
| 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public boolean equals(Object obj) { |
| 501 | if (this == obj) { |
| 502 | return true; |
| 503 | } |
| 504 | if (obj instanceof TunnelKey) { |
| 505 | final TunnelKey other = (TunnelKey) obj; |
| 506 | return Objects.equals(this.src, other.src) |
| 507 | && Objects.equals(this.dst, other.dst); |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public String toString() { |
| 514 | return MoreObjects.toStringHelper(getClass()).add("src", src) |
| 515 | .add("dst", dst).toString(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | } |