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