Fixed a bug that optical intent compilers never release resources reserved during compilation.
Removed tempoary code from optical app (ONOS-2129).

Change-Id: Iee0343964f886196922469eaabe4a72c3f404888
diff --git a/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java b/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
index 9d72a6c..f373e4e 100644
--- a/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
+++ b/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
@@ -15,7 +15,6 @@
  */
 package org.onosproject.optical;
 
-import com.google.common.collect.ImmutableList;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -47,16 +46,12 @@
 import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.PointToPointIntent;
-import org.onosproject.net.newresource.ResourceAllocation;
-import org.onosproject.net.newresource.ResourceService;
-import org.onosproject.net.resource.device.IntentSetMultimap;
 import org.onosproject.net.topology.LinkWeight;
 import org.onosproject.net.topology.PathService;
 import org.onosproject.net.topology.TopologyEdge;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -99,12 +94,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DeviceService deviceService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected IntentSetMultimap intentSetMultimap;
-
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected ResourceService resourceService;
-
     private ApplicationId appId;
 
     private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner();
@@ -143,10 +132,6 @@
                     log.info("Intent {} failed, calling optical path provisioning app.", event.subject());
                     setupLightpath(event.subject());
                     break;
-                case WITHDRAWN:
-                    log.info("Intent {} withdrawn.", event.subject());
-                    releaseResources(event.subject());
-                    break;
                 default:
                     break;
             }
@@ -371,26 +356,6 @@
             }
         }
 
-        /**
-         * Release resources associated to the given intent.
-         *
-         * @param intent the intent
-         */
-        private void releaseResources(Intent intent) {
-            Collection<ResourceAllocation> allocations = resourceService.getResourceAllocations(intent.id());
-            if (intent instanceof OpticalConnectivityIntent) {
-                resourceService.release(intent.id());
-                if (!allocations.isEmpty()) {
-                    resourceService.release(ImmutableList.copyOf(allocations));
-                }
-            } else if (intent instanceof OpticalCircuitIntent) {
-                resourceService.release(intent.id());
-                intentSetMultimap.releaseMapping(intent.id());
-                if (!allocations.isEmpty()) {
-                    resourceService.release(ImmutableList.copyOf(allocations));
-                }
-            }
-        }
     }
 
     /**
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java b/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
index fcdb033..c9d6e56 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/IntentManager.java
@@ -44,6 +44,7 @@
 import org.onosproject.net.intent.Key;
 import org.onosproject.net.intent.impl.phase.FinalIntentProcessPhase;
 import org.onosproject.net.intent.impl.phase.IntentProcessPhase;
+import org.onosproject.net.newresource.ResourceService;
 import org.slf4j.Logger;
 
 import java.util.Collection;
@@ -108,6 +109,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected FlowRuleService flowRuleService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ResourceService resourceService;
+
     private ExecutorService batchExecutor;
     private ExecutorService workerExecutor;
 
@@ -238,6 +242,16 @@
         @Override
         public void notify(IntentEvent event) {
             post(event);
+            switch (event.type()) {
+                case WITHDRAWN:
+                    // release resources allocated to withdrawn intent
+                    if (!resourceService.release(event.subject().id())) {
+                        log.error("Failed to release resources allocated to {}", event.subject().id());
+                    }
+                    break;
+                default:
+                    break;
+            }
         }
 
         @Override