tom

Formalized LinkStore in preparation for separating managers and stores.

1 +package org.onlab.onos.net.link;
2 +
3 +import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.Link;
6 +import org.onlab.onos.net.provider.ProviderId;
7 +
8 +import java.util.Set;
9 +
10 +/**
11 + * Manages inventory of infrastructure links using whatever means are appropriate.
12 + */
13 +public interface LinkStore {
14 +
15 + /**
16 + * Returns the number of links in the store.
17 + *
18 + * @return number of links
19 + */
20 + int getLinkCount();
21 +
22 + /**
23 + * Returns an iterable collection of all links in the inventory.
24 + *
25 + * @return collection of all links
26 + */
27 + Iterable<Link> getLinks();
28 +
29 + /**
30 + * Returns all links egressing from the specified device.
31 + *
32 + * @param deviceId device identifier
33 + * @return set of device links
34 + */
35 + Set<Link> getDeviceEgressLinks(DeviceId deviceId);
36 +
37 + /**
38 + * Returns all links ingressing from the specified device.
39 + *
40 + * @param deviceId device identifier
41 + * @return set of device links
42 + */
43 + Set<Link> getDeviceIngressLinks(DeviceId deviceId);
44 +
45 + /**
46 + * Returns the link between the two end-points.
47 + *
48 + * @param src source connection point
49 + * @param dst destination connection point
50 + * @return link or null if one not found between the end-points
51 + */
52 + Link getLink(ConnectPoint src, ConnectPoint dst);
53 +
54 + /**
55 + * Returns all links egressing from the specified connection point.
56 + *
57 + * @param src source connection point
58 + * @return set of connection point links
59 + */
60 + Set<Link> getEgressLinks(ConnectPoint src);
61 +
62 + /**
63 + * Returns all links ingressing to the specified connection point.
64 + *
65 + * @param dst destination connection point
66 + * @return set of connection point links
67 + */
68 + Set<Link> getIngressLinks(ConnectPoint dst);
69 +
70 + /**
71 + * Creates a new link, or updates an existing one, based on the given
72 + * information.
73 + *
74 + * @param providerId provider identity
75 + * @param linkDescription link description
76 + * @return create or update link event, or null if no change resulted
77 + */
78 + public LinkEvent createOrUpdateLink(ProviderId providerId,
79 + LinkDescription linkDescription);
80 +
81 + /**
82 + * Removes the link based on the specified information.
83 + *
84 + * @param src link source
85 + * @param dst link destination
86 + * @return remove link event, or null if no change resulted
87 + */
88 + LinkEvent removeLink(ConnectPoint src, ConnectPoint dst);
89 +
90 +}
...@@ -27,6 +27,7 @@ import org.onlab.onos.net.link.LinkProvider; ...@@ -27,6 +27,7 @@ import org.onlab.onos.net.link.LinkProvider;
27 import org.onlab.onos.net.link.LinkProviderRegistry; 27 import org.onlab.onos.net.link.LinkProviderRegistry;
28 import org.onlab.onos.net.link.LinkProviderService; 28 import org.onlab.onos.net.link.LinkProviderService;
29 import org.onlab.onos.net.link.LinkService; 29 import org.onlab.onos.net.link.LinkService;
30 +import org.onlab.onos.net.link.LinkStore;
30 import org.onlab.onos.net.provider.AbstractProviderRegistry; 31 import org.onlab.onos.net.provider.AbstractProviderRegistry;
31 import org.onlab.onos.net.provider.AbstractProviderService; 32 import org.onlab.onos.net.provider.AbstractProviderService;
32 import org.slf4j.Logger; 33 import org.slf4j.Logger;
...@@ -38,7 +39,7 @@ import com.google.common.collect.Sets; ...@@ -38,7 +39,7 @@ import com.google.common.collect.Sets;
38 */ 39 */
39 @Component(immediate = true) 40 @Component(immediate = true)
40 @Service 41 @Service
41 -public class SimpleLinkManager 42 +public class LinkManager
42 extends AbstractProviderRegistry<LinkProvider, LinkProviderService> 43 extends AbstractProviderRegistry<LinkProvider, LinkProviderService>
43 implements LinkService, LinkAdminService, LinkProviderRegistry { 44 implements LinkService, LinkAdminService, LinkProviderRegistry {
44 45
...@@ -51,10 +52,12 @@ public class SimpleLinkManager ...@@ -51,10 +52,12 @@ public class SimpleLinkManager
51 protected final AbstractListenerRegistry<LinkEvent, LinkListener> 52 protected final AbstractListenerRegistry<LinkEvent, LinkListener>
52 listenerRegistry = new AbstractListenerRegistry<>(); 53 listenerRegistry = new AbstractListenerRegistry<>();
53 54
54 - private final SimpleLinkStore store = new SimpleLinkStore();
55 private final DeviceListener deviceListener = new InnerDeviceListener(); 55 private final DeviceListener deviceListener = new InnerDeviceListener();
56 56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 + protected LinkStore store;
59 +
60 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected DeviceService deviceService; 61 protected DeviceService deviceService;
59 62
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......
...@@ -3,12 +3,15 @@ package org.onlab.onos.net.trivial.link.impl; ...@@ -3,12 +3,15 @@ package org.onlab.onos.net.trivial.link.impl;
3 import com.google.common.collect.HashMultimap; 3 import com.google.common.collect.HashMultimap;
4 import com.google.common.collect.ImmutableSet; 4 import com.google.common.collect.ImmutableSet;
5 import com.google.common.collect.Multimap; 5 import com.google.common.collect.Multimap;
6 +import org.apache.felix.scr.annotations.Component;
7 +import org.apache.felix.scr.annotations.Service;
6 import org.onlab.onos.net.ConnectPoint; 8 import org.onlab.onos.net.ConnectPoint;
7 import org.onlab.onos.net.DefaultLink; 9 import org.onlab.onos.net.DefaultLink;
8 import org.onlab.onos.net.DeviceId; 10 import org.onlab.onos.net.DeviceId;
9 import org.onlab.onos.net.Link; 11 import org.onlab.onos.net.Link;
10 import org.onlab.onos.net.link.LinkDescription; 12 import org.onlab.onos.net.link.LinkDescription;
11 import org.onlab.onos.net.link.LinkEvent; 13 import org.onlab.onos.net.link.LinkEvent;
14 +import org.onlab.onos.net.link.LinkStore;
12 import org.onlab.onos.net.provider.ProviderId; 15 import org.onlab.onos.net.provider.ProviderId;
13 16
14 import java.util.Collections; 17 import java.util.Collections;
...@@ -20,15 +23,15 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -20,15 +23,15 @@ import java.util.concurrent.ConcurrentHashMap;
20 23
21 import static org.onlab.onos.net.Link.Type.DIRECT; 24 import static org.onlab.onos.net.Link.Type.DIRECT;
22 import static org.onlab.onos.net.Link.Type.INDIRECT; 25 import static org.onlab.onos.net.Link.Type.INDIRECT;
23 -import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; 26 +import static org.onlab.onos.net.link.LinkEvent.Type.*;
24 -import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
25 -import static org.onlab.onos.net.link.LinkEvent.Type.LINK_UPDATED;
26 27
27 /** 28 /**
28 * Manages inventory of infrastructure links using trivial in-memory structures 29 * Manages inventory of infrastructure links using trivial in-memory structures
29 * implementation. 30 * implementation.
30 */ 31 */
31 -class SimpleLinkStore { 32 +@Component(immediate = true)
33 +@Service
34 +public class SimpleLinkStore implements LinkStore {
32 35
33 // Link inventory 36 // Link inventory
34 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>(); 37 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
...@@ -39,62 +42,33 @@ class SimpleLinkStore { ...@@ -39,62 +42,33 @@ class SimpleLinkStore {
39 42
40 private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{}); 43 private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{});
41 44
42 - /** 45 + @Override
43 - * Returns the number of links in the store. 46 + public int getLinkCount() {
44 - *
45 - * @return number of links
46 - */
47 - int getLinkCount() {
48 return links.size(); 47 return links.size();
49 } 48 }
50 49
51 - /** 50 + @Override
52 - * Returns an iterable collection of all links in the inventory. 51 + public Iterable<Link> getLinks() {
53 - *
54 - * @return collection of all links
55 - */
56 - Iterable<Link> getLinks() {
57 return Collections.unmodifiableSet(new HashSet<Link>(links.values())); 52 return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
58 } 53 }
59 54
60 - /** 55 + @Override
61 - * Returns all links egressing from the specified device. 56 + public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
62 - *
63 - * @param deviceId device identifier
64 - * @return set of device links
65 - */
66 - Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
67 return ImmutableSet.copyOf(srcLinks.get(deviceId)); 57 return ImmutableSet.copyOf(srcLinks.get(deviceId));
68 } 58 }
69 59
70 - /** 60 + @Override
71 - * Returns all links ingressing from the specified device. 61 + public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
72 - *
73 - * @param deviceId device identifier
74 - * @return set of device links
75 - */
76 - Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
77 return ImmutableSet.copyOf(dstLinks.get(deviceId)); 62 return ImmutableSet.copyOf(dstLinks.get(deviceId));
78 } 63 }
79 64
80 - /** 65 + @Override
81 - * Returns the link between the two end-points. 66 + public Link getLink(ConnectPoint src, ConnectPoint dst) {
82 - *
83 - * @param src source connection point
84 - * @param dst destination connection point
85 - * @return link or null if one not found between the end-points
86 - */
87 - Link getLink(ConnectPoint src, ConnectPoint dst) {
88 return links.get(new LinkKey(src, dst)); 67 return links.get(new LinkKey(src, dst));
89 } 68 }
90 69
91 - /** 70 + @Override
92 - * Returns all links egressing from the specified connection point. 71 + public Set<Link> getEgressLinks(ConnectPoint src) {
93 - *
94 - * @param src source connection point
95 - * @return set of connection point links
96 - */
97 - Set<Link> getEgressLinks(ConnectPoint src) {
98 Set<Link> egress = new HashSet<>(); 72 Set<Link> egress = new HashSet<>();
99 for (Link link : srcLinks.get(src.deviceId())) { 73 for (Link link : srcLinks.get(src.deviceId())) {
100 if (link.src().equals(src)) { 74 if (link.src().equals(src)) {
...@@ -104,13 +78,8 @@ class SimpleLinkStore { ...@@ -104,13 +78,8 @@ class SimpleLinkStore {
104 return egress; 78 return egress;
105 } 79 }
106 80
107 - /** 81 + @Override
108 - * Returns all links ingressing to the specified connection point. 82 + public Set<Link> getIngressLinks(ConnectPoint dst) {
109 - *
110 - * @param dst destination connection point
111 - * @return set of connection point links
112 - */
113 - Set<Link> getIngressLinks(ConnectPoint dst) {
114 Set<Link> ingress = new HashSet<>(); 83 Set<Link> ingress = new HashSet<>();
115 for (Link link : dstLinks.get(dst.deviceId())) { 84 for (Link link : dstLinks.get(dst.deviceId())) {
116 if (link.dst().equals(dst)) { 85 if (link.dst().equals(dst)) {
...@@ -120,14 +89,7 @@ class SimpleLinkStore { ...@@ -120,14 +89,7 @@ class SimpleLinkStore {
120 return ingress; 89 return ingress;
121 } 90 }
122 91
123 - /** 92 + @Override
124 - * Creates a new link, or updates an existing one, based on the given
125 - * information.
126 - *
127 - * @param providerId provider identity
128 - * @param linkDescription link description
129 - * @return create or update link event, or null if no change resulted
130 - */
131 public LinkEvent createOrUpdateLink(ProviderId providerId, 93 public LinkEvent createOrUpdateLink(ProviderId providerId,
132 LinkDescription linkDescription) { 94 LinkDescription linkDescription) {
133 LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst()); 95 LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
...@@ -171,14 +133,8 @@ class SimpleLinkStore { ...@@ -171,14 +133,8 @@ class SimpleLinkStore {
171 return null; 133 return null;
172 } 134 }
173 135
174 - /** 136 + @Override
175 - * Removes the link based on the specified information. 137 + public LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
176 - *
177 - * @param src link source
178 - * @param dst link destination
179 - * @return remove link event, or null if no change resulted
180 - */
181 - LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
182 synchronized (this) { 138 synchronized (this) {
183 Link link = links.remove(new LinkKey(src, dst)); 139 Link link = links.remove(new LinkKey(src, dst));
184 if (link != null) { 140 if (link != null) {
......
...@@ -38,7 +38,7 @@ import static org.onlab.onos.net.link.LinkEvent.Type.*; ...@@ -38,7 +38,7 @@ import static org.onlab.onos.net.link.LinkEvent.Type.*;
38 /** 38 /**
39 * Test codifying the link service & link provider service contracts. 39 * Test codifying the link service & link provider service contracts.
40 */ 40 */
41 -public class SimpleLinkManagerTest { 41 +public class LinkManagerTest {
42 42
43 private static final ProviderId PID = new ProviderId("of", "foo"); 43 private static final ProviderId PID = new ProviderId("of", "foo");
44 private static final DeviceId DID1 = deviceId("of:foo"); 44 private static final DeviceId DID1 = deviceId("of:foo");
...@@ -50,7 +50,7 @@ public class SimpleLinkManagerTest { ...@@ -50,7 +50,7 @@ public class SimpleLinkManagerTest {
50 private static final PortNumber P3 = PortNumber.portNumber(3); 50 private static final PortNumber P3 = PortNumber.portNumber(3);
51 51
52 52
53 - private SimpleLinkManager mgr; 53 + private LinkManager mgr;
54 54
55 protected LinkService service; 55 protected LinkService service;
56 protected LinkAdminService admin; 56 protected LinkAdminService admin;
...@@ -61,10 +61,11 @@ public class SimpleLinkManagerTest { ...@@ -61,10 +61,11 @@ public class SimpleLinkManagerTest {
61 61
62 @Before 62 @Before
63 public void setUp() { 63 public void setUp() {
64 - mgr = new SimpleLinkManager(); 64 + mgr = new LinkManager();
65 service = mgr; 65 service = mgr;
66 admin = mgr; 66 admin = mgr;
67 registry = mgr; 67 registry = mgr;
68 + mgr.store = new SimpleLinkStore();
68 mgr.eventDispatcher = new TestEventDispatcher(); 69 mgr.eventDispatcher = new TestEventDispatcher();
69 mgr.deviceService = new DeviceManager(); 70 mgr.deviceService = new DeviceManager();
70 mgr.activate(); 71 mgr.activate();
......
...@@ -16,7 +16,7 @@ import org.onlab.onos.net.topology.TopologyProvider; ...@@ -16,7 +16,7 @@ import org.onlab.onos.net.topology.TopologyProvider;
16 import org.onlab.onos.net.topology.TopologyProviderRegistry; 16 import org.onlab.onos.net.topology.TopologyProviderRegistry;
17 import org.onlab.onos.net.topology.TopologyProviderService; 17 import org.onlab.onos.net.topology.TopologyProviderService;
18 import org.onlab.onos.net.trivial.device.impl.DeviceManager; 18 import org.onlab.onos.net.trivial.device.impl.DeviceManager;
19 -import org.onlab.onos.net.trivial.link.impl.SimpleLinkManager; 19 +import org.onlab.onos.net.trivial.link.impl.LinkManager;
20 20
21 import java.util.List; 21 import java.util.List;
22 import java.util.Set; 22 import java.util.Set;
...@@ -140,7 +140,7 @@ public class DefaultTopologyProviderTest { ...@@ -140,7 +140,7 @@ public class DefaultTopologyProviderTest {
140 } 140 }
141 } 141 }
142 142
143 - private class TestLinkService extends SimpleLinkManager { 143 + private class TestLinkService extends LinkManager {
144 TestLinkService() { 144 TestLinkService() {
145 eventDispatcher = new TestEventDispatcher(); 145 eventDispatcher = new TestEventDispatcher();
146 eventDispatcher.addSink(LinkEvent.class, listenerRegistry); 146 eventDispatcher.addSink(LinkEvent.class, listenerRegistry);
......