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; |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 25 | import java.util.Map; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 29 | import com.google.common.collect.Maps; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Activate; |
| 31 | import org.apache.felix.scr.annotations.Component; |
| 32 | import org.apache.felix.scr.annotations.Deactivate; |
| 33 | import org.apache.felix.scr.annotations.Reference; |
| 34 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 35 | import org.apache.felix.scr.annotations.Service; |
| 36 | import org.onlab.util.KryoNamespace; |
| 37 | import org.onosproject.cluster.ClusterService; |
| 38 | import org.onosproject.core.ApplicationId; |
| 39 | import org.onosproject.core.CoreService; |
| 40 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 41 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 42 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 43 | import org.onosproject.incubator.net.tunnel.Tunnel.State; |
Thomas Vachuska | bf916ea | 2015-05-20 18:24:34 -0700 | [diff] [blame] | 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; |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 52 | import org.onosproject.net.Annotations; |
| 53 | import org.onosproject.net.DefaultAnnotations; |
| 54 | import org.onosproject.net.SparseAnnotations; |
| 55 | import org.onosproject.net.provider.ProviderId; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 56 | import org.onosproject.store.AbstractStore; |
| 57 | import org.onosproject.store.app.GossipApplicationStore.InternalState; |
| 58 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 59 | import org.onosproject.store.serializers.KryoNamespaces; |
| 60 | import org.onosproject.store.service.EventuallyConsistentMap; |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 61 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 62 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 63 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 64 | import org.onosproject.store.service.StorageService; |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 65 | import org.onosproject.store.service.WallClockTimestamp; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
| 68 | import com.google.common.base.MoreObjects; |
| 69 | import com.google.common.collect.ImmutableSet; |
| 70 | |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 71 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; |
| 72 | import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; |
| 73 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 74 | /** |
| 75 | * Manages inventory of tunnel in distributed data store that uses optimistic |
| 76 | * replication and gossip based techniques. |
| 77 | */ |
| 78 | @Component(immediate = true) |
| 79 | @Service |
| 80 | public class DistributedTunnelStore |
| 81 | extends AbstractStore<TunnelEvent, TunnelStoreDelegate> |
| 82 | implements TunnelStore { |
| 83 | |
| 84 | private final Logger log = getLogger(getClass()); |
| 85 | |
| 86 | /** |
| 87 | * The topic used for obtaining globally unique ids. |
| 88 | */ |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 89 | private String tunnelOpTopic = "tunnel-ops-ids"; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected ClusterCommunicationService clusterCommunicator; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected ClusterService clusterService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected CoreService coreService; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected StorageService storageService; |
| 102 | |
| 103 | // tunnel identity as map key in the store. |
| 104 | private EventuallyConsistentMap<TunnelId, Tunnel> tunnelIdAsKeyStore; |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 105 | // maintains records that app subscribes tunnel. |
| 106 | private EventuallyConsistentMap<ApplicationId, Set<TunnelSubscription>> orderRelationship; |
| 107 | |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 108 | // tunnel name as map key. |
| 109 | private final Map<TunnelName, Set<TunnelId>> tunnelNameAsKeyMap = Maps.newConcurrentMap(); |
| 110 | // maintains all the tunnels between source and destination. |
| 111 | private final Map<TunnelKey, Set<TunnelId>> srcAndDstKeyMap = Maps.newConcurrentMap(); |
| 112 | // maintains all the tunnels by tunnel type. |
| 113 | private final Map<Tunnel.Type, Set<TunnelId>> typeKeyMap = Maps.newConcurrentMap(); |
| 114 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 115 | private IdGenerator idGenerator; |
| 116 | |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 117 | private EventuallyConsistentMapListener<TunnelId, Tunnel> tunnelUpdateListener = |
| 118 | new InternalTunnelChangeEventListener(); |
| 119 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 120 | @Activate |
| 121 | public void activate() { |
| 122 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 123 | .register(KryoNamespaces.API) |
| 124 | .register(MultiValuedTimestamp.class) |
| 125 | .register(InternalState.class); |
| 126 | tunnelIdAsKeyStore = storageService |
| 127 | .<TunnelId, Tunnel>eventuallyConsistentMapBuilder() |
| 128 | .withName("all_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 129 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 130 | orderRelationship = storageService |
| 131 | .<ApplicationId, Set<TunnelSubscription>>eventuallyConsistentMapBuilder() |
| 132 | .withName("type_tunnel").withSerializer(serializer) |
Madan Jampani | bcf1a48 | 2015-06-24 19:05:56 -0700 | [diff] [blame] | 133 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 134 | idGenerator = coreService.getIdGenerator(tunnelOpTopic); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 135 | tunnelIdAsKeyStore.addListener(tunnelUpdateListener); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 136 | log.info("Started"); |
| 137 | } |
| 138 | |
| 139 | @Deactivate |
| 140 | public void deactivate() { |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 141 | tunnelIdAsKeyStore.removeListener(tunnelUpdateListener); |
Madan Jampani | 632f16b | 2015-08-11 12:42:59 -0700 | [diff] [blame] | 142 | orderRelationship.destroy(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 143 | tunnelIdAsKeyStore.destroy(); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 144 | srcAndDstKeyMap.clear(); |
| 145 | typeKeyMap.clear(); |
| 146 | tunnelNameAsKeyMap.clear(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 147 | log.info("Stopped"); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public TunnelId createOrUpdateTunnel(Tunnel tunnel) { |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 152 | return handleCreateOrUpdateTunnel(tunnel, null); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public TunnelId createOrUpdateTunnel(Tunnel tunnel, State state) { |
| 157 | return handleCreateOrUpdateTunnel(tunnel, state); |
| 158 | } |
| 159 | |
| 160 | private TunnelId handleCreateOrUpdateTunnel(Tunnel tunnel, State state) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 161 | // tunnelIdAsKeyStore. |
Satish K | f1b38bc | 2015-11-22 17:13:51 +0530 | [diff] [blame] | 162 | if (tunnel.tunnelId() != null && !"".equals(tunnel.tunnelId().toString())) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 163 | Tunnel old = tunnelIdAsKeyStore.get(tunnel.tunnelId()); |
| 164 | if (old == null) { |
| 165 | log.info("This tunnel[" + tunnel.tunnelId() + "] is not available."); |
| 166 | return tunnel.tunnelId(); |
| 167 | } |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 168 | DefaultAnnotations oldAnno = (DefaultAnnotations) old.annotations(); |
| 169 | SparseAnnotations newAnno = (SparseAnnotations) tunnel.annotations(); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 170 | State newTunnelState = (state != null) ? state : old.state(); |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 171 | Tunnel newT = new DefaultTunnel(old.providerId(), old.src(), |
| 172 | old.dst(), old.type(), |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 173 | newTunnelState, old.groupId(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 174 | old.tunnelId(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 175 | old.tunnelName(), |
| 176 | old.path(), |
Satish K | d70e957 | 2016-04-28 13:58:51 +0530 | [diff] [blame^] | 177 | old.resource(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 178 | DefaultAnnotations.merge(oldAnno, newAnno)); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 179 | tunnelIdAsKeyStore.put(tunnel.tunnelId(), newT); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 180 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED, |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 181 | tunnel); |
| 182 | notifyDelegate(event); |
| 183 | return tunnel.tunnelId(); |
| 184 | } else { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 185 | TunnelId tunnelId = TunnelId.valueOf(String.valueOf(idGenerator.getNewId())); |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 186 | State tunnelState = (state != null) ? state : tunnel.state(); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 187 | Tunnel newT = new DefaultTunnel(tunnel.providerId(), tunnel.src(), |
| 188 | tunnel.dst(), tunnel.type(), |
Avantika-Huawei | 4c3c997 | 2016-04-05 14:51:46 +0530 | [diff] [blame] | 189 | tunnelState, tunnel.groupId(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 190 | tunnelId, |
| 191 | tunnel.tunnelName(), |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 192 | tunnel.path(), |
Satish K | d70e957 | 2016-04-28 13:58:51 +0530 | [diff] [blame^] | 193 | tunnel.resource(), |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 194 | tunnel.annotations()); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 195 | tunnelIdAsKeyStore.put(tunnelId, newT); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 196 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 197 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED, |
| 198 | tunnel); |
| 199 | notifyDelegate(event); |
| 200 | return tunnelId; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void deleteTunnel(TunnelId tunnelId) { |
| 206 | Tunnel deletedTunnel = tunnelIdAsKeyStore.get(tunnelId); |
| 207 | if (deletedTunnel == null) { |
| 208 | return; |
| 209 | } |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 210 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 211 | tunnelIdAsKeyStore.remove(tunnelId); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 212 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 213 | TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 214 | deletedTunnel); |
| 215 | notifyDelegate(event); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, |
| 220 | ProviderId producerName) { |
| 221 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 222 | Set<TunnelId> idSet = srcAndDstKeyMap.get(key); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 223 | if (idSet == null) { |
| 224 | return; |
| 225 | } |
| 226 | Tunnel deletedTunnel = null; |
| 227 | TunnelEvent event = null; |
| 228 | List<TunnelEvent> ls = new ArrayList<TunnelEvent>(); |
| 229 | for (TunnelId id : idSet) { |
| 230 | deletedTunnel = tunnelIdAsKeyStore.get(id); |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 231 | |
| 232 | if (producerName == null || (producerName != null |
| 233 | && producerName.equals(deletedTunnel.providerId()))) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 234 | tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId()); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 235 | |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 236 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 237 | deletedTunnel); |
| 238 | ls.add(event); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 239 | } |
| 240 | } |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 241 | |
| 242 | if (!ls.isEmpty()) { |
| 243 | notifyDelegate(ls); |
| 244 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void deleteTunnel(TunnelEndPoint src, TunnelEndPoint dst, Type type, |
| 249 | ProviderId producerName) { |
| 250 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 251 | Set<TunnelId> idSet = srcAndDstKeyMap.get(key); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 252 | if (idSet == null) { |
| 253 | return; |
| 254 | } |
| 255 | Tunnel deletedTunnel = null; |
| 256 | TunnelEvent event = null; |
| 257 | List<TunnelEvent> ls = new ArrayList<TunnelEvent>(); |
| 258 | for (TunnelId id : idSet) { |
| 259 | deletedTunnel = tunnelIdAsKeyStore.get(id); |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 260 | |
| 261 | if (type.equals(deletedTunnel.type()) && (producerName == null || (producerName != null |
| 262 | && producerName.equals(deletedTunnel.providerId())))) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 263 | tunnelIdAsKeyStore.remove(deletedTunnel.tunnelId()); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 264 | |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 265 | event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, |
| 266 | deletedTunnel); |
| 267 | ls.add(event); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 268 | } |
| 269 | } |
Satish K | fc1a631 | 2016-02-23 13:33:04 +0530 | [diff] [blame] | 270 | |
| 271 | if (!ls.isEmpty()) { |
| 272 | notifyDelegate(ls); |
| 273 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public Tunnel borrowTunnel(ApplicationId appId, TunnelId tunnelId, |
| 278 | Annotations... annotations) { |
| 279 | Set<TunnelSubscription> orderSet = orderRelationship.get(appId); |
| 280 | if (orderSet == null) { |
| 281 | orderSet = new HashSet<TunnelSubscription>(); |
| 282 | } |
| 283 | TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null, |
| 284 | annotations); |
| 285 | Tunnel result = tunnelIdAsKeyStore.get(tunnelId); |
Satish K | d972235 | 2015-11-20 23:19:52 +0530 | [diff] [blame] | 286 | if (result == null || Tunnel.State.INACTIVE.equals(result.state())) { |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 287 | return null; |
| 288 | } |
Satish K | d972235 | 2015-11-20 23:19:52 +0530 | [diff] [blame] | 289 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 290 | orderSet.add(order); |
| 291 | orderRelationship.put(appId, orderSet); |
| 292 | return result; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public Collection<Tunnel> borrowTunnel(ApplicationId appId, |
| 297 | TunnelEndPoint src, |
| 298 | TunnelEndPoint dst, |
| 299 | Annotations... annotations) { |
| 300 | Set<TunnelSubscription> orderSet = orderRelationship.get(appId); |
| 301 | if (orderSet == null) { |
| 302 | orderSet = new HashSet<TunnelSubscription>(); |
| 303 | } |
| 304 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations); |
| 305 | boolean isExist = orderSet.contains(order); |
| 306 | if (!isExist) { |
| 307 | orderSet.add(order); |
| 308 | } |
| 309 | orderRelationship.put(appId, orderSet); |
| 310 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 311 | Set<TunnelId> idSet = srcAndDstKeyMap.get(key); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 312 | if (idSet == null || idSet.size() == 0) { |
| 313 | return Collections.emptySet(); |
| 314 | } |
| 315 | Collection<Tunnel> tunnelSet = new HashSet<Tunnel>(); |
| 316 | for (TunnelId tunnelId : idSet) { |
| 317 | Tunnel result = tunnelIdAsKeyStore.get(tunnelId); |
| 318 | if (Tunnel.State.ACTIVE.equals(result.state())) { |
| 319 | tunnelSet.add(result); |
| 320 | } |
| 321 | } |
| 322 | return tunnelSet; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public Collection<Tunnel> borrowTunnel(ApplicationId appId, |
| 327 | TunnelEndPoint src, |
| 328 | TunnelEndPoint dst, Type type, |
| 329 | Annotations... annotations) { |
| 330 | Set<TunnelSubscription> orderSet = orderRelationship.get(appId); |
| 331 | if (orderSet == null) { |
| 332 | orderSet = new HashSet<TunnelSubscription>(); |
| 333 | } |
| 334 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations); |
| 335 | boolean isExist = orderSet.contains(order); |
| 336 | if (!isExist) { |
| 337 | orderSet.add(order); |
| 338 | } |
| 339 | orderRelationship.put(appId, orderSet); |
| 340 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 341 | Set<TunnelId> idSet = srcAndDstKeyMap.get(key); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 342 | if (idSet == null || idSet.size() == 0) { |
| 343 | return Collections.emptySet(); |
| 344 | } |
| 345 | Collection<Tunnel> tunnelSet = new HashSet<Tunnel>(); |
| 346 | for (TunnelId tunnelId : idSet) { |
| 347 | Tunnel result = tunnelIdAsKeyStore.get(tunnelId); |
| 348 | if (type.equals(result.type()) |
| 349 | && Tunnel.State.ACTIVE.equals(result.state())) { |
| 350 | tunnelSet.add(result); |
| 351 | } |
| 352 | } |
| 353 | return tunnelSet; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public Collection<Tunnel> borrowTunnel(ApplicationId appId, |
| 358 | TunnelName tunnelName, |
| 359 | Annotations... annotations) { |
| 360 | Set<TunnelSubscription> orderSet = orderRelationship.get(appId); |
| 361 | if (orderSet == null) { |
| 362 | orderSet = new HashSet<TunnelSubscription>(); |
| 363 | } |
| 364 | TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName, |
| 365 | annotations); |
| 366 | boolean isExist = orderSet.contains(order); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 367 | |
| 368 | Set<TunnelId> idSet = tunnelNameAsKeyMap.get(tunnelName); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 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 | } |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 379 | |
| 380 | if (!tunnelSet.isEmpty() && !isExist) { |
| 381 | orderSet.add(order); |
| 382 | orderRelationship.put(appId, orderSet); |
| 383 | } |
| 384 | |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 385 | return tunnelSet; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public boolean returnTunnel(ApplicationId appId, TunnelName tunnelName, |
| 390 | Annotations... annotations) { |
| 391 | TunnelSubscription order = new TunnelSubscription(appId, null, null, null, null, tunnelName, |
| 392 | annotations); |
| 393 | return deleteOrder(order); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public boolean returnTunnel(ApplicationId appId, TunnelId tunnelId, |
| 398 | Annotations... annotations) { |
| 399 | TunnelSubscription order = new TunnelSubscription(appId, null, null, tunnelId, null, null, |
| 400 | annotations); |
| 401 | return deleteOrder(order); |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src, |
| 406 | TunnelEndPoint dst, Type type, |
| 407 | Annotations... annotations) { |
| 408 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, type, null, annotations); |
| 409 | return deleteOrder(order); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public boolean returnTunnel(ApplicationId appId, TunnelEndPoint src, |
| 414 | TunnelEndPoint dst, Annotations... annotations) { |
| 415 | TunnelSubscription order = new TunnelSubscription(appId, src, dst, null, null, null, annotations); |
| 416 | return deleteOrder(order); |
| 417 | } |
| 418 | |
| 419 | private boolean deleteOrder(TunnelSubscription order) { |
| 420 | Set<TunnelSubscription> orderSet = orderRelationship.get(order.consumerId()); |
| 421 | if (orderSet == null) { |
| 422 | return true; |
| 423 | } |
| 424 | if (orderSet.contains(order)) { |
| 425 | orderSet.remove(order); |
| 426 | return true; |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public Tunnel queryTunnel(TunnelId tunnelId) { |
| 433 | return tunnelIdAsKeyStore.get(tunnelId); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public Collection<TunnelSubscription> queryTunnelSubscription(ApplicationId appId) { |
| 438 | return orderRelationship.get(appId) != null ? ImmutableSet.copyOf(orderRelationship |
| 439 | .get(appId)) : Collections.emptySet(); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public Collection<Tunnel> queryTunnel(Type type) { |
| 444 | Collection<Tunnel> result = new HashSet<Tunnel>(); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 445 | Set<TunnelId> tunnelIds = typeKeyMap.get(type); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 446 | if (tunnelIds == null) { |
| 447 | return Collections.emptySet(); |
| 448 | } |
| 449 | for (TunnelId id : tunnelIds) { |
| 450 | result.add(tunnelIdAsKeyStore.get(id)); |
| 451 | } |
| 452 | return result.size() == 0 ? Collections.emptySet() : ImmutableSet |
| 453 | .copyOf(result); |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) { |
| 458 | Collection<Tunnel> result = new HashSet<Tunnel>(); |
| 459 | TunnelKey key = TunnelKey.tunnelKey(src, dst); |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 460 | Set<TunnelId> tunnelIds = srcAndDstKeyMap.get(key); |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 461 | if (tunnelIds == null) { |
| 462 | return Collections.emptySet(); |
| 463 | } |
| 464 | for (TunnelId id : tunnelIds) { |
| 465 | result.add(tunnelIdAsKeyStore.get(id)); |
| 466 | } |
| 467 | return result.size() == 0 ? Collections.emptySet() : ImmutableSet |
| 468 | .copyOf(result); |
| 469 | } |
| 470 | |
| 471 | @Override |
samuel | 7a5691a | 2015-05-23 00:36:32 +0800 | [diff] [blame] | 472 | public Collection<Tunnel> queryAllTunnels() { |
| 473 | return tunnelIdAsKeyStore.values(); |
| 474 | } |
| 475 | |
| 476 | @Override |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 477 | public int tunnelCount() { |
| 478 | return tunnelIdAsKeyStore.size(); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Uses source TunnelPoint and destination TunnelPoint as map key. |
| 483 | */ |
| 484 | private static final class TunnelKey { |
| 485 | private final TunnelEndPoint src; |
| 486 | private final TunnelEndPoint dst; |
| 487 | |
| 488 | private TunnelKey(TunnelEndPoint src, TunnelEndPoint dst) { |
| 489 | this.src = src; |
| 490 | this.dst = dst; |
| 491 | |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * create a map key. |
| 496 | * |
| 497 | * @param src |
| 498 | * @param dst |
| 499 | * @return a key using source ip and destination ip |
| 500 | */ |
| 501 | static TunnelKey tunnelKey(TunnelEndPoint src, TunnelEndPoint dst) { |
| 502 | return new TunnelKey(src, dst); |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public int hashCode() { |
| 507 | return Objects.hash(src, dst); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public boolean equals(Object obj) { |
| 512 | if (this == obj) { |
| 513 | return true; |
| 514 | } |
| 515 | if (obj instanceof TunnelKey) { |
| 516 | final TunnelKey other = (TunnelKey) obj; |
| 517 | return Objects.equals(this.src, other.src) |
| 518 | && Objects.equals(this.dst, other.dst); |
| 519 | } |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public String toString() { |
| 525 | return MoreObjects.toStringHelper(getClass()).add("src", src) |
| 526 | .add("dst", dst).toString(); |
| 527 | } |
| 528 | } |
Satish K | d8adcef | 2016-04-26 16:31:14 +0530 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * Eventually consistent map listener for tunnel change event which updated the local map based on event. |
| 532 | */ |
| 533 | private class InternalTunnelChangeEventListener |
| 534 | implements EventuallyConsistentMapListener<TunnelId, Tunnel> { |
| 535 | @Override |
| 536 | public void event(EventuallyConsistentMapEvent<TunnelId, Tunnel> event) { |
| 537 | TunnelId tunnelId = event.key(); |
| 538 | Tunnel tunnel = event.value(); |
| 539 | |
| 540 | if (event.type() == PUT) { |
| 541 | |
| 542 | // Update tunnel name map |
| 543 | Set<TunnelId> tunnelNameSet = tunnelNameAsKeyMap.get(tunnel |
| 544 | .tunnelName()); |
| 545 | if (tunnelNameSet == null) { |
| 546 | tunnelNameSet = new HashSet<TunnelId>(); |
| 547 | } |
| 548 | tunnelNameSet.add(tunnelId); |
| 549 | tunnelNameAsKeyMap.put(tunnel.tunnelName(), tunnelNameSet); |
| 550 | |
| 551 | // Update tunnel source and destination map |
| 552 | TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst()); |
| 553 | Set<TunnelId> srcAndDstKeySet = srcAndDstKeyMap.get(key); |
| 554 | if (srcAndDstKeySet == null) { |
| 555 | srcAndDstKeySet = new HashSet<TunnelId>(); |
| 556 | } |
| 557 | srcAndDstKeySet.add(tunnelId); |
| 558 | srcAndDstKeyMap.put(key, srcAndDstKeySet); |
| 559 | |
| 560 | // Update tunnel type map |
| 561 | Set<TunnelId> typeKeySet = typeKeyMap.get(tunnel.type()); |
| 562 | if (typeKeySet == null) { |
| 563 | typeKeySet = new HashSet<TunnelId>(); |
| 564 | } |
| 565 | typeKeySet.add(tunnelId); |
| 566 | typeKeyMap.put(tunnel.type(), typeKeySet); |
| 567 | } else if (event.type() == REMOVE) { |
| 568 | |
| 569 | // Update tunnel name map |
| 570 | tunnelNameAsKeyMap.get(tunnel.tunnelName()).remove(tunnelId); |
| 571 | if (tunnelNameAsKeyMap.get(tunnel.tunnelName()).isEmpty()) { |
| 572 | tunnelNameAsKeyMap.remove(tunnel.tunnelName()); |
| 573 | } |
| 574 | |
| 575 | // Update tunnel source and destination map |
| 576 | TunnelKey key = TunnelKey.tunnelKey(tunnel.src(), tunnel.dst()); |
| 577 | srcAndDstKeyMap.get(key).remove(tunnelId); |
| 578 | if (srcAndDstKeyMap.get(key).isEmpty()) { |
| 579 | srcAndDstKeyMap.remove(key); |
| 580 | } |
| 581 | |
| 582 | // Update tunnel type map |
| 583 | typeKeyMap.get(tunnel.type()).remove(tunnelId); |
| 584 | if (typeKeyMap.get(tunnel.type()).isEmpty()) { |
| 585 | typeKeyMap.remove(tunnel.type()); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |
jcc | d869723 | 2015-05-05 14:42:23 +0800 | [diff] [blame] | 590 | } |