blob: 0e363d67e5c18ceb7ca6a045323cea7d110a2c50 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
tomea961ff2014-10-01 12:45:15 -070016package org.onlab.onos.store.trivial.impl;
tom4c6606f2014-09-07 11:11:21 -070017
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070018import com.google.common.base.Function;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070019import com.google.common.collect.FluentIterable;
tomeadbb462014-09-07 16:10:19 -070020import com.google.common.collect.HashMultimap;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070021import com.google.common.collect.SetMultimap;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070022
tom5bcc9462014-09-19 10:11:31 -070023import org.apache.felix.scr.annotations.Activate;
tom35c0dc32014-09-19 10:00:58 -070024import org.apache.felix.scr.annotations.Component;
tom5bcc9462014-09-19 10:11:31 -070025import org.apache.felix.scr.annotations.Deactivate;
tom35c0dc32014-09-19 10:00:58 -070026import org.apache.felix.scr.annotations.Service;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070027import org.onlab.onos.net.AnnotationsUtil;
tomeadbb462014-09-07 16:10:19 -070028import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070029import org.onlab.onos.net.DefaultAnnotations;
tomeadbb462014-09-07 16:10:19 -070030import org.onlab.onos.net.DefaultLink;
31import org.onlab.onos.net.DeviceId;
32import org.onlab.onos.net.Link;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070033import org.onlab.onos.net.SparseAnnotations;
34import org.onlab.onos.net.Link.Type;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070035import org.onlab.onos.net.LinkKey;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070036import org.onlab.onos.net.link.DefaultLinkDescription;
tomeadbb462014-09-07 16:10:19 -070037import org.onlab.onos.net.link.LinkDescription;
38import org.onlab.onos.net.link.LinkEvent;
tom35c0dc32014-09-19 10:00:58 -070039import org.onlab.onos.net.link.LinkStore;
tomf80c9722014-09-24 14:49:18 -070040import org.onlab.onos.net.link.LinkStoreDelegate;
tomeadbb462014-09-07 16:10:19 -070041import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070042import org.onlab.onos.store.AbstractStore;
tom5bcc9462014-09-19 10:11:31 -070043import org.slf4j.Logger;
tomeadbb462014-09-07 16:10:19 -070044
45import java.util.Collections;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070046import java.util.HashMap;
tomeadbb462014-09-07 16:10:19 -070047import java.util.HashSet;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070048import java.util.Map;
tomeadbb462014-09-07 16:10:19 -070049import java.util.Set;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070050import java.util.Map.Entry;
tomeadbb462014-09-07 16:10:19 -070051import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070052import java.util.concurrent.ConcurrentMap;
tomeadbb462014-09-07 16:10:19 -070053
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -070054import static org.onlab.onos.net.DefaultAnnotations.union;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070055import static org.onlab.onos.net.DefaultAnnotations.merge;
tomd176fc42014-09-08 00:12:30 -070056import static org.onlab.onos.net.Link.Type.DIRECT;
57import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070058import static org.onlab.onos.net.LinkKey.linkKey;
tom35c0dc32014-09-19 10:00:58 -070059import static org.onlab.onos.net.link.LinkEvent.Type.*;
tom5bcc9462014-09-19 10:11:31 -070060import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070061import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
62import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070063import static com.google.common.base.Verify.verifyNotNull;
tomd176fc42014-09-08 00:12:30 -070064
tom4c6606f2014-09-07 11:11:21 -070065/**
tomcbff9392014-09-10 00:45:23 -070066 * Manages inventory of infrastructure links using trivial in-memory structures
tom4c6606f2014-09-07 11:11:21 -070067 * implementation.
68 */
tom35c0dc32014-09-19 10:00:58 -070069@Component(immediate = true)
70@Service
tomf80c9722014-09-24 14:49:18 -070071public class SimpleLinkStore
72 extends AbstractStore<LinkEvent, LinkStoreDelegate>
73 implements LinkStore {
tomeadbb462014-09-07 16:10:19 -070074
tom5bcc9462014-09-19 10:11:31 -070075 private final Logger log = getLogger(getClass());
76
tomeadbb462014-09-07 16:10:19 -070077 // Link inventory
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070078 private final ConcurrentMap<LinkKey, Map<ProviderId, LinkDescription>>
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070079 linkDescs = new ConcurrentHashMap<>();
80
81 // Link instance cache
82 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
tomeadbb462014-09-07 16:10:19 -070083
84 // Egress and ingress link sets
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070085 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
86 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
87
tomeadbb462014-09-07 16:10:19 -070088
tom5bcc9462014-09-19 10:11:31 -070089 @Activate
90 public void activate() {
91 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070096 linkDescs.clear();
97 links.clear();
98 srcLinks.clear();
99 dstLinks.clear();
tom5bcc9462014-09-19 10:11:31 -0700100 log.info("Stopped");
101 }
tomeadbb462014-09-07 16:10:19 -0700102
tom35c0dc32014-09-19 10:00:58 -0700103 @Override
104 public int getLinkCount() {
tomeadbb462014-09-07 16:10:19 -0700105 return links.size();
106 }
107
tom35c0dc32014-09-19 10:00:58 -0700108 @Override
109 public Iterable<Link> getLinks() {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700110 return Collections.unmodifiableCollection(links.values());
tomeadbb462014-09-07 16:10:19 -0700111 }
112
tom35c0dc32014-09-19 10:00:58 -0700113 @Override
114 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700115 // lock for iteration
116 synchronized (srcLinks) {
117 return FluentIterable.from(srcLinks.get(deviceId))
118 .transform(lookupLink())
119 .filter(notNull())
120 .toSet();
121 }
tomeadbb462014-09-07 16:10:19 -0700122 }
123
tom35c0dc32014-09-19 10:00:58 -0700124 @Override
125 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700126 // lock for iteration
127 synchronized (dstLinks) {
128 return FluentIterable.from(dstLinks.get(deviceId))
129 .transform(lookupLink())
130 .filter(notNull())
131 .toSet();
132 }
tomeadbb462014-09-07 16:10:19 -0700133 }
134
tom35c0dc32014-09-19 10:00:58 -0700135 @Override
136 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700137 return links.get(linkKey(src, dst));
tomd176fc42014-09-08 00:12:30 -0700138 }
139
tom35c0dc32014-09-19 10:00:58 -0700140 @Override
141 public Set<Link> getEgressLinks(ConnectPoint src) {
tomeadbb462014-09-07 16:10:19 -0700142 Set<Link> egress = new HashSet<>();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700143 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
144 if (linkKey.src().equals(src)) {
145 egress.add(links.get(linkKey));
tomeadbb462014-09-07 16:10:19 -0700146 }
147 }
148 return egress;
149 }
150
tom35c0dc32014-09-19 10:00:58 -0700151 @Override
152 public Set<Link> getIngressLinks(ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700153 Set<Link> ingress = new HashSet<>();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700154 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
155 if (linkKey.dst().equals(dst)) {
156 ingress.add(links.get(linkKey));
tomeadbb462014-09-07 16:10:19 -0700157 }
158 }
159 return ingress;
160 }
161
tom35c0dc32014-09-19 10:00:58 -0700162 @Override
tomeadbb462014-09-07 16:10:19 -0700163 public LinkEvent createOrUpdateLink(ProviderId providerId,
164 LinkDescription linkDescription) {
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700165 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700166
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700167 Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key);
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700168 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);
tomeadbb462014-09-07 16:10:19 -0700177 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700178 }
179
180 // Guarded by linkDescs value (=locking each Link)
181 private LinkDescription createOrUpdateLinkDescription(
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700182 Map<ProviderId, LinkDescription> descs,
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700183 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 HIGUCHIa85542b2014-10-21 19:29:49 -0700190 // 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 HIGUCHI885be1d2014-10-04 21:47:26 -0700197 SparseAnnotations merged = union(oldDesc.annotations(),
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700198 linkDescription.annotations());
199 newDesc = new DefaultLinkDescription(
200 linkDescription.src(),
201 linkDescription.dst(),
Yuta HIGUCHIa85542b2014-10-21 19:29:49 -0700202 newType, merged);
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700203 }
204 return descs.put(providerId, newDesc);
tomeadbb462014-09-07 16:10:19 -0700205 }
206
207 // Creates and stores the link and returns the appropriate event.
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700208 // Guarded by linkDescs value (=locking each Link)
209 private LinkEvent createLink(LinkKey key, Link newLink) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700210 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);
tomeadbb462014-09-07 16:10:19 -0700214 }
215
216 // Updates, if necessary the specified link and returns the appropriate event.
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700217 // Guarded by linkDescs value (=locking each Link)
218 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700219 if (newLink.providerId().isAncillary()) {
220 // TODO: revisit ancillary only Link handling
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700221 // 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);
tomd176fc42014-09-08 00:12:30 -0700233 }
tomeadbb462014-09-07 16:10:19 -0700234 return null;
235 }
236
tom35c0dc32014-09-19 10:00:58 -0700237 @Override
238 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700239 final LinkKey key = linkKey(src, dst);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700240 Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key);
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700241 synchronized (descs) {
242 Link link = links.remove(key);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700243 descs.clear();
tomd176fc42014-09-08 00:12:30 -0700244 if (link != null) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700245 srcLinks.remove(link.src().deviceId(), key);
246 dstLinks.remove(link.dst().deviceId(), key);
tomd176fc42014-09-08 00:12:30 -0700247 return new LinkEvent(LINK_REMOVED, link);
248 }
249 return null;
tomeadbb462014-09-07 16:10:19 -0700250 }
251 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700252
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700260 // Guarded by linkDescs value (=locking each Link)
261 private ProviderId getBaseProviderId(Map<ProviderId, LinkDescription> providerDescs) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700262
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700275 // 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 HIGUCHI39ede6a2014-10-03 15:23:33 -0700279
280 ConnectPoint src = base.src();
281 ConnectPoint dst = base.dst();
282 Type type = base.type();
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700283 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700284 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 HIGUCHIdd841b72014-10-16 13:46:09 -0700303 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 HIGUCHI39ede6a2014-10-03 15:23:33 -0700317 }
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700327 if (input == null) {
328 return null;
329 } else {
330 return links.get(input);
331 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700332 }
333 }
tom4c6606f2014-09-07 11:11:21 -0700334}