use of org.onosproject.net.group.GroupListener in project onos by opennetworkinglab.
the class PointToPointIntentCompiler method waitForGroup.
/**
* Waits for specified group to appear until timeout.
*
* @param deviceId {@link DeviceId}
* @param groupKey {@link GroupKey} to wait for.
* @param timeout timeout
* @param unit unit of timeout
* @return {@link Group}
* @throws IntentCompilationException on any error.
*/
private Group waitForGroup(DeviceId deviceId, GroupKey groupKey, long timeout, TimeUnit unit) {
Group group = groupService.getGroup(deviceId, groupKey);
if (group != null) {
return group;
}
final CompletableFuture<Group> future = new CompletableFuture<>();
final GroupListener listener = event -> {
if (event.subject().deviceId() == deviceId && event.subject().appCookie().equals(groupKey)) {
future.complete(event.subject());
return;
}
};
groupService.addListener(listener);
try {
group = groupService.getGroup(deviceId, groupKey);
if (group != null) {
return group;
}
return future.get(timeout, unit);
} catch (InterruptedException e) {
log.debug("Interrupted", e);
Thread.currentThread().interrupt();
throw new IntentCompilationException("Interrupted", e);
} catch (ExecutionException e) {
log.debug("ExecutionException", e);
throw new IntentCompilationException("ExecutionException caught", e);
} catch (TimeoutException e) {
// one last try
group = groupService.getGroup(deviceId, groupKey);
if (group != null) {
return group;
} else {
log.debug("Timeout", e);
throw new IntentCompilationException("Timeout", e);
}
} finally {
groupService.removeListener(listener);
}
}
Aggregations