use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.
the class FlowObjectiveManagerTest method deviceUpEvent.
/**
* Tests receipt of a device up event.
*
* @throws TestUtilsException if lookup of a field fails
*/
@Test
public void deviceUpEvent() throws TestUtilsException {
TrafficSelector selector = DefaultTrafficSelector.emptySelector();
TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, d2);
DeviceListener listener = TestUtils.getField(manager, "deviceListener");
assertThat(listener, notNullValue());
listener.event(event);
ForwardingObjective forward = DefaultForwardingObjective.builder().fromApp(NetTestTools.APP_ID).withFlag(ForwardingObjective.Flag.SPECIFIC).withSelector(selector).withTreatment(treatment).makePermanent().add();
manager.forward(id2, forward);
// new device should have an objective now
TestTools.assertAfter(RETRY_MS, () -> assertThat(forwardingObjectives, hasSize(1)));
assertThat(forwardingObjectives, hasItem("of:d2"));
assertThat(filteringObjectives, hasSize(0));
assertThat(nextObjectives, hasSize(0));
}
use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.
the class EdgeManagerTest method testDeviceUpdates.
@Test
public void testDeviceUpdates() {
// Setup
Device referenceDevice;
DeviceEvent event;
int numDevices = 10;
int numInfraPorts = 5;
totalPorts = 10;
defaultPopulator(numDevices, numInfraPorts);
events.clear();
// Test response to device added events
referenceDevice = NetTestTools.device("11");
devices.put(referenceDevice.id(), referenceDevice);
for (int port = 1; port <= numInfraPorts; port++) {
infrastructurePorts.add(NetTestTools.connectPoint("11", port));
}
event = new DeviceEvent(DEVICE_ADDED, referenceDevice, new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
postTopologyEvent(event);
// Check that ports were populated correctly
assertTrue("Unexpected number of new ports added", mgr.deviceService.getPorts(NetTestTools.did("11")).size() == 10);
// Check that of the ten ports the half that are infrastructure ports aren't added
assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
for (int index = 0; index < numInfraPorts; index++) {
assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
}
// Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
for (int index = 0; index < events.size(); index++) {
assertEquals("Port added had unexpected port number.", events.get(index).subject().port(), NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
}
events.clear();
// Repost the event to test repeated posts
postTopologyEvent(event);
assertEquals("The redundant notification should not have created additional notifications.", 0, events.size());
// Calculate the size of the returned iterable of edge points.
Iterable<ConnectPoint> pts = mgr.getEdgePoints();
Iterator pointIterator = pts.iterator();
int count = 0;
for (; pointIterator.hasNext(); count++) {
pointIterator.next();
}
assertEquals("Unexpected number of edge points", (numDevices + 1) * numInfraPorts, count);
// Testing device removal
events.clear();
event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
postTopologyEvent(event);
assertEquals("There should be five new events from removal of edge points", totalPorts - numInfraPorts, events.size());
for (int index = 0; index < events.size(); index++) {
// Assert that the correct port numbers were removed in the correct order
assertThat("Port removed had unexpected port number.", events.get(index).subject().port().toLong(), is(greaterThanOrEqualTo((long) numInfraPorts)));
// Assert that the events are of the correct type
assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
}
events.clear();
// Rebroadcast event to check that it triggers no new behavior
postTopologyEvent(event);
assertEquals("Rebroadcast of removal event should not produce additional events", 0, events.size());
// Testing device status change, changed from unavailable to available
events.clear();
// Make sure that the devicemanager shows the device as available.
addDevice(referenceDevice, "1", 5);
devices.put(referenceDevice.id(), referenceDevice);
event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
postTopologyEvent(event);
// An earlier setup set half of the reference device ports to infrastructure
assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
for (int i = 0; i < 5; i++) {
assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
}
events.clear();
postTopologyEvent(event);
assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
// Test removal when state changes when the device becomes unavailable
// Ensure that the deviceManager shows the device as unavailable
removeDevice(referenceDevice);
// This variable copies the behavior of the topology by returning ports
// attached to an unavailable device this behavior is necessary for the
// following event to execute properly, if these statements are removed
// no events will be generated since no ports will be provided in
// getPorts() to EdgeManager.
alwaysReturnPorts = true;
postTopologyEvent(event);
alwaysReturnPorts = false;
assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
for (int i = 0; i < 5; i++) {
EdgePortEvent edgeEvent = events.get(i);
assertEquals("The event is of an unexpected type.", EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
assertThat("The event pertains to an unexpected port", edgeEvent.subject().port().toLong(), is(greaterThanOrEqualTo((long) numInfraPorts)));
}
}
use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.
the class EdgeManagerTest method testInternalCache.
@Test
public void testInternalCache() {
int numDevices = 10;
// Number of infrastructure ports per device
int numPorts = 5;
// Total ports per device when requesting all devices
totalPorts = 10;
defaultPopulator(numDevices, numPorts);
for (int i = 0; i < numDevices; i++) {
Device newDevice = NetTestTools.device(Integer.toString(i));
devices.put(newDevice.id(), newDevice);
postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice));
}
// Check all ports have correct designations
ConnectPoint testPoint;
for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) {
for (int portNum = 1; portNum <= totalPorts; portNum++) {
testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum);
if (portNum <= numPorts) {
assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint));
} else {
assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint));
}
}
}
int count = 0;
for (ConnectPoint ignored : mgr.getEdgePoints()) {
count++;
}
assertEquals("There are an unexpeceted number of edge points returned.", (totalPorts - numPorts) * numDevices, count);
for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) {
count = 0;
for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) {
count++;
}
assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count);
}
}
use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.
the class EdgeManager method processDeviceEvent.
// Processes a device event by adding or removing its end-points in our cache.
private void processDeviceEvent(DeviceEvent event) {
// FIXME handle the case where a device is suspended, this may or may not come up
DeviceEvent.Type type = event.type();
DeviceId id = event.subject().id();
if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) {
// When device is added or becomes available, add all its ports
deviceService.getPorts(event.subject().id()).forEach(p -> addEdgePort(new ConnectPoint(id, p.number())));
} else if (type == DEVICE_REMOVED || type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) {
// When device is removed or becomes unavailable, remove all its ports.
// Note: cannot rely on Device subsystem, ports may be gone.
Optional.ofNullable(connectionPoints.remove(id)).orElse(ImmutableSet.of()).forEach(point -> post(new EdgePortEvent(EDGE_PORT_REMOVED, point)));
} else if (type == DeviceEvent.Type.PORT_ADDED || type == PORT_UPDATED && event.port().isEnabled()) {
addEdgePort(new ConnectPoint(id, event.port().number()));
} else if (type == DeviceEvent.Type.PORT_REMOVED || type == PORT_UPDATED && !event.port().isEnabled()) {
removeEdgePort(new ConnectPoint(id, event.port().number()));
}
}
use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.
the class DeviceManager method removeDevicePorts.
@Override
public void removeDevicePorts(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
if (isAvailable(deviceId)) {
log.debug("Cannot remove ports of device {} while it is available.", deviceId);
return;
}
List<PortDescription> portDescriptions = ImmutableList.of();
List<DeviceEvent> events = store.updatePorts(getProvider(deviceId).id(), deviceId, portDescriptions);
if (events != null) {
for (DeviceEvent event : events) {
post(event);
}
}
}
Aggregations