Sho SHIMIZU
Committed by Sho Shimizu

Move inner classes to upper level

Rename as follows.
- ResourceId.Discrete -> DiscreteResourceId
- ResourceId.Continuous -> ContinuousResourceId
- Resource.Discrete -> DiscreteResource
- Resource.Continuous -> ContinuousResource

Change-Id: I80e59d1eec07128743bacc582a8725bd4d5489b4
......@@ -34,6 +34,7 @@ import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.TributarySlot;
import org.onosproject.net.newresource.ContinuousResource;
import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceService;
......@@ -112,10 +113,10 @@ public class ResourcesCommand extends AbstractShellCommand {
return;
}
if (resource instanceof Resource.Continuous) {
if (resource instanceof ContinuousResource) {
print("%s%s: %f", Strings.repeat(" ", level),
resource.last(),
((Resource.Continuous) resource).value());
((ContinuousResource) resource).value());
// Continuous resource is terminal node, stop here
return;
} else {
......@@ -144,7 +145,7 @@ public class ResourcesCommand extends AbstractShellCommand {
List<Resource> nonAggregatable = new ArrayList<>();
for (Resource r : children) {
if (r instanceof Resource.Continuous) {
if (r instanceof ContinuousResource) {
// non-aggregatable terminal node
nonAggregatable.add(r);
} else if (aggregatableTypes.contains(r.last().getClass())) {
......
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import java.util.Objects;
/**
* Represents a resource path which specifies a resource which can be measured
* as continuous value. Bandwidth of a link is an example of the resource.
* <p>
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
@Beta
// TODO: consider how to restrict the visibility
public final class ContinuousResource extends Resource {
private final double value;
ContinuousResource(ResourceId id, double value) {
super(id);
this.value = value;
}
/**
* The user of this methods must receive the return value as Double or double.
* Otherwise, this methods throws an exception.
*
* @param <T> type of the return value
* @return the volume of this resource
*/
@SuppressWarnings("unchecked")
@Override
public <T> T volume() {
return (T) Double.valueOf(value);
}
@Override
public int hashCode() {
return Objects.hash(id(), value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final ContinuousResource other = (ContinuousResource) obj;
return Objects.equals(this.id(), other.id())
&& Objects.equals(this.value, other.value);
}
/**
* Returns the value of the resource amount.
*
* @return the value of the resource amount
*/
// FIXME: overlapping a purpose with volume()
public double value() {
return value;
}
}
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* ResourceId for {@link ContinuousResource}
*
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
@Beta
// TODO: consider how to restrict the visibility
public final class ContinuousResourceId extends ResourceId {
// for printing purpose only (used in toString() implementation)
private final String name;
ContinuousResourceId(ImmutableList<Object> components, String name) {
super(components);
this.name = checkNotNull(name);
}
@Override
public String toString() {
// due to performance consideration, the value might need to be stored in a field
return ImmutableList.builder()
.addAll(components.subList(0, components.size() - 1))
.add(name)
.build().toString();
}
}
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import java.util.Objects;
/**
* Represents a resource path which specifies a resource which can be measured
* as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource.
* <p>
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
* </p>
*/
@Beta
// TODO: consider how to restrict the visibility
public final class DiscreteResource extends Resource {
protected DiscreteResource() {
super();
}
DiscreteResource(ResourceId id) {
super(id);
}
/**
* The user of this methods must receive the return value as the correct type.
* Otherwise, this methods throws an exception.
*
* @param <T> type of the return value
* @return the volume of this resource
*/
@SuppressWarnings("unchecked")
@Override
// TODO: consider receiving Class<T> as an argument. Which approach is convenient?
public <T> T volume() {
return (T) last();
}
@Override
public int hashCode() {
// the value returing from volume() is excluded due to optimization
return id().hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DiscreteResource other = (DiscreteResource) obj;
// the value returing from volume() is excluded due to optimization
return Objects.equals(this.id(), other.id());
}
}
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
/**
* ResourceId for {@link DiscreteResource}.
*
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
@Beta
// TODO: consider how to restrict the visibility
public final class DiscreteResourceId extends ResourceId {
DiscreteResourceId(ImmutableList<Object> components) {
super(components);
}
DiscreteResourceId() {
super();
}
}
......@@ -21,7 +21,6 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
......@@ -47,13 +46,13 @@ import static com.google.common.base.Preconditions.checkState;
@Beta
public abstract class Resource {
private final Discrete parent;
private final DiscreteResource parent;
private final ResourceId id;
public static final Discrete ROOT = new Discrete();
public static final DiscreteResource ROOT = new DiscreteResource();
public static Resource discrete(DeviceId device) {
return new Discrete(ResourceId.discrete(device));
return new DiscreteResource(ResourceId.discrete(device));
}
/**
......@@ -64,7 +63,7 @@ public abstract class Resource {
* @return resource path instance
*/
public static Resource discrete(DeviceId device, Object... components) {
return new Discrete(ResourceId.discrete(device, components));
return new DiscreteResource(ResourceId.discrete(device, components));
}
/**
......@@ -76,7 +75,7 @@ public abstract class Resource {
* @return resource path instance
*/
public static Resource discrete(DeviceId device, PortNumber port, Object... components) {
return new Discrete(ResourceId.discrete(device, port, components));
return new DiscreteResource(ResourceId.discrete(device, port, components));
}
/**
......@@ -93,7 +92,7 @@ public abstract class Resource {
checkArgument(components.length > 0,
"Length of components must be greater thant 0, but " + components.length);
return new Continuous(ResourceId.continuous(device, components), value);
return new ContinuousResource(ResourceId.continuous(device, components), value);
}
/**
......@@ -108,7 +107,7 @@ public abstract class Resource {
* @return resource path instance
*/
public static Resource continuous(double value, DeviceId device, PortNumber port, Object... components) {
return new Continuous(ResourceId.continuous(device, port, components), value);
return new ContinuousResource(ResourceId.continuous(device, port, components), value);
}
/**
......@@ -123,12 +122,12 @@ public abstract class Resource {
if (id.components.size() == 1) {
this.parent = ROOT;
} else {
this.parent = new Discrete(id.parent());
this.parent = new DiscreteResource(id.parent());
}
}
// for serialization
private Resource() {
protected Resource() {
this.parent = null;
this.id = ResourceId.ROOT;
}
......@@ -157,7 +156,7 @@ public abstract class Resource {
* @return the parent resource path of this instance.
* If there is no parent, empty instance will be returned.
*/
public Optional<Discrete> parent() {
public Optional<DiscreteResource> parent() {
return Optional.ofNullable(parent);
}
......@@ -169,9 +168,9 @@ public abstract class Resource {
* @return a child resource path
*/
public Resource child(Object child) {
checkState(this instanceof Discrete);
checkState(this instanceof DiscreteResource);
return new Discrete(id().child(child));
return new DiscreteResource(id().child(child));
}
/**
......@@ -183,9 +182,9 @@ public abstract class Resource {
* @return a child resource path
*/
public Resource child(Object child, double value) {
checkState(this instanceof Discrete);
checkState(this instanceof DiscreteResource);
return new Continuous(id.child(child), value);
return new ContinuousResource(id.child(child), value);
}
/**
......@@ -218,114 +217,4 @@ public abstract class Resource {
.toString();
}
/**
* Represents a resource path which specifies a resource which can be measured
* as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource.
* <p>
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
* </p>
*/
@Beta
public static final class Discrete extends Resource {
private Discrete() {
super();
}
private Discrete(ResourceId id) {
super(id);
}
/**
* The user of this methods must receive the return value as the correct type.
* Otherwise, this methods throws an exception.
*
* @param <T> type of the return value
* @return the volume of this resource
*/
@SuppressWarnings("unchecked")
@Override
// TODO: consider receiving Class<T> as an argument. Which approach is convenient?
public <T> T volume() {
return (T) last();
}
@Override
public int hashCode() {
// the value returing from volume() is excluded due to optimization
return id().hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Discrete other = (Discrete) obj;
// the value returing from volume() is excluded due to optimization
return Objects.equals(this.id(), other.id());
}
}
/**
* Represents a resource path which specifies a resource which can be measured
* as continuous value. Bandwidth of a link is an example of the resource.
* <p>
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
@Beta
public static final class Continuous extends Resource {
private final double value;
private Continuous(ResourceId id, double value) {
super(id);
this.value = value;
}
/**
* The user of this methods must receive the return value as Double or double.
* Otherwise, this methods throws an exception.
*
* @param <T> type of the return value
* @return the volume of this resource
*/
@SuppressWarnings("unchecked")
@Override
public <T> T volume() {
return (T) Double.valueOf(value);
}
@Override
public int hashCode() {
return Objects.hash(id(), value);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Continuous other = (Continuous) obj;
return Objects.equals(this.id(), other.id())
&& Objects.equals(this.value, other.value);
}
/**
* Returns the value of the resource amount.
*
* @return the value of the resource amount
*/
// FIXME: overlapping a purpose with volume()
public double value() {
return value;
}
}
}
......
......@@ -33,19 +33,19 @@ import static com.google.common.base.Preconditions.checkState;
*/
@Beta
public abstract class ResourceId {
static final ResourceId ROOT = new Discrete();
static final ResourceId ROOT = new DiscreteResourceId();
final ImmutableList<Object> components;
static ResourceId discrete(DeviceId device, Object... components) {
return new Discrete(ImmutableList.builder()
return new DiscreteResourceId(ImmutableList.builder()
.add(device)
.add(components)
.build());
}
static ResourceId discrete(DeviceId device, PortNumber port, Object... components) {
return new Discrete(ImmutableList.builder()
return new DiscreteResourceId(ImmutableList.builder()
.add(device)
.add(port)
.add(components)
......@@ -72,17 +72,17 @@ public abstract class ResourceId {
}
private static ResourceId continuous(ImmutableList.Builder<Object> parentComponents, Class<?> last) {
return new Continuous(parentComponents
return new ContinuousResourceId(parentComponents
.add(last.getCanonicalName())
.build(), last.getSimpleName());
}
private ResourceId(ImmutableList<Object> components) {
protected ResourceId(ImmutableList<Object> components) {
this.components = checkNotNull(components);
}
// for serializer
private ResourceId() {
protected ResourceId() {
this.components = ImmutableList.of();
}
......@@ -91,27 +91,27 @@ public abstract class ResourceId {
if (components.size() == 1) {
return ROOT;
} else {
return new Discrete(components.subList(0, components.size() - 1));
return new DiscreteResourceId(components.subList(0, components.size() - 1));
}
}
/**
* Returns a resource ID of a child of this resource based on the specified object.
* If the argument is an instance of {@link Class}, this method returns an instance of
* {@link Continuous}. Otherwise, it returns an instance of {@link Discrete}
* This method only work when the receiver is {@link Discrete}. Otherwise,
* {@link ContinuousResourceId}. Otherwise, it returns an instance of {@link DiscreteResourceId}
* This method only work when the receiver is {@link DiscreteResourceId}. Otherwise,
* this method throws an exception.
*
* @param child the last component of the child
* @return a child resource ID
*/
public ResourceId child(Object child) {
checkState(this instanceof Discrete);
checkState(this instanceof DiscreteResourceId);
if (child instanceof Class<?>) {
return continuous(ImmutableList.builder().addAll(components), (Class<?>) child);
} else {
return new Discrete(ImmutableList.builder()
return new DiscreteResourceId(ImmutableList.builder()
.addAll(components)
.add(child)
.build());
......@@ -140,44 +140,4 @@ public abstract class ResourceId {
return components.toString();
}
/**
* ResourceId for {@link Resource.Discrete}.
*
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
public static final class Discrete extends ResourceId {
private Discrete(ImmutableList<Object> components) {
super(components);
}
private Discrete() {
super();
}
}
/**
* ResourceId for {@link Resource.Continuous}
*
* Note: This class is exposed to the public, but intended to be used in the resource API
* implementation only. It is not for resource API user.
*/
public static final class Continuous extends ResourceId {
// for printing purpose only (used in toString() implementation)
private final String name;
private Continuous(ImmutableList<Object> components, String name) {
super(components);
this.name = checkNotNull(name);
}
@Override
public String toString() {
// due to performance consideration, the value might need to be stored in a field
return ImmutableList.builder()
.addAll(components.subList(0, components.size() - 1))
.add(name)
.build().toString();
}
}
}
......
......@@ -25,6 +25,8 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.GuavaCollectors;
import org.onlab.util.Tools;
import org.onosproject.net.newresource.ContinuousResource;
import org.onosproject.net.newresource.DiscreteResource;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceConsumer;
import org.onosproject.net.newresource.ResourceEvent;
......@@ -83,13 +85,13 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected StorageService service;
private ConsistentMap<Resource.Discrete, ResourceConsumer> discreteConsumers;
private ConsistentMap<DiscreteResource, ResourceConsumer> discreteConsumers;
private ConsistentMap<ResourceId, ContinuousResourceAllocation> continuousConsumers;
private ConsistentMap<Resource.Discrete, Set<Resource>> childMap;
private ConsistentMap<DiscreteResource, Set<Resource>> childMap;
@Activate
public void activate() {
discreteConsumers = service.<Resource.Discrete, ResourceConsumer>consistentMapBuilder()
discreteConsumers = service.<DiscreteResource, ResourceConsumer>consistentMapBuilder()
.withName(DISCRETE_CONSUMER_MAP)
.withSerializer(SERIALIZER)
.build();
......@@ -97,7 +99,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
.withName(CONTINUOUS_CONSUMER_MAP)
.withSerializer(SERIALIZER)
.build();
childMap = service.<Resource.Discrete, Set<Resource>>consistentMapBuilder()
childMap = service.<DiscreteResource, Set<Resource>>consistentMapBuilder()
.withName(CHILD_MAP)
.withSerializer(SERIALIZER)
.build();
......@@ -110,16 +112,16 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
@Override
public List<ResourceConsumer> getConsumers(Resource resource) {
checkNotNull(resource);
checkArgument(resource instanceof Resource.Discrete || resource instanceof Resource.Continuous);
checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource);
if (resource instanceof Resource.Discrete) {
return getConsumer((Resource.Discrete) resource);
if (resource instanceof DiscreteResource) {
return getConsumer((DiscreteResource) resource);
} else {
return getConsumer((Resource.Continuous) resource);
return getConsumer((ContinuousResource) resource);
}
}
private List<ResourceConsumer> getConsumer(Resource.Discrete resource) {
private List<ResourceConsumer> getConsumer(DiscreteResource resource) {
Versioned<ResourceConsumer> consumer = discreteConsumers.get(resource);
if (consumer == null) {
return ImmutableList.of();
......@@ -128,7 +130,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
return ImmutableList.of(consumer.value());
}
private List<ResourceConsumer> getConsumer(Resource.Continuous resource) {
private List<ResourceConsumer> getConsumer(ContinuousResource resource) {
Versioned<ContinuousResourceAllocation> allocations = continuousConsumers.get(resource.id());
if (allocations == null) {
return ImmutableList.of();
......@@ -150,15 +152,15 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap =
TransactionalMap<DiscreteResource, Set<Resource>> childTxMap =
tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream()
Map<DiscreteResource, List<Resource>> resourceMap = resources.stream()
.filter(x -> x.parent().isPresent())
.collect(Collectors.groupingBy(x -> x.parent().get()));
for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) {
Optional<Resource.Discrete> child = lookup(childTxMap, entry.getKey());
for (Map.Entry<DiscreteResource, List<Resource>> entry: resourceMap.entrySet()) {
Optional<DiscreteResource> child = lookup(childTxMap, entry.getKey());
if (!child.isPresent()) {
return abortTransaction(tx);
}
......@@ -186,25 +188,25 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap =
TransactionalMap<DiscreteResource, Set<Resource>> childTxMap =
tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap =
TransactionalMap<DiscreteResource, ResourceConsumer> discreteConsumerTxMap =
tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER);
TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap =
tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER);
// Extract Discrete instances from resources
Map<Resource.Discrete, List<Resource>> resourceMap = resources.stream()
Map<DiscreteResource, List<Resource>> resourceMap = resources.stream()
.filter(x -> x.parent().isPresent())
.collect(Collectors.groupingBy(x -> x.parent().get()));
// even if one of the resources is allocated to a consumer,
// all unregistrations are regarded as failure
for (Map.Entry<Resource.Discrete, List<Resource>> entry: resourceMap.entrySet()) {
for (Map.Entry<DiscreteResource, List<Resource>> entry: resourceMap.entrySet()) {
boolean allocated = entry.getValue().stream().anyMatch(x -> {
if (x instanceof Resource.Discrete) {
return discreteConsumerTxMap.get((Resource.Discrete) x) != null;
} else if (x instanceof Resource.Continuous) {
if (x instanceof DiscreteResource) {
return discreteConsumerTxMap.get((DiscreteResource) x) != null;
} else if (x instanceof ContinuousResource) {
ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(x.id());
return allocations != null && !allocations.allocations().isEmpty();
} else {
......@@ -239,31 +241,31 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalMap<Resource.Discrete, Set<Resource>> childTxMap =
TransactionalMap<DiscreteResource, Set<Resource>> childTxMap =
tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap =
TransactionalMap<DiscreteResource, ResourceConsumer> discreteConsumerTxMap =
tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER);
TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap =
tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER);
for (Resource resource: resources) {
if (resource instanceof Resource.Discrete) {
if (resource instanceof DiscreteResource) {
if (!lookup(childTxMap, resource).isPresent()) {
return abortTransaction(tx);
}
ResourceConsumer oldValue = discreteConsumerTxMap.put((Resource.Discrete) resource, consumer);
ResourceConsumer oldValue = discreteConsumerTxMap.put((DiscreteResource) resource, consumer);
if (oldValue != null) {
return abortTransaction(tx);
}
} else if (resource instanceof Resource.Continuous) {
Optional<Resource.Continuous> continuous = lookup(childTxMap, (Resource.Continuous) resource);
} else if (resource instanceof ContinuousResource) {
Optional<ContinuousResource> continuous = lookup(childTxMap, (ContinuousResource) resource);
if (!continuous.isPresent()) {
return abortTransaction(tx);
}
ContinuousResourceAllocation allocations = continuousConsumerTxMap.get(continuous.get().id());
if (!hasEnoughResource(continuous.get(), (Resource.Continuous) resource, allocations)) {
if (!hasEnoughResource(continuous.get(), (ContinuousResource) resource, allocations)) {
return abortTransaction(tx);
}
......@@ -287,7 +289,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
TransactionContext tx = service.transactionContextBuilder().build();
tx.begin();
TransactionalMap<Resource.Discrete, ResourceConsumer> discreteConsumerTxMap =
TransactionalMap<DiscreteResource, ResourceConsumer> discreteConsumerTxMap =
tx.getTransactionalMap(DISCRETE_CONSUMER_MAP, SERIALIZER);
TransactionalMap<ResourceId, ContinuousResourceAllocation> continuousConsumerTxMap =
tx.getTransactionalMap(CONTINUOUS_CONSUMER_MAP, SERIALIZER);
......@@ -298,18 +300,18 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
Resource resource = resourceIte.next();
ResourceConsumer consumer = consumerIte.next();
if (resource instanceof Resource.Discrete) {
if (resource instanceof DiscreteResource) {
// if this single release fails (because the resource is allocated to another consumer,
// the whole release fails
if (!discreteConsumerTxMap.remove((Resource.Discrete) resource, consumer)) {
if (!discreteConsumerTxMap.remove((DiscreteResource) resource, consumer)) {
return abortTransaction(tx);
}
} else if (resource instanceof Resource.Continuous) {
Resource.Continuous continuous = (Resource.Continuous) resource;
} else if (resource instanceof ContinuousResource) {
ContinuousResource continuous = (ContinuousResource) resource;
ContinuousResourceAllocation allocation = continuousConsumerTxMap.get(continuous.id());
ImmutableList<ResourceAllocation> newAllocations = allocation.allocations().stream()
.filter(x -> !(x.consumer().equals(consumer) &&
((Resource.Continuous) x.resource()).value() == continuous.value()))
((ContinuousResource) x.resource()).value() == continuous.value()))
.collect(GuavaCollectors.toImmutableList());
if (!continuousConsumerTxMap.replace(continuous.id(), allocation,
......@@ -325,7 +327,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
@Override
public boolean isAvailable(Resource resource) {
checkNotNull(resource);
checkArgument(resource instanceof Resource.Discrete || resource instanceof Resource.Continuous);
checkArgument(resource instanceof DiscreteResource || resource instanceof ContinuousResource);
// check if it's registered or not.
Versioned<Set<Resource>> v = childMap.get(resource.parent().get());
......@@ -333,15 +335,15 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
return false;
}
if (resource instanceof Resource.Discrete) {
if (resource instanceof DiscreteResource) {
// check if already consumed
return getConsumer((Resource.Discrete) resource).isEmpty();
return getConsumer((DiscreteResource) resource).isEmpty();
} else {
Resource.Continuous requested = (Resource.Continuous) resource;
Resource.Continuous registered = v.value().stream()
ContinuousResource requested = (ContinuousResource) resource;
ContinuousResource registered = v.value().stream()
.filter(c -> c.id().equals(resource.id()))
.findFirst()
.map(c -> (Resource.Continuous) c)
.map(c -> (ContinuousResource) c)
.get();
if (registered.value() < requested.value()) {
// Capacity < requested, can never satisfy
......@@ -352,7 +354,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
}
}
private boolean isAvailable(Resource.Continuous resource) {
private boolean isAvailable(ContinuousResource resource) {
Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id());
if (allocation == null) {
// no allocation (=no consumer) full registered resources available
......@@ -368,11 +370,11 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
// NOTE: getting all entries may become performance bottleneck
// TODO: revisit for better backend data structure
Stream<Resource.Discrete> discreteStream = discreteConsumers.entrySet().stream()
Stream<DiscreteResource> discreteStream = discreteConsumers.entrySet().stream()
.filter(x -> x.getValue().value().equals(consumer))
.map(Map.Entry::getKey);
Stream<Resource.Continuous> continuousStream = continuousConsumers.values().stream()
Stream<ContinuousResource> continuousStream = continuousConsumers.values().stream()
.flatMap(x -> x.value().allocations().stream()
.map(y -> Maps.immutableEntry(x.value().original(), y)))
.filter(x -> x.getValue().consumer().equals(consumer))
......@@ -384,12 +386,12 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
@Override
public Collection<Resource> getChildResources(Resource parent) {
checkNotNull(parent);
if (!(parent instanceof Resource.Discrete)) {
if (!(parent instanceof DiscreteResource)) {
// only Discrete resource can have child resource
return ImmutableList.of();
}
Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent);
Versioned<Set<Resource>> children = childMap.get((DiscreteResource) parent);
if (children == null) {
return ImmutableList.of();
}
......@@ -401,23 +403,23 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
public <T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls) {
checkNotNull(parent);
checkNotNull(cls);
checkArgument(parent instanceof Resource.Discrete);
checkArgument(parent instanceof DiscreteResource);
Versioned<Set<Resource>> children = childMap.get((Resource.Discrete) parent);
Versioned<Set<Resource>> children = childMap.get((DiscreteResource) parent);
if (children == null) {
return ImmutableList.of();
}
Stream<Resource.Discrete> discrete = children.value().stream()
Stream<DiscreteResource> discrete = children.value().stream()
.filter(x -> x.last().getClass().equals(cls))
.filter(x -> x instanceof Resource.Discrete)
.map(x -> (Resource.Discrete) x)
.filter(x -> x instanceof DiscreteResource)
.map(x -> (DiscreteResource) x)
.filter(discreteConsumers::containsKey);
Stream<Resource.Continuous> continuous = children.value().stream()
Stream<ContinuousResource> continuous = children.value().stream()
.filter(x -> x.id().equals(parent.id().child(cls)))
.filter(x -> x instanceof Resource.Continuous)
.map(x -> (Resource.Continuous) x)
.filter(x -> x instanceof ContinuousResource)
.map(x -> (ContinuousResource) x)
.filter(x -> continuousConsumers.containsKey(x.id()))
.filter(x -> continuousConsumers.get(x.id()) != null)
.filter(x -> !continuousConsumers.get(x.id()).value().allocations().isEmpty());
......@@ -438,7 +440,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
// Appends the specified ResourceAllocation to the existing values stored in the map
private boolean appendValue(TransactionalMap<ResourceId, ContinuousResourceAllocation> map,
Resource.Continuous original, ResourceAllocation value) {
ContinuousResource original, ResourceAllocation value) {
ContinuousResourceAllocation oldValue = map.putIfAbsent(original.id(),
new ContinuousResourceAllocation(original, ImmutableList.of(value)));
if (oldValue == null) {
......@@ -521,7 +523,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
*/
// Naive implementation, which traverses all elements in the list
private <T extends Resource> Optional<T> lookup(
TransactionalMap<Resource.Discrete, Set<Resource>> map, T resource) {
TransactionalMap<DiscreteResource, Set<Resource>> map, T resource) {
// if it is root, always returns itself
if (!resource.parent().isPresent()) {
return Optional.of(resource);
......@@ -549,17 +551,17 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
* @param allocation current allocation of the resource
* @return true if there is enough resource volume. Otherwise, false.
*/
private boolean hasEnoughResource(Resource.Continuous original,
Resource.Continuous request,
private boolean hasEnoughResource(ContinuousResource original,
ContinuousResource request,
ContinuousResourceAllocation allocation) {
if (allocation == null) {
return request.value() <= original.value();
}
double allocated = allocation.allocations().stream()
.filter(x -> x.resource() instanceof Resource.Continuous)
.map(x -> (Resource.Continuous) x.resource())
.mapToDouble(Resource.Continuous::value)
.filter(x -> x.resource() instanceof ContinuousResource)
.map(x -> (ContinuousResource) x.resource())
.mapToDouble(ContinuousResource::value)
.sum();
double left = original.value() - allocated;
return request.value() <= left;
......@@ -567,16 +569,16 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
// internal use only
private static final class ContinuousResourceAllocation {
private final Resource.Continuous original;
private final ContinuousResource original;
private final ImmutableList<ResourceAllocation> allocations;
private ContinuousResourceAllocation(Resource.Continuous original,
private ContinuousResourceAllocation(ContinuousResource original,
ImmutableList<ResourceAllocation> allocations) {
this.original = original;
this.allocations = allocations;
}
private Resource.Continuous original() {
private ContinuousResource original() {
return original;
}
......
......@@ -172,6 +172,10 @@ import org.onosproject.net.intent.constraint.PartialFailureConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onosproject.net.link.DefaultLinkDescription;
import org.onosproject.net.meter.MeterId;
import org.onosproject.net.newresource.ContinuousResource;
import org.onosproject.net.newresource.ContinuousResourceId;
import org.onosproject.net.newresource.DiscreteResource;
import org.onosproject.net.newresource.DiscreteResourceId;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceId;
import org.onosproject.net.newresource.Resource;
......@@ -434,11 +438,11 @@ public final class KryoNamespaces {
BandwidthResourceAllocation.class,
LambdaResourceAllocation.class,
Resource.class,
Resource.Discrete.class,
Resource.Continuous.class,
DiscreteResource.class,
ContinuousResource.class,
ResourceId.class,
ResourceId.Discrete.class,
ResourceId.Continuous.class,
DiscreteResourceId.class,
ContinuousResourceId.class,
ResourceAllocation.class,
// Constraints
LambdaConstraint.class,
......