tom

Adding mode base model and unit tests stuff.

1 +package org.onlab.onos.net;
2 +
3 +import org.onlab.onos.net.provider.ProviderId;
4 +
5 +/**
6 + * Base implementation of network elements, i.e. devices or hosts.
7 + */
8 +public class AbstractElement extends AbstractModel implements Element {
9 +
10 + protected final ElementId id;
11 +
12 + /**
13 + * Creates a network element attributed to the specified provider.
14 + *
15 + * @param providerId identity of the provider
16 + * @param id element identifier
17 + */
18 + protected AbstractElement(ProviderId providerId, ElementId id) {
19 + super(providerId);
20 + this.id = id;
21 + }
22 +
23 + @Override
24 + public ElementId id() {
25 + return id;
26 + }
27 +
28 +}
1 +package org.onlab.onos.net;
2 +
3 +import org.onlab.onos.net.provider.ProviderId;
4 +
5 +/**
6 + * Base implementation of a network model entity.
7 + */
8 +public class AbstractModel implements Provided {
9 +
10 + private final ProviderId providerId;
11 +
12 + /**
13 + * Creates a model entity attributed to the specified provider.
14 + *
15 + * @param providerId identity of the provider
16 + */
17 + protected AbstractModel(ProviderId providerId) {
18 + this.providerId = providerId;
19 + }
20 +
21 + @Override
22 + public ProviderId providerId() {
23 + return providerId;
24 + }
25 +
26 +}
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.Objects.toStringHelper;
8 +
9 +/**
10 + * Default device model implementation.
11 + */
12 +public class DefaultDevice extends AbstractElement implements Device {
13 +
14 + private final Type type;
15 + private final String manufacturer;
16 + private final String serialNumber;
17 + private final String hwVersion;
18 + private final String swVersion;
19 +
20 + /**
21 + * Creates a network element attributed to the specified provider.
22 + *
23 + * @param providerId identity of the provider
24 + * @param id device identifier
25 + * @param type device type
26 + * @param manufacturer device manufacturer
27 + * @param hwVersion device HW version
28 + * @param swVersion device SW version
29 + * @param serialNumber device serial number
30 + */
31 + public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
32 + String manufacturer, String hwVersion, String swVersion,
33 + String serialNumber) {
34 + super(providerId, id);
35 + this.type = type;
36 + this.manufacturer = manufacturer;
37 + this.hwVersion = hwVersion;
38 + this.swVersion = swVersion;
39 + this.serialNumber = serialNumber;
40 + }
41 +
42 + @Override
43 + public DeviceId id() {
44 + return (DeviceId) super.id();
45 + }
46 +
47 + @Override
48 + public Type type() {
49 + return type;
50 + }
51 +
52 + @Override
53 + public String manufacturer() {
54 + return manufacturer;
55 + }
56 +
57 + @Override
58 + public String hwVersion() {
59 + return hwVersion;
60 + }
61 +
62 + @Override
63 + public String swVersion() {
64 + return swVersion;
65 + }
66 +
67 + @Override
68 + public String serialNumber() {
69 + return serialNumber;
70 + }
71 +
72 + @Override
73 + public int hashCode() {
74 + return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
75 + }
76 +
77 + @Override
78 + public boolean equals(Object obj) {
79 + if (obj instanceof DefaultDevice) {
80 + final DefaultDevice other = (DefaultDevice) obj;
81 + return Objects.equals(this.id, other.id) &&
82 + Objects.equals(this.type, other.type) &&
83 + Objects.equals(this.manufacturer, other.manufacturer) &&
84 + Objects.equals(this.hwVersion, other.hwVersion) &&
85 + Objects.equals(this.swVersion, other.swVersion) &&
86 + Objects.equals(this.serialNumber, other.serialNumber);
87 + }
88 + return false;
89 + }
90 +
91 + @Override
92 + public String toString() {
93 + return toStringHelper(this)
94 + .add("id", id)
95 + .add("type", type)
96 + .add("manufacturer", manufacturer)
97 + .add("hwVersion", hwVersion)
98 + .add("swVersion", swVersion)
99 + .add("serialNumber", serialNumber)
100 + .toString();
101 + }
102 +
103 +}
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 java.net.URI;
8 +
9 +import static org.junit.Assert.assertEquals;
10 +import static org.onlab.onos.net.Device.Type.SWITCH;
11 +
12 +/**
13 + * Test of the default device model entity.
14 + */
15 +public class DefaultDeviceTest {
16 +
17 + private static final ProviderId PID = new ProviderId("foo");
18 + private static final DeviceId DID1 = new DeviceId(URI.create("of:foo"));
19 + private static final DeviceId DID2 = new DeviceId(URI.create("of:bar"));
20 + private static final String MFR = "whitebox";
21 + private static final String HW = "1.1.x";
22 + private static final String SW = "3.9.1";
23 + private static final String SN1 = "43311-12345";
24 + private static final String SN2 = "42346-43512";
25 +
26 +
27 + @Test
28 + public void testEquality() {
29 + Device d1 = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
30 + Device d2 = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
31 + Device d3 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN2);
32 + Device d4 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN2);
33 + Device d5 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN1);
34 +
35 + new EqualsTester().addEqualityGroup(d1, d2)
36 + .addEqualityGroup(d3, d4)
37 + .addEqualityGroup(d5)
38 + .testEquals();
39 + }
40 +
41 + @Test
42 + public void basics() {
43 + Device device = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
44 + assertEquals("incorrect provider", PID, device.providerId());
45 + assertEquals("incorrect id", DID1, device.id());
46 + assertEquals("incorrect type", SWITCH, device.type());
47 + assertEquals("incorrect manufacturer", MFR, device.manufacturer());
48 + assertEquals("incorrect hw", HW, device.hwVersion());
49 + assertEquals("incorrect sw", SW, device.swVersion());
50 + assertEquals("incorrect serial", SN1, device.serialNumber());
51 + assertEquals("incorrect serial", SN1, device.serialNumber());
52 + }
53 +
54 +}
1 +package org.onlab.onos.net.device;
2 +
3 +import org.junit.Test;
4 +
5 +import java.net.URI;
6 +
7 +import static org.junit.Assert.assertEquals;
8 +import static org.junit.Assert.assertTrue;
9 +import static org.onlab.onos.net.Device.Type.SWITCH;
10 +
11 +/**
12 + * Test of the default device description.
13 + */
14 +public class DefaultDeviceDescriptionTest {
15 +
16 + private static final URI DURI = URI.create("of:foo");
17 + private static final String MFR = "whitebox";
18 + private static final String HW = "1.1.x";
19 + private static final String SW = "3.9.1";
20 + private static final String SN = "43311-12345";
21 +
22 +
23 + @Test
24 + public void basics() {
25 + DeviceDescription device =
26 + new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN);
27 + assertEquals("incorrect uri", DURI, device.deviceURI());
28 + assertEquals("incorrect type", SWITCH, device.type());
29 + assertEquals("incorrect manufacturer", MFR, device.manufacturer());
30 + assertEquals("incorrect hw", HW, device.hwVersion());
31 + assertEquals("incorrect sw", SW, device.swVersion());
32 + assertEquals("incorrect serial", SN, device.serialNumber());
33 + assertTrue("incorrect toString", device.toString().contains("uri=of:foo"));
34 + }
35 +
36 +}