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);
 }