ONOS-2446: Implement API to declare resource hierarchy
Remove API to define resource boundary
(ResourceAdminService.defineResourceBoundary) to integrate with API for
resource hierarchy
Change-Id: Iffa28dec16320122fe41f4f455000596fa266acb
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAdminService.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAdminService.java
index 5fcb132..1a13c32 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceAdminService.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceAdminService.java
@@ -17,7 +17,8 @@
import com.google.common.annotations.Beta;
-import java.util.function.Predicate;
+import java.util.Arrays;
+import java.util.List;
/**
* Service for administering resource service behavior.
@@ -25,13 +26,26 @@
@Beta
public interface ResourceAdminService {
/**
- * Define a boundary of the resource specified by the class.
- * The specified predicate is expected to return true if the supplied value is
- * in the resource boundary and return false if it is out of the boundary.
+ * Register resources as the children of the parent resource path.
*
- * @param cls class of the resource type
- * @param predicate predicate returning true if the value is in the boundary
- * @param <T> type of the resource
+ * @param parent parent resource path under which the resource are registered
+ * @param children resources to be registered as the children of the parent
+ * @param <T> type of resources
+ * @return true if registration is successfully done, false otherwise. Registration
+ * succeeds when each resource is not registered or unallocated.
*/
- <T> void defineResourceBoundary(Class<T> cls, Predicate<T> predicate);
+ default <T> boolean registerResources(ResourcePath parent, T... children) {
+ return registerResources(parent, Arrays.asList(children));
+ }
+
+ /**
+ * Register resources as the children of the parent resource path.
+ *
+ * @param parent parent resource path under which the resource are registered
+ * @param children resources to be registered as the children of the parent
+ * @param <T> type of resources
+ * @return true if registration is successfully done, false otherwise. Registration
+ * succeeds when each resource is not registered or unallocated.
+ */
+ <T> boolean registerResources(ResourcePath parent, List<T> children);
}
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
index 6a7ab94..7c78a9a 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourcePath.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourcePath.java
@@ -24,7 +24,6 @@
import java.util.Objects;
import java.util.Optional;
-import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
@@ -42,6 +41,16 @@
private final List<Object> resources;
+ public static final ResourcePath ROOT = new ResourcePath(ImmutableList.of());
+
+ public static ResourcePath child(ResourcePath parent, Object child) {
+ ImmutableList<Object> components = ImmutableList.builder()
+ .addAll(parent.components())
+ .add(child)
+ .build();
+ return new ResourcePath(components);
+ }
+
/**
* Creates an resource path from the specified components.
*
@@ -58,7 +67,6 @@
*/
public ResourcePath(List<Object> components) {
checkNotNull(components);
- checkArgument(components.size() > 0);
this.resources = ImmutableList.copyOf(components);
}
@@ -85,7 +93,7 @@
* If there is no parent, empty instance will be returned.
*/
public Optional<ResourcePath> parent() {
- if (resources.size() >= 2) {
+ if (resources.size() > 0) {
return Optional.of(new ResourcePath(resources.subList(0, resources.size() - 1)));
}
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 7280b60..b711f39 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
@@ -11,6 +11,19 @@
*/
@Beta
public interface ResourceStore {
+
+ /**
+ * Registers the resources as children of the parent resource in transactional way.
+ * Resource registration is must be done before resource allocation. The state after completion
+ * of this method is all the resources are registered, or no resource is registered.
+ * The whole registration fails when any one of the resource can't be registered.
+ *
+ * @param parent resource which is the parent of the resource to be registered
+ * @param children resources to be registered
+ * @return true if the registration succeeds, false otherwise
+ */
+ boolean register(ResourcePath parent, List<ResourcePath> children);
+
/**
* Allocates the specified resources to the specified consumer in transactional way.
* The state after completion of this method is all the resources are allocated to the consumer,