[ONOS-4163] PCE service API

Change-Id: Iab8faeb85e9dcd6ec62a65f1ea8168b1be9ca39c
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
new file mode 100644
index 0000000..2ff3a49
--- /dev/null
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/PceManager.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright 2016-present 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.pce.pceservice;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
+
+import java.util.Iterator;
+import java.util.List;
+
+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.apache.felix.scr.annotations.Service;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.IdGenerator;
+import org.onosproject.incubator.net.tunnel.Tunnel;
+import org.onosproject.incubator.net.tunnel.TunnelId;
+import org.onosproject.incubator.net.tunnel.TunnelService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.pce.pceservice.api.PceService;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.DistributedSet;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of PCE service.
+ */
+@Component(immediate = true)
+@Service
+public class PceManager implements PceService {
+    private static final Logger log = LoggerFactory.getLogger(PceManager.class);
+
+    public static final String PCE_SERVICE_APP = "org.onosproject.pce";
+
+    private static final String LOCAL_LSP_ID_GEN_TOPIC = "pcep-local-lsp-id";
+    private IdGenerator localLspIdIdGen;
+    protected DistributedSet<Short> localLspIdFreeList;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected TunnelService tunnelService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected StorageService storageService;
+
+    private ApplicationId appId;
+
+    /**
+     * Creates new instance of PceManager.
+     */
+    public PceManager() {
+    }
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(PCE_SERVICE_APP);
+        log.info("Started");
+
+        localLspIdIdGen = coreService.getIdGenerator(LOCAL_LSP_ID_GEN_TOPIC);
+        localLspIdFreeList = storageService.<Short>setBuilder()
+                .withName("pcepLocalLspIdDeletedList")
+                .withSerializer(Serializer.using(KryoNamespaces.API))
+                .build()
+                .asDistributedSet();
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("Stopped");
+    }
+
+    //[TODO:] handle requests in queue
+    @Override
+    public boolean setupPath(DeviceId src, DeviceId dst, String tunnelName, List<Constraint> constraints,
+                             LspType lspType) {
+        checkNotNull(src);
+        checkNotNull(dst);
+        checkNotNull(tunnelName);
+        checkNotNull(constraints);
+        checkNotNull(lspType);
+
+        // TODO: compute and setup path.
+
+        return true;
+    }
+
+
+    @Override
+    public boolean updatePath(TunnelId tunnelId, List<Constraint> constraints) {
+        checkNotNull(tunnelId);
+        checkNotNull(constraints);
+
+        // TODO: compute and update path.
+
+        return true;
+    }
+
+    @Override
+    public boolean releasePath(TunnelId tunnelId) {
+        checkNotNull(tunnelId);
+        // 1. Query Tunnel from Tunnel manager.
+        Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
+
+        if (tunnel == null) {
+            return false;
+        }
+
+        // 2. Call tunnel service.
+        return tunnelService.downTunnel(appId, tunnel.tunnelId());
+    }
+
+    @Override
+    public Iterable<Tunnel> queryAllPath() {
+        return tunnelService.queryTunnel(MPLS);
+    }
+
+    @Override
+    public Tunnel queryPath(TunnelId tunnelId) {
+        return tunnelService.queryTunnel(tunnelId);
+    }
+
+    /**
+     * Returns the next local LSP identifier to be used either by getting from
+     * freed list if available otherwise generating a new one.
+     *
+     * @return value of local LSP identifier
+     */
+    private short getNextLocalLspId() {
+        // If there is any free id use it. Otherwise generate new id.
+        if (localLspIdFreeList.isEmpty()) {
+            return (short) localLspIdIdGen.getNewId();
+        }
+        Iterator<Short> it = localLspIdFreeList.iterator();
+        Short value = it.next();
+        localLspIdFreeList.remove(value);
+        return value;
+    }
+
+}
\ No newline at end of file
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/TunnelConsumerId.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/TunnelConsumerId.java
new file mode 100644
index 0000000..3c8f49b
--- /dev/null
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/TunnelConsumerId.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2016-present 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.pce.pceservice;
+
+import org.onosproject.net.resource.ResourceConsumer;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Tunnel resource consumer identifier suitable to be used as a consumer id for
+ * resource allocations.
+ */
+@Beta
+public final class TunnelConsumerId implements ResourceConsumer {
+
+    private final long value;
+
+    /**
+     * Creates a tunnel resource consumer identifier from the specified long value.
+     *
+     * @param value long value to be used as tunnel resource consumer id
+     * @return tunnel resource consumer identifier
+     */
+    public static TunnelConsumerId valueOf(long value) {
+        return new TunnelConsumerId(value);
+    }
+
+    /**
+     * Initializes object for serializer.
+     */
+    public TunnelConsumerId() {
+        this.value = 0;
+    }
+
+    /**
+     * Constructs the tunnel resource consumer id corresponding to a given long
+     * value.
+     *
+     * @param value the underlying value in long representation of this tunnel
+     *            resource consumer id
+     */
+    public TunnelConsumerId(long value) {
+        this.value = value;
+    }
+
+    /**
+     * Returns the tunnel resource consumer id value in long format.
+     *
+     * @return value the tunnel resource consumer id's long value
+     */
+    public long value() {
+        return value;
+    }
+
+    @Override
+    public int hashCode() {
+        return Long.hashCode(value);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (!(obj instanceof TunnelConsumerId)) {
+            return false;
+        }
+        TunnelConsumerId that = (TunnelConsumerId) obj;
+        return this.value == that.value;
+    }
+
+    @Override
+    public String toString() {
+        return "0x" + Long.toHexString(value);
+    }
+
+}
diff --git a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/api/PceService.java b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/api/PceService.java
index 74b8519..f2d3622 100644
--- a/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/api/PceService.java
+++ b/apps/pce/app/src/main/java/org/onosproject/pce/pceservice/api/PceService.java
@@ -20,24 +20,56 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.intent.Constraint;
 import org.onosproject.pce.pceservice.LspType;
+import org.onosproject.incubator.net.tunnel.Tunnel;
+import org.onosproject.incubator.net.tunnel.TunnelId;
 
 /**
- * Service to compute path based on constraints, release path and update path with new constraints.
+ * Service to compute path based on constraints, release path,
+ * update path with new constraints and query existing tunnels.
  */
 public interface PceService {
 
     /**
-     * Creates new path based on constraints and lsp type.
+     * Creates new path based on constraints and LSP type.
      *
      * @param src source device
      * @param dst destination device
+     * @param tunnelName name of the tunnel
      * @param constraints list of constraints to be applied on path
      * @param lspType type of path to be setup
      * @return false on failure and true on successful path creation
      */
-    boolean setupPath(DeviceId src, DeviceId dst, List<Constraint> constraints, LspType lspType);
+    boolean setupPath(DeviceId src, DeviceId dst, String tunnelName, List<Constraint> constraints, LspType lspType);
 
-    //TODO: updatePath
-    //TODO: releasePath
-    //TODO: queryPath
+    /**
+     * Updates an existing path.
+     *
+     * @param tunnelId tunnel identifier
+     * @param constraints list of constraints to be applied on path
+     * @return false on failure and true on successful path update
+     */
+    boolean updatePath(TunnelId tunnelId, List<Constraint> constraints);
+
+    /**
+     * Releases an existing path.
+     *
+     * @param tunnelId tunnel identifier
+     * @return false on failure and true on successful path removal
+     */
+    boolean releasePath(TunnelId tunnelId);
+
+    /**
+     * Queries all paths.
+     *
+     * @return iterable of existing tunnels
+     */
+    Iterable<Tunnel> queryAllPath();
+
+    /**
+     * Queries particular path based on tunnel identifier.
+     *
+     * @param tunnelId tunnel identifier
+     * @return tunnel if path exists, otherwise null
+     */
+    Tunnel queryPath(TunnelId tunnelId);
 }
\ No newline at end of file