Adding unit tests for simple link manager.
Converted ConnectPoint and HostLocation to classes.
Showing
3 changed files
with
108 additions
and
28 deletions
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 | } | ... | ... |
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,23 +4,15 @@ import org.junit.After; | ... | @@ -4,23 +4,15 @@ import org.junit.After; |
4 | import org.junit.Before; | 4 | import org.junit.Before; |
5 | import org.junit.Test; | 5 | import org.junit.Test; |
6 | import org.onlab.onos.event.Event; | 6 | import org.onlab.onos.event.Event; |
7 | +import org.onlab.onos.net.ConnectPoint; | ||
7 | import org.onlab.onos.net.Device; | 8 | import org.onlab.onos.net.Device; |
8 | import org.onlab.onos.net.DeviceId; | 9 | import org.onlab.onos.net.DeviceId; |
10 | +import org.onlab.onos.net.Link; | ||
9 | import org.onlab.onos.net.MastershipRole; | 11 | import org.onlab.onos.net.MastershipRole; |
10 | -import org.onlab.onos.net.Port; | ||
11 | import org.onlab.onos.net.PortNumber; | 12 | import org.onlab.onos.net.PortNumber; |
12 | -import org.onlab.onos.net.device.DefaultDeviceDescription; | 13 | +import org.onlab.onos.net.link.DefaultLinkDescription; |
13 | -import org.onlab.onos.net.device.DefaultPortDescription; | ||
14 | -import org.onlab.onos.net.device.DeviceAdminService; | ||
15 | -import org.onlab.onos.net.device.DeviceDescription; | ||
16 | -import org.onlab.onos.net.device.DeviceEvent; | ||
17 | -import org.onlab.onos.net.device.DeviceListener; | ||
18 | -import org.onlab.onos.net.device.DeviceProvider; | ||
19 | -import org.onlab.onos.net.device.DeviceProviderRegistry; | ||
20 | -import org.onlab.onos.net.device.DeviceProviderService; | ||
21 | -import org.onlab.onos.net.device.DeviceService; | ||
22 | -import org.onlab.onos.net.device.PortDescription; | ||
23 | import org.onlab.onos.net.link.LinkAdminService; | 14 | import org.onlab.onos.net.link.LinkAdminService; |
15 | +import org.onlab.onos.net.link.LinkDescription; | ||
24 | import org.onlab.onos.net.link.LinkEvent; | 16 | import org.onlab.onos.net.link.LinkEvent; |
25 | import org.onlab.onos.net.link.LinkListener; | 17 | import org.onlab.onos.net.link.LinkListener; |
26 | import org.onlab.onos.net.link.LinkProvider; | 18 | import org.onlab.onos.net.link.LinkProvider; |
... | @@ -31,13 +23,10 @@ import org.onlab.onos.net.provider.AbstractProvider; | ... | @@ -31,13 +23,10 @@ import org.onlab.onos.net.provider.AbstractProvider; |
31 | import org.onlab.onos.net.provider.ProviderId; | 23 | import org.onlab.onos.net.provider.ProviderId; |
32 | 24 | ||
33 | import java.util.ArrayList; | 25 | import java.util.ArrayList; |
34 | -import java.util.Iterator; | ||
35 | import java.util.List; | 26 | import java.util.List; |
36 | 27 | ||
37 | import static org.junit.Assert.*; | 28 | import static org.junit.Assert.*; |
38 | -import static org.onlab.onos.net.Device.Type.SWITCH; | ||
39 | import static org.onlab.onos.net.DeviceId.deviceId; | 29 | import static org.onlab.onos.net.DeviceId.deviceId; |
40 | -import static org.onlab.onos.net.device.DeviceEvent.Type.*; | ||
41 | 30 | ||
42 | /** | 31 | /** |
43 | * Test codifying the link service & link provider service contracts. | 32 | * Test codifying the link service & link provider service contracts. |
... | @@ -47,11 +36,6 @@ public class SimpleLinkManagerTest { | ... | @@ -47,11 +36,6 @@ public class SimpleLinkManagerTest { |
47 | private static final ProviderId PID = new ProviderId("foo"); | 36 | private static final ProviderId PID = new ProviderId("foo"); |
48 | private static final DeviceId DID1 = deviceId("of:foo"); | 37 | private static final DeviceId DID1 = deviceId("of:foo"); |
49 | private static final DeviceId DID2 = deviceId("of:bar"); | 38 | private static final DeviceId DID2 = deviceId("of:bar"); |
50 | - private static final String MFR = "whitebox"; | ||
51 | - private static final String HW = "1.1.x"; | ||
52 | - private static final String SW1 = "3.8.1"; | ||
53 | - private static final String SW2 = "3.9.5"; | ||
54 | - private static final String SN = "43311-12345"; | ||
55 | 39 | ||
56 | private static final PortNumber P1 = PortNumber.portNumber(1); | 40 | private static final PortNumber P1 = PortNumber.portNumber(1); |
57 | private static final PortNumber P2 = PortNumber.portNumber(2); | 41 | private static final PortNumber P2 = PortNumber.portNumber(2); |
... | @@ -94,6 +78,14 @@ public class SimpleLinkManagerTest { | ... | @@ -94,6 +78,14 @@ public class SimpleLinkManagerTest { |
94 | } | 78 | } |
95 | 79 | ||
96 | 80 | ||
81 | + @Test | ||
82 | + public void createLink() { | ||
83 | + LinkDescription ld = new DefaultLinkDescription(new ConnectPoint(DID1, P1), | ||
84 | + new ConnectPoint(DID2, P2), | ||
85 | + Link.Type.DIRECT); | ||
86 | + providerService.linkDetected(ld); | ||
87 | + } | ||
88 | + | ||
97 | protected void validateEvents(Enum... types) { | 89 | protected void validateEvents(Enum... types) { |
98 | int i = 0; | 90 | int i = 0; |
99 | assertEquals("wrong events received", types.length, listener.events.size()); | 91 | assertEquals("wrong events received", types.length, listener.events.size()); | ... | ... |
-
Please register or login to post a comment