alshabib

merge pom.xml

Showing 46 changed files with 1453 additions and 102 deletions
...@@ -10,15 +10,25 @@ import org.osgi.framework.FrameworkUtil; ...@@ -10,15 +10,25 @@ import org.osgi.framework.FrameworkUtil;
10 public abstract class AbstractShellCommand extends OsgiCommandSupport { 10 public abstract class AbstractShellCommand extends OsgiCommandSupport {
11 11
12 /** 12 /**
13 - * Returns the reference to the implementaiton of the specified service. 13 + * Returns the reference to the implementation of the specified service.
14 * 14 *
15 * @param serviceClass service class 15 * @param serviceClass service class
16 * @param <T> type of service 16 * @param <T> type of service
17 * @return service implementation 17 * @return service implementation
18 */ 18 */
19 - static <T> T get(Class<T> serviceClass) { 19 + public static <T> T get(Class<T> serviceClass) {
20 BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext(); 20 BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext();
21 return bc.getService(bc.getServiceReference(serviceClass)); 21 return bc.getService(bc.getServiceReference(serviceClass));
22 } 22 }
23 23
24 + /**
25 + * Prints the arguments using the specified format.
26 + *
27 + * @param format format string; see {@link String#format}
28 + * @param args arguments
29 + */
30 + public static void print(String format, Object... args) {
31 + System.out.println(String.format(format, args));
32 + }
33 +
24 } 34 }
......
...@@ -2,7 +2,6 @@ package org.onlab.onos.cli; ...@@ -2,7 +2,6 @@ package org.onlab.onos.cli;
2 2
3 import org.apache.karaf.shell.commands.Argument; 3 import org.apache.karaf.shell.commands.Argument;
4 import org.apache.karaf.shell.commands.Command; 4 import org.apache.karaf.shell.commands.Command;
5 -import org.apache.karaf.shell.console.OsgiCommandSupport;
6 import org.onlab.onos.GreetService; 5 import org.onlab.onos.GreetService;
7 6
8 /** 7 /**
...@@ -10,7 +9,7 @@ import org.onlab.onos.GreetService; ...@@ -10,7 +9,7 @@ import org.onlab.onos.GreetService;
10 * use of an optional parameter as well. 9 * use of an optional parameter as well.
11 */ 10 */
12 @Command(scope = "onos", name = "greet", description = "Issues a greeting") 11 @Command(scope = "onos", name = "greet", description = "Issues a greeting")
13 -public class GreetCommand extends OsgiCommandSupport { 12 +public class GreetCommand extends AbstractShellCommand {
14 13
15 @Argument(index = 0, name = "name", description = "Name to greet", 14 @Argument(index = 0, name = "name", description = "Name to greet",
16 required = false, multiValued = false) 15 required = false, multiValued = false)
...@@ -18,7 +17,7 @@ public class GreetCommand extends OsgiCommandSupport { ...@@ -18,7 +17,7 @@ public class GreetCommand extends OsgiCommandSupport {
18 17
19 @Override 18 @Override
20 protected Object doExecute() throws Exception { 19 protected Object doExecute() throws Exception {
21 - System.out.println(getService(GreetService.class).yo(name)); 20 + print(getService(GreetService.class).yo(name));
22 return null; 21 return null;
23 } 22 }
24 } 23 }
......
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.console.Completer;
4 +import org.apache.karaf.shell.console.completer.StringsCompleter;
5 +import org.onlab.onos.cli.AbstractShellCommand;
6 +import org.onlab.onos.net.Device;
7 +import org.onlab.onos.net.device.DeviceService;
8 +
9 +import java.util.Iterator;
10 +import java.util.List;
11 +import java.util.SortedSet;
12 +
13 +/**
14 + * Device ID completer.
15 + */
16 +public class DeviceIdCompleter implements Completer {
17 + @Override
18 + public int complete(String buffer, int cursor, List<String> candidates) {
19 + // Delegate string completer
20 + StringsCompleter delegate = new StringsCompleter();
21 +
22 + // Fetch our service and feed it's offerings to the string completer
23 + DeviceService service = AbstractShellCommand.get(DeviceService.class);
24 + Iterator<Device> it = service.getDevices().iterator();
25 + SortedSet<String> strings = delegate.getStrings();
26 + while (it.hasNext()) {
27 + strings.add(it.next().id().toString());
28 + }
29 +
30 + // Now let the completer do the work for figuring out what to offer.
31 + return delegate.complete(buffer, cursor, candidates);
32 + }
33 +
34 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.commands.Argument;
4 +import org.apache.karaf.shell.commands.Command;
5 +import org.onlab.onos.net.Device;
6 +import org.onlab.onos.net.Port;
7 +import org.onlab.onos.net.device.DeviceService;
8 +
9 +import java.util.ArrayList;
10 +import java.util.Collections;
11 +import java.util.Comparator;
12 +import java.util.List;
13 +
14 +import static org.onlab.onos.net.DeviceId.deviceId;
15 +
16 +/**
17 + * Lists all ports or all ports of a device.
18 + */
19 +@Command(scope = "onos", name = "ports",
20 + description = "Lists all ports or all ports of a device")
21 +public class DevicePortsListCommand extends DevicesListCommand {
22 +
23 + private static final String FMT = " port=%s, state=%s";
24 +
25 + @Argument(index = 0, name = "uri", description = "Device ID",
26 + required = false, multiValued = false)
27 + String uri = null;
28 +
29 + private static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() {
30 + @Override
31 + public int compare(Port p1, Port p2) {
32 + long delta = p1.number().toLong() - p2.number().toLong();
33 + return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
34 + }
35 + };
36 +
37 + @Override
38 + protected Object doExecute() throws Exception {
39 + DeviceService service = getService(DeviceService.class);
40 + if (uri == null) {
41 + for (Device device : getSortedDevices(service)) {
42 + printDevice(service, device);
43 + }
44 + } else {
45 + printDevice(service, service.getDevice(deviceId(uri)));
46 + }
47 + return null;
48 + }
49 +
50 + @Override
51 + protected void printDevice(DeviceService service, Device device) {
52 + super.printDevice(service, device);
53 + List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
54 + Collections.sort(ports, PORT_COMPARATOR);
55 + for (Port port : ports) {
56 + print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
57 + }
58 + }
59 +
60 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.commands.Argument;
4 +import org.apache.karaf.shell.commands.Command;
5 +import org.onlab.onos.cli.AbstractShellCommand;
6 +import org.onlab.onos.net.DeviceId;
7 +import org.onlab.onos.net.device.DeviceAdminService;
8 +
9 +/**
10 + * Removes an infrastructure device.
11 + */
12 +@Command(scope = "onos", name = "device-remove",
13 + description = "Removes an infrastructure device")
14 +public class DeviceRemoveCommand extends AbstractShellCommand {
15 +
16 + @Argument(index = 0, name = "uri", description = "Device ID",
17 + required = true, multiValued = false)
18 + String uri = null;
19 +
20 + @Override
21 + protected Object doExecute() throws Exception {
22 + getService(DeviceAdminService.class).removeDevice(DeviceId.deviceId(uri));
23 + return null;
24 + }
25 +
26 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.commands.Command;
4 +import org.onlab.onos.cli.AbstractShellCommand;
5 +import org.onlab.onos.net.Device;
6 +import org.onlab.onos.net.device.DeviceService;
7 +
8 +import java.util.Collections;
9 +import java.util.Comparator;
10 +import java.util.List;
11 +
12 +import static com.google.common.collect.Lists.newArrayList;
13 +
14 +/**
15 + * Lists all infrastructure devices.
16 + */
17 +@Command(scope = "onos", name = "devices",
18 + description = "Lists all infrastructure devices")
19 +public class DevicesListCommand extends AbstractShellCommand {
20 +
21 + private static final String FMT =
22 + "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s";
23 +
24 + protected static final Comparator<Device> ID_COMPARATOR = new Comparator<Device>() {
25 + @Override
26 + public int compare(Device d1, Device d2) {
27 + return d1.id().uri().toString().compareTo(d2.id().uri().toString());
28 + }
29 + };
30 +
31 + @Override
32 + protected Object doExecute() throws Exception {
33 + DeviceService service = getService(DeviceService.class);
34 + for (Device device : getSortedDevices(service)) {
35 + printDevice(service, device);
36 + }
37 + return null;
38 + }
39 +
40 + /**
41 + * Returns the list of devices sorted using the device ID URIs.
42 + *
43 + * @param service device service
44 + * @return sorted device list
45 + */
46 + protected List<Device> getSortedDevices(DeviceService service) {
47 + List<Device> devices = newArrayList(service.getDevices());
48 + Collections.sort(devices, ID_COMPARATOR);
49 + return devices;
50 + }
51 +
52 + /**
53 + * Prints information about the specified device.
54 + *
55 + * @param service device service
56 + * @param device infrastructure device
57 + */
58 + protected void printDevice(DeviceService service, Device device) {
59 + print(FMT, device.id(), service.isAvailable(device.id()),
60 + service.getRole(device.id()), device.type(),
61 + device.manufacturer(), device.hwVersion(), device.swVersion(),
62 + device.serialNumber());
63 + }
64 +
65 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import org.apache.karaf.shell.commands.Argument;
4 +import org.apache.karaf.shell.commands.Command;
5 +import org.onlab.onos.cli.AbstractShellCommand;
6 +import org.onlab.onos.net.Link;
7 +import org.onlab.onos.net.link.LinkService;
8 +
9 +import static org.onlab.onos.net.DeviceId.deviceId;
10 +
11 +/**
12 + * Lists all infrastructure links.
13 + */
14 +@Command(scope = "onos", name = "links",
15 + description = "Lists all infrastructure links")
16 +public class LinksListCommand extends AbstractShellCommand {
17 +
18 + private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
19 +
20 + @Argument(index = 0, name = "uri", description = "Device ID",
21 + required = false, multiValued = false)
22 + String uri = null;
23 +
24 + @Override
25 + protected Object doExecute() throws Exception {
26 + LinkService service = getService(LinkService.class);
27 + Iterable<Link> links = uri != null ?
28 + service.getDeviceLinks(deviceId(uri)) : service.getLinks();
29 + for (Link link : links) {
30 + print(FMT, link.src().deviceId(), link.src().port(),
31 + link.dst().deviceId(), link.dst().port(), link.type());
32 + }
33 + return null;
34 + }
35 +}
...@@ -2,6 +2,29 @@ ...@@ -2,6 +2,29 @@
2 2
3 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> 3 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
4 <command> 4 <command>
5 + <action class="org.onlab.onos.cli.net.DevicesListCommand"/>
6 + </command>
7 + <command>
8 + <action class="org.onlab.onos.cli.net.DevicePortsListCommand"/>
9 + <completers>
10 + <ref component-id="deviceIdCompleter"/>
11 + </completers>
12 + </command>
13 + <command>
14 + <action class="org.onlab.onos.cli.net.DeviceRemoveCommand"/>
15 + <completers>
16 + <ref component-id="deviceIdCompleter"/>
17 + </completers>
18 + </command>
19 +
20 + <command>
21 + <action class="org.onlab.onos.cli.net.LinksListCommand"/>
22 + <completers>
23 + <ref component-id="deviceIdCompleter"/>
24 + </completers>
25 + </command>
26 +
27 + <command>
5 <action class="org.onlab.onos.cli.GreetCommand"/> 28 <action class="org.onlab.onos.cli.GreetCommand"/>
6 <completers> 29 <completers>
7 <ref component-id="nameCompleter"/> 30 <ref component-id="nameCompleter"/>
...@@ -9,6 +32,8 @@ ...@@ -9,6 +32,8 @@
9 </command> 32 </command>
10 </command-bundle> 33 </command-bundle>
11 34
35 + <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
36 +
12 <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/> 37 <bean id="nameCompleter" class="org.onlab.onos.cli.NameCompleter"/>
13 38
14 </blueprint> 39 </blueprint>
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
5 5
6 <feature name="onos-thirdparty-base" version="1.0.0" 6 <feature name="onos-thirdparty-base" version="1.0.0"
7 description="ONOS 3rd party dependencies"> 7 description="ONOS 3rd party dependencies">
8 - <bundle>mvn:com.google.guava/guava/17.0</bundle> 8 + <bundle>mvn:commons-lang/commons-lang/2.6</bundle>
9 + <bundle>mvn:com.google.guava/guava/18.0</bundle>
9 </feature> 10 </feature>
10 11
11 <feature name="onos-thirdparty-web" version="1.0.0" 12 <feature name="onos-thirdparty-web" version="1.0.0"
......
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 +import java.util.Objects;
4 +
5 +import static com.google.common.base.MoreObjects.toStringHelper;
6 +
3 /** 7 /**
4 * Abstraction of a network connection point expressed as a pair of the 8 * Abstraction of a network connection point expressed as a pair of the
5 - * device identifier and the device port number. 9 + * network element identifier and port number.
6 */ 10 */
7 -public interface ConnectPoint { 11 +public class ConnectPoint {
12 +
13 + private final ElementId elementId;
14 + private final PortNumber portNumber;
15 +
16 + /**
17 + * Creates a new connection point.
18 + *
19 + * @param elementId network element identifier
20 + * @param portNumber port number
21 + */
22 + public ConnectPoint(ElementId elementId, PortNumber portNumber) {
23 + this.elementId = elementId;
24 + this.portNumber = portNumber;
25 + }
8 26
9 /** 27 /**
10 - * Returns the connection device identifier. 28 + * Returns the network element identifier.
11 * 29 *
12 - * @return device id 30 + * @return element identifier
13 */ 31 */
14 - DeviceId deviceId(); 32 + public ElementId elementId() {
33 + return elementId;
34 + }
35 +
36 + /**
37 + * Returns the identifier of the infrastructure device if the connection
38 + * point belongs to a network element which is indeed an infrastructure
39 + * device.
40 + *
41 + * @return network element identifier as a device identifier
42 + * @throws java.lang.IllegalStateException if connection point is not
43 + * associated with a device
44 + */
45 + @SuppressWarnings("unchecked")
46 + public DeviceId deviceId() {
47 + if (elementId instanceof DeviceId) {
48 + return (DeviceId) elementId;
49 + }
50 + throw new IllegalStateException("Connection point not associated " +
51 + "with an infrastructure device");
52 + }
15 53
16 /** 54 /**
17 * Returns the connection port number. 55 * Returns the connection port number.
18 * 56 *
19 * @return port number 57 * @return port number
20 */ 58 */
21 - PortNumber port(); 59 + public PortNumber port() {
60 + return portNumber;
61 + }
62 +
63 + @Override
64 + public int hashCode() {
65 + return Objects.hash(elementId, portNumber);
66 + }
67 +
68 + @Override
69 + public boolean equals(Object obj) {
70 + if (obj instanceof ConnectPoint) {
71 + final ConnectPoint other = (ConnectPoint) obj;
72 + return Objects.equals(this.elementId, other.elementId) &&
73 + Objects.equals(this.portNumber, other.portNumber);
74 + }
75 + return false;
76 + }
77 +
78 + @Override
79 + public String toString() {
80 + return toStringHelper(this)
81 + .add("elementId", elementId)
82 + .add("portNumber", portNumber)
83 + .toString();
84 + }
22 85
23 } 86 }
......
...@@ -4,10 +4,10 @@ import org.onlab.onos.net.provider.ProviderId; ...@@ -4,10 +4,10 @@ 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 device model implementation. 10 + * Default infrastructure device model implementation.
11 */ 11 */
12 public class DefaultDevice extends AbstractElement implements Device { 12 public class DefaultDevice extends AbstractElement implements Device {
13 13
......
1 +package org.onlab.onos.net;
2 +
3 +import org.onlab.onos.net.provider.ProviderId;
4 +
5 +import java.util.Objects;
6 +
7 +import static com.google.common.base.MoreObjects.toStringHelper;
8 +
9 +/**
10 + * Default infrastructure link model implementation.
11 + */
12 +public class DefaultLink extends AbstractModel implements Link {
13 +
14 + private final ConnectPoint src;
15 + private final ConnectPoint dst;
16 + private final Type type;
17 +
18 + /**
19 + * Creates a link description using the supplied information.
20 + *
21 + * @param providerId provider identity
22 + * @param src link source
23 + * @param dst link destination
24 + * @param type link type
25 + */
26 + public DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
27 + Type type) {
28 + super(providerId);
29 + this.src = src;
30 + this.dst = dst;
31 + this.type = type;
32 + }
33 +
34 + @Override
35 + public ConnectPoint src() {
36 + return src;
37 + }
38 +
39 + @Override
40 + public ConnectPoint dst() {
41 + return dst;
42 + }
43 +
44 + @Override
45 + public Type type() {
46 + return type;
47 + }
48 +
49 + @Override
50 + public int hashCode() {
51 + return Objects.hash(src, dst, type);
52 + }
53 +
54 + @Override
55 + public boolean equals(Object obj) {
56 + if (obj instanceof DefaultLink) {
57 + final DefaultLink other = (DefaultLink) obj;
58 + return Objects.equals(this.src, other.src) &&
59 + Objects.equals(this.dst, other.dst) &&
60 + Objects.equals(this.type, other.type);
61 + }
62 + return false;
63 + }
64 +
65 + @Override
66 + public String toString() {
67 + return toStringHelper(this)
68 + .add("src", src)
69 + .add("dst", dst)
70 + .add("type", type)
71 + .toString();
72 + }
73 +
74 +}
...@@ -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,8 +3,6 @@ package org.onlab.onos.net; ...@@ -3,8 +3,6 @@ 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;
7 -
8 /** 6 /**
9 * Immutable representation of a network element identity. 7 * Immutable representation of a network element identity.
10 */ 8 */
...@@ -47,7 +45,7 @@ public abstract class ElementId { ...@@ -47,7 +45,7 @@ public abstract class ElementId {
47 45
48 @Override 46 @Override
49 public String toString() { 47 public String toString() {
50 - return toStringHelper(this).add("uri", uri).toString(); 48 + return uri.toString();
51 } 49 }
52 50
53 } 51 }
......
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 +import java.util.Objects;
4 +
3 /** 5 /**
4 * Representation of a network edge location where an end-station host is 6 * Representation of a network edge location where an end-station host is
5 * connected. 7 * connected.
6 */ 8 */
7 -public interface HostLocation extends ConnectPoint { 9 +public class HostLocation extends ConnectPoint {
10 +
11 + private final long time;
12 +
13 + public HostLocation(DeviceId deviceId, PortNumber portNumber, long time) {
14 + super(deviceId, portNumber);
15 + this.time = time;
16 + }
8 17
9 /** 18 /**
10 * Returns the timestamp when the location was established, given in 19 * Returns the timestamp when the location was established, given in
...@@ -12,6 +21,22 @@ public interface HostLocation extends ConnectPoint { ...@@ -12,6 +21,22 @@ public interface HostLocation extends ConnectPoint {
12 * 21 *
13 * @return timestamp in milliseconds since start of epoch 22 * @return timestamp in milliseconds since start of epoch
14 */ 23 */
15 - long time(); 24 + public long time() {
25 + return time;
26 + }
27 +
28 + @Override
29 + public int hashCode() {
30 + return 31 * super.hashCode() + Objects.hash(time);
31 + }
32 +
33 + @Override
34 + public boolean equals(Object obj) {
35 + if (obj instanceof HostLocation) {
36 + final HostLocation other = (HostLocation) obj;
37 + return super.equals(obj) && Objects.equals(this.time, other.time);
38 + }
39 + return false;
40 + }
16 41
17 } 42 }
......
...@@ -4,7 +4,6 @@ package org.onlab.onos.net; ...@@ -4,7 +4,6 @@ package org.onlab.onos.net;
4 * Abstraction of a network infrastructure link. 4 * Abstraction of a network infrastructure link.
5 */ 5 */
6 public interface Link extends Provided { 6 public interface Link extends Provided {
7 -// TODO: Consider extending graph Edge<Element> once the graph module is available
8 7
9 /** 8 /**
10 * Coarse representation of the link type. 9 * Coarse representation of the link type.
...@@ -38,6 +37,13 @@ public interface Link extends Provided { ...@@ -38,6 +37,13 @@ public interface Link extends Provided {
38 */ 37 */
39 ConnectPoint dst(); 38 ConnectPoint dst();
40 39
40 + /**
41 + * Returns the link type.
42 + *
43 + * @return link type
44 + */
45 + Type type();
46 +
41 // LinkInfo info(); // Additional link information / decorations 47 // LinkInfo info(); // Additional link information / decorations
42 48
43 } 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
......
...@@ -55,13 +55,22 @@ public interface DeviceService { ...@@ -55,13 +55,22 @@ public interface DeviceService {
55 55
56 /** 56 /**
57 * Returns the port with the specified number and hosted by the given device. 57 * Returns the port with the specified number and hosted by the given device.
58 - * @param deviceId device identifier 58 + *
59 + * @param deviceId device identifier
59 * @param portNumber port number 60 * @param portNumber port number
60 * @return device port 61 * @return device port
61 */ 62 */
62 Port getPort(DeviceId deviceId, PortNumber portNumber); 63 Port getPort(DeviceId deviceId, PortNumber portNumber);
63 64
64 /** 65 /**
66 + * Indicates whether or not the device is presently online and available.
67 + *
68 + * @param deviceId device identifier
69 + * @return true if the device is available
70 + */
71 + boolean isAvailable(DeviceId deviceId);
72 +
73 + /**
65 * Adds the specified device listener. 74 * Adds the specified device listener.
66 * 75 *
67 * @param listener device listener 76 * @param listener device listener
......
1 +package org.onlab.onos.net.link;
2 +
3 +import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.Link;
5 +
6 +/**
7 + * Default implementation of immutable link description entity.
8 + */
9 +public class DefaultLinkDescription implements LinkDescription {
10 +
11 + private final ConnectPoint src;
12 + private final ConnectPoint dst;
13 + private final Link.Type type;
14 +
15 + /**
16 + * Creates a link description using the supplied information.
17 + *
18 + * @param src link source
19 + * @param dst link destination
20 + * @param type link type
21 + */
22 + public DefaultLinkDescription(ConnectPoint src, ConnectPoint dst, Link.Type type) {
23 + this.src = src;
24 + this.dst = dst;
25 + this.type = type;
26 + }
27 +
28 + @Override
29 + public ConnectPoint src() {
30 + return src;
31 + }
32 +
33 + @Override
34 + public ConnectPoint dst() {
35 + return dst;
36 + }
37 +
38 + @Override
39 + public Link.Type type() {
40 + return type;
41 + }
42 +
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;
4 +import org.onlab.onos.net.Link;
5 +
3 /** 6 /**
4 * Describes an infrastructure link. 7 * Describes an infrastructure link.
5 */ 8 */
6 public interface LinkDescription { 9 public interface LinkDescription {
7 10
8 - // TODO: src, dst connection points, which are pairs of (DeviceId, PortNumber) 11 + /**
12 + * Returns the link source.
13 + *
14 + * @return links source
15 + */
16 + ConnectPoint src();
17 +
18 + /**
19 + * Returns the link destination.
20 + *
21 + * @return links destination
22 + */
23 + ConnectPoint dst();
9 24
10 -// On the north: 25 + /**
11 -// Link = (ConnectPoint src, ConnectPoint dst); 26 + * Returns the link type.
12 -// ConnectPoint = (DeviceId, PortNumber); 27 + *
28 + * @return link type
29 + */
30 + Link.Type type();
13 31
14 -// On the south
15 -// LinkDescription ~ Link
16 32
33 + // Add further link attributes
17 } 34 }
......
...@@ -18,6 +18,11 @@ public class LinkEvent extends AbstractEvent<LinkEvent.Type, Link> { ...@@ -18,6 +18,11 @@ public class LinkEvent extends AbstractEvent<LinkEvent.Type, Link> {
18 LINK_ADDED, 18 LINK_ADDED,
19 19
20 /** 20 /**
21 + * Signifies that a link has been updated.
22 + */
23 + LINK_UPDATED,
24 +
25 + /**
21 * Signifies that a link has been removed. 26 * Signifies that a link has been removed.
22 */ 27 */
23 LINK_REMOVED 28 LINK_REMOVED
......
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
...@@ -40,7 +48,44 @@ public interface LinkService { ...@@ -40,7 +48,44 @@ public interface LinkService {
40 * @param deviceId device identifier 48 * @param deviceId device identifier
41 * @return set of device ingress links 49 * @return set of device ingress links
42 */ 50 */
43 - Set<Link> getDeviceInressLinks(DeviceId deviceId); 51 + Set<Link> getDeviceIngressLinks(DeviceId deviceId);
52 +
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> getIngressLinks(ConnectPoint connectPoint);
79 +
80 + /**
81 + * Returns the 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 link from source to destination; null if none found
87 + */
88 + Link getLink(ConnectPoint src, ConnectPoint dst);
44 89
45 /** 90 /**
46 * Adds the specified link listener. 91 * Adds the specified 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.
......
1 +package org.onlab.onos.net;
2 +
3 +import com.google.common.testing.EqualsTester;
4 +import org.junit.Test;
5 +import org.onlab.onos.net.provider.ProviderId;
6 +
7 +import static org.junit.Assert.assertEquals;
8 +import static org.onlab.onos.net.DeviceId.deviceId;
9 +import static org.onlab.onos.net.Link.Type.DIRECT;
10 +import static org.onlab.onos.net.Link.Type.INDIRECT;
11 +import static org.onlab.onos.net.PortNumber.portNumber;
12 +
13 +/**
14 + * Test of the default link model entity.
15 + */
16 +public class DefaultLinkTest {
17 +
18 + private static final ProviderId PID = new ProviderId("foo");
19 + private static final DeviceId DID1 = deviceId("of:foo");
20 + private static final DeviceId DID2 = deviceId("of:bar");
21 + private static final PortNumber P1 = portNumber(1);
22 + private static final PortNumber P2 = portNumber(2);
23 +
24 + public static ConnectPoint cp(DeviceId id, PortNumber pn) {
25 + return new ConnectPoint(id, pn);
26 + }
27 +
28 + @Test
29 + public void testEquality() {
30 + Link l1 = new DefaultLink(PID, cp(DID1, P1), cp(DID2, P2), DIRECT);
31 + Link l2 = new DefaultLink(PID, cp(DID1, P1), cp(DID2, P2), DIRECT);
32 + Link l3 = new DefaultLink(PID, cp(DID1, P2), cp(DID2, P2), DIRECT);
33 + Link l4 = new DefaultLink(PID, cp(DID1, P2), cp(DID2, P2), DIRECT);
34 + Link l5 = new DefaultLink(PID, cp(DID1, P2), cp(DID2, P2), INDIRECT);
35 +
36 + new EqualsTester().addEqualityGroup(l1, l2)
37 + .addEqualityGroup(l3, l4)
38 + .addEqualityGroup(l5)
39 + .testEquals();
40 + }
41 +
42 + @Test
43 + public void basics() {
44 + Link link = new DefaultLink(PID, cp(DID1, P1), cp(DID2, P2), DIRECT);
45 + assertEquals("incorrect src", cp(DID1, P1), link.src());
46 + assertEquals("incorrect dst", cp(DID2, P2), link.dst());
47 + assertEquals("incorrect type", DIRECT, link.type());
48 + }
49 +
50 +}
1 +package org.onlab.onos.net.link;
2 +
3 +import org.junit.Test;
4 +import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.PortNumber;
6 +
7 +import static org.junit.Assert.assertEquals;
8 +import static org.onlab.onos.net.DefaultLinkTest.cp;
9 +import static org.onlab.onos.net.DeviceId.deviceId;
10 +import static org.onlab.onos.net.Link.Type.DIRECT;
11 +import static org.onlab.onos.net.PortNumber.portNumber;
12 +
13 +/**
14 + * Test of the default link description.
15 + */
16 +public class DefaultLinkDescriptionTest {
17 +
18 + private static final DeviceId DID1 = deviceId("of:foo");
19 + private static final DeviceId DID2 = deviceId("of:bar");
20 + private static final PortNumber P1 = portNumber(1);
21 +
22 + @Test
23 + public void basics() {
24 + LinkDescription desc = new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P1), DIRECT);
25 + assertEquals("incorrect src", cp(DID1, P1), desc.src());
26 + assertEquals("incorrect dst", cp(DID2, P1), desc.dst());
27 + assertEquals("incorrect type", DIRECT, desc.type());
28 + }
29 +
30 +}
1 +package org.onlab.onos.net.link;
2 +
3 +import org.junit.Test;
4 +import org.onlab.onos.event.AbstractEventTest;
5 +import org.onlab.onos.net.ConnectPoint;
6 +import org.onlab.onos.net.DefaultLink;
7 +import org.onlab.onos.net.Link;
8 +import org.onlab.onos.net.provider.ProviderId;
9 +
10 +import static org.onlab.onos.net.DeviceId.deviceId;
11 +import static org.onlab.onos.net.PortNumber.portNumber;
12 +
13 +/**
14 + * Tests of the device event.
15 + */
16 +public class LinkEventTest extends AbstractEventTest {
17 +
18 + private Link createLink() {
19 + return new DefaultLink(new ProviderId("foo"),
20 + new ConnectPoint(deviceId("of:foo"), portNumber(1)),
21 + new ConnectPoint(deviceId("of:bar"), portNumber(2)),
22 + Link.Type.INDIRECT);
23 + }
24 +
25 + @Test
26 + public void withTime() {
27 + Link link = createLink();
28 + LinkEvent event = new LinkEvent(LinkEvent.Type.LINK_ADDED, link, 123L);
29 + validateEvent(event, LinkEvent.Type.LINK_ADDED, link, 123L);
30 + }
31 +
32 + @Test
33 + public void withoutTime() {
34 + Link link = createLink();
35 + long before = System.currentTimeMillis();
36 + LinkEvent event = new LinkEvent(LinkEvent.Type.LINK_ADDED, link);
37 + long after = System.currentTimeMillis();
38 + validateEvent(event, LinkEvent.Type.LINK_ADDED, link, before, after);
39 + }
40 +
41 +}
...@@ -104,6 +104,12 @@ public class SimpleDeviceManager ...@@ -104,6 +104,12 @@ public class SimpleDeviceManager
104 } 104 }
105 105
106 @Override 106 @Override
107 + public boolean isAvailable(DeviceId deviceId) {
108 + checkNotNull(deviceId, DEVICE_ID_NULL);
109 + return store.isAvailable(deviceId);
110 + }
111 +
112 + @Override
107 public void addListener(DeviceListener listener) { 113 public void addListener(DeviceListener listener) {
108 listenerRegistry.addListener(listener); 114 listenerRegistry.addListener(listener);
109 } 115 }
...@@ -152,6 +158,7 @@ public class SimpleDeviceManager ...@@ -152,6 +158,7 @@ public class SimpleDeviceManager
152 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { 158 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
153 checkNotNull(deviceId, DEVICE_ID_NULL); 159 checkNotNull(deviceId, DEVICE_ID_NULL);
154 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); 160 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
161 + checkValidity();
155 log.info("Device {} connected", deviceId); 162 log.info("Device {} connected", deviceId);
156 DeviceEvent event = store.createOrUpdateDevice(provider().id(), 163 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
157 deviceId, deviceDescription); 164 deviceId, deviceDescription);
...@@ -161,6 +168,7 @@ public class SimpleDeviceManager ...@@ -161,6 +168,7 @@ public class SimpleDeviceManager
161 @Override 168 @Override
162 public void deviceDisconnected(DeviceId deviceId) { 169 public void deviceDisconnected(DeviceId deviceId) {
163 checkNotNull(deviceId, DEVICE_ID_NULL); 170 checkNotNull(deviceId, DEVICE_ID_NULL);
171 + checkValidity();
164 log.info("Device {} disconnected", deviceId); 172 log.info("Device {} disconnected", deviceId);
165 DeviceEvent event = store.markOffline(deviceId); 173 DeviceEvent event = store.markOffline(deviceId);
166 post(event); 174 post(event);
...@@ -170,6 +178,7 @@ public class SimpleDeviceManager ...@@ -170,6 +178,7 @@ public class SimpleDeviceManager
170 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { 178 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
171 checkNotNull(deviceId, DEVICE_ID_NULL); 179 checkNotNull(deviceId, DEVICE_ID_NULL);
172 checkNotNull(portDescriptions, "Port descriptions list cannot be null"); 180 checkNotNull(portDescriptions, "Port descriptions list cannot be null");
181 + checkValidity();
173 log.info("Device {} ports updated", deviceId); 182 log.info("Device {} ports updated", deviceId);
174 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions); 183 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
175 for (DeviceEvent event : events) { 184 for (DeviceEvent event : events) {
...@@ -181,13 +190,16 @@ public class SimpleDeviceManager ...@@ -181,13 +190,16 @@ public class SimpleDeviceManager
181 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { 190 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
182 checkNotNull(deviceId, DEVICE_ID_NULL); 191 checkNotNull(deviceId, DEVICE_ID_NULL);
183 checkNotNull(portDescription, PORT_DESCRIPTION_NULL); 192 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
184 - log.info("Device {} port status changed", deviceId); 193 + checkValidity();
185 DeviceEvent event = store.updatePortStatus(deviceId, portDescription); 194 DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
195 + if (event != null) {
196 + log.info("Device {} port status changed", deviceId);
197 + }
186 post(event); 198 post(event);
187 } 199 }
188 } 200 }
189 201
190 - // Posts the specified event to a local event dispatcher 202 + // Posts the specified event to the local event dispatcher.
191 private void post(DeviceEvent event) { 203 private void post(DeviceEvent event) {
192 if (event != null && eventDispatcher != null) { 204 if (event != null && eventDispatcher != null) {
193 eventDispatcher.post(event); 205 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
...@@ -271,6 +271,16 @@ class SimpleDeviceStore { ...@@ -271,6 +271,16 @@ class SimpleDeviceStore {
271 } 271 }
272 272
273 /** 273 /**
274 + * Indicates whether the specified device is available/online.
275 + *
276 + * @param deviceId device identifier
277 + * @return true if device is available
278 + */
279 + boolean isAvailable(DeviceId deviceId) {
280 + return availableDevices.contains(deviceId);
281 + }
282 +
283 + /**
274 * Returns the mastership role determined for this device. 284 * Returns the mastership role determined for this device.
275 * 285 *
276 * @param deviceId device identifier 286 * @param deviceId device identifier
......
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,15 +36,21 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -27,15 +36,21 @@ 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
34 private final AbstractListenerRegistry<LinkEvent, LinkListener> 47 private final AbstractListenerRegistry<LinkEvent, LinkListener>
35 listenerRegistry = new AbstractListenerRegistry<>(); 48 listenerRegistry = new AbstractListenerRegistry<>();
36 49
50 + private final SimpleLinkStore store = new SimpleLinkStore();
51 +
37 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 - private EventDeliveryService eventDispatcher; 53 + protected EventDeliveryService eventDispatcher;
39 54
40 @Activate 55 @Activate
41 public void activate() { 56 public void activate() {
...@@ -54,6 +69,81 @@ public class SimpleLinkManager ...@@ -54,6 +69,81 @@ public class SimpleLinkManager
54 return new InternalLinkProviderService(provider); 69 return new InternalLinkProviderService(provider);
55 } 70 }
56 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> getDeviceIngressLinks(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> getIngressLinks(ConnectPoint connectPoint) {
116 + checkNotNull(connectPoint, CONNECT_POINT_NULL);
117 + return store.getIngressLinks(connectPoint);
118 + }
119 +
120 + @Override
121 + public Link getLink(ConnectPoint src, ConnectPoint dst) {
122 + checkNotNull(src, CONNECT_POINT_NULL);
123 + checkNotNull(dst, CONNECT_POINT_NULL);
124 + return store.getLink(src, dst);
125 + }
126 +
127 + @Override
128 + public void removeLinks(ConnectPoint connectPoint) {
129 + removeLinks(getLinks(connectPoint));
130 + }
131 +
132 + @Override
133 + public void removeLinks(DeviceId deviceId) {
134 + removeLinks(getDeviceLinks(deviceId));
135 + }
136 +
137 + @Override
138 + public void addListener(LinkListener listener) {
139 + listenerRegistry.addListener(listener);
140 + }
141 +
142 + @Override
143 + public void removeListener(LinkListener listener) {
144 + listenerRegistry.removeListener(listener);
145 + }
146 +
57 // Personalized link provider service issued to the supplied provider. 147 // Personalized link provider service issued to the supplied provider.
58 private class InternalLinkProviderService extends AbstractProviderService<LinkProvider> 148 private class InternalLinkProviderService extends AbstractProviderService<LinkProvider>
59 implements LinkProviderService { 149 implements LinkProviderService {
...@@ -64,12 +154,54 @@ public class SimpleLinkManager ...@@ -64,12 +154,54 @@ public class SimpleLinkManager
64 154
65 @Override 155 @Override
66 public void linkDetected(LinkDescription linkDescription) { 156 public void linkDetected(LinkDescription linkDescription) {
157 + checkNotNull(linkDescription, LINK_DESC_NULL);
158 + checkValidity();
67 log.info("Link {} detected", linkDescription); 159 log.info("Link {} detected", linkDescription);
160 + LinkEvent event = store.createOrUpdateLink(provider().id(),
161 + linkDescription);
162 + post(event);
68 } 163 }
69 164
70 @Override 165 @Override
71 public void linkVanished(LinkDescription linkDescription) { 166 public void linkVanished(LinkDescription linkDescription) {
167 + checkNotNull(linkDescription, LINK_DESC_NULL);
168 + checkValidity();
72 log.info("Link {} vanished", linkDescription); 169 log.info("Link {} vanished", linkDescription);
170 + LinkEvent event = store.removeLink(linkDescription.src(),
171 + linkDescription.dst());
172 + post(event);
173 + }
174 +
175 + @Override
176 + public void linksVanished(ConnectPoint connectPoint) {
177 + checkNotNull(connectPoint, "Connect point cannot be null");
178 + checkValidity();
179 + log.info("Link for connection point {} vanished", connectPoint);
180 + removeLinks(getLinks(connectPoint));
181 + }
182 +
183 + @Override
184 + public void linksVanished(DeviceId deviceId) {
185 + checkNotNull(deviceId, DEVICE_ID_NULL);
186 + checkValidity();
187 + log.info("Link for device {} vanished", deviceId);
188 + removeLinks(getDeviceLinks(deviceId));
73 } 189 }
74 } 190 }
191 +
192 + // Removes all links in the specified set and emits appropriate events.
193 + private void removeLinks(Set<Link> links) {
194 + for (Link link : links) {
195 + LinkEvent event = store.removeLink(link.src(), link.dst());
196 + post(event);
197 + }
198 + }
199 +
200 + // Posts the specified event to the local event dispatcher.
201 + private void post(LinkEvent event) {
202 + if (event != null && eventDispatcher != null) {
203 + eventDispatcher.post(event);
204 + }
205 + }
206 +
75 } 207 }
......
1 +package org.onlab.onos.net.trivial.impl;
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 +
21 +import static org.onlab.onos.net.Link.Type.DIRECT;
22 +import static org.onlab.onos.net.Link.Type.INDIRECT;
23 +import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
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 +/**
28 + * Manages inventory of infrastructure links using trivial in-memory link
29 + * implementation.
30 + */
31 +class SimpleLinkStore {
32 +
33 + // Link inventory
34 + private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
35 +
36 + // Egress and ingress link sets
37 + private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
38 + private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
39 +
40 + private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{});
41 +
42 + /**
43 + * Returns the number of links in the store.
44 + *
45 + * @return number of links
46 + */
47 + int getLinkCount() {
48 + return links.size();
49 + }
50 +
51 + /**
52 + * Returns an iterable collection of all links in the inventory.
53 + *
54 + * @return collection of all links
55 + */
56 + Iterable<Link> getLinks() {
57 + return Collections.unmodifiableSet(new HashSet<Link>(links.values()));
58 + }
59 +
60 + /**
61 + * Returns all links egressing from the specified device.
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));
68 + }
69 +
70 + /**
71 + * Returns all links ingressing from the specified device.
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));
78 + }
79 +
80 + /**
81 + * Returns the link between the two end-points.
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));
89 + }
90 +
91 + /**
92 + * Returns all links egressing from the specified connection point.
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<>();
99 + for (Link link : srcLinks.get(src.deviceId())) {
100 + if (link.src().equals(src)) {
101 + egress.add(link);
102 + }
103 + }
104 + return egress;
105 + }
106 +
107 + /**
108 + * Returns all links ingressing to the specified connection point.
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<>();
115 + for (Link link : dstLinks.get(dst.deviceId())) {
116 + if (link.dst().equals(dst)) {
117 + ingress.add(link);
118 + }
119 + }
120 + return ingress;
121 + }
122 +
123 + /**
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,
132 + LinkDescription linkDescription) {
133 + LinkKey key = new LinkKey(linkDescription.src(), linkDescription.dst());
134 + DefaultLink link = links.get(key);
135 + if (link == null) {
136 + return createLink(providerId, key, linkDescription);
137 + }
138 + return updateLink(providerId, link, key, linkDescription);
139 + }
140 +
141 + // Creates and stores the link and returns the appropriate event.
142 + private LinkEvent createLink(ProviderId providerId, LinkKey key,
143 + LinkDescription linkDescription) {
144 + DefaultLink link = new DefaultLink(providerId, key.src, key.dst,
145 + linkDescription.type());
146 + synchronized (this) {
147 + links.put(key, link);
148 + srcLinks.put(link.src().deviceId(), link);
149 + dstLinks.put(link.dst().deviceId(), link);
150 + }
151 + return new LinkEvent(LINK_ADDED, link);
152 + }
153 +
154 + // Updates, if necessary the specified link and returns the appropriate event.
155 + private LinkEvent updateLink(ProviderId providerId, DefaultLink link,
156 + LinkKey key, LinkDescription linkDescription) {
157 + if (link.type() == INDIRECT && linkDescription.type() == DIRECT) {
158 + synchronized (this) {
159 + srcLinks.remove(link.src().deviceId(), link);
160 + dstLinks.remove(link.dst().deviceId(), link);
161 +
162 + DefaultLink updated =
163 + new DefaultLink(providerId, link.src(), link.dst(),
164 + linkDescription.type());
165 + links.put(key, updated);
166 + srcLinks.put(link.src().deviceId(), updated);
167 + dstLinks.put(link.dst().deviceId(), updated);
168 + return new LinkEvent(LINK_UPDATED, updated);
169 + }
170 + }
171 + return null;
172 + }
173 +
174 + /**
175 + * Removes the link based on the specified information.
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) {
183 + Link link = links.remove(new LinkKey(src, dst));
184 + if (link != null) {
185 + srcLinks.remove(link.src().deviceId(), link);
186 + dstLinks.remove(link.dst().deviceId(), link);
187 + return new LinkEvent(LINK_REMOVED, link);
188 + }
189 + return null;
190 + }
191 + }
192 +
193 + // Auxiliary key to track links.
194 + private class LinkKey {
195 + final ConnectPoint src;
196 + final ConnectPoint dst;
197 +
198 + LinkKey(ConnectPoint src, ConnectPoint dst) {
199 + this.src = src;
200 + this.dst = dst;
201 + }
202 +
203 + @Override
204 + public int hashCode() {
205 + return Objects.hash(src, dst);
206 + }
207 +
208 + @Override
209 + public boolean equals(Object obj) {
210 + if (obj instanceof LinkKey) {
211 + final LinkKey other = (LinkKey) obj;
212 + return Objects.equals(this.src, other.src) &&
213 + Objects.equals(this.dst, other.dst);
214 + }
215 + return false;
216 + }
217 + }
218 +}
...@@ -104,6 +104,7 @@ public class SimpleDeviceManagerTest { ...@@ -104,6 +104,7 @@ public class SimpleDeviceManagerTest {
104 assertNotNull("one device expected", it.next()); 104 assertNotNull("one device expected", it.next());
105 assertFalse("only one device expected", it.hasNext()); 105 assertFalse("only one device expected", it.hasNext());
106 assertEquals("incorrect device count", 1, service.getDeviceCount()); 106 assertEquals("incorrect device count", 1, service.getDeviceCount());
107 + assertTrue("device should be available", service.isAvailable(DID1));
107 } 108 }
108 109
109 @Test 110 @Test
...@@ -111,10 +112,12 @@ public class SimpleDeviceManagerTest { ...@@ -111,10 +112,12 @@ public class SimpleDeviceManagerTest {
111 connectDevice(DID1, SW1); 112 connectDevice(DID1, SW1);
112 connectDevice(DID2, SW1); 113 connectDevice(DID2, SW1);
113 validateEvents(DEVICE_ADDED, DEVICE_ADDED); 114 validateEvents(DEVICE_ADDED, DEVICE_ADDED);
115 + assertTrue("device should be available", service.isAvailable(DID1));
114 116
115 // Disconnect 117 // Disconnect
116 providerService.deviceDisconnected(DID1); 118 providerService.deviceDisconnected(DID1);
117 assertNotNull("device should not be found", service.getDevice(DID1)); 119 assertNotNull("device should not be found", service.getDevice(DID1));
120 + assertFalse("device should not be available", service.isAvailable(DID1));
118 validateEvents(DEVICE_AVAILABILITY_CHANGED); 121 validateEvents(DEVICE_AVAILABILITY_CHANGED);
119 122
120 // Reconnect 123 // Reconnect
......
1 +package org.onlab.onos.net.trivial.impl;
2 +
3 +import com.google.common.collect.ImmutableSet;
4 +import org.junit.After;
5 +import org.junit.Before;
6 +import org.junit.Test;
7 +import org.onlab.onos.event.Event;
8 +import org.onlab.onos.net.ConnectPoint;
9 +import org.onlab.onos.net.Device;
10 +import org.onlab.onos.net.DeviceId;
11 +import org.onlab.onos.net.Link;
12 +import org.onlab.onos.net.MastershipRole;
13 +import org.onlab.onos.net.PortNumber;
14 +import org.onlab.onos.net.link.DefaultLinkDescription;
15 +import org.onlab.onos.net.link.LinkAdminService;
16 +import org.onlab.onos.net.link.LinkEvent;
17 +import org.onlab.onos.net.link.LinkListener;
18 +import org.onlab.onos.net.link.LinkProvider;
19 +import org.onlab.onos.net.link.LinkProviderRegistry;
20 +import org.onlab.onos.net.link.LinkProviderService;
21 +import org.onlab.onos.net.link.LinkService;
22 +import org.onlab.onos.net.provider.AbstractProvider;
23 +import org.onlab.onos.net.provider.ProviderId;
24 +
25 +import java.util.ArrayList;
26 +import java.util.Iterator;
27 +import java.util.List;
28 +import java.util.Set;
29 +
30 +import static org.junit.Assert.*;
31 +import static org.onlab.onos.net.DeviceId.deviceId;
32 +import static org.onlab.onos.net.Link.Type.DIRECT;
33 +import static org.onlab.onos.net.Link.Type.INDIRECT;
34 +import static org.onlab.onos.net.link.LinkEvent.Type.*;
35 +
36 +/**
37 + * Test codifying the link service & link provider service contracts.
38 + */
39 +public class SimpleLinkManagerTest {
40 +
41 + private static final ProviderId PID = new ProviderId("foo");
42 + private static final DeviceId DID1 = deviceId("of:foo");
43 + private static final DeviceId DID2 = deviceId("of:bar");
44 + private static final DeviceId DID3 = deviceId("of:goo");
45 +
46 + private static final PortNumber P1 = PortNumber.portNumber(1);
47 + private static final PortNumber P2 = PortNumber.portNumber(2);
48 + private static final PortNumber P3 = PortNumber.portNumber(3);
49 +
50 +
51 + private SimpleLinkManager mgr;
52 +
53 + protected LinkService service;
54 + protected LinkAdminService admin;
55 + protected LinkProviderRegistry registry;
56 + protected LinkProviderService providerService;
57 + protected TestProvider provider;
58 + protected TestListener listener = new TestListener();
59 +
60 + @Before
61 + public void setUp() {
62 + mgr = new SimpleLinkManager();
63 + service = mgr;
64 + admin = mgr;
65 + registry = mgr;
66 + mgr.eventDispatcher = new TestEventDispatcher();
67 + mgr.activate();
68 +
69 + service.addListener(listener);
70 +
71 + provider = new TestProvider();
72 + providerService = registry.register(provider);
73 + assertTrue("provider should be registered",
74 + registry.getProviders().contains(provider.id()));
75 + }
76 +
77 + @After
78 + public void tearDown() {
79 + registry.unregister(provider);
80 + assertFalse("provider should not be registered",
81 + registry.getProviders().contains(provider.id()));
82 + service.removeListener(listener);
83 + mgr.deactivate();
84 + }
85 +
86 + @Test
87 + public void createLink() {
88 + addLink(DID1, P1, DID2, P2, DIRECT);
89 + addLink(DID2, P2, DID1, P1, DIRECT);
90 + assertEquals("incorrect link count", 2, service.getLinkCount());
91 +
92 + Iterator<Link> it = service.getLinks().iterator();
93 + it.next();
94 + it.next();
95 + assertFalse("incorrect link count", it.hasNext());
96 + }
97 +
98 + @Test
99 + public void updateLink() {
100 + addLink(DID1, P1, DID2, P2, DIRECT);
101 + addLink(DID2, P2, DID1, P1, INDIRECT);
102 + assertEquals("incorrect link count", 2, service.getLinkCount());
103 +
104 + providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
105 + validateEvents(LINK_UPDATED);
106 + assertEquals("incorrect link count", 2, service.getLinkCount());
107 +
108 + providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), INDIRECT));
109 + providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT));
110 + assertEquals("no events expected", 0, listener.events.size());
111 + }
112 +
113 + @Test
114 + public void removeLink() {
115 + addLink(DID1, P1, DID2, P2, DIRECT);
116 + addLink(DID2, P2, DID1, P1, DIRECT);
117 + assertEquals("incorrect link count", 2, service.getLinkCount());
118 +
119 + providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
120 + validateEvents(LINK_REMOVED);
121 + assertEquals("incorrect link count", 1, service.getLinkCount());
122 + assertNull("link should not be found", service.getLink(cp(DID1, P1), cp(DID2, P2)));
123 + assertNotNull("link should be found", service.getLink(cp(DID2, P2), cp(DID1, P1)));
124 +
125 + providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT));
126 + assertEquals("no events expected", 0, listener.events.size());
127 + }
128 +
129 + @Test
130 + public void removeLinksByConnectionPoint() {
131 + Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
132 + Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
133 + addLink(DID3, P3, DID2, P1, DIRECT);
134 + addLink(DID2, P1, DID3, P3, DIRECT);
135 + assertEquals("incorrect link count", 4, service.getLinkCount());
136 +
137 + providerService.linksVanished(cp(DID1, P1));
138 + assertEquals("incorrect link count", 2, service.getLinkCount());
139 + assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
140 + assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
141 + }
142 +
143 + @Test
144 + public void removeLinksByDevice() {
145 + addLink(DID1, P1, DID2, P2, DIRECT);
146 + addLink(DID2, P2, DID1, P1, DIRECT);
147 + addLink(DID3, P3, DID2, P1, DIRECT);
148 + addLink(DID2, P1, DID3, P3, DIRECT);
149 + Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
150 + Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
151 + assertEquals("incorrect link count", 6, service.getLinkCount());
152 +
153 + providerService.linksVanished(DID2);
154 + assertEquals("incorrect link count", 2, service.getLinkCount());
155 + assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
156 + assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
157 + }
158 +
159 + @Test
160 + public void removeLinksAsAdminByConnectionPoint() {
161 + Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
162 + Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
163 + addLink(DID3, P3, DID2, P1, DIRECT);
164 + addLink(DID2, P1, DID3, P3, DIRECT);
165 + assertEquals("incorrect link count", 4, service.getLinkCount());
166 +
167 + admin.removeLinks(cp(DID1, P1));
168 + assertEquals("incorrect link count", 2, service.getLinkCount());
169 + assertNull("link should be gone", service.getLink(l1.src(), l1.dst()));
170 + assertNull("link should be gone", service.getLink(l2.src(), l2.dst()));
171 + }
172 +
173 + @Test
174 + public void removeLinksAsAdminByDevice() {
175 + addLink(DID1, P1, DID2, P2, DIRECT);
176 + addLink(DID2, P2, DID1, P1, DIRECT);
177 + addLink(DID3, P3, DID2, P1, DIRECT);
178 + addLink(DID2, P1, DID3, P3, DIRECT);
179 + Link l5 = addLink(DID3, P1, DID1, P2, DIRECT);
180 + Link l6 = addLink(DID1, P2, DID3, P1, DIRECT);
181 + assertEquals("incorrect link count", 6, service.getLinkCount());
182 +
183 + admin.removeLinks(DID2);
184 + assertEquals("incorrect link count", 2, service.getLinkCount());
185 + assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst()));
186 + assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst()));
187 + }
188 +
189 + @Test
190 + public void getLinks() {
191 + Link l1 = addLink(DID1, P1, DID2, P2, DIRECT);
192 + Link l2 = addLink(DID2, P2, DID1, P1, DIRECT);
193 + Link l3 = addLink(DID3, P3, DID2, P1, DIRECT);
194 + Link l4 = addLink(DID2, P1, DID3, P3, DIRECT);
195 + assertEquals("incorrect link count", 4, service.getLinkCount());
196 +
197 + Set<Link> links = service.getLinks(cp(DID1, P1));
198 + assertEquals("incorrect links", ImmutableSet.of(l1, l2), links);
199 + links = service.getEgressLinks(cp(DID1, P1));
200 + assertEquals("incorrect links", ImmutableSet.of(l1), links);
201 + links = service.getIngressLinks(cp(DID1, P1));
202 + assertEquals("incorrect links", ImmutableSet.of(l2), links);
203 +
204 + links = service.getDeviceLinks(DID2);
205 + assertEquals("incorrect links", ImmutableSet.of(l1, l2, l3, l4), links);
206 + links = service.getDeviceLinks(DID3);
207 + assertEquals("incorrect links", ImmutableSet.of(l3, l4), links);
208 +
209 + links = service.getDeviceEgressLinks(DID2);
210 + assertEquals("incorrect links", ImmutableSet.of(l2, l4), links);
211 + links = service.getDeviceIngressLinks(DID2);
212 + assertEquals("incorrect links", ImmutableSet.of(l1, l3), links);
213 + }
214 +
215 +
216 + private Link addLink(DeviceId sd, PortNumber sp, DeviceId dd, PortNumber dp,
217 + Link.Type type) {
218 + providerService.linkDetected(new DefaultLinkDescription(cp(sd, sp), cp(dd, dp), type));
219 + Link link = listener.events.get(0).subject();
220 + validateEvents(LINK_ADDED);
221 + return link;
222 + }
223 +
224 + private ConnectPoint cp(DeviceId id, PortNumber portNumber) {
225 + return new ConnectPoint(id, portNumber);
226 + }
227 +
228 + protected void validateEvents(Enum... types) {
229 + int i = 0;
230 + assertEquals("wrong events received", types.length, listener.events.size());
231 + for (Event event : listener.events) {
232 + assertEquals("incorrect event type", types[i], event.type());
233 + i++;
234 + }
235 + listener.events.clear();
236 + }
237 +
238 +
239 + private class TestProvider extends AbstractProvider implements LinkProvider {
240 + private Device deviceReceived;
241 + private MastershipRole roleReceived;
242 +
243 + public TestProvider() {
244 + super(PID);
245 + }
246 + }
247 +
248 + private static class TestListener implements LinkListener {
249 + final List<LinkEvent> events = new ArrayList<>();
250 +
251 + @Override
252 + public void event(LinkEvent event) {
253 + events.add(event);
254 + }
255 + }
256 +
257 +}
...@@ -232,7 +232,7 @@ public class Controller { ...@@ -232,7 +232,7 @@ public class Controller {
232 } 232 }
233 233
234 public void start(OpenFlowAgent ag) { 234 public void start(OpenFlowAgent ag) {
235 - log.info("Initialising OpenFlow Lib and IO"); 235 + log.info("Starting OpenFlow IO");
236 this.agent = ag; 236 this.agent = ag;
237 this.init(new HashMap<String, String>()); 237 this.init(new HashMap<String, String>());
238 this.run(); 238 this.run();
...@@ -240,6 +240,7 @@ public class Controller { ...@@ -240,6 +240,7 @@ public class Controller {
240 240
241 241
242 public void stop() { 242 public void stop() {
243 + log.info("Stopping OpenFlow IO");
243 execFactory.shutdown(); 244 execFactory.shutdown();
244 cg.close(); 245 cg.close();
245 } 246 }
......
1 package org.onlab.onos.of.controller.impl; 1 package org.onlab.onos.of.controller.impl;
2 2
3 -import java.util.ArrayList;
4 -import java.util.concurrent.ConcurrentHashMap;
5 -import java.util.concurrent.locks.Lock;
6 -import java.util.concurrent.locks.ReentrantLock;
7 -
8 import org.apache.felix.scr.annotations.Activate; 3 import org.apache.felix.scr.annotations.Activate;
4 +import org.apache.felix.scr.annotations.Component;
9 import org.apache.felix.scr.annotations.Deactivate; 5 import org.apache.felix.scr.annotations.Deactivate;
6 +import org.apache.felix.scr.annotations.Service;
10 import org.onlab.onos.of.controller.Dpid; 7 import org.onlab.onos.of.controller.Dpid;
11 import org.onlab.onos.of.controller.OpenFlowController; 8 import org.onlab.onos.of.controller.OpenFlowController;
12 import org.onlab.onos.of.controller.OpenFlowSwitch; 9 import org.onlab.onos.of.controller.OpenFlowSwitch;
...@@ -19,6 +16,16 @@ import org.projectfloodlight.openflow.protocol.OFPortStatus; ...@@ -19,6 +16,16 @@ import org.projectfloodlight.openflow.protocol.OFPortStatus;
19 import org.slf4j.Logger; 16 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory; 17 import org.slf4j.LoggerFactory;
21 18
19 +import java.util.ArrayList;
20 +import java.util.HashSet;
21 +import java.util.List;
22 +import java.util.Set;
23 +import java.util.concurrent.ConcurrentHashMap;
24 +import java.util.concurrent.locks.Lock;
25 +import java.util.concurrent.locks.ReentrantLock;
26 +
27 +@Component(immediate = true)
28 +@Service
22 public class OpenFlowControllerImpl implements OpenFlowController { 29 public class OpenFlowControllerImpl implements OpenFlowController {
23 30
24 private static final Logger log = 31 private static final Logger log =
...@@ -32,11 +39,11 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -32,11 +39,11 @@ public class OpenFlowControllerImpl implements OpenFlowController {
32 new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); 39 new ConcurrentHashMap<Dpid, OpenFlowSwitch>();
33 40
34 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); 41 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
35 - protected ArrayList<OpenFlowSwitchListener> ofEventListener = 42 + protected Set<OpenFlowSwitchListener> ofEventListener =
36 - new ArrayList<OpenFlowSwitchListener>(); 43 + new HashSet<>();
37 44
38 - protected ArrayList<PacketListener> ofPacketListener = 45 + protected List<PacketListener> ofPacketListener =
39 - new ArrayList<PacketListener>(); 46 + new ArrayList<>();
40 47
41 private final Controller ctrl = new Controller(); 48 private final Controller ctrl = new Controller();
42 49
...@@ -110,19 +117,19 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -110,19 +117,19 @@ public class OpenFlowControllerImpl implements OpenFlowController {
110 @Override 117 @Override
111 public void processPacket(Dpid dpid, OFMessage msg) { 118 public void processPacket(Dpid dpid, OFMessage msg) {
112 switch (msg.getType()) { 119 switch (msg.getType()) {
113 - case PORT_STATUS: 120 + case PORT_STATUS:
114 - for (OpenFlowSwitchListener l : ofEventListener) { 121 + for (OpenFlowSwitchListener l : ofEventListener) {
115 - l.portChanged(dpid, (OFPortStatus) msg); 122 + l.portChanged(dpid, (OFPortStatus) msg);
116 - } 123 + }
117 - break; 124 + break;
118 - case PACKET_IN: 125 + case PACKET_IN:
119 - for (PacketListener p : ofPacketListener) { 126 + for (PacketListener p : ofPacketListener) {
120 - //TODO fix me! 127 + //TODO fix me!
121 - p.handlePacket(null); 128 + p.handlePacket(null);
122 - } 129 + }
123 - break; 130 + break;
124 - default: 131 + default:
125 - log.warn("Handling message type {} not yet implemented", msg.getType()); 132 + log.warn("Handling message type {} not yet implemented", msg.getType());
126 } 133 }
127 } 134 }
128 135
...@@ -135,7 +142,6 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -135,7 +142,6 @@ public class OpenFlowControllerImpl implements OpenFlowController {
135 * Implementation of an OpenFlow Agent which is responsible for 142 * Implementation of an OpenFlow Agent which is responsible for
136 * keeping track of connected switches and the state in which 143 * keeping track of connected switches and the state in which
137 * they are. 144 * they are.
138 - *
139 */ 145 */
140 public class OpenFlowSwitchAgent implements OpenFlowAgent { 146 public class OpenFlowSwitchAgent implements OpenFlowAgent {
141 147
...@@ -146,7 +152,7 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -146,7 +152,7 @@ public class OpenFlowControllerImpl implements OpenFlowController {
146 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { 152 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
147 if (connectedSwitches.get(dpid) != null) { 153 if (connectedSwitches.get(dpid) != null) {
148 log.error("Trying to add connectedSwitch but found a previous " 154 log.error("Trying to add connectedSwitch but found a previous "
149 - + "value for dpid: {}", dpid); 155 + + "value for dpid: {}", dpid);
150 return false; 156 return false;
151 } else { 157 } else {
152 log.error("Added switch {}", dpid); 158 log.error("Added switch {}", dpid);
...@@ -162,18 +168,18 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -162,18 +168,18 @@ public class OpenFlowControllerImpl implements OpenFlowController {
162 public boolean validActivation(Dpid dpid) { 168 public boolean validActivation(Dpid dpid) {
163 if (connectedSwitches.get(dpid) == null) { 169 if (connectedSwitches.get(dpid) == null) {
164 log.error("Trying to activate switch but is not in " 170 log.error("Trying to activate switch but is not in "
165 - + "connected switches: dpid {}. Aborting ..", 171 + + "connected switches: dpid {}. Aborting ..",
166 - dpid); 172 + dpid);
167 return false; 173 return false;
168 } 174 }
169 if (activeMasterSwitches.get(dpid) != null || 175 if (activeMasterSwitches.get(dpid) != null ||
170 activeEqualSwitches.get(dpid) != null) { 176 activeEqualSwitches.get(dpid) != null) {
171 log.error("Trying to activate switch but it is already " 177 log.error("Trying to activate switch but it is already "
172 - + "activated: dpid {}. Found in activeMaster: {} " 178 + + "activated: dpid {}. Found in activeMaster: {} "
173 - + "Found in activeEqual: {}. Aborting ..", new Object[] { 179 + + "Found in activeEqual: {}. Aborting ..", new Object[]{
174 - dpid, 180 + dpid,
175 - (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', 181 + (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
176 - (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); 182 + (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'});
177 return false; 183 return false;
178 } 184 }
179 return true; 185 return true;
...@@ -219,7 +225,7 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -219,7 +225,7 @@ public class OpenFlowControllerImpl implements OpenFlowController {
219 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); 225 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
220 if (sw == null) { 226 if (sw == null) {
221 log.error("Transition to master called on sw {}, but switch " 227 log.error("Transition to master called on sw {}, but switch "
222 - + "was not found in controller-cache", dpid); 228 + + "was not found in controller-cache", dpid);
223 return; 229 return;
224 } 230 }
225 log.info("Transitioned switch {} to MASTER", dpid); 231 log.info("Transitioned switch {} to MASTER", dpid);
...@@ -240,7 +246,7 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -240,7 +246,7 @@ public class OpenFlowControllerImpl implements OpenFlowController {
240 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); 246 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
241 if (sw == null) { 247 if (sw == null) {
242 log.error("Transition to equal called on sw {}, but switch " 248 log.error("Transition to equal called on sw {}, but switch "
243 - + "was not found in controller-cache", dpid); 249 + + "was not found in controller-cache", dpid);
244 return; 250 return;
245 } 251 }
246 log.info("Transitioned switch {} to EQUAL", dpid); 252 log.info("Transitioned switch {} to EQUAL", dpid);
...@@ -270,5 +276,4 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -270,5 +276,4 @@ public class OpenFlowControllerImpl implements OpenFlowController {
270 } 276 }
271 277
272 278
273 -
274 } 279 }
......
...@@ -58,16 +58,21 @@ ...@@ -58,16 +58,21 @@
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
71 + <dependency>
72 + <groupId>commons-lang</groupId>
73 + <artifactId>commons-lang</artifactId>
74 + <version>2.6</version>
75 + </dependency>
71 76
72 <!-- Web related --> 77 <!-- Web related -->
73 <dependency> 78 <dependency>
......
...@@ -85,19 +85,19 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -85,19 +85,19 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
85 @Override 85 @Override
86 public void roleChanged(Device device, MastershipRole newRole) { 86 public void roleChanged(Device device, MastershipRole newRole) {
87 switch (newRole) { 87 switch (newRole) {
88 - case MASTER: 88 + case MASTER:
89 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 89 + controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
90 - RoleState.MASTER); 90 + RoleState.MASTER);
91 - break; 91 + break;
92 - case STANDBY: 92 + case STANDBY:
93 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 93 + controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
94 - RoleState.EQUAL); 94 + RoleState.EQUAL);
95 - case NONE: 95 + case NONE:
96 - controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), 96 + controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
97 - RoleState.SLAVE); 97 + RoleState.SLAVE);
98 - break; 98 + break;
99 - default: 99 + default:
100 - log.error("Unknown Mastership state : {}", newRole); 100 + log.error("Unknown Mastership state : {}", newRole);
101 101
102 } 102 }
103 log.info("Accepting mastership role change for device {}", device.id()); 103 log.info("Accepting mastership role change for device {}", device.id());
...@@ -114,10 +114,10 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -114,10 +114,10 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
114 114
115 DeviceDescription description = 115 DeviceDescription description =
116 new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH, 116 new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH,
117 - sw.manfacturerDescription(), 117 + sw.manfacturerDescription(),
118 - sw.hardwareDescription(), 118 + sw.hardwareDescription(),
119 - sw.softwareDescription(), 119 + sw.softwareDescription(),
120 - sw.softwareDescription()); 120 + sw.serialNumber());
121 providerService.deviceConnected(deviceId(uri), description); 121 providerService.deviceConnected(deviceId(uri), description);
122 providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts())); 122 providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts()));
123 } 123 }
...@@ -140,6 +140,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -140,6 +140,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
140 140
141 /** 141 /**
142 * Given a dpid builds a URI for the device. 142 * Given a dpid builds a URI for the device.
143 + *
143 * @param dpid the dpid to build the uri from 144 * @param dpid the dpid to build the uri from
144 * @return returns a uri of the form of:<dpidHexForm> 145 * @return returns a uri of the form of:<dpidHexForm>
145 */ 146 */
...@@ -155,6 +156,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -155,6 +156,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
155 156
156 /** 157 /**
157 * Builds a list of port descriptions for a given list of ports. 158 * Builds a list of port descriptions for a given list of ports.
159 + *
158 * @param ports the list of ports 160 * @param ports the list of ports
159 * @return list of portdescriptions 161 * @return list of portdescriptions
160 */ 162 */
...@@ -169,6 +171,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -169,6 +171,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
169 171
170 /** 172 /**
171 * Build a portDescription from a given port. 173 * Build a portDescription from a given port.
174 + *
172 * @param port the port to build from. 175 * @param port the port to build from.
173 * @return portDescription for the port. 176 * @return portDescription for the port.
174 */ 177 */
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
20 <dependency> 20 <dependency>
21 <groupId>com.google.guava</groupId> 21 <groupId>com.google.guava</groupId>
22 <artifactId>guava-testlib</artifactId> 22 <artifactId>guava-testlib</artifactId>
23 - <version>17.0</version>
24 <scope>test</scope> 23 <scope>test</scope>
25 </dependency> 24 </dependency>
26 <dependency> 25 <dependency>
......
...@@ -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.
......
...@@ -27,11 +27,6 @@ ...@@ -27,11 +27,6 @@
27 <groupId>com.google.guava</groupId> 27 <groupId>com.google.guava</groupId>
28 <artifactId>guava</artifactId> 28 <artifactId>guava</artifactId>
29 </dependency> 29 </dependency>
30 - <dependency>
31 - <groupId>commons-lang</groupId>
32 - <artifactId>commons-lang</artifactId>
33 - <version>2.3</version>
34 - </dependency>
35 </dependencies> 30 </dependencies>
36 31
37 <build> 32 <build>
......