tom | 202175a | 2014-09-19 19:00:11 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 2 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 3 | import com.google.common.collect.HashMultimap; |
| 4 | import com.google.common.collect.ImmutableSet; |
| 5 | import com.google.common.collect.Multimap; |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 6 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Component; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 9 | import org.apache.felix.scr.annotations.Deactivate; |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 10 | import org.apache.felix.scr.annotations.Service; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.ConnectPoint; |
| 12 | import org.onlab.onos.net.DefaultLink; |
| 13 | import org.onlab.onos.net.DeviceId; |
| 14 | import org.onlab.onos.net.Link; |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.LinkKey; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.link.LinkDescription; |
| 17 | import org.onlab.onos.net.link.LinkEvent; |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.link.LinkStore; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.link.LinkStoreDelegate; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.provider.ProviderId; |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 21 | import org.onlab.onos.store.AbstractStore; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 22 | import org.slf4j.Logger; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.Collections; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Map; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 30 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 31 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 32 | import static org.onlab.onos.net.link.LinkEvent.Type.*; |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 33 | import static org.slf4j.LoggerFactory.getLogger; |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 34 | |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 35 | /** |
tom | cbff939 | 2014-09-10 00:45:23 -0700 | [diff] [blame] | 36 | * Manages inventory of infrastructure links using trivial in-memory structures |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 37 | * implementation. |
| 38 | */ |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 39 | @Component(immediate = true) |
| 40 | @Service |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 41 | public class SimpleLinkStore |
| 42 | extends AbstractStore<LinkEvent, LinkStoreDelegate> |
| 43 | implements LinkStore { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 44 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 47 | // 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 | |
tom | 5bcc946 | 2014-09-19 10:11:31 -0700 | [diff] [blame] | 54 | @Activate |
| 55 | public void activate() { |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
| 61 | log.info("Stopped"); |
| 62 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 63 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public int getLinkCount() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 66 | return links.size(); |
| 67 | } |
| 68 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 69 | @Override |
| 70 | public Iterable<Link> getLinks() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 71 | return Collections.unmodifiableSet(new HashSet<Link>(links.values())); |
| 72 | } |
| 73 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 76 | return ImmutableSet.copyOf(srcLinks.get(deviceId)); |
| 77 | } |
| 78 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 79 | @Override |
| 80 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 81 | return ImmutableSet.copyOf(dstLinks.get(deviceId)); |
| 82 | } |
| 83 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public Link getLink(ConnectPoint src, ConnectPoint dst) { |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 86 | return links.get(new LinkKey(src, dst)); |
| 87 | } |
| 88 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public Set<Link> getEgressLinks(ConnectPoint src) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 91 | 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 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public Set<Link> getIngressLinks(ConnectPoint dst) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 102 | Set<Link> ingress = new HashSet<>(); |
| 103 | for (Link link : dstLinks.get(dst.deviceId())) { |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 104 | if (link.dst().equals(dst)) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 105 | ingress.add(link); |
| 106 | } |
| 107 | } |
| 108 | return ingress; |
| 109 | } |
| 110 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 111 | @Override |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 112 | 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 | } |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 119 | return updateLink(providerId, link, key, linkDescription); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Creates and stores the link and returns the appropriate event. |
| 123 | private LinkEvent createLink(ProviderId providerId, LinkKey key, |
| 124 | LinkDescription linkDescription) { |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 125 | DefaultLink link = new DefaultLink(providerId, key.src(), key.dst(), |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 126 | 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 | } |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 132 | return new LinkEvent(LINK_ADDED, link); |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Updates, if necessary the specified link and returns the appropriate event. |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 136 | 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 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 152 | return null; |
| 153 | } |
| 154 | |
tom | 35c0dc3 | 2014-09-19 10:00:58 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 157 | synchronized (this) { |
| 158 | Link link = links.remove(new LinkKey(src, dst)); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 159 | 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; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 167 | } |