Made ResourceManager to accept arbitrary ResourceConsumer implementation. (ONOS-4406)
Change-Id: If41564824770c2a8b78237a206c036df94141117
diff --git a/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java b/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java
index 4c04f9a..0882429 100644
--- a/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java
@@ -40,6 +40,7 @@
import org.onosproject.net.TributarySlot;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.intent.IntentId;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.Resources;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.ResourceAllocation;
@@ -145,15 +146,15 @@
resourceService.getResourceAllocations(resourceId, t).stream()
.filter(a -> isSubjectToPrint(a))
.forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
- a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())));
+ a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
}
}
private boolean isSubjectToPrint(ResourceAllocation allocation) {
if (!intentsToPrint.isEmpty()
- && allocation.consumer() instanceof IntentId
- && !intentsToPrint.contains(allocation.consumer().toString())) {
+ && allocation.consumerId().isClassOf(IntentId.class)
+ && !intentsToPrint.contains(allocation.consumerId().toString())) {
return false;
}
@@ -184,4 +185,8 @@
}
}
+ private static String asVerboseString(ResourceConsumerId consumerId) {
+ return String.format("%s:%s", consumerId.consumerClass(), consumerId.value());
+ }
+
}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/IntentId.java b/core/api/src/main/java/org/onosproject/net/intent/IntentId.java
index 0e50eb0..c86471d 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/IntentId.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/IntentId.java
@@ -18,6 +18,7 @@
import com.google.common.annotations.Beta;
import org.onlab.util.Identifier;
import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
/**
* Intent identifier suitable as an external key.
@@ -66,4 +67,8 @@
return "0x" + Long.toHexString(identifier);
}
+ @Override
+ public ResourceConsumerId consumerId() {
+ return ResourceConsumerId.of(this);
+ }
}
diff --git a/core/api/src/main/java/org/onosproject/net/resource/ResourceAllocation.java b/core/api/src/main/java/org/onosproject/net/resource/ResourceAllocation.java
index 63bee4e..3fa16af 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/ResourceAllocation.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/ResourceAllocation.java
@@ -29,7 +29,18 @@
public class ResourceAllocation {
private final Resource resource;
- private final ResourceConsumer consumer;
+ private final ResourceConsumerId consumerId;
+
+ /**
+ * Creates an instance with the specified subject, resource and consumerId.
+ *
+ * @param resource resource of the subject
+ * @param consumerId consumer ID of this resource
+ */
+ public ResourceAllocation(Resource resource, ResourceConsumerId consumerId) {
+ this.resource = checkNotNull(resource);
+ this.consumerId = checkNotNull(consumerId);
+ }
/**
* Creates an instance with the specified subject, resource and consumer.
@@ -38,14 +49,13 @@
* @param consumer consumer of this resource
*/
public ResourceAllocation(Resource resource, ResourceConsumer consumer) {
- this.resource = checkNotNull(resource);
- this.consumer = consumer;
+ this(resource, checkNotNull(consumer).consumerId());
}
// for serialization
private ResourceAllocation() {
this.resource = null;
- this.consumer = null;
+ this.consumerId = null;
}
/**
@@ -58,17 +68,17 @@
}
/**
- * Returns the consumer of this resource.
+ * Returns ID of the consumer of this resource.
*
- * @return the consumer of this resource
+ * @return ID of the consumer of this resource
*/
- public ResourceConsumer consumer() {
- return consumer;
+ public ResourceConsumerId consumerId() {
+ return consumerId;
}
@Override
public int hashCode() {
- return Objects.hash(resource, consumer);
+ return Objects.hash(resource, consumerId);
}
@Override
@@ -81,14 +91,14 @@
}
final ResourceAllocation that = (ResourceAllocation) obj;
return Objects.equals(this.resource, that.resource)
- && Objects.equals(this.consumer, that.consumer);
+ && Objects.equals(this.consumerId, that.consumerId);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("resource", resource)
- .add("consumer", consumer)
+ .add("consumerId", consumerId)
.toString();
}
}
diff --git a/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumer.java b/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumer.java
index c9d5691..f4b0e11 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumer.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumer.java
@@ -22,4 +22,10 @@
*/
@Beta
public interface ResourceConsumer {
+ /**
+ * Returns ID of this consumer.
+ *
+ * @return ID of this consumer
+ */
+ ResourceConsumerId consumerId();
}
diff --git a/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumerId.java b/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumerId.java
new file mode 100644
index 0000000..630c845
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/resource/ResourceConsumerId.java
@@ -0,0 +1,114 @@
+/*
+ * 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.resource;
+
+import com.google.common.base.Objects;
+import org.onlab.util.Identifier;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of global unique ID for ResourceConsumer object.
+ */
+public class ResourceConsumerId {
+ private final String className;
+ private final long value;
+
+ // Constructor for serializer.
+ protected ResourceConsumerId() {
+ this.className = null;
+ this.value = 0L;
+ }
+
+ /**
+ * Constructor with specifying every fields.
+ *
+ * @param value ID value unique within the given class
+ * @param cls class of ResourceConsumer implementation
+ */
+ ResourceConsumerId(long value, Class<?> cls) {
+ this.className = checkNotNull(cls.getName());
+ this.value = value;
+ }
+
+ /**
+ * Checks if the consumer is an instance of given class.
+ *
+ * @param cls class object
+ * @return result of check
+ */
+ public boolean isClassOf(Class<?> cls) {
+ return checkNotNull(cls).getName().equals(className);
+ }
+
+ /**
+ * Returns class name of the consumer.
+ *
+ * @return class name of the consumer in String
+ */
+ public String consumerClass() {
+ return className;
+ }
+
+ /**
+ * Returns ID value.
+ *
+ * @return ID value
+ */
+ public long value() {
+ return value;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ ResourceConsumerId that = (ResourceConsumerId) o;
+ return Objects.equal(className, that.className) &&
+ Objects.equal(value, that.value);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(className, value);
+ }
+
+ /**
+ * Creates ResourceConsumerId from given value and class.
+ *
+ * @param value ID value unique within the given class
+ * @param cls class of ResourceConsumer implementation
+ * @return created ResourceConsumerId object
+ */
+ public static <T extends ResourceConsumer> ResourceConsumerId of(long value, Class<T> cls) {
+ return new ResourceConsumerId(value, cls);
+ }
+
+ /**
+ * Creates ResourceConsumerId instance from Identifier object.
+ *
+ * @param id identifier object backed by Long value
+ * @return created ResourceConsumerId object
+ */
+ public static <T extends Identifier<Long> & ResourceConsumer> ResourceConsumerId of(T id) {
+ return new ResourceConsumerId(id.id(), id.getClass());
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/resource/ResourceAllocationTest.java b/core/api/src/test/java/org/onosproject/net/resource/ResourceAllocationTest.java
index 2ff8cc6..0c2adb5 100644
--- a/core/api/src/test/java/org/onosproject/net/resource/ResourceAllocationTest.java
+++ b/core/api/src/test/java/org/onosproject/net/resource/ResourceAllocationTest.java
@@ -18,6 +18,7 @@
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.onlab.packet.VlanId;
+import org.onlab.util.Identifier;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.intent.IntentId;
@@ -27,18 +28,38 @@
private static final DeviceId D1 = DeviceId.deviceId("of:001");
private static final DeviceId D2 = DeviceId.deviceId("of:002");
private static final PortNumber P1 = PortNumber.portNumber(1);
+ private static final PortNumber P2 = PortNumber.portNumber(2);
private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
- private static final IntentId IID1 = IntentId.valueOf(30);
+ private static final VlanId VLAN2 = VlanId.vlanId((short) 200);
+ private static final TestResourceConsumer RC2 = new TestResourceConsumer(2L);
+
+ // ResourceConsumerId generated by specifying ID and class name
+ private static final ResourceConsumerId RCID1 = ResourceConsumerId.of(30L, IntentId.class);
+
+ // ResourceConsumerId generated from Identifier<Long> class
+ private static final ResourceConsumerId RCID2 = ResourceConsumerId.of(RC2);
@Test
public void testEquals() {
- ResourceAllocation alloc1 = new ResourceAllocation(Resources.discrete(D1, P1, VLAN1).resource(), IID1);
- ResourceAllocation sameAsAlloc1 = new ResourceAllocation(Resources.discrete(D1, P1, VLAN1).resource(), IID1);
- ResourceAllocation alloc2 = new ResourceAllocation(Resources.discrete(D2, P1, VLAN1).resource(), IID1);
+ ResourceAllocation alloc1 = new ResourceAllocation(Resources.discrete(D1, P1, VLAN1).resource(), RCID1);
+ ResourceAllocation sameAsAlloc1 = new ResourceAllocation(Resources.discrete(D1, P1, VLAN1).resource(), RCID1);
+ ResourceAllocation alloc2 = new ResourceAllocation(Resources.discrete(D2, P2, VLAN2).resource(), RCID2);
+ ResourceAllocation sameAsAlloc2 = new ResourceAllocation(Resources.discrete(D2, P2, VLAN2).resource(), RCID2);
new EqualsTester()
.addEqualityGroup(alloc1, sameAsAlloc1)
- .addEqualityGroup(alloc2)
+ .addEqualityGroup(alloc2, sameAsAlloc2)
.testEquals();
}
+
+ private static class TestResourceConsumer extends Identifier<Long> implements ResourceConsumer {
+ public TestResourceConsumer(long idValue) {
+ super(idValue);
+ }
+
+ @Override
+ public ResourceConsumerId consumerId() {
+ return ResourceConsumerId.of(this);
+ }
+ }
}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/ResourceHelper.java b/core/net/src/main/java/org/onosproject/net/intent/impl/ResourceHelper.java
new file mode 100644
index 0000000..525d09e
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/ResourceHelper.java
@@ -0,0 +1,53 @@
+/*
+ * 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.intent.impl;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.intent.IntentId;
+import org.onosproject.net.resource.ResourceConsumerId;
+
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Helper class for ResourceService related processes.
+ */
+@Beta
+public final class ResourceHelper {
+
+ // To avoid instantiation
+ private ResourceHelper() {
+ }
+
+ /**
+ * Creates IntentId object from given consumer ID.
+ *
+ * @param consumerId ConsumerId object
+ * @return Created IntentId object. null if failed to create or given consumer is not instance of IntentId.
+ */
+ public static Optional<IntentId> getIntentId(ResourceConsumerId consumerId) {
+ checkNotNull(consumerId);
+
+ if (!consumerId.isClassOf(IntentId.class)) {
+ return Optional.empty();
+ }
+
+ return Optional.of(IntentId.valueOf(consumerId.value()));
+ }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
index baa5260..b598b73 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
@@ -61,6 +61,7 @@
import org.onosproject.net.optical.OchPort;
import org.onosproject.net.optical.OduCltPort;
import org.onosproject.net.intent.IntentSetMultimap;
+import org.onosproject.net.intent.impl.ResourceHelper;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.resource.Resource;
import org.onosproject.net.resource.ResourceService;
@@ -446,9 +447,9 @@
Optional<IntentId> intentId =
resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
.stream()
- .map(ResourceAllocation::consumer)
- .filter(x -> x instanceof IntentId)
- .map(x -> (IntentId) x)
+ .map(ResourceAllocation::consumerId)
+ .map(ResourceHelper::getIntentId)
+ .flatMap(Tools::stream)
.findAny();
if (isAvailable(intentId.orElse(null))) {
@@ -476,9 +477,9 @@
Optional<IntentId> intentId =
resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
.stream()
- .map(ResourceAllocation::consumer)
- .filter(x -> x instanceof IntentId)
- .map(x -> (IntentId) x)
+ .map(ResourceAllocation::consumerId)
+ .map(ResourceHelper::getIntentId)
+ .flatMap(Tools::stream)
.findAny();
if (isAvailable(intentId.orElse(null))) {
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentContinuousResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentContinuousResourceStore.java
index 00ad671..54751a6 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentContinuousResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentContinuousResourceStore.java
@@ -24,7 +24,7 @@
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resource;
import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.ConsistentMapException;
import org.onosproject.store.service.StorageService;
@@ -132,10 +132,10 @@
});
}
- Stream<ContinuousResource> getResources(ResourceConsumer consumer) {
+ Stream<ContinuousResource> getResources(ResourceConsumerId consumerId) {
return consumers.values().stream()
.flatMap(x -> x.value().allocations().stream())
- .filter(x -> x.consumer().equals(consumer))
+ .filter(x -> x.consumerId().equals(consumerId))
// this cast is safe because this class stores
// continuous resource allocations only
.map(x -> (ContinuousResource) x.resource());
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDiscreteResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDiscreteResourceStore.java
index ce645c0..06b458e 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDiscreteResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDiscreteResourceStore.java
@@ -22,7 +22,7 @@
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resource;
import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.Resources;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.ConsistentMapException;
@@ -41,11 +41,11 @@
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
class ConsistentDiscreteResourceStore {
- private ConsistentMap<DiscreteResourceId, ResourceConsumer> consumers;
+ private ConsistentMap<DiscreteResourceId, ResourceConsumerId> consumers;
private ConsistentMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
ConsistentDiscreteResourceStore(StorageService service) {
- this.consumers = service.<DiscreteResourceId, ResourceConsumer>consistentMapBuilder()
+ this.consumers = service.<DiscreteResourceId, ResourceConsumerId>consistentMapBuilder()
.withName(MapNames.DISCRETE_CONSUMER_MAP)
.withSerializer(SERIALIZER)
.build();
@@ -64,12 +64,12 @@
// computational complexity: O(1)
List<ResourceAllocation> getResourceAllocations(DiscreteResourceId resource) {
- Versioned<ResourceConsumer> consumer = consumers.get(resource);
- if (consumer == null) {
+ Versioned<ResourceConsumerId> consumerId = consumers.get(resource);
+ if (consumerId == null) {
return ImmutableList.of();
}
- return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumer.value()));
+ return ImmutableList.of(new ResourceAllocation(Resources.discrete(resource).resource(), consumerId.value()));
}
Set<DiscreteResource> getChildResources(DiscreteResourceId parent) {
@@ -97,9 +97,9 @@
.filter(x -> consumers.containsKey(x.id()));
}
- Stream<DiscreteResource> getResources(ResourceConsumer consumer) {
+ Stream<DiscreteResource> getResources(ResourceConsumerId consumerId) {
return consumers.entrySet().stream()
- .filter(x -> x.getValue().value().equals(consumer))
+ .filter(x -> x.getValue().value().equals(consumerId))
.map(Map.Entry::getKey)
.map(x -> Resources.discrete(x).resource());
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
index 0be8a37..a11aabc 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentResourceStore.java
@@ -29,6 +29,7 @@
import org.onosproject.net.resource.Resource;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.ResourceEvent;
import org.onosproject.net.resource.ResourceId;
import org.onosproject.net.resource.ResourceStore;
@@ -208,11 +209,11 @@
TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx);
for (Resource resource : resources) {
if (resource instanceof DiscreteResource) {
- if (!discreteTxStore.allocate(consumer, (DiscreteResource) resource)) {
+ if (!discreteTxStore.allocate(consumer.consumerId(), (DiscreteResource) resource)) {
return abortTransaction(tx);
}
} else if (resource instanceof ContinuousResource) {
- if (!continuousTxStore.allocate(consumer, (ContinuousResource) resource)) {
+ if (!continuousTxStore.allocate(consumer.consumerId(), (ContinuousResource) resource)) {
return abortTransaction(tx);
}
}
@@ -232,14 +233,14 @@
TransactionalContinuousResourceStore continuousTxStore = continuousStore.transactional(tx);
for (ResourceAllocation allocation : allocations) {
Resource resource = allocation.resource();
- ResourceConsumer consumer = allocation.consumer();
+ ResourceConsumerId consumerId = allocation.consumerId();
if (resource instanceof DiscreteResource) {
- if (!discreteTxStore.release((DiscreteResource) resource, consumer)) {
+ if (!discreteTxStore.release((DiscreteResource) resource, consumerId)) {
return abortTransaction(tx);
}
} else if (resource instanceof ContinuousResource) {
- if (!continuousTxStore.release((ContinuousResource) resource, consumer)) {
+ if (!continuousTxStore.release((ContinuousResource) resource, consumerId)) {
return abortTransaction(tx);
}
}
@@ -269,11 +270,12 @@
@Override
public Collection<Resource> getResources(ResourceConsumer consumer) {
checkNotNull(consumer);
+ ResourceConsumerId consumerId = consumer.consumerId();
// NOTE: getting all entries may become performance bottleneck
// TODO: revisit for better backend data structure
- Stream<DiscreteResource> discrete = discreteStore.getResources(consumer);
- Stream<ContinuousResource> continuous = continuousStore.getResources(consumer);
+ Stream<DiscreteResource> discrete = discreteStore.getResources(consumer.consumerId());
+ Stream<ContinuousResource> continuous = continuousStore.getResources(consumer.consumerId());
return Stream.concat(discrete, continuous).collect(Collectors.toList());
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
index 7de85f8..f926369 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
@@ -22,7 +22,7 @@
import org.onosproject.net.resource.ContinuousResourceId;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.ResourceAllocation;
-import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.store.service.TransactionContext;
import org.onosproject.store.service.TransactionalMap;
import org.slf4j.Logger;
@@ -133,7 +133,7 @@
return allocations != null && !allocations.allocations().isEmpty();
}
- boolean allocate(ResourceConsumer consumer, ContinuousResource request) {
+ boolean allocate(ResourceConsumerId consumerId, ContinuousResource request) {
// if the resource is not registered, then abort
Optional<ContinuousResource> lookedUp = lookup(request.id());
if (!lookedUp.isPresent()) {
@@ -146,7 +146,7 @@
return false;
}
- return appendValue(original, new ResourceAllocation(request, consumer));
+ return appendValue(original, new ResourceAllocation(request, consumerId));
}
// Appends the specified ResourceAllocation to the existing values stored in the map
@@ -166,16 +166,16 @@
return consumers.replace(original.id(), oldValue, newValue);
}
- boolean release(ContinuousResource resource, ResourceConsumer consumer) {
+ boolean release(ContinuousResource resource, ResourceConsumerId consumerId) {
ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
List<ResourceAllocation> nonMatched = oldAllocation.allocations().stream()
- .filter(x -> !(x.consumer().equals(consumer) &&
+ .filter(x -> !(x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
List<ResourceAllocation> matched = oldAllocation.allocations().stream()
- .filter(x -> (x.consumer().equals(consumer) &&
+ .filter(x -> (x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalDiscreteResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalDiscreteResourceStore.java
index 68194d9..83589dc 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalDiscreteResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalDiscreteResourceStore.java
@@ -19,7 +19,7 @@
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resource;
-import org.onosproject.net.resource.ResourceConsumer;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.Resources;
import org.onosproject.store.service.TransactionContext;
import org.onosproject.store.service.TransactionalMap;
@@ -36,7 +36,7 @@
class TransactionalDiscreteResourceStore {
private final Logger log = LoggerFactory.getLogger(getClass());
private final TransactionalMap<DiscreteResourceId, Set<DiscreteResource>> childMap;
- private final TransactionalMap<DiscreteResourceId, ResourceConsumer> consumers;
+ private final TransactionalMap<DiscreteResourceId, ResourceConsumerId> consumers;
TransactionalDiscreteResourceStore(TransactionContext tx) {
this.childMap = tx.getTransactionalMap(MapNames.DISCRETE_CHILD_MAP, SERIALIZER);
@@ -121,21 +121,21 @@
return consumers.get(id) != null;
}
- boolean allocate(ResourceConsumer consumer, DiscreteResource resource) {
+ boolean allocate(ResourceConsumerId consumerId, DiscreteResource resource) {
// if the resource is not registered, then abort
Optional<DiscreteResource> lookedUp = lookup(resource.id());
if (!lookedUp.isPresent()) {
return false;
}
- ResourceConsumer oldValue = consumers.put(resource.id(), consumer);
+ ResourceConsumerId oldValue = consumers.put(resource.id(), consumerId);
return oldValue == null;
}
- boolean release(DiscreteResource resource, ResourceConsumer consumer) {
+ boolean release(DiscreteResource resource, ResourceConsumerId consumerId) {
// if this single release fails (because the resource is allocated to another consumer)
// the whole release fails
- if (!consumers.remove(resource.id(), consumer)) {
+ if (!consumers.remove(resource.id(), consumerId)) {
return false;
}
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index a62d422..c1d25b7 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -199,6 +199,7 @@
import org.onosproject.net.resource.MplsCodec;
import org.onosproject.net.resource.NoOpCodec;
import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.DefaultPacketRequest;
import org.onosproject.net.packet.PacketPriority;
@@ -454,6 +455,7 @@
DiscreteResourceId.class,
ContinuousResourceId.class,
ResourceAllocation.class,
+ ResourceConsumerId.class,
// Constraints
BandwidthConstraint.class,
LinkTypeConstraint.class,
diff --git a/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java b/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
index 19abfba..4f245cb 100644
--- a/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
+++ b/core/store/serializers/src/test/java/org/onosproject/store/serializers/KryoSerializerTest.java
@@ -67,6 +67,7 @@
import org.onosproject.net.resource.DiscreteResourceSet;
import org.onosproject.net.resource.MplsCodec;
import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceConsumerId;
import org.onosproject.net.resource.Resources;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.intent.constraint.AnnotationConstraint;
@@ -404,7 +405,7 @@
public void testResourceAllocation() {
testSerializedEquals(new ResourceAllocation(
Resources.discrete(DID1, P1, VLAN1).resource(),
- IntentId.valueOf(30)));
+ ResourceConsumerId.of(30L, IntentId.class)));
}
@Test