Search in sources :

Example 1 with DEVICE_ADDED

use of org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED in project onos by opennetworkinglab.

the class GossipDeviceStoreTest method testEvents.

// If Delegates should be called only on remote events,
// then Simple* should never call them, thus not test required.
// TODO add test for Port events when we have them
@Ignore("Ignore until Delegate spec. is clear.")
@Test
public final void testEvents() throws InterruptedException {
    final CountDownLatch addLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkAdd = event -> {
        assertEquals(DEVICE_ADDED, event.type());
        assertDevice(DID1, SW1, event.subject());
        addLatch.countDown();
    };
    final CountDownLatch updateLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkUpdate = event -> {
        assertEquals(DEVICE_UPDATED, event.type());
        assertDevice(DID1, SW2, event.subject());
        updateLatch.countDown();
    };
    final CountDownLatch removeLatch = new CountDownLatch(1);
    DeviceStoreDelegate checkRemove = event -> {
        assertEquals(DEVICE_REMOVED, event.type());
        assertDevice(DID1, SW2, event.subject());
        removeLatch.countDown();
    };
    DeviceDescription description = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID);
    deviceStore.setDelegate(checkAdd);
    deviceStore.createOrUpdateDevice(PID, DID1, description);
    assertTrue("Add event fired", addLatch.await(1, TimeUnit.SECONDS));
    DeviceDescription description2 = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID);
    deviceStore.unsetDelegate(checkAdd);
    deviceStore.setDelegate(checkUpdate);
    deviceStore.createOrUpdateDevice(PID, DID1, description2);
    assertTrue("Update event fired", updateLatch.await(1, TimeUnit.SECONDS));
    deviceStore.unsetDelegate(checkUpdate);
    deviceStore.setDelegate(checkRemove);
    deviceStore.removeDevice(DID1);
    assertTrue("Remove event fired", removeLatch.await(1, TimeUnit.SECONDS));
}
Also used : DeviceClockServiceAdapter(org.onosproject.net.device.DeviceClockServiceAdapter) Arrays(java.util.Arrays) EasyMock.capture(org.easymock.EasyMock.capture) BiFunction(java.util.function.BiFunction) PortNumber(org.onosproject.net.PortNumber) DeviceStore(org.onosproject.net.device.DeviceStore) EventuallyConsistentMapBuilder(org.onosproject.store.service.EventuallyConsistentMapBuilder) DEVICE_REMOVED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED) DefaultControllerNode(org.onosproject.cluster.DefaultControllerNode) StorageService(org.onosproject.store.service.StorageService) EasyMock.reset(org.easymock.EasyMock.reset) Port(org.onosproject.net.Port) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Map(java.util.Map) MessageSubject(org.onosproject.store.cluster.messaging.MessageSubject) SWITCH(org.onosproject.net.Device.Type.SWITCH) Assert.fail(org.junit.Assert.fail) DeviceId.deviceId(org.onosproject.net.DeviceId.deviceId) DeviceStoreDelegate(org.onosproject.net.device.DeviceStoreDelegate) DeviceDescription(org.onosproject.net.device.DeviceDescription) NodeId(org.onosproject.cluster.NodeId) AfterClass(org.junit.AfterClass) Device(org.onosproject.net.Device) Set(java.util.Set) PORT_REMOVED(org.onosproject.net.device.DeviceEvent.Type.PORT_REMOVED) Sets(com.google.common.collect.Sets) CountDownLatch(java.util.concurrent.CountDownLatch) DeviceClockService(org.onosproject.net.device.DeviceClockService) List(java.util.List) Annotations(org.onosproject.net.Annotations) SparseAnnotations(org.onosproject.net.SparseAnnotations) Assert.assertFalse(org.junit.Assert.assertFalse) EasyMock.createNiceMock(org.easymock.EasyMock.createNiceMock) DeviceEvent(org.onosproject.net.device.DeviceEvent) ClusterCommunicationService(org.onosproject.store.cluster.messaging.ClusterCommunicationService) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) DEVICE_AVAILABILITY_CHANGED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED) ChassisId(org.onlab.packet.ChassisId) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) DEVICE_ADDED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) KryoNamespace(org.onlab.util.KryoNamespace) Function(java.util.function.Function) ControllerNode(org.onosproject.cluster.ControllerNode) PortDescription(org.onosproject.net.device.PortDescription) Timestamp(org.onosproject.store.Timestamp) ACTIVE(org.onosproject.cluster.ControllerNode.State.ACTIVE) EasyMock.replay(org.easymock.EasyMock.replay) EventuallyConsistentMap(org.onosproject.store.service.EventuallyConsistentMap) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) Before(org.junit.Before) IpAddress(org.onlab.packet.IpAddress) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Capture(org.easymock.Capture) StaticClusterService(org.onosproject.store.cluster.StaticClusterService) MastershipRole(org.onosproject.net.MastershipRole) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ProviderId(org.onosproject.net.provider.ProviderId) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) IOException(java.io.IOException) EasyMock.expect(org.easymock.EasyMock.expect) DefaultAnnotations.union(org.onosproject.net.DefaultAnnotations.union) ClusterMessage(org.onosproject.store.cluster.messaging.ClusterMessage) PORT_ADDED(org.onosproject.net.device.DeviceEvent.Type.PORT_ADDED) TimeUnit(java.util.concurrent.TimeUnit) EasyMock.expectLastCall(org.easymock.EasyMock.expectLastCall) AtomicLong(java.util.concurrent.atomic.AtomicLong) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) DefaultPortDescription(org.onosproject.net.device.DefaultPortDescription) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) MastershipServiceAdapter(org.onosproject.mastership.MastershipServiceAdapter) EasyMock.verify(org.easymock.EasyMock.verify) PORT_UPDATED(org.onosproject.net.device.DeviceEvent.Type.PORT_UPDATED) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) DEVICE_UPDATED(org.onosproject.net.device.DeviceEvent.Type.DEVICE_UPDATED) DeviceDescription(org.onosproject.net.device.DeviceDescription) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) DeviceStoreDelegate(org.onosproject.net.device.DeviceStoreDelegate) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with DEVICE_ADDED

use of org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED 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

Sets (com.google.common.collect.Sets)2 Map (java.util.Map)2 Set (java.util.Set)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1