Support for OTN using optical circuit intents.
Refined DeviceResourceService.

Change-Id: I489f368a0fac5f4a8d0a1a1cb716f845558db35e
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
index e99b56b..4d8077e 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java
@@ -15,7 +15,8 @@
  */
 package org.onosproject.net.intent.impl.compiler;
 
-import java.util.Collections;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -41,6 +42,7 @@
 import org.onosproject.net.intent.IntentExtensionService;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.OpticalPathIntent;
+import org.onosproject.net.intent.impl.IntentCompilationException;
 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
 import org.onosproject.net.resource.device.DeviceResourceService;
 import org.onosproject.net.resource.link.LambdaResource;
@@ -56,6 +58,8 @@
 import org.onosproject.net.topology.TopologyService;
 
 import com.google.common.collect.ImmutableList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
@@ -65,6 +69,8 @@
 @Component(immediate = true)
 public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
 
+    protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
+
     private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM;
     private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
 
@@ -105,6 +111,13 @@
         checkArgument(srcPort instanceof OchPort);
         checkArgument(dstPort instanceof OchPort);
 
+        log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
+
+        // Reserve OCh ports
+        if (!deviceResourceService.requestPorts(new HashSet(Arrays.asList(srcPort, dstPort)), intent)) {
+            throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
+        }
+
         // Calculate available light paths
         Set<Path> paths = getOpticalPaths(intent);
 
@@ -118,13 +131,6 @@
 
             OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
 
-            // Try to reserve port resources, roll back if unsuccessful
-            Set<Port> portAllocs = deviceResourceService.requestPorts(intent);
-            if (portAllocs == null) {
-                linkResourceService.releaseResources(linkAllocs);
-                continue;
-            }
-
             // Create installable optical path intent
             LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
             OchSignal ochSignal = getOchSignal(lambdaAlloc, omsPort.minFrequency(), omsPort.grid());
@@ -143,7 +149,10 @@
             return ImmutableList.of(newIntent);
         }
 
-        return Collections.emptyList();
+        // Release port allocations if unsuccessful
+        deviceResourceService.releasePorts(intent.id());
+
+        throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent);
     }
 
     /**