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