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