tom

Added simple link manager implementation.

Added link admin stuff; unit tests to come.
Showing 23 changed files with 472 additions and 29 deletions
...@@ -4,7 +4,7 @@ import org.onlab.onos.net.provider.ProviderId; ...@@ -4,7 +4,7 @@ import org.onlab.onos.net.provider.ProviderId;
4 4
5 import java.util.Objects; 5 import java.util.Objects;
6 6
7 -import static com.google.common.base.Objects.toStringHelper; 7 +import static com.google.common.base.MoreObjects.toStringHelper;
8 8
9 /** 9 /**
10 * Default infrastructure device model implementation. 10 * Default infrastructure device model implementation.
......
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 -import org.onlab.onos.net.link.LinkDescription; 3 +import org.onlab.onos.net.provider.ProviderId;
4 4
5 import java.util.Objects; 5 import java.util.Objects;
6 6
7 -import static com.google.common.base.Objects.toStringHelper; 7 +import static com.google.common.base.MoreObjects.toStringHelper;
8 8
9 /** 9 /**
10 * Default infrastructure link model implementation. 10 * Default infrastructure link model implementation.
11 */ 11 */
12 -public class DefaultLink implements LinkDescription { 12 +public class DefaultLink extends AbstractModel implements Link {
13 13
14 - private ConnectPoint src; 14 + private final ConnectPoint src;
15 - private ConnectPoint dst; 15 + private final ConnectPoint dst;
16 + private final Type type;
16 17
17 /** 18 /**
18 * Creates a link description using the supplied information. 19 * Creates a link description using the supplied information.
19 * 20 *
21 + * @param providerId provider identity
20 * @param src link source 22 * @param src link source
21 * @param dst link destination 23 * @param dst link destination
24 + * @param type link type
22 */ 25 */
23 - public DefaultLink(ConnectPoint src, ConnectPoint dst) { 26 + public DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
27 + Type type) {
28 + super(providerId);
24 this.src = src; 29 this.src = src;
25 this.dst = dst; 30 this.dst = dst;
31 + this.type = type;
26 } 32 }
27 33
28 @Override 34 @Override
...@@ -35,18 +41,23 @@ public class DefaultLink implements LinkDescription { ...@@ -35,18 +41,23 @@ public class DefaultLink implements LinkDescription {
35 return dst; 41 return dst;
36 } 42 }
37 43
44 + @Override
45 + public Type type() {
46 + return type;
47 + }
38 48
39 @Override 49 @Override
40 public int hashCode() { 50 public int hashCode() {
41 - return Objects.hash(src, dst); 51 + return Objects.hash(src, dst, type);
42 } 52 }
43 53
44 @Override 54 @Override
45 public boolean equals(Object obj) { 55 public boolean equals(Object obj) {
46 - if (obj instanceof DefaultDevice) { 56 + if (obj instanceof DefaultLink) {
47 final DefaultLink other = (DefaultLink) obj; 57 final DefaultLink other = (DefaultLink) obj;
48 return Objects.equals(this.src, other.src) && 58 return Objects.equals(this.src, other.src) &&
49 - Objects.equals(this.dst, other.dst); 59 + Objects.equals(this.dst, other.dst) &&
60 + Objects.equals(this.type, other.type);
50 } 61 }
51 return false; 62 return false;
52 } 63 }
...@@ -56,6 +67,7 @@ public class DefaultLink implements LinkDescription { ...@@ -56,6 +67,7 @@ public class DefaultLink implements LinkDescription {
56 return toStringHelper(this) 67 return toStringHelper(this)
57 .add("src", src) 68 .add("src", src)
58 .add("dst", dst) 69 .add("dst", dst)
70 + .add("type", type)
59 .toString(); 71 .toString();
60 } 72 }
61 73
......
...@@ -2,7 +2,7 @@ package org.onlab.onos.net; ...@@ -2,7 +2,7 @@ package org.onlab.onos.net;
2 2
3 import java.util.Objects; 3 import java.util.Objects;
4 4
5 -import static com.google.common.base.Objects.toStringHelper; 5 +import static com.google.common.base.MoreObjects.toStringHelper;
6 6
7 /** 7 /**
8 * Default port implementation. 8 * Default port implementation.
......
...@@ -3,7 +3,7 @@ package org.onlab.onos.net; ...@@ -3,7 +3,7 @@ package org.onlab.onos.net;
3 import java.net.URI; 3 import java.net.URI;
4 import java.util.Objects; 4 import java.util.Objects;
5 5
6 -import static com.google.common.base.Objects.toStringHelper; 6 +import static com.google.common.base.MoreObjects.toStringHelper;
7 7
8 /** 8 /**
9 * Immutable representation of a network element identity. 9 * Immutable representation of a network element identity.
......
...@@ -37,6 +37,13 @@ public interface Link extends Provided { ...@@ -37,6 +37,13 @@ public interface Link extends Provided {
37 */ 37 */
38 ConnectPoint dst(); 38 ConnectPoint dst();
39 39
40 + /**
41 + * Returns the link type.
42 + *
43 + * @return link type
44 + */
45 + Type type();
46 +
40 // LinkInfo info(); // Additional link information / decorations 47 // LinkInfo info(); // Additional link information / decorations
41 48
42 } 49 }
......
...@@ -2,7 +2,7 @@ package org.onlab.onos.net.device; ...@@ -2,7 +2,7 @@ package org.onlab.onos.net.device;
2 2
3 import java.net.URI; 3 import java.net.URI;
4 4
5 -import static com.google.common.base.Objects.toStringHelper; 5 +import static com.google.common.base.MoreObjects.toStringHelper;
6 import static com.google.common.base.Preconditions.checkNotNull; 6 import static com.google.common.base.Preconditions.checkNotNull;
7 import static org.onlab.onos.net.Device.Type; 7 import static org.onlab.onos.net.Device.Type;
8 8
......
1 package org.onlab.onos.net.link; 1 package org.onlab.onos.net.link;
2 2
3 import org.onlab.onos.net.ConnectPoint; 3 import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.Link;
4 5
5 /** 6 /**
6 * Default implementation of immutable link description entity. 7 * Default implementation of immutable link description entity.
7 */ 8 */
8 public class DefaultLinkDescription implements LinkDescription { 9 public class DefaultLinkDescription implements LinkDescription {
9 10
10 - private ConnectPoint src; 11 + private final ConnectPoint src;
11 - private ConnectPoint dst; 12 + private final ConnectPoint dst;
13 + private final Link.Type type;
12 14
13 /** 15 /**
14 * Creates a link description using the supplied information. 16 * Creates a link description using the supplied information.
15 * 17 *
16 * @param src link source 18 * @param src link source
17 * @param dst link destination 19 * @param dst link destination
20 + * @param type link type
18 */ 21 */
19 - public DefaultLinkDescription(ConnectPoint src, ConnectPoint dst) { 22 + public DefaultLinkDescription(ConnectPoint src, ConnectPoint dst, Link.Type type) {
20 this.src = src; 23 this.src = src;
21 this.dst = dst; 24 this.dst = dst;
25 + this.type = type;
22 } 26 }
23 27
24 @Override 28 @Override
...@@ -31,4 +35,9 @@ public class DefaultLinkDescription implements LinkDescription { ...@@ -31,4 +35,9 @@ public class DefaultLinkDescription implements LinkDescription {
31 return dst; 35 return dst;
32 } 36 }
33 37
38 + @Override
39 + public Link.Type type() {
40 + return null;
41 + }
42 +
34 } 43 }
......
1 +package org.onlab.onos.net.link;
2 +
3 +import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.DeviceId;
5 +
6 +/**
7 + * Service for administering the inventory of infrastructure links.
8 + */
9 +public interface LinkAdminService {
10 +
11 + /**
12 + * Removes all infrastructure links leading to and from the
13 + * specified connection point.
14 + *
15 + * @param connectPoint connection point
16 + */
17 + void removeLinks(ConnectPoint connectPoint);
18 +
19 + /**
20 + * Removes all infrastructure links leading to and from the
21 + * specified device.
22 + *
23 + * @param deviceId device identifier
24 + */
25 + void removeLinks(DeviceId deviceId);
26 +
27 +}
1 package org.onlab.onos.net.link; 1 package org.onlab.onos.net.link;
2 2
3 import org.onlab.onos.net.ConnectPoint; 3 import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.Link;
4 5
5 /** 6 /**
6 * Describes an infrastructure link. 7 * Describes an infrastructure link.
...@@ -21,5 +22,13 @@ public interface LinkDescription { ...@@ -21,5 +22,13 @@ public interface LinkDescription {
21 */ 22 */
22 ConnectPoint dst(); 23 ConnectPoint dst();
23 24
25 + /**
26 + * Returns the link type.
27 + *
28 + * @return link type
29 + */
30 + Link.Type type();
31 +
32 +
24 // Add further link attributes 33 // Add further link attributes
25 } 34 }
......
1 package org.onlab.onos.net.link; 1 package org.onlab.onos.net.link;
2 2
3 +import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.DeviceId;
3 import org.onlab.onos.net.provider.ProviderService; 5 import org.onlab.onos.net.provider.ProviderService;
4 6
5 /** 7 /**
...@@ -21,4 +23,20 @@ public interface LinkProviderService extends ProviderService<LinkProvider> { ...@@ -21,4 +23,20 @@ public interface LinkProviderService extends ProviderService<LinkProvider> {
21 */ 23 */
22 void linkVanished(LinkDescription linkDescription); 24 void linkVanished(LinkDescription linkDescription);
23 25
26 + /**
27 + * Signals that infrastructure links associated with the specified
28 + * connect point have vanished.
29 + *
30 + * @param connectPoint connect point
31 + */
32 + void linksVanished(ConnectPoint connectPoint);
33 +
34 + /**
35 + * Signals that infrastructure links associated with the specified
36 + * device have vanished.
37 + *
38 + * @param deviceId device identifier
39 + */
40 + void linksVanished(DeviceId deviceId);
41 +
24 } 42 }
......
1 package org.onlab.onos.net.link; 1 package org.onlab.onos.net.link;
2 2
3 +import org.onlab.onos.net.ConnectPoint;
3 import org.onlab.onos.net.DeviceId; 4 import org.onlab.onos.net.DeviceId;
4 import org.onlab.onos.net.Link; 5 import org.onlab.onos.net.Link;
5 6
...@@ -11,6 +12,13 @@ import java.util.Set; ...@@ -11,6 +12,13 @@ import java.util.Set;
11 public interface LinkService { 12 public interface LinkService {
12 13
13 /** 14 /**
15 + * Returns the count of all known infrastructure links.
16 + *
17 + * @return number of infrastructure links
18 + */
19 + int getLinkCount();
20 +
21 + /**
14 * Returns a collection of all known infrastructure links. 22 * Returns a collection of all known infrastructure links.
15 * 23 *
16 * @return all infrastructure links 24 * @return all infrastructure links
...@@ -43,6 +51,43 @@ public interface LinkService { ...@@ -43,6 +51,43 @@ public interface LinkService {
43 Set<Link> getDeviceInressLinks(DeviceId deviceId); 51 Set<Link> getDeviceInressLinks(DeviceId deviceId);
44 52
45 /** 53 /**
54 + * Returns set of all infrastructure links leading to and from the
55 + * specified connection point.
56 + *
57 + * @param connectPoint connection point
58 + * @return set of links
59 + */
60 + Set<Link> getLinks(ConnectPoint connectPoint);
61 +
62 + /**
63 + * Returns set of all infrastructure links leading from the specified
64 + * connection point.
65 + *
66 + * @param connectPoint connection point
67 + * @return set of device egress links
68 + */
69 + Set<Link> getEgressLinks(ConnectPoint connectPoint);
70 +
71 + /**
72 + * Returns set of all infrastructure links leading to the specified
73 + * connection point.
74 + *
75 + * @param connectPoint connection point
76 + * @return set of device ingress links
77 + */
78 + Set<Link> getInressLinks(ConnectPoint connectPoint);
79 +
80 + /**
81 + * Returns set of all infrastructure links between the specified source
82 + * and destination connection points.
83 + *
84 + * @param src source connection point
85 + * @param dst destination connection point
86 + * @return set of links
87 + */
88 + Set<Link> getLinks(ConnectPoint src, ConnectPoint dst);
89 +
90 + /**
46 * Adds the specified link listener. 91 * Adds the specified link listener.
47 * 92 *
48 * @param listener link listener 93 * @param listener link listener
......
...@@ -2,7 +2,7 @@ package org.onlab.onos.net.provider; ...@@ -2,7 +2,7 @@ package org.onlab.onos.net.provider;
2 2
3 import java.util.Objects; 3 import java.util.Objects;
4 4
5 -import static com.google.common.base.Objects.toStringHelper; 5 +import static com.google.common.base.MoreObjects.toStringHelper;
6 6
7 /** 7 /**
8 * Notion of provider identity. 8 * Notion of provider identity.
......
...@@ -152,6 +152,7 @@ public class SimpleDeviceManager ...@@ -152,6 +152,7 @@ public class SimpleDeviceManager
152 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { 152 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
153 checkNotNull(deviceId, DEVICE_ID_NULL); 153 checkNotNull(deviceId, DEVICE_ID_NULL);
154 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); 154 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
155 + checkValidity();
155 log.info("Device {} connected", deviceId); 156 log.info("Device {} connected", deviceId);
156 DeviceEvent event = store.createOrUpdateDevice(provider().id(), 157 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
157 deviceId, deviceDescription); 158 deviceId, deviceDescription);
...@@ -161,6 +162,7 @@ public class SimpleDeviceManager ...@@ -161,6 +162,7 @@ public class SimpleDeviceManager
161 @Override 162 @Override
162 public void deviceDisconnected(DeviceId deviceId) { 163 public void deviceDisconnected(DeviceId deviceId) {
163 checkNotNull(deviceId, DEVICE_ID_NULL); 164 checkNotNull(deviceId, DEVICE_ID_NULL);
165 + checkValidity();
164 log.info("Device {} disconnected", deviceId); 166 log.info("Device {} disconnected", deviceId);
165 DeviceEvent event = store.markOffline(deviceId); 167 DeviceEvent event = store.markOffline(deviceId);
166 post(event); 168 post(event);
...@@ -170,6 +172,7 @@ public class SimpleDeviceManager ...@@ -170,6 +172,7 @@ public class SimpleDeviceManager
170 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { 172 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
171 checkNotNull(deviceId, DEVICE_ID_NULL); 173 checkNotNull(deviceId, DEVICE_ID_NULL);
172 checkNotNull(portDescriptions, "Port descriptions list cannot be null"); 174 checkNotNull(portDescriptions, "Port descriptions list cannot be null");
175 + checkValidity();
173 log.info("Device {} ports updated", deviceId); 176 log.info("Device {} ports updated", deviceId);
174 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions); 177 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
175 for (DeviceEvent event : events) { 178 for (DeviceEvent event : events) {
...@@ -181,13 +184,14 @@ public class SimpleDeviceManager ...@@ -181,13 +184,14 @@ public class SimpleDeviceManager
181 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { 184 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
182 checkNotNull(deviceId, DEVICE_ID_NULL); 185 checkNotNull(deviceId, DEVICE_ID_NULL);
183 checkNotNull(portDescription, PORT_DESCRIPTION_NULL); 186 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
187 + checkValidity();
184 log.info("Device {} port status changed", deviceId); 188 log.info("Device {} port status changed", deviceId);
185 DeviceEvent event = store.updatePortStatus(deviceId, portDescription); 189 DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
186 post(event); 190 post(event);
187 } 191 }
188 } 192 }
189 193
190 - // Posts the specified event to a local event dispatcher 194 + // Posts the specified event to the local event dispatcher.
191 private void post(DeviceEvent event) { 195 private void post(DeviceEvent event) {
192 if (event != null && eventDispatcher != null) { 196 if (event != null && eventDispatcher != null) {
193 eventDispatcher.post(event); 197 eventDispatcher.post(event);
......
...@@ -45,7 +45,7 @@ class SimpleDeviceStore { ...@@ -45,7 +45,7 @@ class SimpleDeviceStore {
45 * 45 *
46 * @return number of devices 46 * @return number of devices
47 */ 47 */
48 - public int getDeviceCount() { 48 + int getDeviceCount() {
49 return devices.size(); 49 return devices.size();
50 } 50 }
51 51
......
1 package org.onlab.onos.net.trivial.impl; 1 package org.onlab.onos.net.trivial.impl;
2 2
3 +import com.google.common.collect.Sets;
3 import org.apache.felix.scr.annotations.Activate; 4 import org.apache.felix.scr.annotations.Activate;
4 import org.apache.felix.scr.annotations.Component; 5 import org.apache.felix.scr.annotations.Component;
5 import org.apache.felix.scr.annotations.Deactivate; 6 import org.apache.felix.scr.annotations.Deactivate;
...@@ -8,16 +9,24 @@ import org.apache.felix.scr.annotations.ReferenceCardinality; ...@@ -8,16 +9,24 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
8 import org.apache.felix.scr.annotations.Service; 9 import org.apache.felix.scr.annotations.Service;
9 import org.onlab.onos.event.AbstractListenerRegistry; 10 import org.onlab.onos.event.AbstractListenerRegistry;
10 import org.onlab.onos.event.EventDeliveryService; 11 import org.onlab.onos.event.EventDeliveryService;
12 +import org.onlab.onos.net.ConnectPoint;
13 +import org.onlab.onos.net.DeviceId;
14 +import org.onlab.onos.net.Link;
15 +import org.onlab.onos.net.link.LinkAdminService;
11 import org.onlab.onos.net.link.LinkDescription; 16 import org.onlab.onos.net.link.LinkDescription;
12 import org.onlab.onos.net.link.LinkEvent; 17 import org.onlab.onos.net.link.LinkEvent;
13 import org.onlab.onos.net.link.LinkListener; 18 import org.onlab.onos.net.link.LinkListener;
14 import org.onlab.onos.net.link.LinkProvider; 19 import org.onlab.onos.net.link.LinkProvider;
15 import org.onlab.onos.net.link.LinkProviderRegistry; 20 import org.onlab.onos.net.link.LinkProviderRegistry;
16 import org.onlab.onos.net.link.LinkProviderService; 21 import org.onlab.onos.net.link.LinkProviderService;
22 +import org.onlab.onos.net.link.LinkService;
17 import org.onlab.onos.net.provider.AbstractProviderRegistry; 23 import org.onlab.onos.net.provider.AbstractProviderRegistry;
18 import org.onlab.onos.net.provider.AbstractProviderService; 24 import org.onlab.onos.net.provider.AbstractProviderService;
19 import org.slf4j.Logger; 25 import org.slf4j.Logger;
20 26
27 +import java.util.Set;
28 +
29 +import static com.google.common.base.Preconditions.checkNotNull;
21 import static org.slf4j.LoggerFactory.getLogger; 30 import static org.slf4j.LoggerFactory.getLogger;
22 31
23 /** 32 /**
...@@ -27,7 +36,11 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -27,7 +36,11 @@ import static org.slf4j.LoggerFactory.getLogger;
27 @Service 36 @Service
28 public class SimpleLinkManager 37 public class SimpleLinkManager
29 extends AbstractProviderRegistry<LinkProvider, LinkProviderService> 38 extends AbstractProviderRegistry<LinkProvider, LinkProviderService>
30 - implements LinkProviderRegistry { 39 + implements LinkService, LinkAdminService, LinkProviderRegistry {
40 +
41 + private static final String DEVICE_ID_NULL = "Device ID cannot be null";
42 + private static final String LINK_DESC_NULL = "Link description cannot be null";
43 + private static final String CONNECT_POINT_NULL = "Connection point cannot be null";
31 44
32 private final Logger log = getLogger(getClass()); 45 private final Logger log = getLogger(getClass());
33 46
...@@ -56,6 +69,82 @@ public class SimpleLinkManager ...@@ -56,6 +69,82 @@ public class SimpleLinkManager
56 return new InternalLinkProviderService(provider); 69 return new InternalLinkProviderService(provider);
57 } 70 }
58 71
72 + @Override
73 + public int getLinkCount() {
74 + return store.getLinkCount();
75 + }
76 +
77 + @Override
78 + public Iterable<Link> getLinks() {
79 + return store.getLinks();
80 + }
81 +
82 + @Override
83 + public Set<Link> getDeviceLinks(DeviceId deviceId) {
84 + checkNotNull(deviceId, DEVICE_ID_NULL);
85 + return Sets.union(store.getDeviceEgressLinks(deviceId),
86 + store.getDeviceIngressLinks(deviceId));
87 + }
88 +
89 + @Override
90 + public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
91 + checkNotNull(deviceId, DEVICE_ID_NULL);
92 + return store.getDeviceEgressLinks(deviceId);
93 + }
94 +
95 + @Override
96 + public Set<Link> getDeviceInressLinks(DeviceId deviceId) {
97 + checkNotNull(deviceId, DEVICE_ID_NULL);
98 + return store.getDeviceIngressLinks(deviceId);
99 + }
100 +
101 + @Override
102 + public Set<Link> getLinks(ConnectPoint connectPoint) {
103 + checkNotNull(connectPoint, CONNECT_POINT_NULL);
104 + return Sets.union(store.getEgressLinks(connectPoint),
105 + store.getIngressLinks(connectPoint));
106 + }
107 +
108 + @Override
109 + public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
110 + checkNotNull(connectPoint, CONNECT_POINT_NULL);
111 + return store.getEgressLinks(connectPoint);
112 + }
113 +
114 + @Override
115 + public Set<Link> getInressLinks(ConnectPoint connectPoint) {
116 + checkNotNull(connectPoint, CONNECT_POINT_NULL);
117 + return store.getIngressLinks(connectPoint);
118 + }
119 +
120 + @Override
121 + public Set<Link> getLinks(ConnectPoint src, ConnectPoint dst) {
122 + checkNotNull(src, CONNECT_POINT_NULL);
123 + checkNotNull(dst, CONNECT_POINT_NULL);
124 + return Sets.intersection(store.getEgressLinks(src),
125 + store.getIngressLinks(dst));
126 + }
127 +
128 + @Override
129 + public void removeLinks(ConnectPoint connectPoint) {
130 + removeLinks(getLinks(connectPoint));
131 + }
132 +
133 + @Override
134 + public void removeLinks(DeviceId deviceId) {
135 + removeLinks(getDeviceLinks(deviceId));
136 + }
137 +
138 + @Override
139 + public void addListener(LinkListener listener) {
140 + listenerRegistry.addListener(listener);
141 + }
142 +
143 + @Override
144 + public void removeListener(LinkListener listener) {
145 + listenerRegistry.removeListener(listener);
146 + }
147 +
59 // Personalized link provider service issued to the supplied provider. 148 // Personalized link provider service issued to the supplied provider.
60 private class InternalLinkProviderService extends AbstractProviderService<LinkProvider> 149 private class InternalLinkProviderService extends AbstractProviderService<LinkProvider>
61 implements LinkProviderService { 150 implements LinkProviderService {
...@@ -66,12 +155,54 @@ public class SimpleLinkManager ...@@ -66,12 +155,54 @@ public class SimpleLinkManager
66 155
67 @Override 156 @Override
68 public void linkDetected(LinkDescription linkDescription) { 157 public void linkDetected(LinkDescription linkDescription) {
158 + checkNotNull(linkDescription, LINK_DESC_NULL);
159 + checkValidity();
69 log.info("Link {} detected", linkDescription); 160 log.info("Link {} detected", linkDescription);
161 + LinkEvent event = store.createOrUpdateLink(provider().id(),
162 + linkDescription);
163 + post(event);
70 } 164 }
71 165
72 @Override 166 @Override
73 public void linkVanished(LinkDescription linkDescription) { 167 public void linkVanished(LinkDescription linkDescription) {
168 + checkNotNull(linkDescription, LINK_DESC_NULL);
169 + checkValidity();
74 log.info("Link {} vanished", linkDescription); 170 log.info("Link {} vanished", linkDescription);
171 + LinkEvent event = store.removeLink(linkDescription.src(),
172 + linkDescription.dst());
173 + post(event);
75 } 174 }
175 +
176 + @Override
177 + public void linksVanished(ConnectPoint connectPoint) {
178 + checkNotNull(connectPoint, "Connect point cannot be null");
179 + checkValidity();
180 + log.info("Link for connection point {} vanished", connectPoint);
181 + removeLinks(getLinks(connectPoint));
76 } 182 }
183 +
184 + @Override
185 + public void linksVanished(DeviceId deviceId) {
186 + checkNotNull(deviceId, DEVICE_ID_NULL);
187 + checkValidity();
188 + log.info("Link for device {} vanished", deviceId);
189 + removeLinks(getDeviceLinks(deviceId));
190 + }
191 + }
192 +
193 + // Removes all links in the specified set and emits appropriate events.
194 + private void removeLinks(Set<Link> links) {
195 + for (Link link : links) {
196 + LinkEvent event = store.removeLink(link.src(), link.dst());
197 + post(event);
198 + }
199 + }
200 +
201 + // Posts the specified event to the local event dispatcher.
202 + private void post(LinkEvent event) {
203 + if (event != null && eventDispatcher != null) {
204 + eventDispatcher.post(event);
205 + }
206 + }
207 +
77 } 208 }
......
1 package org.onlab.onos.net.trivial.impl; 1 package org.onlab.onos.net.trivial.impl;
2 2
3 +import com.google.common.collect.HashMultimap;
4 +import com.google.common.collect.ImmutableSet;
5 +import com.google.common.collect.Multimap;
6 +import org.onlab.onos.net.ConnectPoint;
7 +import org.onlab.onos.net.DefaultLink;
8 +import org.onlab.onos.net.DeviceId;
9 +import org.onlab.onos.net.Link;
10 +import org.onlab.onos.net.link.LinkDescription;
11 +import org.onlab.onos.net.link.LinkEvent;
12 +import org.onlab.onos.net.provider.ProviderId;
13 +
14 +import java.util.Collections;
15 +import java.util.HashSet;
16 +import java.util.Map;
17 +import java.util.Objects;
18 +import java.util.Set;
19 +import java.util.concurrent.ConcurrentHashMap;
20 +
3 /** 21 /**
4 * Manages inventory of infrastructure links using trivial in-memory link 22 * Manages inventory of infrastructure links using trivial in-memory link
5 * implementation. 23 * implementation.
6 */ 24 */
7 -public class SimpleLinkStore { 25 +class SimpleLinkStore {
26 +
27 + // Link inventory
28 + private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
29 +
30 + // Egress and ingress link sets
31 + private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
32 + private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
33 +
34 + private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{});
35 +
36 + /**
37 + * Returns the number of links in the store.
38 + *
39 + * @return number of links
40 + */
41 + int getLinkCount() {
42 + return links.size();
43 + }
44 +
45 + /**
46 + * Returns an iterable collection of all links in the inventory.
47 + *
48 + * @return collection of all links
49 + */
50 + Iterable<Link> getLinks() {
51 + return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
52 + }
53 +
54 + /**
55 + * Returns all links egressing from the specified device.
56 + *
57 + * @param deviceId device identifier
58 + * @return set of device links
59 + */
60 + Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
61 + return ImmutableSet.copyOf(srcLinks.get(deviceId));
62 + }
63 +
64 + /**
65 + * Returns all links ingressing from the specified device.
66 + *
67 + * @param deviceId device identifier
68 + * @return set of device links
69 + */
70 + Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
71 + return ImmutableSet.copyOf(dstLinks.get(deviceId));
72 + }
73 +
74 + /**
75 + * Returns all links egressing from the specified connection point.
76 + *
77 + * @param src source connection point
78 + * @return set of connection point links
79 + */
80 + Set<Link> getEgressLinks(ConnectPoint src) {
81 + Set<Link> egress = new HashSet<>();
82 + for (Link link : srcLinks.get(src.deviceId())) {
83 + if (link.src().equals(src)) {
84 + egress.add(link);
85 + }
86 + }
87 + return egress;
88 + }
89 +
90 + /**
91 + * Returns all links ingressing to the specified connection point.
92 + *
93 + * @param dst destination connection point
94 + * @return set of connection point links
95 + */
96 + Set<Link> getIngressLinks(ConnectPoint dst) {
97 + Set<Link> ingress = new HashSet<>();
98 + for (Link link : dstLinks.get(dst.deviceId())) {
99 + if (link.src().equals(dst)) {
100 + ingress.add(link);
101 + }
102 + }
103 + return ingress;
104 + }
105 +
106 +
107 + /**
108 + * Creates a new link, or updates an existing one, based on the given
109 + * information.
110 + *
111 + * @param providerId provider identity
112 + * @param linkDescription link description
113 + * @return create or update link event, or null if no change resulted
114 + */
115 + public LinkEvent createOrUpdateLink(ProviderId providerId,
116 + LinkDescription linkDescription) {
117 + LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
118 + DefaultLink link = links.get(key);
119 + if (link == null) {
120 + return createLink(providerId, key, linkDescription);
121 + }
122 + return updateLink(link, linkDescription);
123 + }
124 +
125 + // Creates and stores the link and returns the appropriate event.
126 + private LinkEvent createLink(ProviderId providerId, LinkKey key,
127 + LinkDescription linkDescription) {
128 + DefaultLink link = new DefaultLink(providerId, key.src, key.dst,
129 + linkDescription.type());
130 + synchronized (this) {
131 + links.put(key, link);
132 + srcLinks.put(link.src().deviceId(), link);
133 + dstLinks.put(link.dst().deviceId(), link);
134 + }
135 + return new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
136 + }
137 +
138 + // Updates, if necessary the specified link and returns the appropriate event.
139 + private LinkEvent updateLink(DefaultLink link, LinkDescription linkDescription) {
140 + return null;
141 + }
142 +
143 + /**
144 + * Removes the link based on the specified information.
145 + *
146 + * @param src link source
147 + * @param dst link destination
148 + * @return remove link event, or null if no change resulted
149 + */
150 + LinkEvent removeLink(ConnectPoint src, ConnectPoint dst) {
151 + synchronized (this) {
152 + Link link = links.remove(new LinkKey(src, dst));
153 + srcLinks.remove(link.src().deviceId(), link);
154 + dstLinks.remove(link.dst().deviceId(), link);
155 + return link == null ? null : new LinkEvent(LinkEvent.Type.LINK_REMOVED, link);
156 + }
157 + }
158 +
159 + // Auxiliary key to track links.
160 + private class LinkKey {
161 + final ConnectPoint src;
162 + final ConnectPoint dst;
163 +
164 + LinkKey(ConnectPoint src, ConnectPoint dst) {
165 + this.src = src;
166 + this.dst = dst;
167 + }
168 +
169 + @Override
170 + public int hashCode() {
171 + return Objects.hash(src, dst);
172 + }
173 +
174 + @Override
175 + public boolean equals(Object obj) {
176 + if (obj instanceof LinkKey) {
177 + final LinkKey other = (LinkKey) obj;
178 + return Objects.equals(this.src, other.src) &&
179 + Objects.equals(this.dst, other.dst);
180 + }
181 + return false;
182 + }
183 + }
8 } 184 }
......
...@@ -58,13 +58,13 @@ ...@@ -58,13 +58,13 @@
58 <dependency> 58 <dependency>
59 <groupId>com.google.guava</groupId> 59 <groupId>com.google.guava</groupId>
60 <artifactId>guava</artifactId> 60 <artifactId>guava</artifactId>
61 - <version>17.0</version> 61 + <version>18.0</version>
62 </dependency> 62 </dependency>
63 63
64 <dependency> 64 <dependency>
65 <groupId>com.google.guava</groupId> 65 <groupId>com.google.guava</groupId>
66 <artifactId>guava-testlib</artifactId> 66 <artifactId>guava-testlib</artifactId>
67 - <version>17.0</version> 67 + <version>18.0</version>
68 <scope>test</scope> 68 <scope>test</scope>
69 </dependency> 69 </dependency>
70 70
......
...@@ -2,6 +2,7 @@ package org.onlab.graph; ...@@ -2,6 +2,7 @@ package org.onlab.graph;
2 2
3 import java.util.Objects; 3 import java.util.Objects;
4 4
5 +import static com.google.common.base.MoreObjects.toStringHelper;
5 import static com.google.common.base.Preconditions.checkNotNull; 6 import static com.google.common.base.Preconditions.checkNotNull;
6 7
7 /** 8 /**
...@@ -49,7 +50,7 @@ public abstract class AbstractEdge<V extends Vertex> implements Edge<V> { ...@@ -49,7 +50,7 @@ public abstract class AbstractEdge<V extends Vertex> implements Edge<V> {
49 50
50 @Override 51 @Override
51 public String toString() { 52 public String toString() {
52 - return com.google.common.base.Objects.toStringHelper(this) 53 + return toStringHelper(this)
53 .add("src", src) 54 .add("src", src)
54 .add("dst", dst) 55 .add("dst", dst)
55 .toString(); 56 .toString();
......
...@@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSetMultimap; ...@@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableSetMultimap;
6 import java.util.Objects; 6 import java.util.Objects;
7 import java.util.Set; 7 import java.util.Set;
8 8
9 +import static com.google.common.base.MoreObjects.toStringHelper;
9 import static com.google.common.base.Preconditions.checkNotNull; 10 import static com.google.common.base.Preconditions.checkNotNull;
10 11
11 /** 12 /**
...@@ -94,7 +95,7 @@ public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>> implements ...@@ -94,7 +95,7 @@ public class AdjacencyListsGraph<V extends Vertex, E extends Edge<V>> implements
94 95
95 @Override 96 @Override
96 public String toString() { 97 public String toString() {
97 - return com.google.common.base.Objects.toStringHelper(this) 98 + return toStringHelper(this)
98 .add("vertexes", vertexes) 99 .add("vertexes", vertexes)
99 .add("edges", edges) 100 .add("edges", edges)
100 .toString(); 101 .toString();
......
...@@ -6,6 +6,7 @@ import java.util.ArrayList; ...@@ -6,6 +6,7 @@ import java.util.ArrayList;
6 import java.util.List; 6 import java.util.List;
7 import java.util.Objects; 7 import java.util.Objects;
8 8
9 +import static com.google.common.base.MoreObjects.toStringHelper;
9 import static com.google.common.base.Preconditions.checkArgument; 10 import static com.google.common.base.Preconditions.checkArgument;
10 import static com.google.common.base.Preconditions.checkNotNull; 11 import static com.google.common.base.Preconditions.checkNotNull;
11 12
...@@ -91,7 +92,7 @@ public class DefaultMutablePath<V extends Vertex, E extends Edge<V>> implements ...@@ -91,7 +92,7 @@ public class DefaultMutablePath<V extends Vertex, E extends Edge<V>> implements
91 92
92 @Override 93 @Override
93 public String toString() { 94 public String toString() {
94 - return com.google.common.base.Objects.toStringHelper(this) 95 + return toStringHelper(this)
95 .add("src", src()) 96 .add("src", src())
96 .add("dst", dst()) 97 .add("dst", dst())
97 .add("cost", cost) 98 .add("cost", cost)
......
...@@ -6,6 +6,7 @@ import java.util.Collections; ...@@ -6,6 +6,7 @@ import java.util.Collections;
6 import java.util.List; 6 import java.util.List;
7 import java.util.Objects; 7 import java.util.Objects;
8 8
9 +import static com.google.common.base.MoreObjects.toStringHelper;
9 import static com.google.common.base.Preconditions.checkArgument; 10 import static com.google.common.base.Preconditions.checkArgument;
10 import static com.google.common.base.Preconditions.checkNotNull; 11 import static com.google.common.base.Preconditions.checkNotNull;
11 12
...@@ -56,7 +57,7 @@ public class DefaultPath<V extends Vertex, E extends Edge<V>> implements Path<V, ...@@ -56,7 +57,7 @@ public class DefaultPath<V extends Vertex, E extends Edge<V>> implements Path<V,
56 57
57 @Override 58 @Override
58 public String toString() { 59 public String toString() {
59 - return com.google.common.base.Objects.toStringHelper(this) 60 + return toStringHelper(this)
60 .add("src", src) 61 .add("src", src)
61 .add("dst", dst) 62 .add("dst", dst)
62 .add("cost", cost) 63 .add("cost", cost)
......
...@@ -7,6 +7,7 @@ import java.util.Iterator; ...@@ -7,6 +7,7 @@ import java.util.Iterator;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Objects; 8 import java.util.Objects;
9 9
10 +import static com.google.common.base.MoreObjects.toStringHelper;
10 import static com.google.common.base.Preconditions.checkNotNull; 11 import static com.google.common.base.Preconditions.checkNotNull;
11 12
12 /** 13 /**
...@@ -181,7 +182,7 @@ public class Heap<T> { ...@@ -181,7 +182,7 @@ public class Heap<T> {
181 182
182 @Override 183 @Override
183 public String toString() { 184 public String toString() {
184 - return com.google.common.base.Objects.toStringHelper(this) 185 + return toStringHelper(this)
185 .add("data", data) 186 .add("data", data)
186 .add("comparator", comparator) 187 .add("comparator", comparator)
187 .toString(); 188 .toString();
......
...@@ -2,7 +2,7 @@ package org.onlab.graph; ...@@ -2,7 +2,7 @@ package org.onlab.graph;
2 2
3 import java.util.Objects; 3 import java.util.Objects;
4 4
5 -import static com.google.common.base.Objects.toStringHelper; 5 +import static com.google.common.base.MoreObjects.toStringHelper;
6 6
7 /** 7 /**
8 * Test edge. 8 * Test edge.
......