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

Change-Id: If41564824770c2a8b78237a206c036df94141117
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))) {