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