Resource API changes as preparation for hierarchy support (ONOS-2446)
The changes:
- Introduce ResourcePath to point a resource
- Remove Resource interface
- Make ResourceAllocation concrete class and remove DefaultResourceAllocation
- Remove DefaultResource
- Changes in interfaces due to the above changes
Change-Id: I0f3f846be67b0f7917117943aac31e3099c851ec
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DefaultResource.java b/core/api/src/main/java/org/onosproject/net/newresource/DefaultResource.java
deleted file mode 100644
index 1db5eb7..0000000
--- a/core/api/src/main/java/org/onosproject/net/newresource/DefaultResource.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright 2015 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;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * Default implementation of a class representing resource which belongs to a particular subject.
- *
- * @param <S> type of the subject
- * @param <T> type of the resource
- */
-@Beta
-public class DefaultResource<S, T> implements Resource<S, T> {
-
- private final S subject;
- private final T resource;
-
- /**
- * Creates a resource with the specified subject and resource.
- *
- * @param subject identifier which this resource belongs to
- * @param resource resource of the subject
- */
- public DefaultResource(S subject, T resource) {
- this.subject = checkNotNull(subject);
- this.resource = checkNotNull(resource);
- }
-
- // for serialization
- private DefaultResource() {
- this.subject = null;
- this.resource = null;
- }
-
- @Override
- public S subject() {
- return subject;
- }
-
- @Override
- public T resource() {
- return resource;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(subject, resource);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof DefaultResource)) {
- return false;
- }
- final DefaultResource that = (DefaultResource) obj;
- return Objects.equals(this.subject, that.subject)
- && Objects.equals(this.resource, that.resource);
- }
-}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DefaultResourceAllocation.java b/core/api/src/main/java/org/onosproject/net/newresource/DefaultResourceAllocation.java
deleted file mode 100644
index 06b6dda..0000000
--- a/core/api/src/main/java/org/onosproject/net/newresource/DefaultResourceAllocation.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright 2015 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;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * Default implementation of a class representing allocation of resource which belongs to a particular subject.
- *
- * @param <S> type of the subject
- * @param <T> type of the resource
- */
-@Beta
-public class DefaultResourceAllocation<S, T> implements ResourceAllocation<S, T> {
-
- private final S subject;
- private final T resource;
- private final ResourceConsumer consumer;
-
- /**
- * Creates an instance with the specified subject, resource and consumer.
- *
- * @param subject identifier which this resource belongs to
- * @param resource resource of the subject
- * @param consumer consumer ot this resource
- */
- public DefaultResourceAllocation(S subject, T resource, ResourceConsumer consumer) {
- this.subject = checkNotNull(subject);
- this.resource = checkNotNull(resource);
- this.consumer = consumer;
- }
-
- // for serialization
- private DefaultResourceAllocation() {
- this.subject = null;
- this.resource = null;
- this.consumer = null;
- }
-
- @Override
- public S subject() {
- return subject;
- }
-
- @Override
- public T resource() {
- return resource;
- }
-
- @Override
- public ResourceConsumer consumer() {
- return consumer;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(subject, resource, consumer);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof DefaultResourceAllocation)) {
- return false;
- }
- final DefaultResourceAllocation that = (DefaultResourceAllocation) obj;
- return Objects.equals(this.subject, that.subject)
- && Objects.equals(this.resource, that.resource)
- && Objects.equals(this.consumer, that.consumer);
- }
-}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
deleted file mode 100644
index 0bc72ed..0000000
--- a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright 2015 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;
-
-/**
- * Represents resource which belongs to a particular subject.
- *
- * @param <S> type of the subject
- * @param <T> type of the resource
- */
-@Beta
-public interface Resource<S, T> {
- /**
- * Returns the subject of the resource.
- * The value is the identifier which this resource belongs to.
- *
- * @return the subject of the resource
- */
- S subject();
-
- /**
- * Returns the resource of the subject.
- *
- * @return the resource of the subject
- */
- T resource();
-}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
index f2d9707..e698026 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAllocation.java
@@ -16,34 +16,79 @@
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+import static com.google.common.base.Preconditions.checkNotNull;
/**
- * Represents allocation of resource which belongs to a particular subject.
- *
- * @param <S> type of the subject
- * @param <T> type of the resource
+ * Represents allocation of resource which is identified by the specifier.
*/
@Beta
-public interface ResourceAllocation<S, T> {
- /**
- * Returns the subject of the resource.
- * The value is the identifier which this resource belongs to.
- *
- * @return the subject of the resource
- */
- S subject();
+public class ResourceAllocation {
+
+ private final ResourcePath resource;
+ private final ResourceConsumer consumer;
/**
- * Returns the resource which belongs to the subject.
+ * Creates an instance with the specified subject, resource and consumer.
*
- * @return the resource which belongs to the subject
+ * @param resource resource of the subject
+ * @param consumer consumer ot this resource
*/
- T resource();
+ public ResourceAllocation(ResourcePath resource, ResourceConsumer consumer) {
+ this.resource = checkNotNull(resource);
+ this.consumer = consumer;
+ }
+
+ // for serialization
+ private ResourceAllocation() {
+ this.resource = null;
+ this.consumer = null;
+ }
+
+ /**
+ * Returns the specifier of the resource this allocation uses.
+ *
+ * @return the specifier of the resource this allocation uses
+ */
+ public ResourcePath resource() {
+ return resource;
+ }
/**
* Returns the consumer of this resource.
*
* @return the consumer of this resource
*/
- ResourceConsumer consumer();
+ public ResourceConsumer consumer() {
+ return consumer;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(resource, consumer);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof ResourceAllocation)) {
+ return false;
+ }
+ final ResourceAllocation that = (ResourceAllocation) obj;
+ return Objects.equals(this.resource, that.resource)
+ && Objects.equals(this.consumer, that.consumer);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("resource", resource)
+ .add("consumer", consumer)
+ .toString();
+ }
}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourcePath.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourcePath.java
new file mode 100644
index 0000000..6a7ab94
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourcePath.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2015 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.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An object that is used to locate a resource in a network.
+ * A ResourcePath represents a path that is hierarchical and composed of a sequence
+ * of elementary resources that are not globally identifiable. A ResourcePath can be a globally
+ * unique resource identifier.
+ *
+ * Users of this class must keep the semantics of resources regarding the hierarchical structure.
+ * For example, resource path, Link:1/VLAN ID:100, is valid, but resource path, VLAN ID:100/Link:1
+ * is not valid because a link is not a sub-component of a VLAN ID.
+ */
+@Beta
+public final class ResourcePath {
+
+ private final List<Object> resources;
+
+ /**
+ * Creates an resource path from the specified components.
+ *
+ * @param components components of the path. The order represents hierarchical structure of the resource.
+ */
+ public ResourcePath(Object... components) {
+ this(Arrays.asList(components));
+ }
+
+ /**
+ * Creates an resource path from the specified components.
+ *
+ * @param components components of the path. The order represents hierarchical structure of the resource.
+ */
+ public ResourcePath(List<Object> components) {
+ checkNotNull(components);
+ checkArgument(components.size() > 0);
+
+ this.resources = ImmutableList.copyOf(components);
+ }
+
+ // for serialization
+ private ResourcePath() {
+ this.resources = null;
+ }
+
+ /**
+ * Returns the components of this resource path.
+ *
+ * @return the components of this resource path
+ */
+ public List<Object> components() {
+ return resources;
+ }
+
+ /**
+ * Returns the parent resource path of this instance.
+ * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1.
+ *
+ * @return the parent resource path of this instance.
+ * If there is no parent, empty instance will be returned.
+ */
+ public Optional<ResourcePath> parent() {
+ if (resources.size() >= 2) {
+ return Optional.of(new ResourcePath(resources.subList(0, resources.size() - 1)));
+ }
+
+ return Optional.empty();
+ }
+
+ /**
+ * Returns the last component of this instance.
+ *
+ * @return the last component of this instance.
+ * The return value is equal to the last object of {@code components()}.
+ */
+ public Object lastComponent() {
+ int last = resources.size() - 1;
+ return resources.get(last);
+ }
+
+ @Override
+ public int hashCode() {
+ return resources.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof ResourcePath)) {
+ return false;
+ }
+ final ResourcePath that = (ResourcePath) obj;
+ return Objects.equals(this.resources, that.resources);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("resources", resources)
+ .toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
index 3322362..b9b2b52 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceService.java
@@ -31,11 +31,9 @@
*
* @param consumer resource user which the resource is allocated to
* @param resource resource to be allocated
- * @param <S> type of the subject which this resource belongs to
- * @param <T> type of the resource
* @return allocation information enclosed by Optional. If the allocation fails, the return value is empty
*/
- <S, T> Optional<ResourceAllocation<S, T>> allocate(ResourceConsumer consumer, Resource<S, T> resource);
+ Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource);
/**
* Transactionally allocates the specified resources to the specified user.
@@ -45,7 +43,7 @@
* @param resources resources to be allocated
* @return non-empty list of allocation information if succeeded, otherwise empty list
*/
- List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, List<? extends Resource<?, ?>> resources);
+ List<ResourceAllocation> allocate(ResourceConsumer consumer, List<ResourcePath> resources);
/**
* Transactionally allocates the specified resources to the specified user.
@@ -55,17 +53,15 @@
* @param resources resources to be allocated
* @return non-empty list of allocation information if succeeded, otherwise empty list
*/
- List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, Resource<?, ?>... resources);
+ List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources);
/**
* Releases the specified resource allocation.
*
* @param allocation resource allocation to be released
- * @param <S> type of the subject which this resource belongs to
- * @param <T> type of the device resource
* @return true if succeeded, otherwise false
*/
- <S, T> boolean release(ResourceAllocation<S, T> allocation);
+ boolean release(ResourceAllocation allocation);
/**
* Transactionally releases the specified resource allocations.
@@ -74,7 +70,7 @@
* @param allocations resource allocations to be released
* @return true if succeeded, otherwise false
*/
- boolean release(List<? extends ResourceAllocation<?, ?>> allocations);
+ boolean release(List<ResourceAllocation> allocations);
/**
* Transactionally releases the specified resource allocations.
@@ -83,7 +79,7 @@
* @param allocations resource allocations to be released
* @return true if succeeded, otherwise false
*/
- boolean release(ResourceAllocation<?, ?>... allocations);
+ boolean release(ResourceAllocation... allocations);
/**
* Transactionally releases the resources allocated to the specified consumer.
@@ -95,16 +91,14 @@
boolean release(ResourceConsumer consumer);
/**
- * Returns allocated resources in the specified subject regarding the specified resource type.
+ * Returns allocated resources being as children of the specified parent and being the specified resource type.
*
- * @param subject subject where resource allocations are obtained
* @param cls class to specify a type of resource
- * @param <S> type of the subject
* @param <T> type of the resource
* @return non-empty collection of resource allocations if resources are allocated with the subject and type,
* empty collection if no resource is allocated with the subject and type
*/
- <S, T> Collection<ResourceAllocation<S, T>> getResourceAllocations(S subject, Class<T> cls);
+ <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls);
/**
* Returns resources allocated to the specified consumer.
@@ -112,17 +106,15 @@
* @param consumer consumer whose allocated resources are to be returned
* @return resources allocated to the consumer
*/
- Collection<ResourceAllocation<?, ?>> getResourceAllocations(ResourceConsumer consumer);
+ Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
/**
- * Returns the availability of the specified device resource.
+ * Returns the availability of the specified resource.
*
* @param resource resource to check the availability
- * @param <S> type of the subject
- * @param <T> type of the resource
* @return true if available, otherwise false
*/
- <S, T> boolean isAvailable(Resource<S, T> resource);
+ boolean isAvailable(ResourcePath resource);
// TODO: listener and event mechanism need to be considered
}
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
index f2f546b..7280b60 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
@@ -21,7 +21,7 @@
* @param consumer resource consumer which the resources are allocated to
* @return true if the allocation succeeds, false otherwise.
*/
- boolean allocate(List<? extends Resource<?, ?>> resources, ResourceConsumer consumer);
+ boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer);
/**
* Releases the specified resources allocated to the specified corresponding consumers
@@ -35,17 +35,15 @@
* @param consumers resource consumers to whom the resource allocated to
* @return true if succeeds, otherwise false
*/
- boolean release(List<? extends Resource<?, ?>> resources, List<ResourceConsumer> consumers);
+ boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers);
/**
* Returns the resource consumer to whom the specified resource is allocated.
*
* @param resource resource whose allocated consumer to be returned
- * @param <S> type of subject of the resource
- * @param <T> type of resource
* @return resource consumer who are allocated the resource
*/
- <S, T> Optional<ResourceConsumer> getConsumer(Resource<S, T> resource);
+ Optional<ResourceConsumer> getConsumer(ResourcePath resource);
/**
* Returns a collection of the resources allocated to the specified consumer.
@@ -53,18 +51,17 @@
* @param consumer resource consumer whose allocated resource are searched for
* @return a collection of the resources allocated to the specified consumer
*/
- Collection<Resource<?, ?>> getResources(ResourceConsumer consumer);
+ Collection<ResourcePath> getResources(ResourceConsumer consumer);
/**
- * Returns a collection of the resources which belongs to the specified subject and
+ * Returns a collection of the resources which are children of the specified parent and
* whose type is the specified class.
*
- * @param subject subject of the resources to be returned
- * @param cls class instance of the resources
- * @param <S> type of the subject
+ * @param parent parent of the resources to be returned
+ * @param cls class instance of the children
* @param <T> type of the resource
* @return a collection of the resources which belongs to the specified subject and
* whose type is the specified class.
*/
- <S, T> Collection<Resource<S, T>> getAllocatedResources(S subject, Class<T> cls);
+ <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls);
}
diff --git a/core/api/src/test/java/org/onosproject/net/newresource/DefaultResourceTest.java b/core/api/src/test/java/org/onosproject/net/newresource/DefaultResourceTest.java
deleted file mode 100644
index b4e3731..0000000
--- a/core/api/src/test/java/org/onosproject/net/newresource/DefaultResourceTest.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright 2015 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.testing.EqualsTester;
-import org.junit.Test;
-import org.onlab.packet.VlanId;
-import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.LinkKey;
-import org.onosproject.net.PortNumber;
-
-public class DefaultResourceTest {
-
- 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 ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
- private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
- private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
-
- @Test
- public void testEquals() {
- DefaultResource<LinkKey, VlanId> resource1 =
- new DefaultResource<>(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
- DefaultResource<LinkKey, VlanId> sameAsResource1 =
- new DefaultResource<>(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
- DefaultResource<LinkKey, VlanId> resource2 =
- new DefaultResource<>(LinkKey.linkKey(CP2_1, CP1_1), VLAN1);
-
- new EqualsTester()
- .addEqualityGroup(resource1, sameAsResource1)
- .addEqualityGroup(resource2)
- .testEquals();
- }
-}
diff --git a/core/api/src/test/java/org/onosproject/net/newresource/ResourceAllocationTest.java b/core/api/src/test/java/org/onosproject/net/newresource/ResourceAllocationTest.java
index 2872046..a84927a 100644
--- a/core/api/src/test/java/org/onosproject/net/newresource/ResourceAllocationTest.java
+++ b/core/api/src/test/java/org/onosproject/net/newresource/ResourceAllocationTest.java
@@ -33,15 +33,14 @@
private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
private static final IntentId IID1 = IntentId.valueOf(30);
+ private static final LinkKey LK1 = LinkKey.linkKey(CP1_1, CP2_1);
+ private static final LinkKey LK2 = LinkKey.linkKey(CP2_1, CP1_1);
@Test
public void testEquals() {
- DefaultResourceAllocation<LinkKey, VlanId> alloc1 =
- new DefaultResourceAllocation<>(LinkKey.linkKey(CP1_1, CP2_1), VLAN1, IID1);
- DefaultResourceAllocation<LinkKey, VlanId> sameAsAlloc1 =
- new DefaultResourceAllocation<>(LinkKey.linkKey(CP1_1, CP2_1), VLAN1, IID1);
- DefaultResourceAllocation<LinkKey, VlanId> alloc2 =
- new DefaultResourceAllocation<>(LinkKey.linkKey(CP2_1, CP1_1), VLAN1, IID1);
+ ResourceAllocation alloc1 = new ResourceAllocation(new ResourcePath(LK1, VLAN1), IID1);
+ ResourceAllocation sameAsAlloc1 = new ResourceAllocation(new ResourcePath(LK1, VLAN1), IID1);
+ ResourceAllocation alloc2 = new ResourceAllocation(new ResourcePath(LK2, VLAN1), IID1);
new EqualsTester()
.addEqualityGroup(alloc1, sameAsAlloc1)
diff --git a/core/api/src/test/java/org/onosproject/net/newresource/ResourcePathTest.java b/core/api/src/test/java/org/onosproject/net/newresource/ResourcePathTest.java
new file mode 100644
index 0000000..3034048
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/newresource/ResourcePathTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2015 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.testing.EqualsTester;
+import org.junit.Test;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.PortNumber;
+
+import java.util.Optional;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class ResourcePathTest {
+
+ 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 ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
+ private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
+ private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
+
+ @Test
+ public void testEquals() {
+ ResourcePath resource1 = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
+ ResourcePath sameAsResource1 = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
+ ResourcePath resource2 = new ResourcePath(LinkKey.linkKey(CP2_1, CP1_1), VLAN1);
+
+ new EqualsTester()
+ .addEqualityGroup(resource1, sameAsResource1)
+ .addEqualityGroup(resource2)
+ .testEquals();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testCreateWithZeroComponent() {
+ ResourcePath path = new ResourcePath();
+ }
+
+ @Test
+ public void testThereIsParent() {
+ ResourcePath path = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
+ ResourcePath parent = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1));
+
+ assertThat(path.parent(), is(Optional.of(parent)));
+ }
+
+ @Test
+ public void testNoParent() {
+ ResourcePath path = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1));
+
+ assertThat(path.parent(), is(Optional.empty()));
+ }
+
+ @Test
+ public void testBase() {
+ LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
+ ResourcePath path = new ResourcePath(linkKey);
+
+ LinkKey child = (LinkKey) path.lastComponent();
+ assertThat(child, is(linkKey));
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
index 785613c..7ff6514 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
@@ -21,13 +21,11 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
-import org.onosproject.net.newresource.DefaultResource;
-import org.onosproject.net.newresource.DefaultResourceAllocation;
-import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceAdminService;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceConsumer;
import org.onosproject.net.newresource.ResourceService;
+import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.newresource.ResourceStore;
import java.util.ArrayList;
@@ -55,31 +53,29 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ResourceStore store;
- @SuppressWarnings("unchecked")
@Override
- public <S, T> Optional<ResourceAllocation<S, T>> allocate(ResourceConsumer consumer, Resource<S, T> resource) {
+ public Optional<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath resource) {
checkNotNull(consumer);
checkNotNull(resource);
- List<ResourceAllocation<?, ?>> allocations = allocate(consumer, ImmutableList.of(resource));
+ List<ResourceAllocation> allocations = allocate(consumer, ImmutableList.of(resource));
if (allocations.isEmpty()) {
return Optional.empty();
}
assert allocations.size() == 1;
- ResourceAllocation<?, ?> allocation = allocations.get(0);
+ ResourceAllocation allocation = allocations.get(0);
- assert allocation.subject().getClass() == resource.subject().getClass();
- assert allocation.resource().getClass() == resource.resource().getClass();
+ assert allocation.resource().equals(resource);
// cast is ensured by the assertions above
- return Optional.of((ResourceAllocation<S, T>) allocation);
+ return Optional.of(allocation);
}
@Override
- public List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer,
- List<? extends Resource<?, ?>> resources) {
+ public List<ResourceAllocation> allocate(ResourceConsumer consumer,
+ List<ResourcePath> resources) {
checkNotNull(consumer);
checkNotNull(resources);
@@ -96,12 +92,12 @@
}
return resources.stream()
- .map(x -> new DefaultResourceAllocation<>(x.subject(), x.resource(), consumer))
+ .map(x -> new ResourceAllocation(x, consumer))
.collect(Collectors.toList());
}
@Override
- public List<ResourceAllocation<?, ?>> allocate(ResourceConsumer consumer, Resource<?, ?>... resources) {
+ public List<ResourceAllocation> allocate(ResourceConsumer consumer, ResourcePath... resources) {
checkNotNull(consumer);
checkNotNull(resources);
@@ -109,18 +105,18 @@
}
@Override
- public <S, T> boolean release(ResourceAllocation<S, T> allocation) {
+ public boolean release(ResourceAllocation allocation) {
checkNotNull(allocation);
return release(ImmutableList.of(allocation));
}
@Override
- public boolean release(List<? extends ResourceAllocation<?, ?>> allocations) {
+ public boolean release(List<ResourceAllocation> allocations) {
checkNotNull(allocations);
- List<DefaultResource<?, ?>> resources = allocations.stream()
- .map(x -> new DefaultResource<>(x.subject(), x.resource()))
+ List<ResourcePath> resources = allocations.stream()
+ .map(ResourceAllocation::resource)
.collect(Collectors.toList());
List<ResourceConsumer> consumers = allocations.stream()
.map(ResourceAllocation::consumer)
@@ -130,7 +126,7 @@
}
@Override
- public boolean release(ResourceAllocation<?, ?>... allocations) {
+ public boolean release(ResourceAllocation... allocations) {
checkNotNull(allocations);
return release(ImmutableList.copyOf(allocations));
@@ -140,23 +136,22 @@
public boolean release(ResourceConsumer consumer) {
checkNotNull(consumer);
- Collection<ResourceAllocation<?, ?>> allocations = getResourceAllocations(consumer);
+ Collection<ResourceAllocation> allocations = getResourceAllocations(consumer);
return release(ImmutableList.copyOf(allocations));
}
@Override
- public <S, T> Collection<ResourceAllocation<S, T>> getResourceAllocations(S subject, Class<T> cls) {
- checkNotNull(subject);
+ public <T> Collection<ResourceAllocation> getResourceAllocations(ResourcePath parent, Class<T> cls) {
+ checkNotNull(parent);
checkNotNull(cls);
- Collection<Resource<S, T>> resources = store.getAllocatedResources(subject, cls);
- List<ResourceAllocation<S, T>> allocations = new ArrayList<>(resources.size());
- for (Resource<S, T> resource: resources) {
+ 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 DefaultResourceAllocation<>(resource.subject(), resource.resource(), consumer.get()));
+ allocations.add(new ResourceAllocation(resource, consumer.get()));
}
}
@@ -164,17 +159,17 @@
}
@Override
- public Collection<ResourceAllocation<?, ?>> getResourceAllocations(ResourceConsumer consumer) {
+ public Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer) {
checkNotNull(consumer);
- Collection<Resource<?, ?>> resources = store.getResources(consumer);
+ Collection<ResourcePath> resources = store.getResources(consumer);
return resources.stream()
- .map(x -> new DefaultResourceAllocation<>(x.subject(), x.resource(), consumer))
+ .map(x -> new ResourceAllocation(x, consumer))
.collect(Collectors.toList());
}
@Override
- public <S, T> boolean isAvailable(Resource<S, T> resource) {
+ public boolean isAvailable(ResourcePath resource) {
checkNotNull(resource);
Optional<ResourceConsumer> consumer = store.getConsumer(resource);
@@ -204,16 +199,16 @@
* E.g. VLAN ID against a link must be within 12 bit address space.
*
* @param resource resource to be checked if it is within the resource range
- * @param <S> type of the subject
- * @param <T> type of the resource
* @return true if the resource within the range, false otherwise
*/
- <S, T> boolean isValid(Resource<S, T> resource) {
- Predicate<T> predicate = lookupPredicate(resource.resource());
+ boolean isValid(ResourcePath resource) {
+ List<Object> flatten = resource.components();
+ Object bottom = flatten.get(flatten.size() - 1);
+ Predicate<Object> predicate = lookupPredicate(bottom);
if (predicate == null) {
return true;
}
- return predicate.test(resource.resource());
+ return predicate.test(bottom);
}
}
diff --git a/core/net/src/test/java/org/onosproject/net/newresource/impl/ResourceManagerTest.java b/core/net/src/test/java/org/onosproject/net/newresource/impl/ResourceManagerTest.java
index f3b61fc..ae6d103 100644
--- a/core/net/src/test/java/org/onosproject/net/newresource/impl/ResourceManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/newresource/impl/ResourceManagerTest.java
@@ -22,12 +22,12 @@
import org.onosproject.net.DeviceId;
import org.onosproject.net.LinkKey;
import org.onosproject.net.PortNumber;
-import org.onosproject.net.newresource.DefaultResource;
+import org.onosproject.net.newresource.ResourcePath;
import java.util.function.Predicate;
import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertThat;
/**
* Unit tests for ResourceManager.
@@ -60,19 +60,19 @@
LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
is(false));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) 100))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
is(false));
}
@@ -83,19 +83,19 @@
public void testWhenBoundaryNotSet() {
LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) 100))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) 100))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
is(true));
- assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
+ assertThat(manager.isValid(new ResourcePath(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
is(true));
}
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
index a6921e4..9c88c54 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
@@ -21,8 +21,8 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
-import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceConsumer;
+import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.newresource.ResourceStore;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.ConsistentMap;
@@ -60,18 +60,18 @@
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected StorageService service;
- private ConsistentMap<Resource<?, ?>, ResourceConsumer> consumers;
+ private ConsistentMap<ResourcePath, ResourceConsumer> consumers;
@Activate
public void activate() {
- consumers = service.<Resource<?, ?>, ResourceConsumer>consistentMapBuilder()
+ consumers = service.<ResourcePath, ResourceConsumer>consistentMapBuilder()
.withName(MAP_NAME)
.withSerializer(SERIALIZER)
.build();
}
@Override
- public <S, T> Optional<ResourceConsumer> getConsumer(Resource<S, T> resource) {
+ public Optional<ResourceConsumer> getConsumer(ResourcePath resource) {
checkNotNull(resource);
Versioned<ResourceConsumer> consumer = consumers.get(resource);
@@ -83,7 +83,7 @@
}
@Override
- public boolean allocate(List<? extends Resource<?, ?>> resources, ResourceConsumer consumer) {
+ public boolean allocate(List<ResourcePath> resources, ResourceConsumer consumer) {
checkNotNull(resources);
checkNotNull(consumer);
@@ -91,8 +91,8 @@
tx.begin();
try {
- TransactionalMap<Resource<?, ?>, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER);
- for (Resource<?, ?> resource: resources) {
+ TransactionalMap<ResourcePath, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER);
+ for (ResourcePath resource: resources) {
ResourceConsumer existing = txMap.putIfAbsent(resource, consumer);
// if the resource is already allocated to another consumer, the whole allocation fails
if (existing != null) {
@@ -108,7 +108,7 @@
}
@Override
- public boolean release(List<? extends Resource<?, ?>> resources, List<ResourceConsumer> consumers) {
+ public boolean release(List<ResourcePath> resources, List<ResourceConsumer> consumers) {
checkNotNull(resources);
checkNotNull(consumers);
checkArgument(resources.size() == consumers.size());
@@ -117,12 +117,12 @@
tx.begin();
try {
- TransactionalMap<Resource<?, ?>, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER);
- Iterator<? extends Resource<?, ?>> resourceIte = resources.iterator();
+ TransactionalMap<ResourcePath, ResourceConsumer> txMap = tx.getTransactionalMap(MAP_NAME, SERIALIZER);
+ Iterator<ResourcePath> resourceIte = resources.iterator();
Iterator<ResourceConsumer> consumerIte = consumers.iterator();
while (resourceIte.hasNext() && consumerIte.hasNext()) {
- Resource<?, ?> resource = resourceIte.next();
+ ResourcePath resource = resourceIte.next();
ResourceConsumer consumer = consumerIte.next();
// if this single release fails (because the resource is allocated to another consumer,
@@ -140,7 +140,7 @@
}
@Override
- public Collection<Resource<?, ?>> getResources(ResourceConsumer consumer) {
+ public Collection<ResourcePath> getResources(ResourceConsumer consumer) {
checkNotNull(consumer);
// NOTE: getting all entries may become performance bottleneck
@@ -151,18 +151,17 @@
.collect(Collectors.toList());
}
- @SuppressWarnings("unchecked")
@Override
- public <S, T> Collection<Resource<S, T>> getAllocatedResources(S subject, Class<T> cls) {
- checkNotNull(subject);
+ public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) {
+ checkNotNull(parent);
checkNotNull(cls);
// NOTE: getting all entries may become performance bottleneck
// TODO: revisit for better backend data structure
return consumers.entrySet().stream()
- .filter(x -> x.getKey().subject().equals(subject) && x.getKey().resource().getClass() == cls)
- // cast is ensured by the above filter method
- .map(x -> (Resource<S, T>) x.getKey())
+ .filter(x -> x.getKey().parent().isPresent() && x.getKey().parent().get().equals(parent))
+ .filter(x -> x.getKey().lastComponent().getClass() == cls)
+ .map(Map.Entry::getKey)
.collect(Collectors.toList());
}
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 fffc86e..212d4a0 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
@@ -160,8 +160,8 @@
import org.onosproject.net.intent.constraint.PartialFailureConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onosproject.net.link.DefaultLinkDescription;
-import org.onosproject.net.newresource.DefaultResource;
-import org.onosproject.net.newresource.DefaultResourceAllocation;
+import org.onosproject.net.newresource.ResourceAllocation;
+import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.DefaultPacketRequest;
import org.onosproject.net.packet.PacketPriority;
@@ -402,8 +402,8 @@
DefaultLinkResourceAllocations.class,
BandwidthResourceAllocation.class,
LambdaResourceAllocation.class,
- DefaultResource.class,
- DefaultResourceAllocation.class,
+ ResourcePath.class,
+ ResourceAllocation.class,
// Constraints
LambdaConstraint.class,
BandwidthConstraint.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 49b75f0..97ccb83 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
@@ -60,8 +60,7 @@
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchEntry;
import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.newresource.DefaultResource;
-import org.onosproject.net.newresource.DefaultResourceAllocation;
+import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.BandwidthResourceAllocation;
@@ -373,15 +372,14 @@
}
@Test
- public void testDefaultResouce() {
- testSerializedEquals(new DefaultResource<>(LinkKey.linkKey(CP1, CP2), VLAN1));
+ public void testResourcePath() {
+ testSerializedEquals(new ResourcePath(LinkKey.linkKey(CP1, CP2), VLAN1));
}
@Test
- public void testDefaultResourceAllocation() {
- testSerializedEquals(new DefaultResourceAllocation<>(
- LinkKey.linkKey(CP1, CP2),
- VLAN1,
+ public void testResourceAllocation() {
+ testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation(
+ new ResourcePath(LinkKey.linkKey(CP1, CP2), VLAN1),
IntentId.valueOf(30)));
}