Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | 4f1a60c | 2014-10-28 13:39:07 -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 | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 18 | import com.google.common.base.Function; |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 19 | import com.google.common.collect.FluentIterable; |
Yuta HIGUCHI | 800fac6 | 2014-12-11 19:23:01 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multimaps; |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 21 | import com.google.common.collect.SetMultimap; |
Yuta HIGUCHI | 800fac6 | 2014-12-11 19:23:01 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.AnnotationsUtil; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.DefaultLink; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.Link.Type; |
| 31 | import org.onosproject.net.LinkKey; |
| 32 | import org.onosproject.net.SparseAnnotations; |
| 33 | import org.onosproject.net.link.DefaultLinkDescription; |
| 34 | import org.onosproject.net.link.LinkDescription; |
| 35 | import org.onosproject.net.link.LinkEvent; |
| 36 | import org.onosproject.net.link.LinkStore; |
| 37 | import org.onosproject.net.link.LinkStoreDelegate; |
| 38 | import org.onosproject.net.provider.ProviderId; |
| 39 | import org.onosproject.store.AbstractStore; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 44 | |
| 45 | import java.util.Collections; |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 46 | import java.util.HashMap; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 47 | import java.util.HashSet; |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 48 | import java.util.Map; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 49 | import java.util.Map.Entry; |
Thomas Vachuska | 29a6a78 | 2014-11-10 21:31:41 -0800 | [diff] [blame] | 50 | import java.util.Objects; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 51 | import java.util.Set; |
| 52 | import java.util.concurrent.ConcurrentHashMap; |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 53 | import java.util.concurrent.ConcurrentMap; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 54 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 55 | import static com.google.common.base.Predicates.notNull; |
| 56 | import static com.google.common.base.Verify.verifyNotNull; |
| 57 | import static com.google.common.collect.Multimaps.synchronizedSetMultimap; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 58 | import static org.onosproject.net.DefaultAnnotations.merge; |
| 59 | import static org.onosproject.net.DefaultAnnotations.union; |
| 60 | import static org.onosproject.net.Link.State.ACTIVE; |
| 61 | import static org.onosproject.net.Link.State.INACTIVE; |
| 62 | import static org.onosproject.net.Link.Type.DIRECT; |
| 63 | import static org.onosproject.net.Link.Type.INDIRECT; |
| 64 | import static org.onosproject.net.LinkKey.linkKey; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 66 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 67 | import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 68 | import static org.slf4j.LoggerFactory.getLogger; |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 69 | |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 70 | /** |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 71 | * Manages inventory of infrastructure links using trivial in-memory structures |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 72 | * implementation. |
| 73 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Component(immediate = true, service = LinkStore.class) |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 75 | public class SimpleLinkStore |
| 76 | extends AbstractStore<LinkEvent, LinkStoreDelegate> |
| 77 | implements LinkStore { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 78 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 81 | // Link inventory |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 82 | private final ConcurrentMap<LinkKey, Map<ProviderId, LinkDescription>> |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 83 | linkDescs = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 84 | |
| 85 | // Link instance cache |
| 86 | private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>(); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 87 | |
| 88 | // Egress and ingress link sets |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 89 | private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap(); |
| 90 | private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap(); |
| 91 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 92 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 93 | @Activate |
| 94 | public void activate() { |
| 95 | log.info("Started"); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 100 | linkDescs.clear(); |
| 101 | links.clear(); |
| 102 | srcLinks.clear(); |
| 103 | dstLinks.clear(); |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 104 | log.info("Stopped"); |
| 105 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 106 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public int getLinkCount() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 109 | return links.size(); |
| 110 | } |
| 111 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public Iterable<Link> getLinks() { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 114 | return Collections.unmodifiableCollection(links.values()); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 115 | } |
| 116 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 119 | // lock for iteration |
| 120 | synchronized (srcLinks) { |
| 121 | return FluentIterable.from(srcLinks.get(deviceId)) |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 122 | .transform(lookupLink()) |
| 123 | .filter(notNull()) |
| 124 | .toSet(); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 125 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 126 | } |
| 127 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 130 | // lock for iteration |
| 131 | synchronized (dstLinks) { |
| 132 | return FluentIterable.from(dstLinks.get(deviceId)) |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 133 | .transform(lookupLink()) |
| 134 | .filter(notNull()) |
| 135 | .toSet(); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 136 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 141 | return links.get(linkKey(src, dst)); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 142 | } |
| 143 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public Set<Link> getEgressLinks(ConnectPoint src) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 146 | Set<Link> egress = new HashSet<>(); |
HIGUCHI Yuta | 4d973eb | 2015-02-26 14:28:49 -0800 | [diff] [blame] | 147 | synchronized (srcLinks) { |
| 148 | for (LinkKey linkKey : srcLinks.get(src.deviceId())) { |
| 149 | if (linkKey.src().equals(src)) { |
| 150 | egress.add(links.get(linkKey)); |
| 151 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | return egress; |
| 155 | } |
| 156 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public Set<Link> getIngressLinks(ConnectPoint dst) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 159 | Set<Link> ingress = new HashSet<>(); |
HIGUCHI Yuta | 4d973eb | 2015-02-26 14:28:49 -0800 | [diff] [blame] | 160 | synchronized (dstLinks) { |
| 161 | for (LinkKey linkKey : dstLinks.get(dst.deviceId())) { |
| 162 | if (linkKey.dst().equals(dst)) { |
| 163 | ingress.add(links.get(linkKey)); |
| 164 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | return ingress; |
| 168 | } |
| 169 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 170 | @Override |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 171 | public LinkEvent createOrUpdateLink(ProviderId providerId, |
| 172 | LinkDescription linkDescription) { |
Yuta HIGUCHI | 990aecc | 2014-10-13 22:21:42 -0700 | [diff] [blame] | 173 | LinkKey key = linkKey(linkDescription.src(), linkDescription.dst()); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 174 | |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 175 | Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 176 | synchronized (descs) { |
| 177 | final Link oldLink = links.get(key); |
| 178 | // update description |
| 179 | createOrUpdateLinkDescription(descs, providerId, linkDescription); |
| 180 | final Link newLink = composeLink(descs); |
| 181 | if (oldLink == null) { |
| 182 | return createLink(key, newLink); |
| 183 | } |
| 184 | return updateLink(key, oldLink, newLink); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 185 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 188 | @Override |
| 189 | public LinkEvent removeOrDownLink(ConnectPoint src, ConnectPoint dst) { |
| 190 | Link link = getLink(src, dst); |
| 191 | if (link == null) { |
| 192 | return null; |
| 193 | } |
| 194 | |
Ray Milkey | 8521f81 | 2017-05-24 09:49:26 -0700 | [diff] [blame] | 195 | if (link.isExpected()) { |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 196 | return link.state() == INACTIVE ? null : |
| 197 | updateLink(linkKey(link.src(), link.dst()), link, |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 198 | DefaultLink.builder() |
| 199 | .providerId(link.providerId()) |
| 200 | .src(link.src()) |
| 201 | .dst(link.dst()) |
| 202 | .type(link.type()) |
| 203 | .state(INACTIVE) |
| 204 | .isExpected(link.isExpected()) |
| 205 | .annotations(link.annotations()).build()); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 206 | } |
| 207 | return removeLink(src, dst); |
| 208 | } |
| 209 | |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 210 | // Guarded by linkDescs value (=locking each Link) |
| 211 | private LinkDescription createOrUpdateLinkDescription( |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 212 | Map<ProviderId, LinkDescription> descs, |
| 213 | ProviderId providerId, |
| 214 | LinkDescription linkDescription) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 215 | |
| 216 | // merge existing attributes and merge |
| 217 | LinkDescription oldDesc = descs.get(providerId); |
| 218 | LinkDescription newDesc = linkDescription; |
| 219 | if (oldDesc != null) { |
Yuta HIGUCHI | a85542b | 2014-10-21 19:29:49 -0700 | [diff] [blame] | 220 | // we only allow transition from INDIRECT -> DIRECT |
| 221 | final Type newType; |
| 222 | if (oldDesc.type() == DIRECT) { |
| 223 | newType = DIRECT; |
| 224 | } else { |
| 225 | newType = linkDescription.type(); |
| 226 | } |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 227 | SparseAnnotations merged = union(oldDesc.annotations(), |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 228 | linkDescription.annotations()); |
| 229 | newDesc = new DefaultLinkDescription(linkDescription.src(), |
| 230 | linkDescription.dst(), |
| 231 | newType, merged); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 232 | } |
| 233 | return descs.put(providerId, newDesc); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Creates and stores the link and returns the appropriate event. |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 237 | // Guarded by linkDescs value (=locking each Link) |
| 238 | private LinkEvent createLink(LinkKey key, Link newLink) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 239 | links.put(key, newLink); |
| 240 | srcLinks.put(newLink.src().deviceId(), key); |
| 241 | dstLinks.put(newLink.dst().deviceId(), key); |
| 242 | return new LinkEvent(LINK_ADDED, newLink); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Updates, if necessary the specified link and returns the appropriate event. |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 246 | // Guarded by linkDescs value (=locking each Link) |
| 247 | private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) { |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 248 | if (oldLink.state() != newLink.state() || |
| 249 | (oldLink.type() == INDIRECT && newLink.type() == DIRECT) || |
| 250 | !AnnotationsUtil.isEqual(oldLink.annotations(), newLink.annotations())) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 251 | |
| 252 | links.put(key, newLink); |
Frank Wang | d8ab096 | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 253 | // strictly speaking following can be omitted |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 254 | srcLinks.put(oldLink.src().deviceId(), key); |
| 255 | dstLinks.put(oldLink.dst().deviceId(), key); |
| 256 | return new LinkEvent(LINK_UPDATED, newLink); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 257 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 258 | return null; |
| 259 | } |
| 260 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 261 | @Override |
| 262 | public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) { |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 263 | final LinkKey key = linkKey(src, dst); |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 264 | Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 265 | synchronized (descs) { |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 266 | Link link = links.remove(key); |
Yuta HIGUCHI | 0d6a5e6 | 2014-10-03 15:54:09 -0700 | [diff] [blame] | 267 | descs.clear(); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 268 | if (link != null) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 269 | srcLinks.remove(link.src().deviceId(), key); |
| 270 | dstLinks.remove(link.dst().deviceId(), key); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 271 | return new LinkEvent(LINK_REMOVED, link); |
| 272 | } |
| 273 | return null; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 276 | |
Yuta HIGUCHI | 800fac6 | 2014-12-11 19:23:01 -0800 | [diff] [blame] | 277 | /** |
| 278 | * Creates concurrent readable, synchronized HashMultimap. |
| 279 | * |
| 280 | * @return SetMultimap |
| 281 | */ |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 282 | private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() { |
Yuta HIGUCHI | 800fac6 | 2014-12-11 19:23:01 -0800 | [diff] [blame] | 283 | return synchronizedSetMultimap( |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 284 | Multimaps.newSetMultimap(new ConcurrentHashMap<>(), |
Yuta HIGUCHI | 800fac6 | 2014-12-11 19:23:01 -0800 | [diff] [blame] | 285 | () -> Sets.newConcurrentHashSet())); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * @return primary ProviderID, or randomly chosen one if none exists |
| 290 | */ |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 291 | // Guarded by linkDescs value (=locking each Link) |
| 292 | private ProviderId getBaseProviderId(Map<ProviderId, LinkDescription> providerDescs) { |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 293 | |
| 294 | ProviderId fallBackPrimary = null; |
| 295 | for (Entry<ProviderId, LinkDescription> e : providerDescs.entrySet()) { |
| 296 | if (!e.getKey().isAncillary()) { |
| 297 | return e.getKey(); |
| 298 | } else if (fallBackPrimary == null) { |
| 299 | // pick randomly as a fallback in case there is no primary |
| 300 | fallBackPrimary = e.getKey(); |
| 301 | } |
| 302 | } |
| 303 | return fallBackPrimary; |
| 304 | } |
| 305 | |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 306 | // Guarded by linkDescs value (=locking each Link) |
| 307 | private Link composeLink(Map<ProviderId, LinkDescription> descs) { |
| 308 | ProviderId primary = getBaseProviderId(descs); |
| 309 | LinkDescription base = descs.get(verifyNotNull(primary)); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 310 | |
| 311 | ConnectPoint src = base.src(); |
| 312 | ConnectPoint dst = base.dst(); |
| 313 | Type type = base.type(); |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 314 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 315 | annotations = merge(annotations, base.annotations()); |
| 316 | |
| 317 | for (Entry<ProviderId, LinkDescription> e : descs.entrySet()) { |
| 318 | if (primary.equals(e.getKey())) { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | // TODO: should keep track of Description timestamp |
| 323 | // and only merge conflicting keys when timestamp is newer |
| 324 | // Currently assuming there will never be a key conflict between |
| 325 | // providers |
| 326 | |
| 327 | // annotation merging. not so efficient, should revisit later |
| 328 | annotations = merge(annotations, e.getValue().annotations()); |
| 329 | } |
| 330 | |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 331 | boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true"); |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 332 | return DefaultLink.builder() |
| 333 | .providerId(primary) |
| 334 | .src(src) |
| 335 | .dst(dst) |
| 336 | .type(type) |
| 337 | .state(ACTIVE) |
| 338 | .isExpected(isDurable) |
| 339 | .annotations(annotations) |
| 340 | .build(); |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 343 | private Map<ProviderId, LinkDescription> getOrCreateLinkDescriptions(LinkKey key) { |
| 344 | Map<ProviderId, LinkDescription> r; |
| 345 | r = linkDescs.get(key); |
| 346 | if (r != null) { |
| 347 | return r; |
| 348 | } |
| 349 | r = new HashMap<>(); |
| 350 | final Map<ProviderId, LinkDescription> concurrentlyAdded; |
| 351 | concurrentlyAdded = linkDescs.putIfAbsent(key, r); |
| 352 | if (concurrentlyAdded == null) { |
| 353 | return r; |
| 354 | } else { |
| 355 | return concurrentlyAdded; |
| 356 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private final Function<LinkKey, Link> lookupLink = new LookupLink(); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 360 | |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 361 | private Function<LinkKey, Link> lookupLink() { |
| 362 | return lookupLink; |
| 363 | } |
| 364 | |
| 365 | private final class LookupLink implements Function<LinkKey, Link> { |
| 366 | @Override |
| 367 | public Link apply(LinkKey input) { |
Yuta HIGUCHI | dd841b7 | 2014-10-16 13:46:09 -0700 | [diff] [blame] | 368 | if (input == null) { |
| 369 | return null; |
| 370 | } else { |
| 371 | return links.get(input); |
| 372 | } |
Yuta HIGUCHI | 39ede6a | 2014-10-03 15:23:33 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 375 | } |