Search in sources :

Example 56 with DeviceEvent

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));
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) DeviceListener(org.onosproject.net.device.DeviceListener) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Test(org.junit.Test)

Example 57 with DeviceEvent

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)));
    }
}
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 58 with DeviceEvent

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

Example 59 with DeviceEvent

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()));
    }
}
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)

Example 60 with DeviceEvent

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);
        }
    }
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) PortDescription(org.onosproject.net.device.PortDescription) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription)

Aggregations

DeviceEvent (org.onosproject.net.device.DeviceEvent)77 Test (org.junit.Test)42 Device (org.onosproject.net.Device)26 DefaultDevice (org.onosproject.net.DefaultDevice)17 DefaultPort (org.onosproject.net.DefaultPort)17 PortDescription (org.onosproject.net.device.PortDescription)14 PortNumber (org.onosproject.net.PortNumber)11 Port (org.onosproject.net.Port)10 DefaultPortDescription (org.onosproject.net.device.DefaultPortDescription)10 ProviderId (org.onosproject.net.provider.ProviderId)10 ArrayList (java.util.ArrayList)8 ConnectPoint (org.onosproject.net.ConnectPoint)8 Timestamp (org.onosproject.store.Timestamp)8 MessageSubject (org.onosproject.store.cluster.messaging.MessageSubject)8 MastershipBasedTimestamp (org.onosproject.store.impl.MastershipBasedTimestamp)8 MultiValuedTimestamp (org.onosproject.store.service.MultiValuedTimestamp)8 WallClockTimestamp (org.onosproject.store.service.WallClockTimestamp)8 BiFunction (java.util.function.BiFunction)7 Function (java.util.function.Function)7 DeviceDescription (org.onosproject.net.device.DeviceDescription)7