Search in sources :

Example 1 with GroupListener

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);
    }
}
Also used : DefaultEdgeLink.createEdgeLink(org.onosproject.net.DefaultEdgeLink.createEdgeLink) ListIterator(java.util.ListIterator) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) PortNumber(org.onosproject.net.PortNumber) TimeoutException(java.util.concurrent.TimeoutException) Link(org.onosproject.net.Link) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Pair(org.apache.commons.lang3.tuple.Pair) Port(org.onosproject.net.Port) GroupListener(org.onosproject.net.group.GroupListener) Arrays.asList(java.util.Arrays.asList) PathNotFoundException(org.onosproject.net.intent.impl.PathNotFoundException) FlowRuleIntent(org.onosproject.net.intent.FlowRuleIntent) ImmutableSet(com.google.common.collect.ImmutableSet) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) FlowRule(org.onosproject.net.flow.FlowRule) GroupBuckets(org.onosproject.net.group.GroupBuckets) LinkService(org.onosproject.net.link.LinkService) Path(org.onosproject.net.Path) DeviceId(org.onosproject.net.DeviceId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) GroupDescription(org.onosproject.net.group.GroupDescription) IntStream(java.util.stream.IntStream) IntentCompilationException(org.onosproject.net.intent.IntentCompilationException) CompletableFuture(java.util.concurrent.CompletableFuture) GroupBucket(org.onosproject.net.group.GroupBucket) GroupKey(org.onosproject.net.group.GroupKey) Group(org.onosproject.net.group.Group) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) DefaultPath(org.onosproject.net.DefaultPath) Intent(org.onosproject.net.intent.Intent) Activate(org.osgi.service.component.annotations.Activate) EdgeLink(org.onosproject.net.EdgeLink) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) IntentId(org.onosproject.net.intent.IntentId) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PathIntent(org.onosproject.net.intent.PathIntent) Instructions(org.onosproject.net.flow.instructions.Instructions) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Instruction(org.onosproject.net.flow.instructions.Instruction) GroupService(org.onosproject.net.group.GroupService) ProtectionConstraint(org.onosproject.net.intent.constraint.ProtectionConstraint) LinkCollectionIntent(org.onosproject.net.intent.LinkCollectionIntent) ProviderId(org.onosproject.net.provider.ProviderId) DefaultGroupBucket(org.onosproject.net.group.DefaultGroupBucket) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PointToPointIntent(org.onosproject.net.intent.PointToPointIntent) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ScalarWeight(org.onlab.graph.ScalarWeight) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) DisjointPath(org.onosproject.net.DisjointPath) Group(org.onosproject.net.group.Group) CompletableFuture(java.util.concurrent.CompletableFuture) GroupListener(org.onosproject.net.group.GroupListener) IntentCompilationException(org.onosproject.net.intent.IntentCompilationException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Pair (org.apache.commons.lang3.tuple.Pair)1 ScalarWeight (org.onlab.graph.ScalarWeight)1 ConnectPoint (org.onosproject.net.ConnectPoint)1 DefaultEdgeLink.createEdgeLink (org.onosproject.net.DefaultEdgeLink.createEdgeLink)1