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