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