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