Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 16 | package org.onosproject.incubator.store.tunnel.impl; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Collection; |
| 22 | import java.util.Collections; |
| 23 | import java.util.HashSet; |
| 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.apache.felix.scr.annotations.Activate; |
| 29 | import org.apache.felix.scr.annotations.Component; |
| 30 | import org.apache.felix.scr.annotations.Deactivate; |
| 31 | import org.apache.felix.scr.annotations.Reference; |
| 32 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 33 | import org.apache.felix.scr.annotations.Service; |
| 34 | import org.onlab.util.KryoNamespace; |
| 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 39 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 40 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 41 | import org.onosproject.incubator.net.tunnel.Tunnel.State; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 42 | import org.onosproject.incubator.net.tunnel.Tunnel.Type; |
| 43 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 44 | import org.onosproject.incubator.net.tunnel.TunnelEvent; |
| 45 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 46 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 47 | import org.onosproject.incubator.net.tunnel.TunnelStore; |
| 48 | import org.onosproject.incubator.net.tunnel.TunnelStoreDelegate; |
| 49 | import org.onosproject.incubator.net.tunnel.TunnelSubscription; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 50 | import org.onosproject.net.Annotations; |
| 51 | import org.onosproject.net.DefaultAnnotations; |
| 52 | import org.onosproject.net.SparseAnnotations; |
| 53 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 54 | import org.onosproject.store.AbstractStore; |
| 55 | import org.onosproject.store.app.GossipApplicationStore.InternalState; |
| 56 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 57 | import org.onosproject.store.serializers.KryoNamespaces; |
| 58 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 59 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 60 | import org.onosproject.store.service.StorageService; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 61 | import org.onosproject.store.service.WallClockTimestamp; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
| 64 | import com.google.common.base.MoreObjects; |
| 65 | import 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 |
| 73 | public 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 K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 82 | private String tunnelOpTopic = "tunnel-ops-ids"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 83 | |
| 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 Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 118 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 119 | tunnelNameAsKeyStore = storageService |
| 120 | .<TunnelName, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 121 | .withName("tunnel_name_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 122 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 123 | srcAndDstKeyStore = storageService |
| 124 | .<TunnelKey, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 125 | .withName("src_dst_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 126 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 127 | typeKeyStore = storageService |
| 128 | .<Tunnel.Type, Set<TunnelId>>eventuallyConsistentMapBuilder() |
| 129 | .withName("type_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 130 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 131 | orderRelationship = storageService |
| 132 | .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder() |
| 133 | .withName("type_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 134 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 135 | idGenerator = coreService.getIdGenerator(tunnelOpTopic); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 136 | log.info("Started"); |
| 137 | } |
| 138 | |
| 139 | @Deactivate |
| 140 | public void deactivate() { |
Madan Jampani | 632f16b | 2015-08-11 12:42:59 -0700 | [diff] [blame] | 141 | orderRelationship.destroy(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 142 | 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-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 151 | 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) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 160 | // tunnelIdAsKeyStore. |
Satish K | f1b38bc | 2015-11-22 17:13:51 +0530 | [diff] [blame] | 161 | if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId().toString())) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 162 | 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 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 167 | DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations(); |
| 168 | SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations(); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 169 | State newTunnelState = (state != null) ? state : old.state(); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 170 | Tunnel newT = new DefaultTunnel(old.providerId(), old.src(), |
| 171 | old.dst(), old.type(), |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 172 | newTunnelState, old.groupId(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 173 | old.tunnelId(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 174 | old.tunnelName(), |
| 175 | old.path(), |
| 176 | DefaultAnnotations.merge(oldAnno, newAnno)); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 177 | tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 178 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED, |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 179 | tunnel); |
| 180 | notifyDelegate(event); |
| 181 | return tunnel.tunnelId(); |
| 182 | } else { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 183 | TunnelId tunnelId = TunnelId.valueOf(String.valueOf(idGenerator.getNewId())); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 184 | State tunnelState = (state != null) ? state : tunnel.state(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 185 | Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(), |
| 186 | tunnel.dst(), tunnel.type(), |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 187 | tunnelState, tunnel.groupId(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 188 | tunnelId, |
| 189 | tunnel.tunnelName(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 190 | tunnel.path(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 191 | 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 K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 250 | |
| 251 | if (producerName == null || (producerName != null |
| 252 | && producerName.equals(deletedTunnel.providerId()))) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 253 | 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 K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 259 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 260 | deletedTunnel); |
| 261 | ls.add(event); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 262 | } |
| 263 | } |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 264 | |
| 265 | if (!ls.isEmpty()) { |
| 266 | notifyDelegate(ls); |
| 267 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 268 | } |
| 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 K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 283 | |
| 284 | if (type.equals(deletedTunnel.type()) && (producerName == null || (producerName != null |
| 285 | && producerName.equals(deletedTunnel.providerId())))) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 286 | 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 K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 292 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 293 | deletedTunnel); |
| 294 | ls.add(event); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 295 | } |
| 296 | } |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 297 | |
| 298 | if (!ls.isEmpty()) { |
| 299 | notifyDelegate(ls); |
| 300 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 301 | } |
| 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 K | d972235 | 2015-11-20 23:19:52 +0530 | [diff] [blame] | 313 | if (result == null || Tunnel.State.INACTIVE.equals(result.state())) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 314 | return null; |
| 315 | } |
Satish K | d972235 | 2015-11-20 23:19:52 +0530 | [diff] [blame] | 316 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 317 | 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 |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 496 | public Collection<Tunnel> queryAllTunnels() { |
| 497 | return tunnelIdAsKeyStore.values(); |
| 498 | } |
| 499 | |
| 500 | @Override |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 501 | 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 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 553 | } |