Made ResourceManager to accept arbitrary ResourceConsumer implementation. (ONOS-4406)

Change-Id: If41564824770c2a8b78237a206c036df94141117
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());
+    }
+}