Sho SHIMIZU
Committed by Gerrit Code Review

ONOS-3296: Support continuous type resources

Change-Id: I155e41e7a7c1750ff45986a55bedab353485d3fa
......@@ -126,13 +126,13 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
boolean release(ResourceConsumer consumer);
/**
* Returns resource allocation of the specified resource.
* Returns resource allocations of the specified resource.
*
* @param resource resource to check the allocation
* @return allocation information enclosed by Optional.
* If the resource is not allocated, the return value is empty.
* @return list of allocation information.
* If the resource is not allocated, the return value is an empty list.
*/
Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource);
List<ResourceAllocation> getResourceAllocation(ResourcePath resource);
/**
* Returns allocated resources being as children of the specified parent and being the specified resource type.
......
/*
* Copyright 2015 Open Networking Laboratory
* Copyright 2015-2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -20,7 +20,6 @@ import org.onosproject.store.Store;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* Service for storing resource and consumer information.
......@@ -77,12 +76,23 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers);
/**
* Returns the resource consumer to whom the specified resource is allocated.
* Returns the resource consumers to whom the specified resource is allocated.
* The return value is a list having only one element when the given resource is discrete type.
* The return value may have multiple elements when the given resource is continuous type.
*
* @param resource resource whose allocated consumer to be returned
* @return resource consumer who are allocated the resource
* @return resource consumers who are allocated the resource.
* Returns empty list if there is no such consumer.
*/
Optional<ResourceConsumer> getConsumer(ResourcePath resource);
List<ResourceConsumer> getConsumers(ResourcePath resource);
/**
* Returns the availability of the specified resource.
*
* @param resource resource to check the availability
* @return true if available, otherwise false
*/
boolean isAvailable(ResourcePath resource);
/**
* Returns a collection of the resources allocated to the specified consumer.
......
......@@ -313,9 +313,11 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Optional<IntentId> intentId =
resourceService.getResourceAllocation(ResourcePath.discrete(ochCP.deviceId(), ochCP.port()))
.stream()
.map(ResourceAllocation::consumer)
.filter(x -> x instanceof IntentId)
.map(x -> (IntentId) x);
.map(x -> (IntentId) x)
.findAny();
if (isAvailable(intentId.orElse(null))) {
return ochPort;
......@@ -332,9 +334,12 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
Optional<IntentId> intentId =
resourceService.getResourceAllocation(ResourcePath.discrete(oduPort.deviceId(), port.number()))
.stream()
.map(ResourceAllocation::consumer)
.filter(x -> x instanceof IntentId)
.map(x -> (IntentId) x);
.map(x -> (IntentId) x)
.findAny();
if (isAvailable(intentId.orElse(null))) {
return (OchPort) port;
}
......
/*
* Copyright 2015 Open Networking Laboratory
* Copyright 2015-2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.GuavaCollectors;
import org.onosproject.event.AbstractListenerManager;
import org.onosproject.net.newresource.ResourceAdminService;
import org.onosproject.net.newresource.ResourceAllocation;
......@@ -34,10 +35,8 @@ import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.newresource.ResourceStore;
import org.onosproject.net.newresource.ResourceStoreDelegate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -107,11 +106,13 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
checkNotNull(resource);
Optional<ResourceConsumer> consumer = store.getConsumer(resource);
return consumer.map(x -> new ResourceAllocation(resource, x));
List<ResourceConsumer> consumers = store.getConsumers(resource);
return consumers.stream()
.map(x -> new ResourceAllocation(resource, x))
.collect(GuavaCollectors.toImmutableList());
}
@Override
......@@ -119,17 +120,12 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
checkNotNull(parent);
checkNotNull(cls);
Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
List<ResourceAllocation> allocations = new ArrayList<>(resources.size());
for (ResourcePath resource: resources) {
// We access store twice in this method, then the store may be updated by others
Optional<ResourceConsumer> consumer = store.getConsumer(resource);
if (consumer.isPresent()) {
allocations.add(new ResourceAllocation(resource, consumer.get()));
}
}
return allocations;
Collection<ResourcePath> resources = store.getAllocatedResources(parent, cls);
return resources.stream()
.flatMap(resource -> store.getConsumers(resource).stream()
.map(consumer -> new ResourceAllocation(resource, consumer)))
.collect(GuavaCollectors.toImmutableList());
}
@Override
......@@ -149,7 +145,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
Collection<ResourcePath> children = store.getChildResources(parent);
return children.stream()
// We access store twice in this method, then the store may be updated by others
.filter(x -> !store.getConsumer(x).isPresent())
.filter(store::isAvailable)
.collect(Collectors.toList());
}
......@@ -157,8 +153,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
public boolean isAvailable(ResourcePath resource) {
checkNotNull(resource);
Optional<ResourceConsumer> consumer = store.getConsumer(resource);
return !consumer.isPresent();
return store.isAvailable(resource);
}
@Override
......
......@@ -66,9 +66,10 @@ class MockResourceService implements ResourceService {
}
@Override
public Optional<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
public List<ResourceAllocation> getResourceAllocation(ResourcePath resource) {
return Optional.ofNullable(assignment.get(resource))
.map(x -> new ResourceAllocation(resource, x));
.map(x -> ImmutableList.of(new ResourceAllocation(resource, x)))
.orElse(ImmutableList.of());
}
@Override
......
......@@ -204,6 +204,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
......@@ -239,7 +240,8 @@ public final class KryoNamespaces {
.register(CopyOnWriteArraySet.class)
.register(ArrayList.class,
LinkedList.class,
HashSet.class
HashSet.class,
LinkedHashSet.class
)
.register(Maps.immutableEntry("a", "b").getClass())
.register(new ArraysAsListSerializer(), Arrays.asList().getClass())
......