Sho SHIMIZU
Committed by Gerrit Code Review

Define a class representing a set of discrete resources

Change-Id: Iaafc5ca68ae3b1330e6c8b0b2476d835a42731b6
......@@ -30,7 +30,6 @@ import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.TransactionContext;
import org.onosproject.store.service.Versioned;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -42,19 +41,19 @@ import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIAL
class ConsistentDiscreteResourceSubStore {
private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
private ConsistentMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
private ConsistentMap<DiscreteResourceId, DiscreteResources> childMap;
ConsistentDiscreteResourceSubStore(StorageService service) {
this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
.withName(MapNames.DISCRETE_CONSUMER_MAP)
.withSerializer(SERIALIZER)
.build();
this.childMap = service.<DiscreteResourceId, Set<DiscreteResource>>consistentMapBuilder()
this.childMap = service.<DiscreteResourceId, DiscreteResources>consistentMapBuilder()
.withName(MapNames.DISCRETE_CHILD_MAP)
.withSerializer(SERIALIZER)
.build();
Tools.retryable(() -> childMap.put(Resource.ROOT.id(), new LinkedHashSet<>()),
Tools.retryable(() -> childMap.put(Resource.ROOT.id(), DiscreteResources.empty()),
ConsistentMapException.class, MAX_RETRIES, RETRY_DELAY);
}
......@@ -73,13 +72,13 @@ class ConsistentDiscreteResourceSubStore {
}
Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
Versioned<Set<DiscreteResource>> children = childMap.get(parent);
Versioned<DiscreteResources> children = childMap.get(parent);
if (children == null) {
return ImmutableSet.of();
}
return children.value();
return children.value().values();
}
boolean isAvailable(DiscreteResource resource) {
......
......@@ -71,6 +71,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
static final Serializer SERIALIZER = Serializer.using(
Arrays.asList(KryoNamespaces.API),
DiscreteResources.class,
ContinuousResourceAllocation.class);
// TODO: We should provide centralized values for this
......
/*
* Copyright 2016-present 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.store.resource.impl;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Sets;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resources;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
final class DiscreteResources {
private final Set<DiscreteResource> values;
static DiscreteResources empty() {
return new DiscreteResources();
}
private DiscreteResources() {
this.values = new LinkedHashSet<>();
}
DiscreteResources(List<DiscreteResource> values) {
this.values = new LinkedHashSet<>(values);
}
private DiscreteResources(Set<DiscreteResource> values) {
this.values = values;
}
Optional<DiscreteResource> lookup(DiscreteResourceId id) {
DiscreteResource resource = Resources.discrete(id).resource();
if (values.contains(resource)) {
return Optional.of(resource);
} else {
return Optional.empty();
}
}
DiscreteResources difference(DiscreteResources other) {
return new DiscreteResources(Sets.difference(this.values, other.values));
}
boolean isEmpty() {
return values.isEmpty();
}
boolean containsAny(List<DiscreteResource> other) {
return other.stream().anyMatch(values::contains);
}
// returns a new instance, not mutate the current instance
DiscreteResources add(DiscreteResources other) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.addAll(other.values);
return new DiscreteResources(newValues);
}
// returns a new instance, not mutate the current instance
DiscreteResources remove(List<DiscreteResource> removed) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.removeAll(removed);
return new DiscreteResources(newValues);
}
Set<DiscreteResource> values() {
// breaks immutability, but intentionally returns the field
// because this class is transient
return values;
}
@Override
public int hashCode() {
return Objects.hash(values);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DiscreteResources other = (DiscreteResources) obj;
return Objects.equals(this.values, other.values);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("values", values)
.toString();
}
}
......@@ -15,27 +15,23 @@
*/
package org.onosproject.store.resource.impl;
import com.google.common.collect.Sets;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resource;
import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.Resources;
import org.onosproject.store.service.TransactionContext;
import org.onosproject.store.service.TransactionalMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
class TransactionalDiscreteResourceSubStore {
private final Logger log = LoggerFactory.getLogger(getClass());
private final TransactionalMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
private final TransactionalMap<DiscreteResourceId, DiscreteResources> childMap;
private final TransactionalMap<DiscreteResourceId, ResourceConsumerId> consumers;
TransactionalDiscreteResourceSubStore(TransactionContext tx) {
......@@ -49,17 +45,12 @@ class TransactionalDiscreteResourceSubStore {
return Optional.of(Resource.ROOT);
}
Set<DiscreteResource> values = childMap.get(id.parent().get());
DiscreteResources values = childMap.get(id.parent().get());
if (values == null) {
return Optional.empty();
}
DiscreteResource resource = Resources.discrete(id).resource();
if (values.contains(resource)) {
return Optional.of(resource);
} else {
return Optional.empty();
}
return values.lookup(id);
}
boolean register(DiscreteResourceId key, List<DiscreteResource> values) {
......@@ -68,21 +59,20 @@ class TransactionalDiscreteResourceSubStore {
return true;
}
Set<DiscreteResource> requested = new LinkedHashSet<>(values);
Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested);
DiscreteResources requested = new DiscreteResources(values);
DiscreteResources oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
return true;
}
Set<DiscreteResource> addedValues = Sets.difference(requested, oldValues);
DiscreteResources addedValues = requested.difference(oldValues);
// no new value, then no-op
if (addedValues.isEmpty()) {
// don't write to map because all values are already stored
return true;
}
Set<DiscreteResource> newValues = new LinkedHashSet<>(oldValues);
newValues.addAll(addedValues);
DiscreteResources newValues = oldValues.add(addedValues);
return childMap.replace(key, oldValues, newValues);
}
......@@ -100,20 +90,19 @@ class TransactionalDiscreteResourceSubStore {
return false;
}
Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
DiscreteResources oldValues = childMap.putIfAbsent(key, DiscreteResources.empty());
if (oldValues == null) {
log.trace("No-Op removing values. key {} did not exist", key);
return true;
}
if (values.stream().allMatch(x -> !oldValues.contains(x))) {
if (!oldValues.containsAny(values)) {
// don't write map because none of the values are stored
log.trace("No-Op removing values. key {} did not contain {}", key, values);
return true;
}
LinkedHashSet<DiscreteResource> newValues = new LinkedHashSet<>(oldValues);
newValues.removeAll(values);
DiscreteResources newValues = oldValues.remove(values);
return childMap.replace(key, oldValues, newValues);
}
......