use of org.onosproject.net.edge.EdgePortEvent 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.edge.EdgePortEvent 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()));
}
}
Aggregations