Support for OTN using optical circuit intents.
Refined DeviceResourceService.

Change-Id: I489f368a0fac5f4a8d0a1a1cb716f845558db35e
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 47efc1b..dc41ee9 100644
--- a/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
+++ b/apps/optical/src/main/java/org/onosproject/optical/OpticalPathProvisioner.java
@@ -29,7 +29,9 @@
 import org.onosproject.net.Device;
 import org.onosproject.net.Host;
 import org.onosproject.net.Link;
+import org.onosproject.net.OchPort;
 import org.onosproject.net.OduCltPort;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.Path;
 import org.onosproject.net.Port;
 import org.onosproject.net.device.DeviceService;
@@ -40,6 +42,7 @@
 import org.onosproject.net.intent.IntentListener;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.intent.IntentState;
+import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.PointToPointIntent;
 import org.onosproject.net.resource.device.DeviceResourceService;
@@ -307,17 +310,33 @@
 
                 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
                 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
-                // Create lightpath
-                // TODO: Ensure src & dst are of type OchPort
-                Intent opticalIntent = OpticalConnectivityIntent.builder()
-                        .appId(appId)
-                        .src(src)
-                        .dst(dst)
-                        .build();
-                intents.add(opticalIntent);
+
                 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
+                    // TODO: Check availability of ports
+
+                    // Create OTN circuit
+                    Intent circuitIntent = OpticalCircuitIntent.builder()
+                            .appId(appId)
+                            .src(src)
+                            .dst(dst)
+                            .signalType(OduCltPort.SignalType.CLT_10GBE)
+                            .build();
+                    intents.add(circuitIntent);
                     continue;
-                    // also create OTN service
+                } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
+                    // Create lightpath
+                    // FIXME: hardcoded ODU signal type
+                    Intent opticalIntent = OpticalConnectivityIntent.builder()
+                            .appId(appId)
+                            .src(src)
+                            .dst(dst)
+                            .signalType(OduSignalType.ODU4)
+                            .build();
+                    intents.add(opticalIntent);
+                    continue;
+                } else {
+                    log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type());
+                    return Collections.emptyList();
                 }
             }
 
diff --git a/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java b/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
index f0e2bd2..6f00174 100644
--- a/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/AddOpticalIntentCommand.java
@@ -18,6 +18,7 @@
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
@@ -47,11 +48,13 @@
 
         ConnectPoint egress = ConnectPoint.deviceConnectPoint(egressDeviceString);
 
+        // FIXME: Hardcoded ODU signal type
         Intent intent = OpticalConnectivityIntent.builder()
                 .appId(appId())
                 .key(key())
                 .src(ingress)
                 .dst(egress)
+                .signalType(OduSignalType.ODU4)
                 .build();
         service.submit(intent);
         print("Optical intent submitted:\n%s", intent.toString());
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/OchSignalTypeCriterion.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/OchSignalTypeCriterion.java
index f70a504..cf838bf 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/criteria/OchSignalTypeCriterion.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/OchSignalTypeCriterion.java
@@ -23,7 +23,7 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
- * Implementation of OCh (Optical Channel) singal type criterion.
+ * Implementation of OCh (Optical Channel) signal type criterion.
  */
 public class OchSignalTypeCriterion implements Criterion {
 
diff --git a/core/api/src/main/java/org/onosproject/net/intent/OpticalCircuitIntent.java b/core/api/src/main/java/org/onosproject/net/intent/OpticalCircuitIntent.java
new file mode 100644
index 0000000..5f95d70
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/intent/OpticalCircuitIntent.java
@@ -0,0 +1,190 @@
+/*
+ * Copyright 2015 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;
+
+import com.google.common.base.MoreObjects;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.OduCltPort;
+
+import java.util.Collections;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * An optical layer intent for circuits between two OduClt ports.
+ * No traffic selector or traffic treatment are needed.
+ */
+public class OpticalCircuitIntent extends Intent {
+    private final ConnectPoint src;
+    private final ConnectPoint dst;
+    private final OduCltPort.SignalType signalType;
+
+    /**
+     * Creates an optical circuit intent between the specified
+     * connection points.
+     *
+     * @param appId application identification
+     * @param key intent key
+     * @param src the source transponder port
+     * @param dst the destination transponder port
+     * @param signalType ODU signal type
+     * @param priority priority to use for flows from this intent
+     */
+    protected OpticalCircuitIntent(ApplicationId appId, Key key, ConnectPoint src, ConnectPoint dst,
+                                   OduCltPort.SignalType signalType, int priority) {
+        super(appId, key, Collections.emptyList(), priority);
+        this.src = checkNotNull(src);
+        this.dst = checkNotNull(dst);
+        this.signalType = checkNotNull(signalType);
+    }
+
+    /**
+     * Returns a new optical circuit intent builder.
+     *
+     * @return host to host intent builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+
+    /**
+     * Builder for optical circuit intents.
+     */
+    public static class Builder extends Intent.Builder {
+        private ConnectPoint src;
+        private ConnectPoint dst;
+        private OduCltPort.SignalType signalType;
+
+        @Override
+        public Builder appId(ApplicationId appId) {
+            return (Builder) super.appId(appId);
+        }
+
+        @Override
+        public Builder key(Key key) {
+            return (Builder) super.key(key);
+        }
+
+        @Override
+        public Builder priority(int priority) {
+            return (Builder) super.priority(priority);
+        }
+
+        /**
+         * Sets the source for the intent that will be built.
+         *
+         * @param src source to use for built intent
+         * @return this builder
+         */
+        public Builder src(ConnectPoint src) {
+            this.src = src;
+            return this;
+        }
+
+        /**
+         * Sets the destination for the intent that will be built.
+         *
+         * @param dst dest to use for built intent
+         * @return this builder
+         */
+        public Builder dst(ConnectPoint dst) {
+            this.dst = dst;
+            return this;
+        }
+
+        /**
+         * Sets the ODU signal type for the intent that will be built.
+         *
+         * @param signalType signal type to use for built intent
+         * @return this builder
+         */
+        public Builder signalType(OduCltPort.SignalType signalType) {
+            this.signalType = signalType;
+            return this;
+        }
+
+        /**
+         * Builds an optical circuit intent from the accumulated parameters.
+         *
+         * @return point to point intent
+         */
+        public OpticalCircuitIntent build() {
+
+            return new OpticalCircuitIntent(
+                    appId,
+                    key,
+                    src,
+                    dst,
+                    signalType,
+                    priority
+            );
+        }
+    }
+
+    /**
+     * Constructor for serializer.
+     */
+    protected OpticalCircuitIntent() {
+        super();
+        this.src = null;
+        this.dst = null;
+        this.signalType = null;
+    }
+
+    /**
+     * Returns the source transponder port.
+     *
+     * @return source transponder port
+     */
+    public ConnectPoint getSrc() {
+        return src;
+    }
+
+    /**
+     * Returns the destination transponder port.
+     *
+     * @return source transponder port
+     */
+    public ConnectPoint getDst() {
+        return dst;
+    }
+
+    /**
+     * Returns the ODU signal type.
+     *
+     * @return ODU signal type
+     */
+    public OduCltPort.SignalType getSignalType() {
+        return signalType;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("id", id())
+                .add("key", key())
+                .add("appId", appId())
+                .add("priority", priority())
+                .add("resources", resources())
+                .add("src", src)
+                .add("dst", dst)
+                .add("signalType", signalType)
+                .toString();
+    }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java b/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
index c2b84ab..f28fd29 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/OpticalConnectivityIntent.java
@@ -18,6 +18,7 @@
 import com.google.common.base.MoreObjects;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.OduSignalType;
 
 import java.util.Collections;
 
@@ -30,6 +31,7 @@
 public final class OpticalConnectivityIntent extends Intent {
     private final ConnectPoint src;
     private final ConnectPoint dst;
+    private final OduSignalType signalType;
 
     /**
      * Creates an optical connectivity intent between the specified
@@ -42,13 +44,15 @@
      * @param priority priority to use for flows from this intent
      */
     protected OpticalConnectivityIntent(ApplicationId appId,
-                                     Key key,
-                                     ConnectPoint src,
-                                     ConnectPoint dst,
-                                     int priority) {
+                                        Key key,
+                                        ConnectPoint src,
+                                        ConnectPoint dst,
+                                        OduSignalType signalType,
+                                        int priority) {
         super(appId, key, Collections.emptyList(), priority);
         this.src = checkNotNull(src);
         this.dst = checkNotNull(dst);
+        this.signalType = checkNotNull(signalType);
     }
 
     /**
@@ -67,6 +71,7 @@
     public static class Builder extends Intent.Builder {
         private ConnectPoint src;
         private ConnectPoint dst;
+        private OduSignalType signalType;
 
         @Override
         public Builder appId(ApplicationId appId) {
@@ -106,6 +111,17 @@
         }
 
         /**
+         * Sets the ODU signal type for the intent that will be built.
+         *
+         * @param signalType ODU signal type
+         * @return this builder
+         */
+        public Builder signalType(OduSignalType signalType) {
+            this.signalType = signalType;
+            return this;
+        }
+
+        /**
          * Builds an optical connectivity intent from the accumulated parameters.
          *
          * @return point to point intent
@@ -117,6 +133,7 @@
                     key,
                     src,
                     dst,
+                    signalType,
                     priority
             );
         }
@@ -129,6 +146,7 @@
         super();
         this.src = null;
         this.dst = null;
+        this.signalType = null;
     }
 
     /**
@@ -149,6 +167,15 @@
         return dst;
     }
 
+    /**
+     * Returns the ODU signal type.
+     *
+     * @return ODU signal type
+     */
+    public OduSignalType getSignalType() {
+        return signalType;
+    }
+
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
@@ -159,6 +186,7 @@
                 .add("resources", resources())
                 .add("src", src)
                 .add("dst", dst)
+                .add("signalType", signalType)
                 .toString();
     }
 }
diff --git a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceService.java b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceService.java
index acbc486..81bd1b7 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceService.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceService.java
@@ -28,10 +28,46 @@
     /**
      * Request a set of ports needed to satisfy the intent.
      *
+     * @param ports set of ports to allocate
      * @param intent the intent
-     * @return set of ports
+     * @return true if ports were successfully allocated, false otherwise
      */
-    Set<Port> requestPorts(Intent intent);
+    boolean requestPorts(Set<Port> ports, Intent intent);
+
+    /**
+     * Returns the set of ports allocated for an intent.
+     *
+     * @param intentId the intent ID
+     * @return set of allocated ports
+     */
+    Set<Port> getAllocations(IntentId intentId);
+
+    /**
+     * Returns the intent allocated to a port.
+     *
+     * @param port the port
+     * @return intent ID allocated to the port
+     */
+    IntentId getAllocations(Port port);
+
+    /**
+     * Request a mapping between the given intents.
+     *
+     * @param keyIntentId the key intent ID
+     * @param valIntentId the value intent ID
+     * @return true if mapping was successful, false otherwise
+     */
+    boolean requestMapping(IntentId keyIntentId, IntentId valIntentId);
+
+    /**
+     * Returns the intents mapped to a lower intent.
+     *
+     * @param intentId the intent ID
+     * @return the set of intent IDs
+     */
+    Set<IntentId> getMapping(IntentId intentId);
+
+    void releaseMapping(IntentId keyIntentId, IntentId valIntentId);
 
     /**
      * Release ports associated with given intent ID.
diff --git a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
index 3437f4b..c73cab7 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/device/DeviceResourceStore.java
@@ -24,7 +24,60 @@
 public interface DeviceResourceStore {
     Set<Port> getFreePorts(DeviceId deviceId);
 
-    void allocatePorts(Set<Port> port, IntentId intent);
+    /**
+     * Allocates the given ports to the given intent.
+     * @param ports set of ports to allocate
+     * @param intentId intent ID
+     * @return true if allocation was successful, false otherwise
+     */
+    boolean allocatePorts(Set<Port> ports, IntentId intentId);
 
-    void releasePorts(IntentId intent);
+    /**
+     * Returns set of ports allocated for an intent.
+     *
+     * @param intentId the intent ID
+     * @return set of allocated ports
+     */
+    Set<Port> getAllocations(IntentId intentId);
+
+    /**
+     * Returns intent allocated to a port.
+     *
+     * @param port the port
+     * @return intent ID allocated to the port
+     */
+    IntentId getAllocations(Port port);
+
+    /**
+     * Allocates the mapping between the given intents.
+     *
+     * @param keyIntentId key intent ID
+     * @param valIntentId value intent ID
+     * @return true if mapping was successful, false otherwise
+     */
+    boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId);
+
+    /**
+     * Returns the set of intents mapped to a lower intent.
+     *
+     * @param intentId intent ID
+     * @return set of intent IDs
+     */
+    Set<IntentId> getMapping(IntentId intentId);
+
+    /**
+     * Releases the mapping between the given intents.
+     *
+     * @param keyIntentId key intent ID
+     * @param valIntentId value intent ID
+     */
+    void releaseMapping(IntentId keyIntentId, IntentId valIntentId);
+
+    /**
+     * Releases the ports allocated to the given intent.
+     *
+     * @param intentId intent ID
+     * @return true if release was successful, false otherwise
+     */
+    boolean releasePorts(IntentId intentId);
 }
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
new file mode 100644
index 0000000..4da6d4f
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalCircuitIntentCompiler.java
@@ -0,0 +1,282 @@
+/*
+ * Copyright 2015 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.compiler;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.OchPort;
+import org.onosproject.net.OduCltPort;
+import org.onosproject.net.OduSignalType;
+import org.onosproject.net.Port;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.FlowRuleIntent;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.IntentCompiler;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentId;
+import org.onosproject.net.intent.IntentService;
+import org.onosproject.net.intent.OpticalCircuitIntent;
+import org.onosproject.net.intent.OpticalConnectivityIntent;
+import org.onosproject.net.intent.impl.IntentCompilationException;
+import org.onosproject.net.resource.device.DeviceResourceService;
+import org.onosproject.net.resource.link.LinkResourceAllocations;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}.
+ */
+@Component(immediate = true)
+public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> {
+
+    private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntentExtensionService intentManager;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceResourceService deviceResourceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntentService intentService;
+
+    private ApplicationId appId;
+
+    @Activate
+    public void activate() {
+        appId = coreService.registerApplication("org.onosproject.net.intent");
+        intentManager.registerCompiler(OpticalCircuitIntent.class, this);
+    }
+
+    @Deactivate
+    public void deactivate() {
+        intentManager.unregisterCompiler(OpticalCircuitIntent.class);
+    }
+
+    @Override
+    public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable,
+                                Set<LinkResourceAllocations> resources) {
+        // Check if ports are OduClt ports
+        ConnectPoint src = intent.getSrc();
+        ConnectPoint dst = intent.getDst();
+        Port srcPort = deviceService.getPort(src.deviceId(), src.port());
+        Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
+        checkArgument(srcPort instanceof OduCltPort);
+        checkArgument(dstPort instanceof OduCltPort);
+
+        log.debug("Compiling optical circuit intent between {} and {}", src, dst);
+
+        // Reserve OduClt ports
+        if (!deviceResourceService.requestPorts(new HashSet(Arrays.asList(srcPort, dstPort)), intent)) {
+            throw new IntentCompilationException("Unable to reserve ports for intent " + intent);
+        }
+
+        LinkedList<Intent> intents = new LinkedList<>();
+
+        FlowRuleIntent circuitIntent;
+        OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent);
+
+        // Create optical connectivity intent if needed
+        if (connIntent == null) {
+            // Find OCh ports with available resources
+            Pair<OchPort, OchPort> ochPorts = findPorts(intent);
+
+            if (ochPorts == null) {
+                return Collections.emptyList();
+            }
+
+            // Create optical connectivity intent
+            ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number());
+            ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number());
+            // FIXME: hardcoded ODU signal type
+            connIntent = OpticalConnectivityIntent.builder()
+                    .appId(appId)
+                    .src(srcCP)
+                    .dst(dstCP)
+                    .signalType(OduSignalType.ODU4)
+                    .build();
+            intents.add(connIntent);
+        }
+
+        // Create optical circuit intent
+        circuitIntent = new FlowRuleIntent(
+                appId,
+                createRules(src, connIntent.getSrc(), dst, connIntent.getDst()),
+                intent.resources());
+
+        // Save circuit to connectivity intent mapping
+        deviceResourceService.requestMapping(connIntent.id(), circuitIntent.id());
+        intents.add(circuitIntent);
+
+        return intents;
+    }
+
+    /**
+     * Checks if current allocations on given resource can satisfy request.
+     *
+     * @param request
+     * @param resource
+     * @return
+     */
+    private boolean isAvailable(Intent request, IntentId resource) {
+        Set<IntentId> mapping = deviceResourceService.getMapping(resource);
+
+        // TODO: hardcoded 10 x 10G
+        return mapping.size() < 10;
+    }
+
+    /**
+     * Returns existing and available optical connectivity intent that matches the given circuit intent.
+     *
+     * @param circuitIntent optical circuit intent
+     * @return existing optical connectivity intent, null otherwise.
+     */
+    private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent) {
+        for (Intent intent : intentService.getIntents()) {
+            if (!(intent instanceof OpticalConnectivityIntent)) {
+                continue;
+            }
+
+            OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent;
+
+            ConnectPoint src = circuitIntent.getSrc();
+            ConnectPoint dst = circuitIntent.getDst();
+            if (!src.equals(connIntent.getSrc()) && !dst.equals(connIntent.getDst())) {
+                continue;
+            }
+
+            if (isAvailable(circuitIntent, connIntent.id())) {
+                return connIntent;
+            }
+        }
+
+        return null;
+    }
+
+    private OchPort findAvailableOchPort(DeviceId deviceId, OpticalCircuitIntent circuitIntent) {
+        List<Port> ports = deviceService.getPorts(deviceId);
+
+        for (Port port : ports) {
+            if (!(port instanceof OchPort)) {
+                continue;
+            }
+
+            // Port is not used
+            IntentId intentId = deviceResourceService.getAllocations(port);
+            if (intentId == null) {
+                return (OchPort) port;
+            }
+
+            // Port is used but has free resources
+            if (isAvailable(circuitIntent, intentId)) {
+                return (OchPort) port;
+            }
+        }
+
+        return null;
+    }
+
+    // TODO: Add constraints for OduClt to OCh port mappings
+    // E.g., ports need to belong to same line card.
+    private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) {
+
+        OchPort srcPort = findAvailableOchPort(intent.getSrc().deviceId(), intent);
+        if (srcPort == null) {
+            return null;
+        }
+
+        OchPort dstPort = findAvailableOchPort(intent.getDst().deviceId(), intent);
+        if (dstPort == null) {
+            return null;
+        }
+
+        return Pair.of(srcPort, dstPort);
+    }
+
+    /**
+     * Builds flow rules for mapping between ODU and OCh ports.
+     *
+     * @param srcOdu
+     * @param dstOdu
+     * @return
+     */
+    private List<FlowRule> createRules(ConnectPoint srcOdu, ConnectPoint srcOch,
+                                       ConnectPoint dstOdu, ConnectPoint dstOch) {
+        TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
+        TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
+
+        // Source flow rule
+        selectorBuilder.matchInPort(srcOdu.port());
+        //selectorBuilder.add(Criteria.matchCltSignalType)
+        treatmentBuilder.setOutput(srcOch.port());
+        //treatmentBuilder.add(Instructions.modL1OduSignalType)
+
+        FlowRule srcRule = DefaultFlowRule.builder()
+                .forDevice(srcOdu.deviceId())
+                .withSelector(selectorBuilder.build())
+                .withTreatment(treatmentBuilder.build())
+                .withPriority(100)
+                .fromApp(appId)
+                .makePermanent()
+                .build();
+
+        // Destination flow rule
+        selectorBuilder.matchInPort(dstOch.port());
+        //selectorBuilder.add(Criteria.matchOduSignalType)
+        treatmentBuilder.setOutput(dstOdu.port());
+        //treatmentBuilder.add(Instructions.modL1CltSignalType)
+
+        FlowRule dstRule = DefaultFlowRule.builder()
+                .forDevice(dstOdu.deviceId())
+                .withSelector(selectorBuilder.build())
+                .withTreatment(treatmentBuilder.build())
+                .withPriority(100)
+                .fromApp(appId)
+                .makePermanent()
+                .build();
+
+        return Arrays.<FlowRule>asList(srcRule, dstRule);
+    }
+}
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);
     }
 
     /**
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
index 274b98d..e7e0562 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalPathIntentCompiler.java
@@ -39,6 +39,8 @@
 import org.onosproject.net.intent.OpticalPathIntent;
 import org.onosproject.net.resource.link.LinkResourceAllocations;
 import org.onosproject.net.resource.link.LinkResourceService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.LinkedList;
@@ -48,6 +50,8 @@
 @Component(immediate = true)
 public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
 
+    private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected IntentExtensionService intentManager;
 
@@ -73,6 +77,8 @@
     @Override
     public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable,
                                 Set<LinkResourceAllocations> resources) {
+        log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
+
         return Collections.singletonList(
                 new FlowRuleIntent(appId, createRules(intent), intent.resources()));
     }
diff --git a/core/net/src/main/java/org/onosproject/net/resource/impl/DeviceResourceManager.java b/core/net/src/main/java/org/onosproject/net/resource/impl/DeviceResourceManager.java
index 71fa9d5..c9437de 100644
--- a/core/net/src/main/java/org/onosproject/net/resource/impl/DeviceResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/resource/impl/DeviceResourceManager.java
@@ -24,13 +24,10 @@
 import org.onosproject.net.Port;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.IntentId;
-import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.resource.device.DeviceResourceService;
 import org.onosproject.net.resource.device.DeviceResourceStore;
 import org.slf4j.Logger;
 
-import java.util.Arrays;
-import java.util.HashSet;
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -59,28 +56,35 @@
     }
 
     @Override
-    public Set<Port> requestPorts(Intent intent) {
+    public boolean requestPorts(Set<Port> ports, Intent intent) {
         checkNotNull(intent);
-        if (intent instanceof OpticalConnectivityIntent) {
-            OpticalConnectivityIntent opticalIntent = (OpticalConnectivityIntent) intent;
-            Set<Port> srcPorts = store.getFreePorts(opticalIntent.getSrc().deviceId());
-            Set<Port> dstPorts = store.getFreePorts(opticalIntent.getDst().deviceId());
 
-            Port srcPort = getTypedPort(srcPorts, Port.Type.OCH);
-            Port dstPort = getTypedPort(dstPorts, Port.Type.OCH);
+        return store.allocatePorts(ports, intent.id());
+    }
 
-            if (srcPort == null || dstPort == null) {
-                return null;
-            }
+    @Override
+    public Set<Port> getAllocations(IntentId intentId) {
+        return store.getAllocations(intentId);
+    }
 
-            Set<Port> allocPorts = new HashSet(Arrays.asList(srcPort, dstPort));
+    @Override
+    public IntentId getAllocations(Port port) {
+        return store.getAllocations(port);
+    }
 
-            store.allocatePorts(allocPorts, intent.id());
+    @Override
+    public void releaseMapping(IntentId keyIntentId, IntentId valIntentId) {
+        store.releaseMapping(keyIntentId, valIntentId);
+    }
 
-            return allocPorts;
-        }
+    @Override
+    public boolean requestMapping(IntentId keyIntentId, IntentId valIntentId) {
+        return store.allocateMapping(keyIntentId, valIntentId);
+    }
 
-        return null;
+    @Override
+    public Set<IntentId> getMapping(IntentId intentId) {
+        return store.getMapping(intentId);
     }
 
     @Override
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java
index bfdc536..519fc83 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/ConsistentDeviceResourceStore.java
@@ -35,6 +35,7 @@
 import org.onosproject.store.service.TransactionalMap;
 import org.slf4j.Logger;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -52,12 +53,14 @@
 
     private static final String PORT_ALLOCATIONS = "PortAllocations";
     private static final String INTENT_ALLOCATIONS = "IntentAllocations";
+    private static final String INTENT_MAPPING = "IntentMapping";
 
     private static final Serializer SERIALIZER = Serializer.using(
             new KryoNamespace.Builder().register(KryoNamespaces.API).build());
 
     private ConsistentMap<Port, IntentId> portAllocMap;
     private ConsistentMap<IntentId, Set<Port>> intentAllocMap;
+    private ConsistentMap<IntentId, Set<IntentId>> intentMapping;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected StorageService storageService;
@@ -75,6 +78,10 @@
                 .withName(INTENT_ALLOCATIONS)
                 .withSerializer(SERIALIZER)
                 .build();
+        intentMapping = storageService.<IntentId, Set<IntentId>>consistentMapBuilder()
+                .withName(INTENT_MAPPING)
+                .withSerializer(SERIALIZER)
+                .build();
         log.info("Started");
     }
 
@@ -110,7 +117,7 @@
     }
 
     @Override
-    public void allocatePorts(Set<Port> ports, IntentId intentId) {
+    public boolean allocatePorts(Set<Port> ports, IntentId intentId) {
         checkNotNull(ports);
         checkArgument(ports.size() > 0);
         checkNotNull(intentId);
@@ -120,20 +127,78 @@
         try {
             TransactionalMap<Port, IntentId> portAllocs = getPortAllocs(tx);
             for (Port port : ports) {
-                portAllocs.put(port, intentId);
+                if (portAllocs.putIfAbsent(port, intentId) != null) {
+                    throw new Exception("Port already allocated " + port.toString());
+                }
             }
+
             TransactionalMap<IntentId, Set<Port>> intentAllocs = getIntentAllocs(tx);
             intentAllocs.put(intentId, ports);
             tx.commit();
         } catch (Exception e) {
             log.error("Exception thrown, rolling back", e);
             tx.abort();
-            throw e;
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public Set<Port> getAllocations(IntentId intentId) {
+        if (!intentAllocMap.containsKey(intentId)) {
+            Collections.emptySet();
+        }
+
+        return intentAllocMap.get(intentId).value();
+    }
+
+    @Override
+    public IntentId getAllocations(Port port) {
+        if (!portAllocMap.containsKey(port)) {
+            return null;
+        }
+
+        return portAllocMap.get(port).value();
+    }
+
+    @Override
+    public Set<IntentId> getMapping(IntentId intentId) {
+        return intentMapping.get(intentId).value();
+    }
+
+    @Override
+    public void releaseMapping(IntentId keyIntentId, IntentId valIntentId) {
+        if (!intentMapping.containsKey(keyIntentId)) {
+            return;
+        }
+
+        Set<IntentId> intents = intentMapping.get(keyIntentId).value();
+
+        try {
+            intents.remove(valIntentId);
+        } catch (Exception e) {
+            log.error("Trying to remove non-existing mapping {} {}", keyIntentId, valIntentId);
         }
     }
 
     @Override
-    public void releasePorts(IntentId intentId) {
+    public boolean allocateMapping(IntentId keyIntentId, IntentId valIntentId) {
+        Set<IntentId> intents = intentMapping.get(keyIntentId).value();
+
+        if (intents == null) {
+            intents = Collections.singleton(valIntentId);
+        } else {
+            intents.add(valIntentId);
+        }
+
+        intentMapping.put(keyIntentId, intents);
+
+        return true;
+    }
+
+    @Override
+    public boolean releasePorts(IntentId intentId) {
         checkNotNull(intentId);
 
         TransactionContext tx = getTxContext();
@@ -150,7 +215,9 @@
         } catch (Exception e) {
             log.error("Exception thrown, rolling back", e);
             tx.abort();
-            throw e;
+            return false;
         }
+
+        return true;
     }
 }
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index b3e7a75..00fa550 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -136,6 +136,7 @@
 import org.onosproject.net.intent.MplsIntent;
 import org.onosproject.net.intent.MplsPathIntent;
 import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.net.intent.OpticalCircuitIntent;
 import org.onosproject.net.intent.OpticalConnectivityIntent;
 import org.onosproject.net.intent.OpticalPathIntent;
 import org.onosproject.net.intent.PathIntent;
@@ -361,6 +362,7 @@
                     LinkCollectionIntent.class,
                     OpticalConnectivityIntent.class,
                     OpticalPathIntent.class,
+                    OpticalCircuitIntent.class,
                     LinkResourceRequest.class,
                     DefaultLinkResourceRequest.class,
                     BandwidthResourceRequest.class,