tom

Adding mode base model and unit tests stuff.

package org.onlab.onos.net;
import org.onlab.onos.net.provider.ProviderId;
/**
* Base implementation of network elements, i.e. devices or hosts.
*/
public class AbstractElement extends AbstractModel implements Element {
protected final ElementId id;
/**
* Creates a network element attributed to the specified provider.
*
* @param providerId identity of the provider
* @param id element identifier
*/
protected AbstractElement(ProviderId providerId, ElementId id) {
super(providerId);
this.id = id;
}
@Override
public ElementId id() {
return id;
}
}
package org.onlab.onos.net;
import org.onlab.onos.net.provider.ProviderId;
/**
* Base implementation of a network model entity.
*/
public class AbstractModel implements Provided {
private final ProviderId providerId;
/**
* Creates a model entity attributed to the specified provider.
*
* @param providerId identity of the provider
*/
protected AbstractModel(ProviderId providerId) {
this.providerId = providerId;
}
@Override
public ProviderId providerId() {
return providerId;
}
}
package org.onlab.onos.net;
import org.onlab.onos.net.provider.ProviderId;
import java.util.Objects;
import static com.google.common.base.Objects.toStringHelper;
/**
* Default device model implementation.
*/
public class DefaultDevice extends AbstractElement implements Device {
private final Type type;
private final String manufacturer;
private final String serialNumber;
private final String hwVersion;
private final String swVersion;
/**
* Creates a network element attributed to the specified provider.
*
* @param providerId identity of the provider
* @param id device identifier
* @param type device type
* @param manufacturer device manufacturer
* @param hwVersion device HW version
* @param swVersion device SW version
* @param serialNumber device serial number
*/
public DefaultDevice(ProviderId providerId, DeviceId id, Type type,
String manufacturer, String hwVersion, String swVersion,
String serialNumber) {
super(providerId, id);
this.type = type;
this.manufacturer = manufacturer;
this.hwVersion = hwVersion;
this.swVersion = swVersion;
this.serialNumber = serialNumber;
}
@Override
public DeviceId id() {
return (DeviceId) super.id();
}
@Override
public Type type() {
return type;
}
@Override
public String manufacturer() {
return manufacturer;
}
@Override
public String hwVersion() {
return hwVersion;
}
@Override
public String swVersion() {
return swVersion;
}
@Override
public String serialNumber() {
return serialNumber;
}
@Override
public int hashCode() {
return Objects.hash(id, type, manufacturer, hwVersion, swVersion, serialNumber);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof DefaultDevice) {
final DefaultDevice other = (DefaultDevice) obj;
return Objects.equals(this.id, other.id) &&
Objects.equals(this.type, other.type) &&
Objects.equals(this.manufacturer, other.manufacturer) &&
Objects.equals(this.hwVersion, other.hwVersion) &&
Objects.equals(this.swVersion, other.swVersion) &&
Objects.equals(this.serialNumber, other.serialNumber);
}
return false;
}
@Override
public String toString() {
return toStringHelper(this)
.add("id", id)
.add("type", type)
.add("manufacturer", manufacturer)
.add("hwVersion", hwVersion)
.add("swVersion", swVersion)
.add("serialNumber", serialNumber)
.toString();
}
}
package org.onlab.onos.net;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.onlab.onos.net.provider.ProviderId;
import java.net.URI;
import static org.junit.Assert.assertEquals;
import static org.onlab.onos.net.Device.Type.SWITCH;
/**
* Test of the default device model entity.
*/
public class DefaultDeviceTest {
private static final ProviderId PID = new ProviderId("foo");
private static final DeviceId DID1 = new DeviceId(URI.create("of:foo"));
private static final DeviceId DID2 = new DeviceId(URI.create("of:bar"));
private static final String MFR = "whitebox";
private static final String HW = "1.1.x";
private static final String SW = "3.9.1";
private static final String SN1 = "43311-12345";
private static final String SN2 = "42346-43512";
@Test
public void testEquality() {
Device d1 = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
Device d2 = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
Device d3 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN2);
Device d4 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN2);
Device d5 = new DefaultDevice(PID, DID2, SWITCH, MFR, HW, SW, SN1);
new EqualsTester().addEqualityGroup(d1, d2)
.addEqualityGroup(d3, d4)
.addEqualityGroup(d5)
.testEquals();
}
@Test
public void basics() {
Device device = new DefaultDevice(PID, DID1, SWITCH, MFR, HW, SW, SN1);
assertEquals("incorrect provider", PID, device.providerId());
assertEquals("incorrect id", DID1, device.id());
assertEquals("incorrect type", SWITCH, device.type());
assertEquals("incorrect manufacturer", MFR, device.manufacturer());
assertEquals("incorrect hw", HW, device.hwVersion());
assertEquals("incorrect sw", SW, device.swVersion());
assertEquals("incorrect serial", SN1, device.serialNumber());
assertEquals("incorrect serial", SN1, device.serialNumber());
}
}
package org.onlab.onos.net.device;
import org.junit.Test;
import java.net.URI;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onlab.onos.net.Device.Type.SWITCH;
/**
* Test of the default device description.
*/
public class DefaultDeviceDescriptionTest {
private static final URI DURI = URI.create("of:foo");
private static final String MFR = "whitebox";
private static final String HW = "1.1.x";
private static final String SW = "3.9.1";
private static final String SN = "43311-12345";
@Test
public void basics() {
DeviceDescription device =
new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN);
assertEquals("incorrect uri", DURI, device.deviceURI());
assertEquals("incorrect type", SWITCH, device.type());
assertEquals("incorrect manufacturer", MFR, device.manufacturer());
assertEquals("incorrect hw", HW, device.hwVersion());
assertEquals("incorrect sw", SW, device.swVersion());
assertEquals("incorrect serial", SN, device.serialNumber());
assertTrue("incorrect toString", device.toString().contains("uri=of:foo"));
}
}