blob: 6b96bc7dd1916bd9d0ea93855e4580df4233a792 [file] [log] [blame]
tomea961ff2014-10-01 12:45:15 -07001package org.onlab.onos.store.trivial.impl;
tom4c6606f2014-09-07 11:11:21 -07002
tomeadbb462014-09-07 16:10:19 -07003import com.google.common.collect.HashMultimap;
4import com.google.common.collect.ImmutableSet;
5import com.google.common.collect.Multimap;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -07006
tom5bcc9462014-09-19 10:11:31 -07007import org.apache.felix.scr.annotations.Activate;
tom35c0dc32014-09-19 10:00:58 -07008import org.apache.felix.scr.annotations.Component;
tom5bcc9462014-09-19 10:11:31 -07009import org.apache.felix.scr.annotations.Deactivate;
tom35c0dc32014-09-19 10:00:58 -070010import org.apache.felix.scr.annotations.Service;
tomeadbb462014-09-07 16:10:19 -070011import org.onlab.onos.net.ConnectPoint;
12import org.onlab.onos.net.DefaultLink;
13import org.onlab.onos.net.DeviceId;
14import org.onlab.onos.net.Link;
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -070015import org.onlab.onos.net.LinkKey;
tomeadbb462014-09-07 16:10:19 -070016import org.onlab.onos.net.link.LinkDescription;
17import org.onlab.onos.net.link.LinkEvent;
tom35c0dc32014-09-19 10:00:58 -070018import org.onlab.onos.net.link.LinkStore;
tomf80c9722014-09-24 14:49:18 -070019import org.onlab.onos.net.link.LinkStoreDelegate;
tomeadbb462014-09-07 16:10:19 -070020import org.onlab.onos.net.provider.ProviderId;
tomf80c9722014-09-24 14:49:18 -070021import org.onlab.onos.store.AbstractStore;
tom5bcc9462014-09-19 10:11:31 -070022import org.slf4j.Logger;
tomeadbb462014-09-07 16:10:19 -070023
24import java.util.Collections;
25import java.util.HashSet;
26import java.util.Map;
tomeadbb462014-09-07 16:10:19 -070027import java.util.Set;
28import java.util.concurrent.ConcurrentHashMap;
29
tomd176fc42014-09-08 00:12:30 -070030import static org.onlab.onos.net.Link.Type.DIRECT;
31import static org.onlab.onos.net.Link.Type.INDIRECT;
tom35c0dc32014-09-19 10:00:58 -070032import static org.onlab.onos.net.link.LinkEvent.Type.*;
tom5bcc9462014-09-19 10:11:31 -070033import static org.slf4j.LoggerFactory.getLogger;
tomd176fc42014-09-08 00:12:30 -070034
Yuta HIGUCHIc99a8d32014-10-02 17:16:34 -070035// TODO: Add support for multiple provider and annotations
tom4c6606f2014-09-07 11:11:21 -070036/**
tomcbff9392014-09-10 00:45:23 -070037 * Manages inventory of infrastructure links using trivial in-memory structures
tom4c6606f2014-09-07 11:11:21 -070038 * implementation.
39 */
tom35c0dc32014-09-19 10:00:58 -070040@Component(immediate = true)
41@Service
tomf80c9722014-09-24 14:49:18 -070042public class SimpleLinkStore
43 extends AbstractStore<LinkEvent, LinkStoreDelegate>
44 implements LinkStore {
tomeadbb462014-09-07 16:10:19 -070045
tom5bcc9462014-09-19 10:11:31 -070046 private final Logger log = getLogger(getClass());
47
tomeadbb462014-09-07 16:10:19 -070048 // Link inventory
49 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
50
51 // Egress and ingress link sets
52 private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
53 private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
54
tom5bcc9462014-09-19 10:11:31 -070055 @Activate
56 public void activate() {
57 log.info("Started");
58 }
59
60 @Deactivate
61 public void deactivate() {
62 log.info("Stopped");
63 }
tomeadbb462014-09-07 16:10:19 -070064
tom35c0dc32014-09-19 10:00:58 -070065 @Override
66 public int getLinkCount() {
tomeadbb462014-09-07 16:10:19 -070067 return links.size();
68 }
69
tom35c0dc32014-09-19 10:00:58 -070070 @Override
71 public Iterable<Link> getLinks() {
tomeadbb462014-09-07 16:10:19 -070072 return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
73 }
74
tom35c0dc32014-09-19 10:00:58 -070075 @Override
76 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
tomeadbb462014-09-07 16:10:19 -070077 return ImmutableSet.copyOf(srcLinks.get(deviceId));
78 }
79
tom35c0dc32014-09-19 10:00:58 -070080 @Override
81 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
tomeadbb462014-09-07 16:10:19 -070082 return ImmutableSet.copyOf(dstLinks.get(deviceId));
83 }
84
tom35c0dc32014-09-19 10:00:58 -070085 @Override
86 public Link getLink(ConnectPoint src, ConnectPoint dst) {
tomd176fc42014-09-08 00:12:30 -070087 return links.get(new LinkKey(src, dst));
88 }
89
tom35c0dc32014-09-19 10:00:58 -070090 @Override
91 public Set<Link> getEgressLinks(ConnectPoint src) {
tomeadbb462014-09-07 16:10:19 -070092 Set<Link> egress = new HashSet<>();
93 for (Link link : srcLinks.get(src.deviceId())) {
94 if (link.src().equals(src)) {
95 egress.add(link);
96 }
97 }
98 return egress;
99 }
100
tom35c0dc32014-09-19 10:00:58 -0700101 @Override
102 public Set<Link> getIngressLinks(ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700103 Set<Link> ingress = new HashSet<>();
104 for (Link link : dstLinks.get(dst.deviceId())) {
tomd176fc42014-09-08 00:12:30 -0700105 if (link.dst().equals(dst)) {
tomeadbb462014-09-07 16:10:19 -0700106 ingress.add(link);
107 }
108 }
109 return ingress;
110 }
111
tom35c0dc32014-09-19 10:00:58 -0700112 @Override
tomeadbb462014-09-07 16:10:19 -0700113 public LinkEvent createOrUpdateLink(ProviderId providerId,
114 LinkDescription linkDescription) {
115 LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
116 DefaultLink link = links.get(key);
117 if (link == null) {
118 return createLink(providerId, key, linkDescription);
119 }
tomd176fc42014-09-08 00:12:30 -0700120 return updateLink(providerId, link, key, linkDescription);
tomeadbb462014-09-07 16:10:19 -0700121 }
122
123 // Creates and stores the link and returns the appropriate event.
124 private LinkEvent createLink(ProviderId providerId, LinkKey key,
125 LinkDescription linkDescription) {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700126 DefaultLink link = new DefaultLink(providerId, key.src(), key.dst(),
tomeadbb462014-09-07 16:10:19 -0700127 linkDescription.type());
128 synchronized (this) {
129 links.put(key, link);
130 srcLinks.put(link.src().deviceId(), link);
131 dstLinks.put(link.dst().deviceId(), link);
132 }
tomd176fc42014-09-08 00:12:30 -0700133 return new LinkEvent(LINK_ADDED, link);
tomeadbb462014-09-07 16:10:19 -0700134 }
135
136 // Updates, if necessary the specified link and returns the appropriate event.
tomd176fc42014-09-08 00:12:30 -0700137 private LinkEvent updateLink(ProviderId providerId, DefaultLink link,
138 LinkKey key, LinkDescription linkDescription) {
139 if (link.type() == INDIRECT && linkDescription.type() == DIRECT) {
140 synchronized (this) {
141 srcLinks.remove(link.src().deviceId(), link);
142 dstLinks.remove(link.dst().deviceId(), link);
143
144 DefaultLink updated =
145 new DefaultLink(providerId, link.src(), link.dst(),
146 linkDescription.type());
147 links.put(key, updated);
148 srcLinks.put(link.src().deviceId(), updated);
149 dstLinks.put(link.dst().deviceId(), updated);
150 return new LinkEvent(LINK_UPDATED, updated);
151 }
152 }
tomeadbb462014-09-07 16:10:19 -0700153 return null;
154 }
155
tom35c0dc32014-09-19 10:00:58 -0700156 @Override
157 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700158 synchronized (this) {
159 Link link = links.remove(new LinkKey(src, dst));
tomd176fc42014-09-08 00:12:30 -0700160 if (link != null) {
161 srcLinks.remove(link.src().deviceId(), link);
162 dstLinks.remove(link.dst().deviceId(), link);
163 return new LinkEvent(LINK_REMOVED, link);
164 }
165 return null;
tomeadbb462014-09-07 16:10:19 -0700166 }
167 }
tom4c6606f2014-09-07 11:11:21 -0700168}