blob: a9a09824c2c26204756f027cc7ece2951501ae49 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
tom4c6606f2014-09-07 11:11:21 -07002
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -07003import com.google.common.base.Function;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -07004import com.google.common.collect.FluentIterable;
tomeadbb462014-09-07 16:10:19 -07005import com.google.common.collect.HashMultimap;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -07006import com.google.common.collect.SetMultimap;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -07007
tom5bcc9462014-09-19 10:11:31 -07008import org.apache.felix.scr.annotations.Activate;
tom35c0dc32014-09-19 10:00:58 -07009import org.apache.felix.scr.annotations.Component;
tom5bcc9462014-09-19 10:11:31 -070010import org.apache.felix.scr.annotations.Deactivate;
tom35c0dc32014-09-19 10:00:58 -070011import org.apache.felix.scr.annotations.Service;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070012import org.onlab.onos.net.AnnotationsUtil;
tomeadbb462014-09-07 16:10:19 -070013import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070014import org.onlab.onos.net.DefaultAnnotations;
tomeadbb462014-09-07 16:10:19 -070015import org.onlab.onos.net.DefaultLink;
16import org.onlab.onos.net.DeviceId;
17import org.onlab.onos.net.Link;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070018import org.onlab.onos.net.SparseAnnotations;
19import org.onlab.onos.net.Link.Type;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070020import org.onlab.onos.net.LinkKey;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070021import org.onlab.onos.net.link.DefaultLinkDescription;
tomeadbb462014-09-07 16:10:19 -070022import org.onlab.onos.net.link.LinkDescription;
23import org.onlab.onos.net.link.LinkEvent;
tom35c0dc32014-09-19 10:00:58 -070024import org.onlab.onos.net.link.LinkStore;
tomf80c9722014-09-24 14:49:18 -070025import org.onlab.onos.net.link.LinkStoreDelegate;
tomeadbb462014-09-07 16:10:19 -070026import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070027import org.onlab.onos.store.AbstractStore;
tom5bcc9462014-09-19 10:11:31 -070028import org.slf4j.Logger;
tomeadbb462014-09-07 16:10:19 -070029
30import java.util.Collections;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070031import java.util.HashMap;
tomeadbb462014-09-07 16:10:19 -070032import java.util.HashSet;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070033import java.util.Map;
tomeadbb462014-09-07 16:10:19 -070034import java.util.Set;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070035import java.util.Map.Entry;
tomeadbb462014-09-07 16:10:19 -070036import java.util.concurrent.ConcurrentHashMap;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070037import java.util.concurrent.ConcurrentMap;
tomeadbb462014-09-07 16:10:19 -070038
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -070039import static org.onlab.onos.net.DefaultAnnotations.union;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070040import static org.onlab.onos.net.DefaultAnnotations.merge;
tomd176fc42014-09-08 00:12:30 -070041import static org.onlab.onos.net.Link.Type.DIRECT;
42import static org.onlab.onos.net.Link.Type.INDIRECT;
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -070043import static org.onlab.onos.net.LinkKey.linkKey;
tom35c0dc32014-09-19 10:00:58 -070044import static org.onlab.onos.net.link.LinkEvent.Type.*;
tom5bcc9462014-09-19 10:11:31 -070045import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070046import static com.google.common.collect.Multimaps.synchronizedSetMultimap;
47import static com.google.common.base.Predicates.notNull;
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070048import static com.google.common.base.Verify.verifyNotNull;
tomd176fc42014-09-08 00:12:30 -070049
tom4c6606f2014-09-07 11:11:21 -070050/**
tomcbff9392014-09-10 00:45:23 -070051 * Manages inventory of infrastructure links using trivial in-memory structures
tom4c6606f2014-09-07 11:11:21 -070052 * implementation.
53 */
tom35c0dc32014-09-19 10:00:58 -070054@Component(immediate = true)
55@Service
tomf80c9722014-09-24 14:49:18 -070056public class SimpleLinkStore
57 extends AbstractStore<LinkEvent, LinkStoreDelegate>
58 implements LinkStore {
tomeadbb462014-09-07 16:10:19 -070059
tom5bcc9462014-09-19 10:11:31 -070060 private final Logger log = getLogger(getClass());
61
tomeadbb462014-09-07 16:10:19 -070062 // Link inventory
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -070063 private final ConcurrentMap<LinkKey, Map<ProviderId, LinkDescription>>
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070064 linkDescs = new ConcurrentHashMap<>();
65
66 // Link instance cache
67 private final ConcurrentMap<LinkKey, Link> links = new ConcurrentHashMap<>();
tomeadbb462014-09-07 16:10:19 -070068
69 // Egress and ingress link sets
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070070 private final SetMultimap<DeviceId, LinkKey> srcLinks = createSynchronizedHashMultiMap();
71 private final SetMultimap<DeviceId, LinkKey> dstLinks = createSynchronizedHashMultiMap();
72
tomeadbb462014-09-07 16:10:19 -070073
tom5bcc9462014-09-19 10:11:31 -070074 @Activate
75 public void activate() {
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070081 linkDescs.clear();
82 links.clear();
83 srcLinks.clear();
84 dstLinks.clear();
tom5bcc9462014-09-19 10:11:31 -070085 log.info("Stopped");
86 }
tomeadbb462014-09-07 16:10:19 -070087
tom35c0dc32014-09-19 10:00:58 -070088 @Override
89 public int getLinkCount() {
tomeadbb462014-09-07 16:10:19 -070090 return links.size();
91 }
92
tom35c0dc32014-09-19 10:00:58 -070093 @Override
94 public Iterable<Link> getLinks() {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -070095 return Collections.unmodifiableCollection(links.values());
tomeadbb462014-09-07 16:10:19 -070096 }
97
tom35c0dc32014-09-19 10:00:58 -070098 @Override
99 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700100 // lock for iteration
101 synchronized (srcLinks) {
102 return FluentIterable.from(srcLinks.get(deviceId))
103 .transform(lookupLink())
104 .filter(notNull())
105 .toSet();
106 }
tomeadbb462014-09-07 16:10:19 -0700107 }
108
tom35c0dc32014-09-19 10:00:58 -0700109 @Override
110 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700111 // lock for iteration
112 synchronized (dstLinks) {
113 return FluentIterable.from(dstLinks.get(deviceId))
114 .transform(lookupLink())
115 .filter(notNull())
116 .toSet();
117 }
tomeadbb462014-09-07 16:10:19 -0700118 }
119
tom35c0dc32014-09-19 10:00:58 -0700120 @Override
121 public Link getLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700122 return links.get(linkKey(src, dst));
tomd176fc42014-09-08 00:12:30 -0700123 }
124
tom35c0dc32014-09-19 10:00:58 -0700125 @Override
126 public Set<Link> getEgressLinks(ConnectPoint src) {
tomeadbb462014-09-07 16:10:19 -0700127 Set<Link> egress = new HashSet<>();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700128 for (LinkKey linkKey : srcLinks.get(src.deviceId())) {
129 if (linkKey.src().equals(src)) {
130 egress.add(links.get(linkKey));
tomeadbb462014-09-07 16:10:19 -0700131 }
132 }
133 return egress;
134 }
135
tom35c0dc32014-09-19 10:00:58 -0700136 @Override
137 public Set<Link> getIngressLinks(ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700138 Set<Link> ingress = new HashSet<>();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700139 for (LinkKey linkKey : dstLinks.get(dst.deviceId())) {
140 if (linkKey.dst().equals(dst)) {
141 ingress.add(links.get(linkKey));
tomeadbb462014-09-07 16:10:19 -0700142 }
143 }
144 return ingress;
145 }
146
tom35c0dc32014-09-19 10:00:58 -0700147 @Override
tomeadbb462014-09-07 16:10:19 -0700148 public LinkEvent createOrUpdateLink(ProviderId providerId,
149 LinkDescription linkDescription) {
Yuta HIGUCHI990aecc2014-10-13 22:21:42 -0700150 LinkKey key = linkKey(linkDescription.src(), linkDescription.dst());
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700151
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700152 Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key);
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700153 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);
tomeadbb462014-09-07 16:10:19 -0700162 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700163 }
164
165 // Guarded by linkDescs value (=locking each Link)
166 private LinkDescription createOrUpdateLinkDescription(
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700167 Map<ProviderId, LinkDescription> descs,
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700168 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 HIGUCHI885be1d2014-10-04 21:47:26 -0700175 SparseAnnotations merged = union(oldDesc.annotations(),
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700176 linkDescription.annotations());
177 newDesc = new DefaultLinkDescription(
178 linkDescription.src(),
179 linkDescription.dst(),
180 linkDescription.type(), merged);
181 }
182 return descs.put(providerId, newDesc);
tomeadbb462014-09-07 16:10:19 -0700183 }
184
185 // Creates and stores the link and returns the appropriate event.
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700186 // 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;
tomeadbb462014-09-07 16:10:19 -0700194 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700195
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);
tomeadbb462014-09-07 16:10:19 -0700200 }
201
202 // Updates, if necessary the specified link and returns the appropriate event.
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700203 // Guarded by linkDescs value (=locking each Link)
204 private LinkEvent updateLink(LinkKey key, Link oldLink, Link newLink) {
tomd176fc42014-09-08 00:12:30 -0700205
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700206 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);
tomd176fc42014-09-08 00:12:30 -0700221 }
tomeadbb462014-09-07 16:10:19 -0700222 return null;
223 }
224
tom35c0dc32014-09-19 10:00:58 -0700225 @Override
226 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700227 final LinkKey key = linkKey(src, dst);
Yuta HIGUCHIdd841b72014-10-16 13:46:09 -0700228 Map<ProviderId, LinkDescription> descs = getOrCreateLinkDescriptions(key);
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700229 synchronized (descs) {
230 Link link = links.remove(key);
Yuta HIGUCHI0d6a5e62014-10-03 15:54:09 -0700231 descs.clear();
tomd176fc42014-09-08 00:12:30 -0700232 if (link != null) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700233 srcLinks.remove(link.src().deviceId(), key);
234 dstLinks.remove(link.dst().deviceId(), key);
tomd176fc42014-09-08 00:12:30 -0700235 return new LinkEvent(LINK_REMOVED, link);
236 }
237 return null;
tomeadbb462014-09-07 16:10:19 -0700238 }
239 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700240
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700248 // Guarded by linkDescs value (=locking each Link)
249 private ProviderId getBaseProviderId(Map<ProviderId, LinkDescription> providerDescs) {
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700250
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700263 // 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 HIGUCHI39ede6a2014-10-03 15:23:33 -0700267
268 ConnectPoint src = base.src();
269 ConnectPoint dst = base.dst();
270 Type type = base.type();
Yuta HIGUCHI885be1d2014-10-04 21:47:26 -0700271 DefaultAnnotations annotations = DefaultAnnotations.builder().build();
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700272 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 HIGUCHIdd841b72014-10-16 13:46:09 -0700291 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 HIGUCHI39ede6a2014-10-03 15:23:33 -0700305 }
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 HIGUCHIdd841b72014-10-16 13:46:09 -0700315 if (input == null) {
316 return null;
317 } else {
318 return links.get(input);
319 }
Yuta HIGUCHI39ede6a2014-10-03 15:23:33 -0700320 }
321 }
tom4c6606f2014-09-07 11:11:21 -0700322}