[ONOS-6481] Fix Intent resource release issue

Lables for encapsulation won't release because Intent manager release the
resources by Intent key only.

Allocate encapsulation labels by using Intent key instead of Intent Id to
resolve this problem.

Change-Id: I8fd52245063ded725e4b2c3911f52c029591ab90
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
index d61d1eb..aafed0c 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentCompiler.java
@@ -110,7 +110,7 @@
 
         if (encapConstraint.isPresent()) {
             labels = labelAllocator.assignLabelToPorts(intent.links(),
-                                                       intent.id(),
+                                                       intent.key(),
                                                        encapConstraint.get().encapType());
         }
 
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentFlowObjectiveCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentFlowObjectiveCompiler.java
index 8ea0433..f4ba053 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentFlowObjectiveCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentFlowObjectiveCompiler.java
@@ -102,7 +102,7 @@
 
         if (encapConstraint.isPresent()) {
             labels = labelAllocator.assignLabelToPorts(intent.links(),
-                                                       intent.id(),
+                                                       intent.key(),
                                                        encapConstraint.get().encapType());
         }
 
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentObjectiveCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentObjectiveCompiler.java
index 5bd90c3..2e46669 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentObjectiveCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/LinkCollectionIntentObjectiveCompiler.java
@@ -117,7 +117,7 @@
 
         if (encapConstraint.isPresent()) {
             labels = labelAllocator.assignLabelToPorts(intent.links(),
-                                                       intent.id(),
+                                                       intent.key(),
                                                        encapConstraint.get().encapType());
         }
 
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathCompiler.java
index b236d59..78a2f39 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathCompiler.java
@@ -121,7 +121,7 @@
 
         Map<LinkKey, Identifier<?>> vlanIds = labelAllocator.assignLabelToLinks(
                 linksSet,
-                intent.id(),
+                intent.key(),
                 EncapsulationType.VLAN
         );
 
@@ -238,7 +238,7 @@
 
         Map<LinkKey, Identifier<?>> mplsLabels = labelAllocator.assignLabelToLinks(
                 linksSet,
-                intent.id(),
+                intent.key(),
                 EncapsulationType.MPLS
         );
         Iterator<Link> links = intent.path().links().iterator();
diff --git a/core/net/src/main/java/org/onosproject/net/resource/impl/LabelAllocator.java b/core/net/src/main/java/org/onosproject/net/resource/impl/LabelAllocator.java
index 22b3500..34ba91e 100644
--- a/core/net/src/main/java/org/onosproject/net/resource/impl/LabelAllocator.java
+++ b/core/net/src/main/java/org/onosproject/net/resource/impl/LabelAllocator.java
@@ -29,9 +29,9 @@
 import org.onosproject.net.EncapsulationType;
 import org.onosproject.net.Link;
 import org.onosproject.net.LinkKey;
-import org.onosproject.net.intent.IntentId;
 import org.onosproject.net.resource.Resource;
 import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceConsumer;
 import org.onosproject.net.resource.ResourceService;
 import org.onosproject.net.resource.Resources;
 
@@ -207,12 +207,13 @@
      * Allocates labels and associates them to links.
      *
      * @param links the links where labels will be allocated
-     * @param id the intent Id
+     * @param resourceConsumer the resource consumer
      * @param type the encapsulation type
      * @return the list of links and associated labels
      */
-    public Map<LinkKey, Identifier<?>> assignLabelToLinks(Set<Link> links, IntentId id, EncapsulationType type) {
-
+    public Map<LinkKey, Identifier<?>> assignLabelToLinks(Set<Link> links,
+                                                          ResourceConsumer resourceConsumer,
+                                                          EncapsulationType type) {
         Set<LinkKey> linkRequest = links.stream()
                 .map(LinkKey::linkKey)
                 .collect(Collectors.toSet());
@@ -237,9 +238,8 @@
                 ))
                 .collect(Collectors.toSet());
 
-        // FIXME resource allocated by IntentId will not be released
-        // when Intent is withdrawn. Behaviour changed by ONOS-5808
-        List<ResourceAllocation> allocations = resourceService.allocate(id, ImmutableList.copyOf(resources));
+        List<ResourceAllocation> allocations = resourceService.allocate(resourceConsumer,
+                                                                        ImmutableList.copyOf(resources));
 
         if (allocations.isEmpty()) {
             return Collections.emptyMap();
@@ -253,19 +253,23 @@
      * and destination ports of a link.
      *
      * @param links the links on which labels will be reserved
-     * @param id the intent Id
+     * @param resourceConsumer the resource consumer
      * @param type the encapsulation type
      * @return the list of ports and associated labels
      */
-    public Map<ConnectPoint, Identifier<?>> assignLabelToPorts(Set<Link> links, IntentId id, EncapsulationType type) {
-        Map<LinkKey, Identifier<?>> allocation = this.assignLabelToLinks(links, id, type);
+    public Map<ConnectPoint, Identifier<?>> assignLabelToPorts(Set<Link> links,
+                                                               ResourceConsumer resourceConsumer,
+                                                               EncapsulationType type) {
+        Map<LinkKey, Identifier<?>> allocation = this.assignLabelToLinks(links,
+                                                                         resourceConsumer,
+                                                                         type);
         if (allocation.isEmpty()) {
             return Collections.emptyMap();
         }
         Map<ConnectPoint, Identifier<?>> finalAllocation = Maps.newHashMap();
-        allocation.forEach((key, value) -> {
-            finalAllocation.putIfAbsent(key.src(), value);
-            finalAllocation.putIfAbsent(key.dst(), value);
+        allocation.forEach((link, value) -> {
+            finalAllocation.putIfAbsent(link.src(), value);
+            finalAllocation.putIfAbsent(link.dst(), value);
         });
         return ImmutableMap.copyOf(finalAllocation);
     }