Search in sources :

Example 1 with EdgePortEvent

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)));
    }
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) Device(org.onosproject.net.Device) Iterator(java.util.Iterator) EdgePortEvent(org.onosproject.net.edge.EdgePortEvent) ConnectPoint(org.onosproject.net.ConnectPoint) ConnectPoint(org.onosproject.net.ConnectPoint) DefaultPort(org.onosproject.net.DefaultPort) Test(org.junit.Test)

Example 2 with EdgePortEvent

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()));
    }
}
Also used : PACKET_WRITE(org.onosproject.security.AppPermission.Type.PACKET_WRITE) EdgePortEvent(org.onosproject.net.edge.EdgePortEvent) LinkEvent(org.onosproject.net.link.LinkEvent) DEVICE_ADDED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED) DeviceService(org.onosproject.net.device.DeviceService) AppGuard.checkPermission(org.onosproject.security.AppGuard.checkPermission) DEVICE_REMOVED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Component(org.osgi.service.component.annotations.Component) OutboundPacket(org.onosproject.net.packet.OutboundPacket) Map(java.util.Map) Activate(org.osgi.service.component.annotations.Activate) EdgePortService(org.onosproject.net.edge.EdgePortService) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PORT_STATS_UPDATED(org.onosproject.net.device.DeviceEvent.Type.PORT_STATS_UPDATED) EDGE_PORT_ADDED(org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED) EDGE_PORT_REMOVED(org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED) DeviceListener(org.onosproject.net.device.DeviceListener) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) AbstractListenerManager(org.onosproject.event.AbstractListenerManager) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) PacketService(org.onosproject.net.packet.PacketService) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) LinkListener(org.onosproject.net.link.LinkListener) EdgePortListener(org.onosproject.net.edge.EdgePortListener) DeviceEvent(org.onosproject.net.device.DeviceEvent) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) LinkService(org.onosproject.net.link.LinkService) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) Reference(org.osgi.service.component.annotations.Reference) PORT_UPDATED(org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED) TOPOLOGY_READ(org.onosproject.security.AppPermission.Type.TOPOLOGY_READ) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) DEVICE_AVAILABILITY_CHANGED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) Type(org.onosproject.net.Link.Type) DeviceEvent(org.onosproject.net.device.DeviceEvent) DeviceId(org.onosproject.net.DeviceId) EdgePortEvent(org.onosproject.net.edge.EdgePortEvent) ConnectPoint(org.onosproject.net.ConnectPoint)

Aggregations

ConnectPoint (org.onosproject.net.ConnectPoint)2 DeviceEvent (org.onosproject.net.device.DeviceEvent)2 EdgePortEvent (org.onosproject.net.edge.EdgePortEvent)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 ByteBuffer (java.nio.ByteBuffer)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Test (org.junit.Test)1 AbstractListenerManager (org.onosproject.event.AbstractListenerManager)1 DefaultPort (org.onosproject.net.DefaultPort)1 Device (org.onosproject.net.Device)1 DeviceId (org.onosproject.net.DeviceId)1 Type (org.onosproject.net.Link.Type)1 DEVICE_ADDED (org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED)1 DEVICE_AVAILABILITY_CHANGED (org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED)1 DEVICE_REMOVED (org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED)1