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