blob: 319df89aca55f34d3424fd91ac3200a2f127c10a [file] [log] [blame]
tom202175a2014-09-19 19:00:11 -07001package org.onlab.onos.net.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
tom4c6606f2014-09-07 11:11:21 -070035/**
tomcbff9392014-09-10 00:45:23 -070036 * Manages inventory of infrastructure links using trivial in-memory structures
tom4c6606f2014-09-07 11:11:21 -070037 * implementation.
38 */
tom35c0dc32014-09-19 10:00:58 -070039@Component(immediate = true)
40@Service
tomf80c9722014-09-24 14:49:18 -070041public class SimpleLinkStore
42 extends AbstractStore<LinkEvent, LinkStoreDelegate>
43 implements LinkStore {
tomeadbb462014-09-07 16:10:19 -070044
tom5bcc9462014-09-19 10:11:31 -070045 private final Logger log = getLogger(getClass());
46
tomeadbb462014-09-07 16:10:19 -070047 // Link inventory
48 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
49
50 // Egress and ingress link sets
51 private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
52 private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
53
tom5bcc9462014-09-19 10:11:31 -070054 @Activate
55 public void activate() {
56 log.info("Started");
57 }
58
59 @Deactivate
60 public void deactivate() {
61 log.info("Stopped");
62 }
tomeadbb462014-09-07 16:10:19 -070063
tom35c0dc32014-09-19 10:00:58 -070064 @Override
65 public int getLinkCount() {
tomeadbb462014-09-07 16:10:19 -070066 return links.size();
67 }
68
tom35c0dc32014-09-19 10:00:58 -070069 @Override
70 public Iterable<Link> getLinks() {
tomeadbb462014-09-07 16:10:19 -070071 return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
72 }
73
tom35c0dc32014-09-19 10:00:58 -070074 @Override
75 public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
tomeadbb462014-09-07 16:10:19 -070076 return ImmutableSet.copyOf(srcLinks.get(deviceId));
77 }
78
tom35c0dc32014-09-19 10:00:58 -070079 @Override
80 public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
tomeadbb462014-09-07 16:10:19 -070081 return ImmutableSet.copyOf(dstLinks.get(deviceId));
82 }
83
tom35c0dc32014-09-19 10:00:58 -070084 @Override
85 public Link getLink(ConnectPoint src, ConnectPoint dst) {
tomd176fc42014-09-08 00:12:30 -070086 return links.get(new LinkKey(src, dst));
87 }
88
tom35c0dc32014-09-19 10:00:58 -070089 @Override
90 public Set<Link> getEgressLinks(ConnectPoint src) {
tomeadbb462014-09-07 16:10:19 -070091 Set<Link> egress = new HashSet<>();
92 for (Link link : srcLinks.get(src.deviceId())) {
93 if (link.src().equals(src)) {
94 egress.add(link);
95 }
96 }
97 return egress;
98 }
99
tom35c0dc32014-09-19 10:00:58 -0700100 @Override
101 public Set<Link> getIngressLinks(ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700102 Set<Link> ingress = new HashSet<>();
103 for (Link link : dstLinks.get(dst.deviceId())) {
tomd176fc42014-09-08 00:12:30 -0700104 if (link.dst().equals(dst)) {
tomeadbb462014-09-07 16:10:19 -0700105 ingress.add(link);
106 }
107 }
108 return ingress;
109 }
110
tom35c0dc32014-09-19 10:00:58 -0700111 @Override
tomeadbb462014-09-07 16:10:19 -0700112 public LinkEvent createOrUpdateLink(ProviderId providerId,
113 LinkDescription linkDescription) {
114 LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
115 DefaultLink link = links.get(key);
116 if (link == null) {
117 return createLink(providerId, key, linkDescription);
118 }
tomd176fc42014-09-08 00:12:30 -0700119 return updateLink(providerId, link, key, linkDescription);
tomeadbb462014-09-07 16:10:19 -0700120 }
121
122 // Creates and stores the link and returns the appropriate event.
123 private LinkEvent createLink(ProviderId providerId, LinkKey key,
124 LinkDescription linkDescription) {
Yuta HIGUCHI06dc6b92014-09-25 16:06:16 -0700125 DefaultLink link = new DefaultLink(providerId, key.src(), key.dst(),
tomeadbb462014-09-07 16:10:19 -0700126 linkDescription.type());
127 synchronized (this) {
128 links.put(key, link);
129 srcLinks.put(link.src().deviceId(), link);
130 dstLinks.put(link.dst().deviceId(), link);
131 }
tomd176fc42014-09-08 00:12:30 -0700132 return new LinkEvent(LINK_ADDED, link);
tomeadbb462014-09-07 16:10:19 -0700133 }
134
135 // Updates, if necessary the specified link and returns the appropriate event.
tomd176fc42014-09-08 00:12:30 -0700136 private LinkEvent updateLink(ProviderId providerId, DefaultLink link,
137 LinkKey key, LinkDescription linkDescription) {
138 if (link.type() == INDIRECT && linkDescription.type() == DIRECT) {
139 synchronized (this) {
140 srcLinks.remove(link.src().deviceId(), link);
141 dstLinks.remove(link.dst().deviceId(), link);
142
143 DefaultLink updated =
144 new DefaultLink(providerId, link.src(), link.dst(),
145 linkDescription.type());
146 links.put(key, updated);
147 srcLinks.put(link.src().deviceId(), updated);
148 dstLinks.put(link.dst().deviceId(), updated);
149 return new LinkEvent(LINK_UPDATED, updated);
150 }
151 }
tomeadbb462014-09-07 16:10:19 -0700152 return null;
153 }
154
tom35c0dc32014-09-19 10:00:58 -0700155 @Override
156 public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
tomeadbb462014-09-07 16:10:19 -0700157 synchronized (this) {
158 Link link = links.remove(new LinkKey(src, dst));
tomd176fc42014-09-08 00:12:30 -0700159 if (link != null) {
160 srcLinks.remove(link.src().deviceId(), link);
161 dstLinks.remove(link.dst().deviceId(), link);
162 return new LinkEvent(LINK_REMOVED, link);
163 }
164 return null;
tomeadbb462014-09-07 16:10:19 -0700165 }
166 }
tom4c6606f2014-09-07 11:11:21 -0700167}