Search in sources :

Example 16 with DeviceEvent

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

the class GossipDeviceStore method removeDeviceInternal.

private DeviceEvent removeDeviceInternal(DeviceId deviceId, Timestamp timestamp) {
    Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId);
    synchronized (descs) {
        // accept removal request if given timestamp is newer than
        // the latest Timestamp from Primary provider
        DeviceDescriptions primDescs = getPrimaryDescriptions(descs);
        if (primDescs == null) {
            return null;
        }
        Timestamp lastTimestamp = primDescs.getLatestTimestamp();
        // If no timestamp is set, default the timestamp to the last timestamp for the device.
        if (timestamp == null) {
            timestamp = lastTimestamp;
        }
        if (timestamp.compareTo(lastTimestamp) <= 0) {
            // outdated event ignore
            return null;
        }
        removalRequest.put(deviceId, timestamp);
        Device device = devices.remove(deviceId);
        // removing internal description
        deviceDescs.remove(deviceId);
        // should DEVICE_REMOVED carry removed ports?
        Map<PortNumber, Port> ports = devicePorts.get(deviceId);
        if (ports != null) {
            ports.clear();
        }
        markOfflineInternal(deviceId, timestamp);
        descs.clear();
        // Forget about the device
        offline.remove(deviceId);
        return device == null ? null : new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, device, null);
    }
}
Also used : ProviderId(org.onosproject.net.provider.ProviderId) DeviceEvent(org.onosproject.net.device.DeviceEvent) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DefaultPort(org.onosproject.net.DefaultPort) PortNumber(org.onosproject.net.PortNumber) WallClockTimestamp(org.onosproject.store.service.WallClockTimestamp) Timestamp(org.onosproject.store.Timestamp) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) MultiValuedTimestamp(org.onosproject.store.service.MultiValuedTimestamp)

Example 17 with DeviceEvent

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

the class ECDeviceStore method refreshDevicePortCache.

private List<DeviceEvent> refreshDevicePortCache(ProviderId providerId, DeviceId deviceId, Optional<PortNumber> portNumber) {
    Device device = devices.get(deviceId);
    checkArgument(device != null, DEVICE_NOT_FOUND, deviceId);
    List<DeviceEvent> events = Lists.newArrayList();
    Map<PortNumber, Port> ports = devicePorts.computeIfAbsent(deviceId, key -> Maps.newConcurrentMap());
    List<PortDescription> descriptions = Lists.newArrayList();
    portDescriptions.entrySet().forEach(e -> {
        PortKey key = e.getKey();
        PortDescription value = e.getValue();
        if (key.deviceId().equals(deviceId) && key.providerId().equals(providerId)) {
            if (portNumber.isPresent()) {
                if (portNumber.get().equals(key.portNumber())) {
                    descriptions.add(value);
                }
            } else {
                descriptions.add(value);
            }
        }
    });
    for (PortDescription description : descriptions) {
        final PortNumber number = description.portNumber();
        ports.compute(number, (k, existingPort) -> {
            Port newPort = composePort(device, number);
            if (existingPort == null) {
                events.add(new DeviceEvent(PORT_ADDED, device, newPort));
            } else {
                if (existingPort.isEnabled() != newPort.isEnabled() || existingPort.type() != newPort.type() || existingPort.portSpeed() != newPort.portSpeed() || !AnnotationsUtil.isEqual(existingPort.annotations(), newPort.annotations())) {
                    events.add(new DeviceEvent(PORT_UPDATED, device, newPort));
                }
            }
            return newPort;
        });
    }
    return events;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DefaultPort(org.onosproject.net.DefaultPort) PortDescription(org.onosproject.net.device.PortDescription) PortNumber(org.onosproject.net.PortNumber)

Example 18 with DeviceEvent

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

the class ECDeviceStore method refreshDeviceCache.

private DeviceEvent refreshDeviceCache(ProviderId providerId, DeviceId deviceId) {
    AtomicReference<DeviceEvent.Type> eventType = new AtomicReference<>();
    Device device = devices.compute(deviceId, (k, existingDevice) -> {
        Device newDevice = composeDevice(deviceId);
        if (existingDevice == null) {
            eventType.set(DEVICE_ADDED);
        } else {
            // We allow only certain attributes to trigger update
            boolean propertiesChanged = !Objects.equals(existingDevice.hwVersion(), newDevice.hwVersion()) || !Objects.equals(existingDevice.swVersion(), newDevice.swVersion()) || !Objects.equals(existingDevice.providerId(), newDevice.providerId());
            boolean annotationsChanged = !AnnotationsUtil.isEqual(existingDevice.annotations(), newDevice.annotations());
            // should respond only to annotation changes.
            if ((providerId.isAncillary() && annotationsChanged) || (!providerId.isAncillary() && (propertiesChanged || annotationsChanged))) {
                boolean replaced = devices.replace(deviceId, existingDevice, newDevice);
                verify(replaced, "Replacing devices cache failed. PID:%s [expected:%s, found:%s, new=%s]", providerId, existingDevice, devices.get(deviceId), newDevice);
                eventType.set(DEVICE_UPDATED);
            }
        }
        return newDevice;
    });
    if (eventType.get() != null && !providerId.isAncillary()) {
        markOnline(deviceId);
    }
    return eventType.get() != null ? new DeviceEvent(eventType.get(), device) : null;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) Type(org.onosproject.net.Device.Type) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 19 with DeviceEvent

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

the class ObjectiveTrackerTest method testEventHostAvailableNoMatch.

/**
 * Tests an event for a host becoming available that matches an intent.
 *
 * @throws InterruptedException if the latch wait fails.
 */
@Test
public void testEventHostAvailableNoMatch() throws Exception {
    final Device host = device("host1");
    final DeviceEvent deviceEvent = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, host);
    reasons.add(deviceEvent);
    deviceListener.event(deviceEvent);
    assertThat(delegate.latch.await(WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS), is(true));
    assertThat(delegate.intentIdsFromEvent, hasSize(0));
    assertThat(delegate.compileAllFailedFromEvent, is(true));
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) Device(org.onosproject.net.Device) Test(org.junit.Test) AbstractIntentTest(org.onosproject.net.intent.AbstractIntentTest)

Example 20 with DeviceEvent

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

the class GossipDeviceStoreTest method testCreateOrUpdateDevice.

@Test
public final void testCreateOrUpdateDevice() throws IOException {
    DeviceDescription description = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID);
    Capture<InternalDeviceEvent> message = Capture.newInstance();
    Capture<MessageSubject> subject = Capture.newInstance();
    Capture<Function<InternalDeviceEvent, byte[]>> encoder = Capture.newInstance();
    resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
    DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description);
    assertEquals(DEVICE_ADDED, event.type());
    assertDevice(DID1, SW1, event.subject());
    verify(clusterCommunicator);
    assertInternalDeviceEvent(NID1, DID1, PID, description, message, subject, encoder);
    DeviceDescription description2 = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID);
    resetCommunicatorExpectingSingleBroadcast(message, subject, encoder);
    DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2);
    assertEquals(DEVICE_UPDATED, event2.type());
    assertDevice(DID1, SW2, event2.subject());
    verify(clusterCommunicator);
    assertInternalDeviceEvent(NID1, DID1, PID, description2, message, subject, encoder);
    reset(clusterCommunicator);
    assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2));
}
Also used : DeviceDescription(org.onosproject.net.device.DeviceDescription) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) DeviceEvent(org.onosproject.net.device.DeviceEvent) MessageSubject(org.onosproject.store.cluster.messaging.MessageSubject) DefaultDeviceDescription(org.onosproject.net.device.DefaultDeviceDescription) Test(org.junit.Test)

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